Repository navigation
Expand file tree
/
Copy pathRakefile
More file actions
452 lines (392 loc) · 12.6 KB
/
Copy pathRakefile
File metadata and controls
452 lines (392 loc) · 12.6 KB
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
require 'rake'
require 'pathname'
require 'erb'
require 'yaml/store'
begin
if require 'colorize'
COLORS = true
end
rescue Exception => e
COLORS = false
end
DEBUG = false
DOTFILE_EXT = "symlink"
ERB_EXT = "erb"
BACKUP_EXT = "bak"
REPO = File.join(ENV['HOME'], ".dotfiles")
CONFIG_FILE = ".dotrc"
PATH_FOR_DIRS = ""
PATH_FOR_BINS = "_bin"
PATH_FOR_OH_MY_ZSH = ".oh-my-zsh"
CONFIG = YAML::Store.new File.join(REPO, CONFIG_FILE)
LINK_OPTIONS = [:skip,:skip_all,:overwrite,:overwrite_all,:backup,:backup_all]
CONFIG_OPTIONS = [:abort,:overwrite]
DEFAULT_IGNORE_FILES = [
".git",
".gitmodules",
".gitignore",
"Rakefile",
"README",
]
DEFAULT_ANSWERS = {
:skip => false,
:skip_all => false,
:overwrite => false,
:overwrite_all => false,
:backup => false,
:backup_all => false
}
module TaskMethods
def task_add args
expanded_glob = File.expand_path(args[:glob])
remove_pseudo_dirs(expanded_glob).each do |filename|
if !File.file?(filename) && !File.directory?(filename)
puts colorize_if_installed("File #{filename} isn't a file or directory.", :red)
next
end
if File.symlink?(filename)
puts colorize_if_installed("File #{filename} is already a symlink.", :red)
next
end
if File.dirname(filename).include? REPO
puts colorize_if_installed("File '#{filename}' does already exist in repository (#{REPO}).", :red)
next
end
target = dotfile_of(filename)
expanded_target = File.join(REPO, target)
FileUtils.mv(filename, expanded_target, :verbose => true, :noop => DEBUG)
link_files [expanded_target]
end
end
def task_dotfiles
files = dotfiles
puts "Rake is managing #{colorize_if_installed files.count, :green} files:"
puts ""
files.each do |dotfile|
puts " #{colorize_if_installed "*", :green} #{dotfile.gsub(REPO+'/', '')}"
end
end
def task_install_bin
bins = Dir["#{REPO}/#{PATH_FOR_BINS}/*"]
puts "No binaries!" if BINS.empty?
bins.each do |file|
target_file = File.join(target_dir, 'sys', 'bin', Pathname.new(file).basename)
FileUtils.cp file, target_file, :preserve => true, :verbose => true, :noop => DEBUG
puts "Installed #{file} to #{target_file}"
end
end
def task_uninstall
uninstall dotfile_targets
end
def task_setup
FileUtils.mkdir_p REPO
answer = ask colorize_if_installed("File already exists: #{CONFIG_FILE}, what do you want to do?", :red), :answers => CONFIG_OPTIONS
if answer == :overwrite
CONFIG.transaction do
CONFIG["ignore"] = DEFAULT_IGNORE_FILES.map(&:to_s)
end
end
end
def task_install
link_files dotfiles
end
def task_remove args
# TODO
expanded_glob = File.expand_path(args[:glob])
file_is_in_repo = File.dirname(expanded_glob).include? REPO
source = file_is_in_repo ? target_of(expanded_glob) : dotfile_of(expanded_glob)
target = file_is_in_repo ? dotfile_of(expanded_glob) : target_of(expanded_glob)
FileUtils.copy_file source, target, :noop => DEBUG
end
def task_sync
task_uninstall
task_install
end
end
include TaskMethods
module Util
module Dotfile
def rm_erb_ext file
file.sub(/\.#{ERB_EXT}$/, '')
end
def rm_basename_ext file
path = Pathname.new(file)
path.basename.sub(/\.(#{DOTFILE_EXT}|#{ERB_EXT})$/, '').to_s
end
def target_of file, options={:remove_dot => true}
basename = rm_basename_ext(file)
basename = basename[1..-1] if options[:remove_dot] && basename.start_with?(".") # removes first "."
File.join(ENV["HOME"], ".#{basename}")
end
def backup_of file, options={:remove_dot => true}
File.join(ENV['HOME'], "#{options[:remove_dot] ? '' : '.'}#{rm_basename_ext(file)}.#{BACKUP_EXT}")
end
# Examples:
# * .../file -> ~/.dotfiles/file.symlink
# * .../dir -> ~/.dotfiles/PATH_FOR_DIRS/dir.symlink
def dotfile_of file
is_dir = File.directory? file
basename = rm_basename_ext file
basename = basename[1..-1] if basename.start_with? "." # remove first "."
path = Pathname.new(".") # like empty pathname
path += Pathname.new(PATH_FOR_DIRS) if is_dir
path += "#{basename}.#{DOTFILE_EXT}"
end
def dotfiles
files = Dir["#{REPO}/**/*.{#{DOTFILE_EXT},#{ERB_EXT}}"]
ignore_files = []
CONFIG.transaction { ignore_files = CONFIG["ignore"] }
files.reject! { |f| ignore_files.any? { |re| f.match(/#{re}\//) } }
if block_given?
files.each { |file| yield file }
end
files
end
def dotfile_targets
files = dotfiles.map { |dotfile| target_of(dotfile) }
if block_given?
files.each { |file| yield file }
end
files
end
end
include Dotfile
module DotfileUtils
def replace_file file
FileUtils.rm_rf target_of(file), :noop => DEBUG
link_file file
end
def link_file file
target = target_of file
if file =~ /.#{ERB_EXT}$/
generate_file file, :noop => DEBUG
else
puts "linking to #{target}"
FileUtils.ln_s(file, target, :verbose => false, :noop => DEBUG)
end
end
def link_files dotfiles
answers = DEFAULT_ANSWERS
dotfiles.each do |dotfile|
update_answers! answers
target = target_of dotfile
if File.exists?(target) || File.symlink?(target)
unless answers[:skip_all] || answers[:overwrite_all] || answers[:backup_all]
answer = ask_linking dotfile
answers[answer] = true
next if answers[:skip]
end
end
FileUtils.rm_rf(target, :noop => DEBUG) if answers[:overwrite] || answers[:overwrite_all]
backup_file dotfile if answers[:backup] || answers[:backup_all]
if !answers[:skip_all] || !File.exist?(target)
link_file dotfile
end
end
end
def generate_file file, options={}
target = target_of file
puts "generating #{target}"
return if options[:noop]
File.open(target, 'w') do |new_file|
CONFIG.transaction do
new_file.write ERB.new(File.read(file)).result(binding)
end
end
rescue Interrupt
end
def backup_file file
src_file = target_of(file, :remove_dot => false)
backup_file = backup_of(file, :remove_dot => false)
puts "backup to #{backup_file}"
if File.directory? file
FileUtils.cp_r(src_file, backup_file, :noop => DEBUG)
else
FileUtils.cp(src_file, backup_file, :noop => DEBUG)
end
end
def restore_file file
backup_file = backup_of(file)
orig_file = target_of(file, :remove_dot => false)
puts "restore #{backup_file}"
FileUtils.mv(backup_file, orig_file, :verbose => true, :noop => DEBUG)
end
def rm_backup_file file
backup = backup_of file
puts "rm backup #{backup}"
FileUtils.rm_f(file, :noop => DEBUG)
end
end
include DotfileUtils
module ZSH
def switch_to_zsh
if ENV["SHELL"] =~ /zsh/
puts "Using ZSH"
else
print "Switch to ZSH? (recommended) [ynq] "
case $stdin.gets.chomp
when 'y'
puts "Switching to ZSH"
system %Q{chsh -s `which zsh`}
when 'q'
exit
else
puts "Skipping ZSH"
end
end
end
def install_oh_my_zsh
if File.exist?(File.join(ENV['HOME'], "#{PATH_FOR_OH_MY_ZSH}"))
puts "Found #{ENV['HOME']}/#{PATH_FOR_OH_MY_ZSH}"
else
print "Install oh-my-zsh? [ynq] "
case $stdin.gets.chomp
when 'y'
puts "Installing oh-my-zsh"
system %Q{git clone https://github.com/robbyrussell/oh-my-zsh.git "#{ENV['HOME']}/#{PATH_FOR_OH_MY_ZSH}"}
when 'q'
exit
else
puts "Skipping oh-my-zsh, you will need to change #{ENV['HOME']}/.zshrc"
end
end
end
end
include ZSH
module HumanInteraction
def colorize_if_installed string, color
string = string.to_s unless string.is_a? String
COLORS ? string.colorize(color) : string
end
def ask msg, options={}
if options[:answers]
answers = options[:answers].reduce({}) { |hash,key| hash.merge({key => true}) } # first_key => true,...
options.merge! answers do |key, old, new|
old && new
end
options.delete(:answers)
end
options = options.select { |k,v| v }.keys # select all keys if true
hash = options.reduce({}) do |hash, sym|
indicator = sym.to_s[0]
indicator.upcase! if sym.to_s.end_with? "all"
hash.merge({indicator.to_sym => sym})
end
options_str = hash.map { |indicator, opt|
desc = opt.to_s[1..-1]
desc = desc.gsub('_', ' ') if opt.to_s.end_with? "all"
"[#{colorize_if_installed(indicator, :blue)}]#{desc}"
}.join(", ")
puts "#{msg} #{options_str}"
answer = STDIN.gets.chomp
hash[answer.to_sym] || ask(file, options)
end
def ask_linking dotfile
ask colorize_if_installed("File already exists: #{target_of(dotfile)}, what do you want to do?", :red), :answers => LINK_OPTIONS
end
def update_answers! answers
no_globals = answers.keys.select { |a| !a.to_s.end_with? "all" }
no_globals.each { |a| answers[a] = false }
end
def gets msg
print(msg)
STDOUT.flush
STDIN.gets.chomp
end
end
include HumanInteraction
def remove_pseudo_dirs glob
dirname = Pathname.new(glob).dirname
pseudo_dirs = %w[. ..]
pseudo_with_path = pseudo_dirs.map {|file| File.join(File.expand_path(dirname), file)}
Dir[glob] - pseudo_with_path
end
def uninstall targets
targets.each do |target|
# Remove all symlinks created during installation
if File.symlink? target
FileUtils.rm target, :verbose => true, :noop => DEBUG
end
# Replace any backups made during installation
if File.exists? "#{target}.#{BACKUP_EXT}"
restore_file target
end
end
end
end
include Util
def create_task(*args, &block)
default_options = {
:desc => "",
:after_message => "Done.",
}
task_args = [args[0]]
options_index = 1
if block_given?
task_args << args[1]
options_index = 2
end
options = default_options.merge(args[options_index])
desc options[:desc].to_s
task *task_args do |t, args|
if block_given?
yield t, args
else
send("task_#{t}".to_sym)
end
puts colorize_if_installed(options[:after_message], :green)
end
end
create_task :install, desc: "Hook our dotfiles into system-standard positions."
create_task :setup, desc: "Setup your .dotfiles directory"
create_task :install_bin, desc: "Install script to bin"
create_task :uninstall, desc: "Delete all symlinked files from home dir. Restores backup if exists"
create_task :dotfiles, desc: "List your dotfiles"
create_task :sync, desc: "Sync dotfiles with filesystem"
create_task :add, :glob, desc: "Add files to dotfiles repository. (e.g. glob: ~/.*[^~])" do |t, args|
task_add args
end
create_task :remove, :glob, desc: "Remove files from dotfiles repository. (e.g. glob: ~/.*[^~])" do |t, args|
task_remove args
end
create_task :update, :glob, desc: "Update all dotfiles and sync them" do |t, args|
task_remove args
end
desc "Information about this Rakefile"
task :info do
msg = colorize_if_installed("You#{COLORS ? "" : " don't"} have installed the gem 'colorize'!", :green)
puts <<-EOM
Author: DSIW (dsiw@dsiw-it.de)
Repository: https://github.com/DSIW/dotfiles
#{msg}
Thanks for using and have fun.
EOM
end
task :update do
puts `git pull --rebase origin master`
puts `git submodule update --recursive --rebase --init`
puts `git push`
task_sync
puts colorize_if_installed("Done.", :green)
end
desc "list tasks"
task :list do
tasks = Rake::Task.tasks - [Rake::Task[:list]]
tasks = tasks.map(&:to_s).map(&:blue) if COLORS
puts "Tasks: #{tasks.join(', ')}"
puts "(type rake -T for more detail)"
end
desc "Switch to ZSH (install unless exists)"
task :init_zsh do
install_oh_my_zsh
switch_to_zsh
puts colorize_if_installed("Done.", :green)
end
desc "Init VIM with Vundle"
task :init_vim do
puts "Installing Vundle"
puts `git clone http://github.com/gmarik/vundle.git "#{ENV['HOME']}/.dotfiles/vim.symlink/bundle/vundle"`
puts "Please run 'vim +BundleInstall! +BundleClean +q' to get bundles"
puts colorize_if_installed("Done.", :green)
end
task :default => ['list']