-
Notifications
You must be signed in to change notification settings - Fork 21
/
Rakefile
558 lines (531 loc) · 16.2 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#------------------------------------------------------------------------------
# FILE: Rakefile
# DESCRIPTION: Installs and uninstalls dot files.
# AUTHOR: Sorin Ionescu <[email protected]>
# VERSION: 2.1.5
#------------------------------------------------------------------------------
require 'date'
require 'open3'
require 'fileutils'
require 'rubygems'
require 'rake'
require 'erb'
# Raised when a Keychain item is not found.
class KeychainError < Exception
end
# Utility function for displaying messages.
def info(text)
STDOUT.puts text
end
# Utility function for displaying error messages.
def error(text)
STDERR.puts "Error: #{text}"
end
# This Rakefile is written for Mac OS X system Ruby.
if RUBY_VERSION >= '1.9'
error "Ruby 1.8.7 is required to run this Rakefile"
exit 1
end
RAW_FILE_EXTENSION = 'rrc'
RAW_FILE_EXTENSION_REGEXP = /\.#{RAW_FILE_EXTENSION}$/
KEYCHAIN_GENERIC_PASSWORD_COMMAND = 'security find-generic-password -gl'
KEYCHAIN_INTERNET_PASSWORD_COMMAND = 'security find-internet-password -gl'
ACCOUNT_REGEXP = /"acct"<blob>=(?:0x([0-9A-F]+)\s*)?(?:"(.*)")?$/
PASSWORD_REGEXP = /^password: (?:0x([0-9A-F]+)\s*)?(?:"(.*)")?$/
SCRIPT_PATH = File.split(File.expand_path(__FILE__))
SCRIPT_NAME = SCRIPT_PATH.last
CONFIG_DIR_PATH = SCRIPT_PATH.first
BACKUP_DIR_PATH = File.join(
ENV['HOME'],
'.dotfiles_backup',
DateTime.now.strftime('%Y-%m-%d-%H-%M-%S'))
EXCLUDES = [
SCRIPT_NAME,
'.DS_Store',
'.bzr',
'.git',
'.gitignore',
'.gitmodules',
'.hg',
'.hgignore',
'.hgmodules',
'.hgtags',
'.svn',
'_MTN',
'_darcs',
'README.md',
/.*~$/,
/^\#.*\#$/,
/backup\/.*$/,
/vim\/(backup|swap|undo|view)\/.*$/
]
BUNDLE_DIR_PATH = File.join(CONFIG_DIR_PATH, 'vim/bundle')
VUNDLE_DIR_PATH = File.join(BUNDLE_DIR_PATH, 'vundle')
VUNDLE_REMOTE_URL = 'https://github.com/gmarik/vundle.git'
# Wrapper around Keychain.
module Keychain
# Holds previously requested Keychain items.
@@cache = {}
# Wrapper around a Keychain item.
class Item
# Returns the accout name.
attr_reader :account
# Returns the account password.
attr_reader :password
# Returns a new Keychain item.
#
# @param [String] account the account name.
# @param [String] password the account password.
# @return [Item] the Keychain item.
def initialize(account, password)
@account = account or raise ArgumentError, "Account cannot be nil"
@password = password or raise ArgumentError, "Password cannot be nil"
end
end
# Returns a Keychain item.
#
# @param [String] label the Keychain item label.
# @return [Item] the Keychain item.
def self.[](label)
return @@cache[label] if @@cache.has_key? label
retry_times = 2
keychain_command = KEYCHAIN_INTERNET_PASSWORD_COMMAND
begin
stdin, stdout, stderr = Open3.popen3("#{keychain_command} '#{label}'")
output = stdout.readlines.join + stderr.readlines.join
[stdin, stdout, stderr].each { |stdio| stdio.close }
if output =~ /The specified item could not be found in Keychain\./
raise NameError
end
# The field value is stored in hexademical (one) or string (two).
field_value = lambda do |one, two|
return one.scan(/../).map { |tuple| tuple.hex.chr }.join unless one.nil?
return two unless two.nil?
return ""
end
account = \
output[ACCOUNT_REGEXP].gsub!(ACCOUNT_REGEXP) { field_value[$1, $2] }
password = \
output[PASSWORD_REGEXP].gsub!(PASSWORD_REGEXP) { field_value[$1, $2] }
@@cache[label] = Item.new(account, password)
rescue NameError
keychain_command = KEYCHAIN_GENERIC_PASSWORD_COMMAND
retry_times -= 1
if retry_times > 0
retry
else
raise KeychainError,
"Item '#{label}' could not be found in Keychain"
end
rescue IOError
raise KeychainError,
"Could not communicate with Keychain for item '#{label}'"
end
end
end
# Moves an existing dot file into the backup directory.
#
# @param [String] from the file to back up.
# @param [String] to the backup destination.
def backup(from, to)
return unless File.exists? from
FileUtils.mkdir_p(File.dirname(to))
File.rename(from, to)
end
# Returns whether a path is excluded from linking into the home directory.
#
# @param [String] path the path a to file or directory.
# @return [true, false] if true, the path is excluded; otherwise, it is not.
def excluded?(path)
strings = EXCLUDES.select { |item| item.class == String }
regexps = EXCLUDES.select { |item| item.class == Regexp }
excluded = strings.include? path
regexps.each do |pattern|
excluded = true if path =~ pattern
end
return excluded
end
# Returns whether a command exists in PATH.
#
# @param [String] command the name of the command.
# @return [true, false] if true, the command exists; otherwise, it does not.
def exists?(command)
ENV['PATH'].split(':').any? do |directory|
File.exists?(File.join(directory, command))
end
end
# Returns the absolute path to a Vim bundle.
#
# @param [String] line the line to be parsed.
# @return [String] the absolute path to a bundle.
def bundle_path(line)
# Strip ANSI escape sequences (may output junk whitespace).
line.gsub!(/\e\[?.*?[\@-~]/, '')
if line =~ /Processing '([^']+)'/
# Get the bundle URL.
bundle_url = $1
# Strip junk whitespace.
bundle_url.gsub!(/\s+/, '')
# Strip .git extension from full URLs.
bundle_url.gsub!(/\.git$/, '')
return File.join(BUNDLE_DIR_PATH, File.split(bundle_url).last)
end
end
namespace :link do
desc 'Symlink dot files'
task :create do
Dir["#{CONFIG_DIR_PATH}/*"].each do |source|
target_relative = source.gsub("#{CONFIG_DIR_PATH}/", '')
target_backup = File.join(BACKUP_DIR_PATH, target_relative)
target = File.join(ENV['HOME'], ".#{target_relative}")
# Do not link if the source is a raw file, the target already exists and
# is a symlink to the source.
next if source =~ RAW_FILE_EXTENSION_REGEXP \
or excluded?(target_relative) \
or (File.exists?(target) \
and File.ftype(target) == 'link' \
and File.identical?(source, target))
info "Linking: #{target}"
begin
backup(target, target_backup)
rescue IOError
error "Could not backup '#{target}', will skip symlinking '#{source}'"
next
end
begin
File.symlink(source, target)
rescue IOError
error "Could not symlink '#{source}' to '#{target}'"
end
end
end
desc 'Unlink broken symlinks'
task :clean do
Dir["#{ENV['HOME']}/.*"].each do |item|
if File.ftype(item) == 'link'
unless File.exists? item
info "Unlinking: #{item}"
begin
File.unlink item
rescue IOError
error "Could not unlink '#{item}'"
end
end
end
end
end
end
namespace :module do
desc 'Initialize submodules'
task :init do
if File.exists? '.gitmodules'
unless exists?('git')
error "Could not initialize submodules, Git is not found"
next
end
# Popen3 does not return the exit status code.
# Echo it onto the last line of stderr.
Open3.popen3(
"git submodule update --init --recursive; echo $? 1>&2"
) do |stdin, stdout, stderr|
stdios = [stdin, stdout, stderr]
threads = []
threads << Thread.new do
Thread.current.abort_on_exception = true
stdout.each do |line|
next if line !~ /^Cloning into .*\.{3}$/
info line.gsub(/^Cloning into (.*)\.{3}$/, "Initializing: \\1")
end
end
threads << Thread.new do
Thread.current.abort_on_exception = true
stderr.each do |line|
if line =~ /Unable to checkout '[^']+' in submodule path '([^']+)'/
error "Could not initialize submodule '#{$1}'"
end
if stderr.eof? and line.to_i != 0
error "Could not initialize submodules"
end
end
end
begin
threads.each(&:join)
stdios.each(&:close)
rescue Exception => e
error e.message if e.class == IOError
end
end
end
end
desc 'Update submodules'
task :update do
if File.exists? '.gitmodules'
unless exists?('git')
error "Could not update submodules, Git is not found"
next
end
# Popen3 does not return the exit status code.
# Echo it onto the last line of stderr.
Open3.popen3(
"git submodule foreach git pull origin master; echo $? 1>&2"
) do |stdin, stdout, stderr|
stdios = [stdin, stdout, stderr]
threads = []
threads << Thread.new do
stdout.each do |line|
if line =~ /Entering '([^']+)'/
info "Updating: #{$1}"
end
end
end
threads << Thread.new do
stderr.each do |line|
if line =~ /Stopping at '([^']+)'/
error "Could not update submodule '#{$1}'"
end
if stderr.eof? and line.to_i != 0
error "Could not update submodules"
end
end
end
begin
threads.each(&:join)
stdios.each(&:close)
Rake::Task[:make].invoke
rescue Exception => e
error e.message if e.class == IOError
end
end
end
end
end
namespace :bundle do
desc 'Initialize Vim bundles'
task :init do
next unless File.exists?('vimrc') and File.directory?('vim')
next if File.directory?(File.join(VUNDLE_DIR_PATH, '.git'))
unless exists?('git')
error "Could not initialize Vim bundles, Git is not found"
next
end
info "Initializing: #{VUNDLE_DIR_PATH}".gsub("#{CONFIG_DIR_PATH}/", '')
Open3.popen3(
"git clone '#{VUNDLE_REMOTE_URL}' '#{VUNDLE_DIR_PATH}'; echo $? 1>&2"
) do |stdin, stdout, stderr|
vundle = File.basename VUNDLE_DIR_PATH
stdios = [stdin, stdout, stderr]
threads = []
threads << Thread.new do
Thread.current.abort_on_exception = true
stderr.each do |line|
if line =~ /^fatal: (.*)$/
error "Could not initialize Vim bundle '#{vundle}'"
end
if stderr.eof? and line.to_i != 0
error "Could not initialize Vim bundles"
end
end
end
begin
threads.each(&:join)
stdios.each(&:close)
rescue Exception => e
error e.message if e.class == IOError
end
end
end
desc 'Update Vim bundles'
task :update => :init do
next unless File.directory?(File.join(VUNDLE_DIR_PATH, '.git'))
unless exists?('git')
error "Could not update Vim bundles, Git is not found"
next
end
Open3.popen3(
"vim -c 'silent!" +
"redir >> /dev/stdout " +
"| execute \"BundleInstall!\" " +
"| only " +
"| quitall!';" +
"echo $? 1>&2"
) do |stdin, stdout, stderr|
stdios = [stdin, stdout, stderr]
threads = []
threads << Thread.new do
Thread.current.abort_on_exception = true
# Vim will not run without reading stdout,
# might as well parse its output.
stdout.each do |line|
bundle = bundle_path(line)
next unless bundle
bundle_relative = bundle.gsub("#{CONFIG_DIR_PATH}/", '')
info "Updating: #{bundle_relative}"
if line =~ /Error processing '([^']+)'/
error "Could not update Vim bundle '#{bundle_relative}'"
end
end
end
threads << Thread.new do
Thread.current.abort_on_exception = true
stderr.each do |line|
if stderr.eof? and line.to_i != 0
error "Could not update Vim bundles"
end
end
end
begin
threads.each(&:join)
stdios.each(&:close)
rescue Exception => e
error e.message if e.class == IOError
end
end
end
desc 'Clean Vim bundles'
task :clean do
next unless File.exists?('vimrc') and File.directory?('vim/bundle/vundle')
Open3.popen3(
"vim -c 'silent!" +
"redir >> /dev/stdout " +
"| execute \"BundleClean!\" "+
"| only " +
"| quitall!'; " +
"echo $? 1>&2"
) do |stdin, stdout, stderr|
stdios = [stdin, stdout, stderr]
threads = []
threads << Thread.new do
Thread.current.abort_on_exception = true
# Vim will not run without reading stdout,
# might as well parse its output.
stdout.each do |line|
bundle = bundle_path(line)
next unless bundle
bundle_relative = bundle.gsub("#{CONFIG_DIR_PATH}/", '')
info "Removing: #{bundle_relative}"
if line =~ /Error processing '([^']+)'/
error "Could not remove Vim bundle '#{bundle_relative}'"
end
end
end
threads << Thread.new do
Thread.current.abort_on_exception = true
stderr.each do |line|
if stderr.eof? and line.to_i != 0
error "Could not clean Vim bundles"
end
end
end
begin
threads.each(&:join)
stdios.each(&:close)
rescue Exception => e
error e.message if e.class == IOError
end
end
end
end
desc 'Render raw dot files'
task :render do
Dir["#{CONFIG_DIR_PATH}/**/*.#{RAW_FILE_EXTENSION}"].each do |source|
target = source.gsub(RAW_FILE_EXTENSION_REGEXP, '')
next if excluded? source
if File.file? source
begin
source_contents = File.read source
source_contents = ERB.new(source_contents).result(binding)
rescue IOError
error "Could not read raw file '#{source}'"
rescue NameError, SyntaxError => e
error "Could not render raw file '#{source}'.\n\n#{e.message}"
rescue KeychainError => e
error e.message
end
begin
target_contents = File.exists?(target) ? File.read(target) : nil
# Only overwrite the rendered dot file if the raw file has changed.
if source_contents != target_contents
File.open(target, 'w') do |file|
info "Writing: #{target}"
file.write source_contents
end
end
rescue IOError
error "Could not write file '#{target}'"
end
end
end
end
desc 'Make submodules and bundles'
task :make do
Dir["#{CONFIG_DIR_PATH}/**/Rakefile"].each do |rake_file|
next if SCRIPT_PATH.join('/') == rake_file
submodule = File.dirname rake_file
submodule_relative = submodule.gsub("#{CONFIG_DIR_PATH}/", '')
read, write = IO.pipe
pid = fork do
Dir.chdir submodule
Rake::Task.clear
load rake_file
next unless Rake::Task.task_defined?(:make)
info "Making: #{submodule_relative}"
# Redirect stdout, stderr since make is noisy.
stdout_old = STDOUT.clone
stderr_old = STDERR.clone
begin
STDOUT.reopen write
STDERR.reopen STDOUT
read.close
Rake::Task[:make].invoke
rescue Exception => e
STDOUT.reopen stdout_old
STDERR.reopen stderr_old
error e.message
end
end
begin
write.close
read.each do |line|
if read.eof? and line =~ /error:/i
error "Could not make '#{submodule_relative}'"
end
end
rescue IOError => e
error e.message
end
Process.waitpid pid
end
end
desc 'Uninstall dot files'
task :uninstall => 'link:clean' do
Dir["#{CONFIG_DIR_PATH}/*"].each do |source|
target_relative = source.gsub("#{CONFIG_DIR_PATH}/", '')
target = File.join(ENV['HOME'], ".#{target_relative}")
next if source =~ RAW_FILE_EXTENSION_REGEXP or excluded?(target_relative)
# Uninstall only if the target exists, is a symlink, and points to source.
if File.exists?(target) \
and File.ftype(target) == 'link' \
and File.identical?(source, target)
info "Unlinking: #{target}"
begin
File.unlink target
rescue IOError
error "Could not unlink '#{target}'"
end
end
end
end
desc 'Install dot files'
task :install => [
'module:init',
'render',
'link:create',
'link:clean',
'bundle:init',
'bundle:update',
'bundle:clean',
'make'
] do
info "Backup: #{BACKUP_DIR_PATH}" if File.directory? BACKUP_DIR_PATH
end
task :default => [:install]