-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
58 lines (48 loc) · 1.84 KB
/
Copy pathRakefile
File metadata and controls
58 lines (48 loc) · 1.84 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
require 'erb'
IGNORED = ["Rakefile", "README", /~$/, /^\./, "mac_fixes.sh"]
NON_DOT = ["bin", "functions"]
def backup(file)
return unless File.exists?(file) || File.symlink?(file)
backup_count = Dir["#{file}.backup*"].length
File.rename(file, "#{file}.backup#{backup_count}") && puts("Moving '#{file}' to '#{file}.backup#{backup_count}'")
end
# For debugging
dry_run = false
puts "- This is a dry run" if dry_run
desc "Create symlinks for the dotfiles, keeping backups of the old files."
task :update do
puts "Getting the latest updates..."
`git pull && git submodule update --init --merge`
`vim +PluginClean! +PluginInstall +qall`
end
task :setup do
puts "Setting up..."
files = `git ls-tree --name-only HEAD`.lines.map(&:strip)
files.reject! { |f| IGNORED.any? { |m| f.match(m) } }
files.each do |file|
repoFile = File.join(Dir.pwd, file)
if file.match('\.erb$')
dotfile = file.gsub(/\.erb$/, '')
homeFile = File.expand_path("~/.#{dotfile}")
generatedFile = ERB.new(File.read(repoFile)).result
if !File.exists?(homeFile) or generatedFile != File.read(homeFile)
backup(homeFile) unless dry_run
puts "Writing generated #{file} to ~/.#{dotfile}"
File.open(homeFile, 'w') { |f| f.write(generatedFile) } unless dry_run
else
puts "~/.#{dotfile} is identical to the one generated by: #{file}"
end
else
filename = NON_DOT.include?(file) ? file : ".#{file}"
homeFile = File.expand_path("~/#{filename}")
if File.symlink?(homeFile) and File.readlink(homeFile) == repoFile
puts "symlink for ~/#{filename} already exists"
else
backup(homeFile) unless dry_run
puts "Creating the symlink: ~/#{filename} -> #{repoFile}"
File.symlink(repoFile, homeFile) unless dry_run
end
end
end
end
task :install => [:setup, :update]