-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRakefile
74 lines (65 loc) · 1.75 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
require 'rake'
require 'erb'
task :default => [ :install ]
desc "Install dotfiles to home directory using symlinks"
task :install do
replace_all = false
home = File.expand_path(ENV['HOME'])
ignored = %w(Rakefile README.md)
Dir['*'].each do |file|
next if ignored.include?(file)
filename = file.sub('.erb', '')
target = File.expand_path(File.join(home, ".#{filename}"))
if File.exist?(target) or File.symlink?(target) or File.directory?(target)
if File.identical?(file, target)
puts "Identical #{filename}"
else
if replace_all
replace_target(file, target)
else
print "Replace existing file #{filename}? [ynaq] "
case $stdin.gets.chomp
when 'a'
replace_all = true
replace_target(file, target)
when 'y'
replace_target(file, target)
when 'q'
puts "Abort"
exit
else
puts "Skipping #{filename}"
end
end
end
else
link_target(file, target)
end
end
end
def link_target(file, target)
filename = file.sub('.erb', '')
if file =~ /.erb$/
puts "Generating #{filename}"
File.open(target, 'w') do |output|
output.write(ERB.new(File.read(file)).result(binding))
end
else
puts "Symlink #{filename}"
source = File.expand_path(File.join(File.dirname(__FILE__), file))
File.symlink(source, target)
end
end
def backup_target(target)
backup = "#{target}.orig"
puts "Backing up #{target} to #{backup}"
File.rename(target, backup)
end
def replace_target(file, target)
backup_target(target)
link_target(file, target)
end
def prompt_for_value(message)
print "Enter #{message}: "
return $stdin.gets.chomp
end