forked from edgecase/pairing-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
84 lines (65 loc) · 1.66 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
require 'rubygems'
require 'rake'
desc "attempt to symlink all dot files from the current user's home directory"
task :symlink do
symlink(:skip => '.gitconfig')
end
namespace :symlink do
desc "symlink all dot files (including .gitconfig) from the current user's home directory"
task :pairing do
symlink
end
end
def symlink(options = {})
files = Dir.glob('.*') - ['.git', '.gitmodules', '.', '..'] - [options[:skip]]
files.each do |file|
case
when file_identical?(file) then skip_identical_file(file)
when replace_all_files? then link_file(file)
when file_missing?(file) then link_file(file)
else prompt_to_link_file(file)
end
end
end
# FILE CHECKS
def file_exists?(file)
File.exists?("#{ENV['HOME']}/#{file}")
end
def file_missing?(file)
!file_exists?(file)
end
def file_identical?(file)
File.identical? file, File.join(ENV['HOME'], "#{file}")
end
def replace_all_files?
@replace_all == true
end
# FILE ACTIONS
def prompt_to_link_file(file)
print "overwrite? ~/#{file} [ynaq] "
case $stdin.gets.chomp
when 'y' then replace_file(file)
when 'a' then replace_all(file)
when 'q' then exit
else skip_file(file)
end
end
def link_file(file)
puts " => symlinking #{file}"
directory = File.dirname(__FILE__)
sh("ln -s #{File.join(directory, file)} #{ENV['HOME']}/#{file}")
end
def replace_file(file)
sh "rm -rf #{ENV['HOME']}/#{file}"
link_file(file)
end
def replace_all(file)
@replace_all = true
replace_file(file)
end
def skip_file(file)
puts " => skipping ~/#{file}"
end
def skip_identical_file(file)
puts " => skipping identical ~/#{file}"
end