forked from mjrusso/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRakefile
134 lines (115 loc) · 4.51 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
# The MIT License
#
# Copyright (c) Zach Holman, http://zachholman.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Note: - Some edits made by Andrew Sardone to create the gitconfig installation
# procedure. https://github.com/andrewsardone/dotfiles
# - Other edits made by Michael Russo, including the ability to
# move files into place that are not in the standard location
# of '*/**{.symlink}'. http://mjrusso.com
require 'rake'
require 'fileutils'
desc "Hook our dotfiles into system-standard positions."
task :install => [:generate_gitconfig_from_template] do
linkables = Dir.glob('*/**{.symlink}').map! do |linkable|
file = linkable.split('/').last.split('.symlink').last
{ "path" => linkable,
"file" => file,
"target" => "#{ENV["HOME"]}/.#{file}"
}
end
# Custom include for fish shell. The fish shell requires config
# files to be in the location of ~/.config/fish/config.fish.
FileUtils.mkdir_p "#{ENV['HOME']}/.config/fish"
linkables << { "path" => "fish/config.fish",
"file" => "config.fish",
"target" => "#{ENV['HOME']}/.config/fish/config.fish"
}
skip_all = false
overwrite_all = false
backup_all = false
# install oh-my-zsh
`curl -L https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh | sh`
linkables.each do |linkable|
overwrite = false
backup = false
path = linkable['path']
file = linkable['file']
target = linkable['target']
if File.exists?(target) || File.symlink?(target)
unless skip_all || overwrite_all || backup_all
puts "File already exists: #{target}, what do you want to do? [s]kip, [S]kip all, [o]verwrite, [O]verwrite all, [b]ackup, [B]ackup all"
case STDIN.gets.chomp
when 'o' then overwrite = true
when 'b' then backup = true
when 'O' then overwrite_all = true
when 'B' then backup_all = true
when 'S' then skip_all = true
when 's' then next
end
end
FileUtils.rm_rf(target) if overwrite || overwrite_all
`mv "#{target}" "#{target}.backup"` if backup || backup_all
end
`ln -s "$PWD/#{path}" "#{target}"`
end
end
desc "Generate a gitconfig file from the template based on user input"
task :generate_gitconfig_from_template do
gitconfig_name = 'git/gitconfig.symlink'
exists = false
skip = false
regenerate = false
if File.exists?(gitconfig_name)
exists = true
puts "gitconfig file already exists, what do you want to do? [s]kip, [r]egenerate"
case STDIN.gets.chomp
when 's' then skip = true
when 'r' then regenerate = true
end
end
if not exists or regenerate
repl = {}
puts "\nGenerating gitconfig"
print("Your Name: "); STDOUT.flush; repl['__USER_NAME__'] = STDIN.gets.chomp
print("Your Email: "); STDOUT.flush; repl['__USER_EMAIL__'] = STDIN.gets.chomp
temp = IO.read('git/gitconfig.template')
repl.each { |k,v| temp.gsub!(k,v) }
File.new(gitconfig_name, File::WRONLY|File::TRUNC|File::CREAT).puts temp
else
puts "\nSkipping generation of gitconfig"
end
end
task :uninstall do
Dir.glob('**/*.symlink').each do |linkable|
file = linkable.split('/').last.split('.symlink').last
target = "#{ENV["HOME"]}/.#{file}"
# Remove all symlinks created during installation
if File.symlink?(target)
FileUtils.rm(target)
end
# Replace any backups made during installation
if File.exists?("#{ENV["HOME"]}/.#{file}.backup")
`mv "$HOME/.#{file}.backup" "$HOME/.#{file}"`
end
end
end
task :default => 'install'