Class: Dottor::App

Inherits:
Thor
  • Object
show all
Defined in:
lib/dottor.rb

Instance Method Summary collapse

Instance Method Details

#initObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dottor.rb', line 35

def init
  if File.exists?('dottor_rules.yml')
    if options[:force]
      FileUtils.rm 'dottor_rules.yml'
    else
      say("Error: dottor_rules.yml already exist. Use the --force to overwrite.")
      exit(1)
    end
  end

  rules_hash = {"profile_name" => []}

  exclude_files = ['.gitignore', 'README', 'README.md']
  if git_repo?
    files_in_current_dir = `git ls-files`.split("\n") - exclude_files
  else
    files_in_current_dir = Dir["*"] - exclude_files
  end

  if files_in_current_dir.empty?
    rules_hash["profile_name"] = [{"source" => ".dotfile", "target" => "target_path/.dotfile"}]
  else
    files_in_current_dir.each do |file_name|
      rules_hash["profile_name"] << {"source" => file_name, "target" => "target_path/#{file_name}"}
    end
  end

  File.open('dottor_rules.yml', 'w') do |file|
    file.write(YAML.dump(rules_hash))
  end

  say("dottor_rules.yml file created. Modify it and run 'dottor symlink <profile_name>'")
end


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dottor.rb', line 11

def symlink(profile_name)
  yaml_rules = options[:file] ? File.open(options[:file]) : File.open('dottor_rules.yml')

  say("Loading rules YAML file")
  rules = YAML::load(yaml_rules)

  if rules[profile_name].nil?
    $stderr.puts("Error: Profile name not found. Make sure that your dottor_rules yaml file define the profile #{profile_name}")
    # exit(1)
    return
  end

  rules[profile_name].each do |mapping|
    dotfile = Dotfile.new(mapping)
    if options[:delete]
      dotfile.delete_symlink
    else
      dotfile.create_symlink
    end
  end
end