-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
92 lines (70 loc) · 1.63 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
# Based on ryanb dotfiles. (https://github.com/ryanb/)
require 'rake'
require 'erb'
desc "install the dot files into user's home directory"
task :install do
replace_all = false
install_files
end
def install_files
Dir['*'].each do |file|
next if %w[Rakefile README.rdoc].include?(file)
filename = filename(file)
filepath = filepath(filename)
if File.exist?(filename)
if File.identical?(file, filepath)
puts "identical ~/#{filename}"
elsif replace_all
replace_file(file)
else
overwrite_file(file)
end
else
link_file(file)
end
end
end
def filename(file)
".#{file.sub('.erb', '')}"
end
def filepath(filename)
File.join(ENV['HOME'], filename)
end
def target_filepath(filename)
"$HOME/#{filename}"
end
def replace_file(file)
filename = filename(file)
system %Q{rm -rf "#{target_filepath(filename)}"}
link_file(file)
end
def overwrite_file(file)
filename = "~/#{filename(file)}"
print "overwrite #{filename}? [ynaq] "
case $stdin.gets.chomp
when 'a'
replace_all = true
replace_file(file)
when 'y'
replace_file(file)
when 'q'
exit
else
puts "skipping #{filename}"
end
end
def link_file(file)
filename = filename(file)
if file =~ /.erb$/
filepath = filepath(filename)
puts "generating ~/#{filename}"
File.open(filepath, 'w') do |new_file|
content = ERB.new(File.read(file)).result(binding)
new_file.write(content)
end
else
puts "linking ~/#{filename}"
folder = File.directory?(file) ? '/' : ''
system %Q{ln -s "$PWD/#{file}#{folder}" "#{target_filepath(filename)}"}
end
end