-
Notifications
You must be signed in to change notification settings - Fork 2
/
Rakefile
147 lines (130 loc) · 4.62 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require 'fileutils'
require 'rake'
require 'set'
# utils ------------------------------------------------------------------- {{{
def file_listing(end_pattern)
files = Dir.glob("locations/**/*#{end_pattern}").map do |name|
old_base = File.join(File.dirname(name), File.readlink(name)).chomp('.placeholder')
new_base = name.sub(/^locations\//, '').chomp(end_pattern)
old_path = File.expand_path(old_base)
new_path = File.expand_path("~/.#{new_base}")
{ old_path: old_path, new_path: new_path }
end
files.to_set
end
def directory_listing
file_listing('.directory.symlink')
end
def locals_listing
file_listing('.symlink').select do |f|
f[:new_path].match('\.local$')
end
end
def dotfile_listing
file_listing('.symlink').select do |f|
!f[:new_path].match('\.directory$') && !f[:new_path].match('\.local$')
end
end
def current_file(f)
_old_path, new_path = f[:old_path], f[:new_path]
if File.directory? new_path
{ type: :directory, path: new_path, old_path: new_path, new_path: new_path }
elsif File.symlink? new_path
{ type: :symlink, path: new_path, old_path: File.expand_path(File.readlink(new_path)), new_path: new_path }
elsif File.file? new_path
{ type: :file, path: new_path, old_path: new_path, new_path: new_path }
else
{ type: nil, path: new_path, old_path: new_path, new_path: new_path }
end
end
def maybe_overwrite_symlink!(old_link, new_link, noinput = false)
if noinput
puts "Warn! | Overwrite | path: #{new_link[:new_path]}, old: #{old_link[:old_path]}, new: #{new_link[:old_path]}"
FileUtils.ln_sf new_link[:old_path], new_link[:new_path]
else
print "Warn! | Prompt | #{new_link[:new_path]} already exists and points to #{old_link[:old_path]}! overwrite? (y/n): "
input = gets.chomp.strip.downcase
if ['y', 'yes'].include? input
puts "Warn! | Overwrite | path: #{new_link[:new_path]}, old: #{old_link[:old_path]}, new: #{new_link[:old_path]}"
FileUtils.ln_sf new_link[:old_path], new_link[:new_path]
else
puts "Info | Info | not overwriting link: #{new_link[:new_path]}, current_val: #{old_link[:old_path]}"
end
end
end
def maybe_overwrite_file!(old_file, new_link, noinput = false)
if noinput
puts "Warn! | Overwrite | file: #{new_link[:new_path]}"
FileUtils.rm old_file[:path]
FileUtils.ln_s new_link[:old_path], new_link[:new_path]
else
print "Warn! | Prompt | #{old_file[:path]} already exists! overwrite? (y/n): "
input = gets.chomp.strip.downcase
if ['y', 'yes'].include? input
puts "Warn! | Overwrite | file: #{old_file[:path]}"
FileUtils.rm old_file[:path]
FileUtils.ln_s new_link[:old_path], new_link[:new_path]
else
puts "Info | Info | not overwriting file: #{new_link[:new_path]}"
end
end
end
def symlink_file!(new_link, noinput = false)
old_path, new_path = new_link[:old_path], new_link[:new_path]
old_file = current_file(new_link)
case old_file[:type]
when :directory
puts "Error! | Exists | #{new_path} : directory"
false
when :symlink
if old_file[:old_path] != old_path
maybe_overwrite_symlink!(old_file, new_link, noinput)
else
puts "Info | Exists | #{new_path} --> #{old_path}"
end
when :file
maybe_overwrite_file!(old_file, new_link, noinput)
when nil
puts "Info | Linking | #{new_path} --> #{old_path}"
FileUtils.ln_s new_link[:old_path], new_link[:new_path]
end
end
# /utils ------------------------------------------------------------------ }}}
# rake tasks -------------------------------------------------------------- {{{
task :ensure_directories_exist do
directory_listing.each do |d|
puts "Info | Touch | #{d[:new_path]}"
FileUtils.mkdir_p d[:new_path]
end
end
task :ensure_locals_exist do
locals_listing.each do |f|
if !current_file(f)[:type]
FileUtils.touch f[:old_path]
symlink_file!(f, true)
else
puts "Info | Exists | #{f[:new_path]}"
FileUtils.touch f[:new_path]
end
end
end
task :ensure_links_exist => [:ensure_directories_exist, :ensure_locals_exist] do
dotfile_listing.each do |f|
symlink_file!(f, true)
end
end
task :uninstall do
locals_listing.each do |f|
if current_file(f)[:type] == :symlink
puts "Warn! | Unlink | file: #{f[:new_path]} --> #{f[:old_path]}"
File.unlink f[:new_path]
end
end
dotfile_listing.each do |f|
puts "Warn! | Unlink | file: #{f[:new_path]} --> #{f[:old_path]}"
File.unlink f[:new_path]
end
end
task :install => :ensure_links_exist
task :default => :install
# /rake tasks ------------------------------------------------------------- }}}