-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathRakefile
executable file
·234 lines (202 loc) · 6.6 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# Most of this is thanks to Square!
ENV['HOMEBREW_CASK_OPTS'] = "--appdir=/Applications"
def brew_install(package, *args)
versions = `brew list #{package} --versions`
options = args.last.is_a?(Hash) ? args.pop : {}
end
def version_match?(requirement, version)
# This is a hack, but it lets us avoid a gem dep for version checking.
# Gem dependencies must be numeric, so we remove non-numeric characters here.
version.gsub!(/[a-zA-Z]/, '')
Gem::Dependency.new('', requirement).match?('', version)
end
def install_github_bundle(user, package)
unless File.exist? File.expand_path("~/.vim/bundle/#{package}")
sh "git clone https://github.com/#{user}/#{package} ~/.vim/bundle/#{package}"
end
end
def step(description)
description = "-- #{description} "
description = description.ljust(80, '-')
puts
puts "\e[32m#{description}\e[0m"
end
def app_path(name)
path = "/Applications/#{name}.app"
["~#{path}", path].each do |full_path|
return full_path if File.directory?(full_path)
end
return nil
end
def app?(name)
return !app_path(name).nil?
end
def get_backup_path(path)
number = 1
backup_path = "#{path}.bak"
loop do
if number > 1
backup_path = "#{backup_path}#{number}"
end
if File.exists?(backup_path) || File.symlink?(backup_path)
number += 1
next
end
break
end
backup_path
end
def link_file(original_filename, symlink_filename)
original_path = File.expand_path(original_filename)
symlink_path = File.expand_path(symlink_filename)
if File.exists?(symlink_path) || File.symlink?(symlink_path)
if File.symlink?(symlink_path)
symlink_points_to_path = File.readlink(symlink_path)
return if symlink_points_to_path == original_path
# Symlinks can't just be moved like regular files. Recreate old one, and
# remove it.
ln_s symlink_points_to_path, get_backup_path(symlink_path), :verbose => true
rm symlink_path
else
# Never move user's files without creating backups first
mv symlink_path, get_backup_path(symlink_path), :verbose => true
end
end
ln_s original_path, symlink_path, :verbose => true
end
def unlink_file(original_filename, symlink_filename)
original_path = File.expand_path(original_filename)
symlink_path = File.expand_path(symlink_filename)
if File.symlink?(symlink_path)
symlink_points_to_path = File.readlink(symlink_path)
if symlink_points_to_path == original_path
# the symlink is installed, so we should uninstall it
rm_f symlink_path, :verbose => true
backups = Dir["#{symlink_path}*.bak"]
case backups.size
when 0
# nothing to do
when 1
mv backups.first, symlink_path, :verbose => true
else
$stderr.puts "found #{backups.size} backups for #{symlink_path}, please restore the one you want."
end
else
$stderr.puts "#{symlink_path} does not point to #{original_path}, skipping."
end
else
$stderr.puts "#{symlink_path} is not a symlink, skipping."
end
end
namespace :install do
desc 'Update or Install Brew'
task :brew do
step 'Homebrew'
unless system('which brew > /dev/null || ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"')
raise "Homebrew must be installed before continuing."
end
end
desc 'Install The Silver Searcher'
task :the_silver_searcher do
step 'the_silver_searcher'
brew_install 'the_silver_searcher'
end
desc 'Install iTerm'
task :iterm do
step 'iterm2'
unless app? 'iTerm'
sh "brew install --cask iterm2"
end
end
desc 'Install ctags'
task :ctags do
step 'ctags'
brew_install 'ctags'
end
desc 'Install reattach-to-user-namespace'
task :reattach_to_user_namespace do
step 'reattach-to-user-namespace'
brew_install 'reattach-to-user-namespace'
end
desc 'Install Vundle'
task :vundle do
step 'vundle'
install_github_bundle 'VundleVim','Vundle.vim'
sh '/usr/bin/vim -c "PluginInstall!" -c "q" -c "q"'
end
end
def filemap(map)
map.inject({}) do |result, (key, value)|
result[File.expand_path(key)] = File.expand_path(value)
result
end.freeze
end
COPIED_FILES = filemap(
'vimrc.local' => '~/.vimrc.local',
'vimrc.bundles.local' => '~/.vimrc.bundles.local',
)
LINKED_FILES = filemap(
'vim' => '~/.vim',
'vimrc' => '~/.vimrc',
'vimrc.bundles' => '~/.vimrc.bundles'
)
desc 'Install these config files.'
task :install do
Rake::Task['install:brew'].invoke
Rake::Task['install:the_silver_searcher'].invoke
Rake::Task['install:iterm'].invoke
Rake::Task['install:ctags'].invoke
Rake::Task['install:reattach_to_user_namespace'].invoke
step 'symlink'
LINKED_FILES.each do |orig, link|
link_file orig, link
end
COPIED_FILES.each do |orig, copy|
cp orig, copy, :verbose => true unless File.exist?(copy)
end
# Install Vundle and bundles
Rake::Task['install:vundle'].invoke
step 'iterm2 profiles'
puts
puts " All done! That was easy!"
puts
puts " Now, for your iTerm colors, you can manually set up your base16 color profiles."
puts " You can do this in 'Preferences' -> 'Profiles' by adding a new profile,"
puts " then clicking the 'Colors' tab, 'Load Presets...' and importing all the colors in the iterm-colors folder."
puts
puts " Then, set Terminal Type to 'xterm-256color' in the 'Terminal' tab."
puts " Then, clone the repository for base16-shell:"
puts " $ git clone https://github.com/chriskempson/base16-shell.git ~/.config/base16-shell"
puts
puts " Once that's done, add these lines to your ~/.bashrc, ~/.bash_profile, or ~/.zshrc:"
puts ' BASE16_SHELL=$HOME/.config/base16-shell/'
puts ' [ -n "$PS1" ] && [ -s $BASE16_SHELL/profile_helper.sh ] && eval "$($BASE16_SHELL/profile_helper.sh)"'
puts
puts " And then, open a new shell and type:"
puts " $ base16_default-dark"
puts " (there are other commands available, try typing base16 and hit tab to see the options)"
puts
puts " And you're done! Have fun with your new and improved vim!"
puts
end
desc 'Uninstall these config files.'
task :uninstall do
step 'un-symlink'
# un-symlink files that still point to the installed locations
LINKED_FILES.each do |orig, link|
unlink_file orig, link
end
# delete unchanged copied files
COPIED_FILES.each do |orig, copy|
rm_f copy, :verbose => true if File.read(orig) == File.read(copy)
end
step 'homebrew'
puts
puts 'Manually uninstall homebrew if you wish: https://gist.github.com/mxcl/1173223.'
step 'iterm2'
puts
puts 'Run this to uninstall iTerm:'
puts
puts ' rm -rf /Applications/iTerm.app'
end
task :default => :install