class Dotfiler::CLI::Commands::Init

Public Instance Methods

call(path:, **options) click to toggle source
# File lib/dotfiler/cli/commands/init.rb, line 12
def call(path:, **options)
  handle_errors do
    dotfiles_path = to_path.(path)

    create_config_file(config_file_contents(dotfiles_path))
    create_dotfiles_dir(dotfiles_path)
    create_dotfiles_file(dotfiles_path)
    initialize_vcs_repo(dotfiles_path) unless options[:git] == false
  end
end

Private Instance Methods

config_file_contents(dotfiles_path) click to toggle source
# File lib/dotfiler/cli/commands/init.rb, line 25
def config_file_contents(dotfiles_path)
  "dotfiles: #{dotfiles_path.to_s}"
end
create_config_file(contents) click to toggle source
# File lib/dotfiler/cli/commands/init.rb, line 29
def create_config_file(contents)
  if config.file_path.exists?
    answer = prompt("Config file (#{config.file_path}) already exists. Would you like to overwrite it?")

    return unless answer == :yes
  end

  info("Creating config file (#{config.file_path})...")
  fs.create_file(config.file_path.to_s, contents)
end
create_dotfiles_dir(dotfiles_path) click to toggle source
# File lib/dotfiler/cli/commands/init.rb, line 40
def create_dotfiles_dir(dotfiles_path)
  if dotfiles_path.exists?
    answer = prompt("Dotfiles directory (#{dotfiles_path}) already exists. Would you like to overwrite it?")

    return unless answer == :yes

    info("Removing existing dotfiles directory (#{dotfiles_path})...")
    fs.remove(dotfiles_path.to_s)
  end

  info("Creating dotfiles directory (#{dotfiles_path})...")
  fs.create_dir(dotfiles_path.to_s)
end
create_dotfiles_file(dotfiles_path) click to toggle source
# File lib/dotfiler/cli/commands/init.rb, line 54
def create_dotfiles_file(dotfiles_path)
  dotfiles_file = dotfiles_path.join(config.dotfiles_file_name)

  if dotfiles_file.exists?
    answer = prompt("Dotfiles file (#{dotfiles_file}) already exists. Would you like to overwrite it?")

    return unless answer == :yes
  end

  info("Creating dotfiles file (#{dotfiles_file})...")
  fs.create_file(dotfiles_file.to_s)
end
initialize_vcs_repo(dotfiles_path) click to toggle source
# File lib/dotfiler/cli/commands/init.rb, line 67
def initialize_vcs_repo(dotfiles_path)
  if dotfiles_path.join(".git").exists?
    answer = prompt("Dotfiles dir (#{dotfiles_path}) is already a git repository. Would you like to reinitialize it?")

    return unless answer == :yes
  end

  info(fs.execute("git", "init", dotfiles_path.to_s, capture: true))
end