forked from HenryHo2015/maximum-awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
307 lines (264 loc) · 8.54 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
ENV['HOMEBREW_CASK_OPTS'] = "--appdir=/Applications"
def brew_install(package, *args)
versions = `brew list #{package} --versions`
options = args.last.is_a?(Hash) ? args.pop : {}
# if brew exits with error we install tmux
if versions.empty?
sh "brew install #{package} #{args.join ' '}"
elsif options[:requires]
# brew did not error out, verify tmux is greater than 1.8
# e.g. brew_tmux_query = 'tmux 1.9a'
installed_version = versions.split(/\n/).first.split(' ')[1]
unless version_match?(options[:requires], installed_version)
sh "brew upgrade #{package} #{args.join ' '}"
end
end
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 brew_cask_install(package, *options)
output = `brew cask info #{package}`
return unless output.include?('Not installed')
sh "brew cask install #{package} #{options.join ' '}"
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 Homebrew Cask'
task :brew_cask do
step 'Homebrew Cask'
system('brew untap phinze/cask') if system('brew tap | grep phinze/cask > /dev/null')
unless system('brew tap | grep caskroom/cask > /dev/null') || system('brew tap caskroom/homebrew-cask')
abort "Failed to tap caskroom/homebrew-cask in Homebrew."
end
brew_install 'brew-cask'
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'
brew_cask_install '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 tmux'
task :tmux do
step 'tmux'
# tmux copy-pipe function needs tmux >= 1.8
brew_install 'tmux', :requires => '>= 1.8'
end
desc 'Install MacVim'
task :macvim do
step 'MacVim'
unless app? 'MacVim'
brew_cask_install 'macvim'
end
bin_dir = File.expand_path('~/bin')
bin_vim = File.join(bin_dir, 'vim')
unless ENV['PATH'].split(':').include?(bin_dir)
puts 'Please add ~/bin to your PATH, e.g. run this command:'
puts
puts %{ echo 'export PATH="~/bin:$PATH"' >> ~/.bashrc}
puts
puts 'The exact command and file will vary by your shell and configuration.'
end
FileUtils.mkdir_p(bin_dir)
unless File.executable?(bin_vim)
File.open(bin_vim, 'w', 0744) do |io|
io << <<-SHELL
#!/bin/bash
exec /Applications/MacVim.app/Contents/MacOS/Vim "$@"
SHELL
end
end
end
desc 'Install Vundle'
task :vundle do
step 'vundle'
install_github_bundle 'gmarik','vundle'
sh '~/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',
'tmux.conf.local' => '~/.tmux.conf.local'
)
LINKED_FILES = filemap(
'vim' => '~/.vim',
'tmux.conf' => '~/.tmux.conf',
'vimrc' => '~/.vimrc',
'vimrc.bundles' => '~/.vimrc.bundles'
)
desc 'Install these config files.'
task :install do
Rake::Task['install:brew'].invoke
Rake::Task['install:brew_cask'].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
Rake::Task['install:tmux'].invoke
Rake::Task['install:macvim'].invoke
# TODO install gem ctags?
# TODO run gem ctags?
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 colorschemes'
colorschemes = `defaults read com.googlecode.iterm2 'Custom Color Presets'`
dark = colorschemes !~ /Solarized Dark/
light = colorschemes !~ /Solarized Light/
sh('open', '-a', '/Applications/iTerm.app', File.expand_path('iterm2-colors-solarized/Solarized Dark.itermcolors')) if dark
sh('open', '-a', '/Applications/iTerm.app', File.expand_path('iterm2-colors-solarized/Solarized Light.itermcolors')) if light
step 'iterm2 profiles'
puts
puts " Your turn!"
puts
puts " Go and manually set up Solarized Light and Dark profiles in iTerm2."
puts " (You can do this in 'Preferences' -> 'Profiles' by adding a new profile,"
puts " then clicking the 'Colors' tab, 'Load Presets...' and choosing a Solarized option.)"
puts " Also be sure to set Terminal Type to 'xterm-256color' in the 'Terminal' tab."
puts
puts " Enjoy!"
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'
step 'macvim'
puts
puts 'Run this to uninstall MacVim:'
puts
puts ' rm -rf /Applications/MacVim.app'
end
task :default => :install