-
Notifications
You must be signed in to change notification settings - Fork 0
/
install
executable file
·249 lines (213 loc) · 5.81 KB
/
install
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
#!/usr/bin/env ruby
require 'fileutils'
require 'open3'
require 'optparse'
OptionParser.new do |opts|
opts.banner = 'Usage: setup [options]'
opts.on('-u', '--update', 'Update instead of install') do |v|
$update = v
end
opts.on('-v', '--[no-]verbose', 'Verbose output') do |v|
$verbose = v
end
end.parse!
$failed = false
class String
def bold; "\e[1m#{self}\e[21m" end
def green; "\e[1;32m#{self}\e[0m" end
def red; "\e[1;31m#{self}\e[0m" end
def yellow; "\e[1;33m#{self}\e[0m" end
end
def backup(path)
path = File.expand_path path
run "mv '#{path}' '#{path}.backup.#{Time.now.getutc.to_i}'" if exists path
end
def contains_line(path, regexp)
File.foreach(File.expand_path path).grep(regexp).any?
end
def exists(path)
path = File.expand_path path
result = Dir.exist?(path) || File.exist?(path) || File.symlink?(path)
if $verbose
if result
report "#{path} exists"
else
report "#{path} does not exist"
end
end
return result
end
def exists_dir(path)
path = File.expand_path path
result = Dir.exist?(path) || File.symlink?(path)
if $verbose
if result
report "#{path} exists"
else
report "#{path} does not exist"
end
end
return result
end
def exists_file(path)
path = File.expand_path path
result = File.exist?(path) || File.symlink?(path)
if $verbose
if result
report "#{path} exists"
else
report "#{path} does not exist"
end
end
return result
end
def hardlink(from_path, to_path)
from_path = File.expand_path from_path
to_path = File.expand_path to_path
return if File.exist?(from_path) && File.lstat(from_path).ino == File.lstat(to_path).ino
backup from_path
from_dir = File.dirname(from_path)
FileUtils.makedirs(from_dir) if !exists_dir(from_dir)
File.link(to_path, from_path)
end
def install(from, to = nil)
to = from if to == nil
softlink (path_home to), (path_repo from)
end
def path_home(path)
File.expand_path File.join(Dir.home, path)
end
def path_repo(path)
File.expand_path File.join(File.dirname(__FILE__), path)
end
def report(message)
puts message
end
def report_bad(message)
report message.red.bold
end
def report_good(message)
report message.green.bold
end
def report_step(message)
report message.yellow.bold
end
def run(command)
success, output = run_report(command)
return success
end
def run_general(command)
stdout, stderr, status = Open3.capture3(command)
if stdout.empty?
output = stderr
elsif stderr.empty?
output = stdout
else
output = stdout + "\n" + stderr
end
return status.success?, output
end
def run_report(command)
if $verbose
report_step "Running: #{command}"
end
success, output = run_general(command)
if $verbose
report output unless output.empty?
if success
report_good 'Succeeded'
else
report_bad 'Failed'
end
end
$failed = !success unless $failed
return success, output
end
def run_output(command)
success, output = run_report(command)
return output
end
def softlink(from_path, to_path)
from_path = File.expand_path from_path
to_path = File.expand_path to_path
return if File.exist?(from_path) && File.symlink?(from_path) && File.readlink(from_path) == to_path
backup from_path
from_dir = File.dirname(from_path)
FileUtils.makedirs(from_dir) if !exists_dir(from_dir)
File.symlink(to_path, from_path)
end
unless run_output('uname').include? 'Darwin'
report_error 'Error: Computer is not a Mac'
exit 1
end
# Update
if $update
report_step 'Updating software...'
run "git -C '#{path_repo '.'}' pull origin master"
run 'brew upgrade'
run 'brew cleanup'
run 'fish -c "fisher update"'
run "sh -c 'cd \"#{path_home '.'}/.vim\" && rake'"
if $failed
report_bad 'Failed'
exit 1
else
report_good 'Succeeded'
exit 0
end
end
# Authenticate
run 'sudo -v'
# Preferences
report_step 'Installing preferences...'
run "osascript -e 'tell application \"System Preferences\" to quit'"
run "#{path_repo '.'}/scripts/defaults.sh"
# Keys
unless exists_file('~/.ssh/id_rsa') && exists_file('~/.ssh/id_rsa.pub')
report_step 'Installing keys...'
backup '~/.ssh/id_rsa'
backup '~/.ssh/id_rsa.pub'
run "ssh-keygen -N '' -f '#{Dir.home}/.ssh/id_rsa' -q"
end
run "ssh-add -q '#{Dir.home}/.ssh/id_rsa'"
# Files
report_step 'Installing files...'
install 'files/.editorconfig', '.editorconfig'
install 'files/.gitconfig', '.gitconfig'
install 'files/ignore', '.config/git/ignore'
install 'files/.hushlogin', '.hushlogin'
install 'files/.inputrc', '.inputrc'
install 'files/.vimrc.after', '.vimrc.after'
install 'files/config.fish', '.config/fish/config.fish'
install 'files/settings.json', 'Library/Application Support/Code/User/settings.json'
# Directories
dev = File.join(Dir.home, 'Developer')
unless exists_dir dev
report_step 'Installing directories...'
Dir.mkdir(dev)
end
# Terminal
report_step 'Installing terminal...'
run "open '#{path_repo 'files/VS Code Dark Plus.terminal'}'"
# Software
report_step 'Installing software...'
# Homebrew
run 'sh -c "curl -fsLS https://raw.githubusercontent.com/Homebrew/install/master/install.sh | bash"' unless exists_file '/usr/local/bin/brew'
run "brew bundle --file='#{path_repo 'files/Brewfile'}'"
run 'brew cleanup'
run 'brew doctor'
run 'sudo chsh -s /usr/local/bin/fish $USER' if exists_file('/usr/local/bin/fish') && !run_output('sh -c "echo $SHELL"').include?('/usr/local/bin/fish')
# Fisher
run 'fish -c "curl -sL https://git.io/fisher | source && fisher install jorgebucaran/fisher"' unless exists_file '~/.config/fish/functions/fisher.fish'
run 'fish -c "fisher install pure-fish/pure"'
# Janus
run 'sh -c "curl -fsLS https://bit.ly/janus-bootstrap | bash"' unless exists_dir '~/.vim'
softlink('~/.config/nvim', '~/.vim')
softlink('~/.config/nvim/init.vim', '~/.vimrc')
if $failed
report_bad 'Failed'
exit 1
else
report_good 'Succeeded'
exit 0
end