-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
97 lines (87 loc) · 2.36 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require 'rake'
require 'erb'
desc "Install dot files into user's home directory."
task :install do
# Make sure all git submodules are initialized and updated
system('git submodule update --init --recursive')
# Get mappings for ERB files and store in hash
$cfg_mappings = {}
mf = 'erb/config.mappings'
if File.exist?(mf)
File.open(mf, 'r') do |file|
while(line = file.gets)
arr = line.split(",")
if arr.size == 2
$cfg_mappings[arr[0].strip] = arr[1].strip
end
end
end
end
# Link files to user home directory
replace_all = link_dir('.', false)
link_dir('erb', replace_all)
end
desc "Update dot files from git repository."
task :update do
system('git pull origin master')
system('git submodule foreach --recursive git pull origin master')
end
def link_dir(dir, replace_all = false)
prev = Dir.pwd
Dir.chdir(dir)
Dir['*'].each do |file|
next if %w[Rakefile README.md erb config.mappings].include? file
ofile = ".#{file.sub('.erb', '')}"
if $cfg_mappings.key?(file)
ofile = $cfg_mappings[file]
end
if File.exist?(File.join(ENV['HOME'], "#{ofile}"))
if File.identical? file, File.join(ENV['HOME'], "#{ofile}")
puts "identical ~/#{ofile}"
elsif replace_all
replace_file(file)
else
print "overwrite ~/#{ofile}? [ynaq] "
case $stdin.gets.chomp
when 'a'
replace_all = true
replace_file(file)
when 'y'
replace_file(file)
when 'q'
exit
else
puts "skipping ~/#{ofile}"
end
end
else
link_file(file)
end
end
Dir.chdir(prev)
return replace_all
end
def replace_file(file)
if $cfg_mappings.key?(file)
system %Q{rm -rf "#{$cfg_mappings[file]}"}
else
system %Q{rm -rf "$HOME/.#{file.sub('.erb', '')}"}
end
link_file(file)
end
def link_file(file)
ofile = file
if $cfg_mappings.key?(file)
ofile = $cfg_mappings[file]
system %Q{mkdir -p "#{File.join(ENV['HOME'], File.dirname(ofile))}"}
end
if file =~ /.erb$/
puts "generating ~/#{ofile.sub('.erb', '')}"
File.open(File.join(ENV['HOME'], "#{ofile.sub('.erb', '')}"), 'w') do |new_file|
new_file.write ERB.new(File.read(file)).result(binding)
end
else
puts "linking ~/.#{ofile}"
system %Q{ln -s "$PWD/#{file}" "$HOME/.#{ofile}"}
end
end