forked from ryanb/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
91 lines (80 loc) · 2.02 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
require 'rake'
require 'erb'
IGNORE_FILES = %w{Rakefile README.rdoc LICENSE}
APPEND_FILES = %w{bashrc}
desc "simple test task"
task :simple do
INSTALL_FROM = File.expand_path(File.dirname(__FILE__))
@file = "some_file_name.erb.html.whackadoo"
puts_file
end
desc "install the dot files into user's home directory"
task :install do
install_from = File.expand_path(File.dirname(__FILE__))
replace_all = false
backup_all = false
Dir['*'].each do |file|
next if IGNORE_FILES.include? file
@file = File.join(install_from, file)
@wo_erb = file.sub(/\.erb$/, '')
@link = File.join(ENV['HOME'], ".#{@wo_erb}")
if APPEND_FILES.include? file
append_file
elsif File.exist?(@link)
if File.identical?(@file, @link)
puts "identical - #{@file} and #{@link}"
elsif replace_all
replace_file
else
print "backup(b), backup all(B), replace(r), replace all(R), no(n), quit(q): #{@link}? [bBrRnq] "
case STDIN.gets.chomp
when 'b'
backup_file
when 'B'
backup_all = true
backup_file
when 'r'
replace_file
when 'R'
replace_all = true
replace_file
when 'q'
exit
else
puts "skipping #{@link}"
end
end
else
link_file
end
end
puts "Installation complete."
end
def backup_file
`mv "#{@link}" "#{@link}.backup_#{Time.now.to_s.split[0,2].join('@')}"`
link_file
end
def replace_file
`rm -rf "#{@link}"`
link_file
end
def link_file
if File.extname(@file) == '.erb'
puts "generating #{@link} from erb"
File.open(@link, 'w') do |new_file|
new_file.write ERB.new(File.read(@file)).result(binding)
end
else
puts "linking #{@link} to #{@file}"
`ln -s "#{@file}" "#{@link}"`
end
end
def append_file
File.open(@link, 'a') do |app_file|
app_file.puts "\n# dotfiles additions"
app_file.write File.read(@file)
end
end
def puts_file
puts "your file is #{File.join(INSTALL_FROM, @file)}"
end