forked from cstrahan/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
69 lines (58 loc) · 1.7 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
require 'rake'
TOPLEVEL = File.expand_path('.')
def windows?
ENV['OS'] =~ /windows/i
end
HOME = if windows?
ENV['USERPROFILE']
else
File.expand_path("~")
end.gsub(%r{(\\|/)+$}, "")
def sym(linkable, target)
target.gsub("~", HOME)
if windows?
`cmd /C mklink "#{target}" "#{linkable}"`
else
`ln -s "#{linkable}" "#{target}"`
end
end
desc "Hook our dotfiles into system-standard positions."
task :install do
linkables = Dir.glob('*/**{.symlink}')
linkables << "bin" # special case
skip_all = false
overwrite_all = false
backup_all = false
linkables.each do |linkable|
skip = false
overwrite = false
backup = false
file = linkable.split('/').last.split('.symlink')[0]
target =
if file == "bin"
"#{HOME}/bin"
else
"#{HOME}/.#{file}"
end
if File.exists?(target) || File.symlink?(target)
unless skip_all || overwrite_all || backup_all
puts "File already exists: #{target}, what do you want to do? [s]kip, [S]kip all, [o]verwrite, [O]verwrite all, [b]ackup, [B]ackup all"
case STDIN.gets.chomp
when 'o' then overwrite = true
when 'b' then backup = true
when 'O' then overwrite_all = true
when 'B' then backup_all = true
when 'S' then skip_all = true
when 's' then skip = true
end
end
end
FileUtils.rm_rf(target) if overwrite || overwrite_all
FileUtils.mv(File.join(HOME, ".#{file}"), File.join(HOME, ".#{file}.backup")) if backup || backup_all
unless skip || skip_all
puts "#{target.gsub(HOME, "~")} => #{linkable}"
sym(File.join(TOPLEVEL, linkable), target)
end
end
end
task :default => 'install'