-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.rb
More file actions
executable file
·469 lines (373 loc) · 14.8 KB
/
setup.rb
File metadata and controls
executable file
·469 lines (373 loc) · 14.8 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/bin/env ruby
# frozen_string_literal: true
# neeeded for $CHILD_STATUS and $PROGRAM_NAME
require 'English'
require 'yaml'
DF_REPO ||= ENV['DOTFILES_REPO'] || 'tklepzig/dotfiles'
DF_BRANCH ||= ENV['DOTFILES_BRANCH']
HOME ||= ENV['HOME']
DF_VARIANT ||= ENV['DOTFILES_VARIANT'] || 'neovim'
DF_THEME ||= ENV['DOTFILES_THEME']
DF_PATH ||= "#{HOME}/.dotfiles".freeze
DF_LOCAL_PATH ||= "#{HOME}/.dotfiles-local".freeze
DF_LOCAL ||= ARGV.include?('--local')
module Logger
@level = 0
def self.format_line(message, color)
prefix = @level.zero? ? "\e[1;38;5;#{color}m\u276f " : ''
indented = message.rjust(message.length + 2 * @level)
"#{prefix}\e[0;38;5;#{color}m#{indented}\e[0m"
end
def self.log(message)
color = (ENV['primaryText'] || '4').sub(/colour/, '')
puts format_line(message, color)
return unless block_given?
@level += 1
yield
@level -= 1
@level = 0 if @level.negative?
end
def self.success(message)
color = (ENV['accentText'] || '6').sub(/colour/, '')
puts format_line(message, color)
end
def self.error(message)
prefix = @level.zero? ? "\e[1;31m\u276f " : ''
indented = message.rjust(message.length + 2 * @level)
puts "#{prefix}\e[0;31m#{indented}\e[0m"
end
end
module OS
def self.mac?
RUBY_PLATFORM.match?(/darwin/)
end
def self.linux?
RUBY_PLATFORM.match?(/linux/)
end
end
def find_override(file_path)
return "#{file_path}.override" if File.exist?("#{file_path}.override")
override_before_extension = file_path.gsub(/(.*)(\..+)$/, '\1.override\2')
override_before_extension if File.exist?(override_before_extension)
end
def merge(base_path, override_path)
base = File.readlines(base_path)
override = File.readlines(override_path)
result = base.reject { |line| override.include?("-#{line}") }
result += override.reject { |line| line.start_with?('-') }
File.write(base_path, result.join)
end
def program_installed?(program)
!`sh -c 'command -v #{program}'`.empty?
end
def check_mandatory_installation(program)
Logger.log "Searching for #{program}" do
unless program_installed? program
Logger.error 'Not found, aborting'
exit(false)
end
path = `which #{program}`
Logger.success "Found: #{path.strip}."
end
end
def check_optional_installation(program, install_name = program)
Logger.log "Searching for #{program}" do
if program_installed? program
path = `which #{program}`
Logger.success "Found: #{path.strip}."
else
Logger.error "Not Found. (Try \"sudo pacman -S #{install_name}\")"
end
end
end
def remove_links(pattern, file)
return unless File.exist?(file)
Logger.log "Removing pattern '#{pattern}' from #{file}"
`sed "/#{pattern}/d" #{file} > #{file}.tmp && mv #{file}.tmp #{file}`
end
def write_link(link, file, command = 'source')
`grep -q #{link} #{file}`
return if $CHILD_STATUS.success?
Logger.log "Adding '#{link}' to #{file}"
File.open(file, 'a') do |f|
f.puts "#{command} #{link}"
end
end
def add_link_with_override(link, file, command = 'source')
File.write(file, '') unless File.exist?(file)
write_link(link, file, command)
override = find_override(link)
return unless override
write_link(override, file, command)
end
def link_vim_plugins
if File.exist?("#{DF_PATH}/vim/vim/plugins.override.vim")
merge("#{DF_PATH}/vim/vim/plugins.vim",
"#{DF_PATH}/vim/vim/plugins.override.vim")
end
`sed 's/\\"pluginfile/source $HOME\\/.dotfiles\\/vim\\/vim\\/plugins.vim\\
\\"pluginfile/g' #{DF_PATH}/vim/plugins.vim > #{DF_PATH}/vim/plugins.vim.tmp && mv #{DF_PATH}/vim/plugins.vim.tmp #{DF_PATH}/vim/plugins.vim`
end
def cleanup_vim
Logger.log 'Cleanup vim' do
require "#{DF_PATH}/vim/vim/uninstall.rb"
return unless DF_VARIANT == 'neovim'
require "#{DF_PATH}/vim/neovim/uninstall.rb"
end
end
def setup_vim(variant)
Logger.log "Setup #{variant}" do
link_vim_plugins
add_link_with_override "#{DF_PATH}/vim/vim/vimrc", "#{HOME}/.vimrc"
require "#{DF_PATH}/vim/vim/install.rb"
return unless variant == 'neovim'
add_link_with_override "#{DF_PATH}/vim/neovim/vimrc", "#{HOME}/.vimrc"
require "#{DF_PATH}/vim/neovim/install.rb"
end
end
def link_docs(path)
return unless Dir.exist?(File.join(path, 'docs'))
Logger.log 'Linking docs'
Dir.glob(File.join(path, 'docs', '*')) do |file|
`ln -sf "#{file}" "#{DF_PATH}/toolbox/docs"`
end
end
def link_scripts(path)
if Dir.exist?(File.join(path, 'scripts'))
Logger.log 'Linking scripts'
Dir.glob(File.join(path, 'scripts', '*')) do |file|
next if File.basename(file) == '_info.yaml'
`ln -sf "#{file}" "#{DF_PATH}/toolbox/scripts"`
end
end
return unless File.exist?(File.join(path, 'scripts', '_info.yaml'))
Logger.log 'Merging _info.yaml'
infos = YAML.load_file("#{DF_PATH}/toolbox/scripts/_info.yaml")
infos_include = YAML.load_file(File.join(path, 'scripts', '_info.yaml'))
File.write("#{DF_PATH}/toolbox/scripts/_info.yaml", infos.merge(infos_include).to_yaml)
end
def add_toolbox_includes
toolbox_includes = "#{DF_LOCAL_PATH}/toolbox-include.yaml"
return unless File.exist?(toolbox_includes)
Logger.log 'Processing includes'
YAML.load_file(toolbox_includes).each do |raw_path|
path = File.expand_path(raw_path, DF_LOCAL_PATH)
next unless Dir.exist?(path)
Logger.log raw_path do
if Dir.exist?(File.join(path, '.git'))
Logger.log 'Found git repo, updating'
`cd "#{path}" && git fetch > /dev/null && git merge > /dev/null`
end
link_docs(path)
link_scripts(path)
end
end
end
# TODO: clean up, split into smaller chunks, don't do all of this for basic variant
def install(variant = DF_VARIANT)
check_mandatory_installation 'git'
check_mandatory_installation 'zsh'
check_optional_installation 'eza'
check_optional_installation 'tmux'
check_optional_installation 'lynx'
unless DF_LOCAL
if Dir.exist?(DF_PATH)
Logger.log "Found existing dotfiles in #{DF_PATH}, updating"
Dir.chdir(DF_PATH) do
current_hash = `git rev-parse --short HEAD`.strip
# Set the branch if it is defined
`git remote set-branches origin #{DF_BRANCH}` if DF_BRANCH
# Update repo
`git fetch --depth=1 > /dev/null 2>&1`
# Remove tracked changes
`git reset --hard origin/#{DF_BRANCH || 'master'} > /dev/null 2>&1`
# Checkout branch if it is defined
if DF_BRANCH
Logger.success "Switching to branch #{DF_BRANCH}"
`git checkout --quiet #{DF_BRANCH}`
end
Logger.success "Updated dotfiles from #{current_hash} to #{`git rev-parse --short HEAD`.strip}."
# Remove ignored changes
# Do not remove ignored changes, e.g. to keep generated certificates
# `git clean -fx > /dev/null 2>&1`
end
else
Logger.log "Cloning repo from #{DF_REPO} to #{DF_PATH}"
Logger.success "Switching to branch #{DF_BRANCH}" if DF_BRANCH
`git clone --quiet --depth=1#{DF_BRANCH ? " -b #{DF_BRANCH}" : ''} https://github.com/#{DF_REPO}.git #{DF_PATH}`
Dir.chdir(DF_PATH) do
Logger.success "Installed dotfiles at #{`git rev-parse --short HEAD`.strip}."
end
end
end
# Update chosen variant in .zshrc (must come before the dotfiles zsh source line,
# since aliases defined there depend on DOTFILES_VARIANT being set correctly)
variant_export = "export DOTFILES_VARIANT='#{variant}'"
zshrc = File.read("#{HOME}/.zshrc")
if zshrc.match?(/DOTFILES_VARIANT/)
# Already present: replace in-place, preserving its position
zshrc.gsub!(/export DOTFILES_VARIANT=.*/, variant_export)
elsif zshrc.include?("#{DF_PATH}/zsh/zshrc")
# Source line already added (e.g. previous install): insert export before it.
# Captures the source line as \1 and prepends the export line above it.
zshrc.gsub!(/^(.*#{Regexp.escape("#{DF_PATH}/zsh/zshrc")}.*)/, "#{variant_export}\n\\1")
else
# First install, source line not yet added: append here; source line will be appended after
zshrc += "#{variant_export}\n"
end
File.write("#{HOME}/.zshrc", zshrc)
`mkdir -p #{DF_LOCAL_PATH}`
Logger.log "Using theme #{ENV['DOTFILES_THEME']}" if ENV['DOTFILES_THEME']
`#{DF_PATH}/toolbox/scripts/set-theme`
add_link_with_override "#{DF_PATH}/colours.vim", "#{HOME}/.vimrc"
add_link_with_override "#{DF_PATH}/colours.zsh", "#{HOME}/.zshrc"
add_link_with_override "#{DF_PATH}/colours.tmux.conf", "#{HOME}/.tmux.conf"
unless File.exist?("#{DF_LOCAL_PATH}/plugins.vim")
File.write("#{DF_LOCAL_PATH}/plugins.vim",
"\"Plug 'any/vim-plugin'")
end
add_link_with_override "#{DF_PATH}/vim/plugins.vim", "#{HOME}/.vimrc"
setup_vim(variant)
# TODO: move before vim setup
add_link_with_override "#{DF_PATH}/zsh/zshrc", "#{HOME}/.zshrc"
unless File.exist?("#{HOME}/.bc")
Logger.log 'Configuring bc'
File.write("#{HOME}/.bc", "scale=2\n")
end
Logger.log 'Configuring ruby' do
Logger.log 'Symlinking default gems configuration'
`ln -sf #{DF_PATH}/ruby/default-gems #{HOME}/.default-gems`
Logger.log 'Symlinking rubocop configuration'
`ln -sf #{DF_PATH}/ruby/rubocop.yml #{HOME}/.rubocop.yml`
end
Logger.log 'Initializing toolbox' do
# TODO: Only when in full mode?
add_link_with_override "#{DF_PATH}/toolbox/init.zsh", "#{HOME}/.zshrc"
add_toolbox_includes
end
# TODO: part of vim setup
# We can't rely on aliases since the subshell from ruby spawns a sh and has no idea about zsh aliases
vim_binary = variant == 'neovim' ? 'nvim' : 'vim'
if variant == 'neovim'
Logger.log 'Installing and syncing neovim plugins'
`#{vim_binary} --headless "+Lazy! sync" "+qa" > /dev/null 2>&1`
Logger.log 'Updating coc extensions'
`echo | #{vim_binary} +CocUpdateSync +qall > /dev/null 2>&1`
else
unless File.exist?("#{HOME}/.vim/autoload/plug.vim")
Logger.log 'Installing vim-plug'
`curl -fLo #{HOME}/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim>/dev/null 2>&1`
end
Logger.log 'Installing and updating vim plugins'
`echo | #{vim_binary} +PlugInstall +PlugUpdate +qall > /dev/null 2>&1`
end
Logger.log 'Configuring tmux' do
# TODO: move up into one block of "linking"
if OS.mac?
Logger.log 'Symlinking tmux variables for macOS'
add_link_with_override "#{DF_PATH}/tmux/vars.osx.conf", "#{HOME}/.tmux.conf"
else
Logger.log 'Symlinking tmux variables for Linux'
add_link_with_override "#{DF_PATH}/tmux/vars.linux.conf", "#{HOME}/.tmux.conf"
end
Logger.log 'Symlinking tmux main configuration'
add_link_with_override "#{DF_PATH}/tmux/tmux.conf", "#{HOME}/.tmux.conf"
end
if program_installed? 'kitty'
Logger.log 'Configuring kitty'
add_link_with_override "#{DF_PATH}/kitty/kitty.conf", "#{HOME}/.config/kitty/kitty.conf", 'include'
add_link_with_override "#{DF_PATH}/kitty/kitty.theme.conf", "#{HOME}/.config/kitty/kitty.conf", 'include'
# Open kitty in fullscreen mode, the macos way
# `ln -sf #{DF_PATH}/kitty/macos-launch-services-cmdline #{HOME}/.config/kitty/macos-launch-services-cmdline` if OS.mac?
end
if program_installed? 'ranger'
Logger.log 'Configuring ranger'
`mkdir -p #{HOME}/.config/ranger`
`ln -sf #{DF_PATH}/ranger/rc.conf #{HOME}/.config/ranger/rc.conf`
end
if program_installed? 'i3'
Logger.log 'Configuring i3' do
Logger.log 'Symlinking i3 main configuration'
add_link_with_override "#{DF_PATH}/i3/config", "#{HOME}/.config/i3/config", 'include'
Logger.log 'Symlinking i3blocks configuration'
`mkdir -p #{HOME}/.config/i3blocks`
`ln -sf #{DF_PATH}/i3/i3blocks.config #{HOME}/.config/i3blocks/config`
# `mkdir -p #{HOME}/.config/i3status`
# `ln -sf #{DF_PATH}/i3/i3status.config #{HOME}/.config/i3status/config`
Logger.log 'Symlinking dunst configuration'
`mkdir -p #{HOME}/.config/dunst`
`ln -sf #{DF_PATH}/i3/dunst.config #{HOME}/.config/dunst/dunstrc`
Logger.log 'Symlinking picom configuration'
`mkdir -p #{HOME}/.config/picom`
`ln -sf #{DF_PATH}/i3/picom.config #{HOME}/.config/picom/picom.conf`
end
end
if OS.mac?
Logger.log 'Configuring aerospace for macOS'
`ln -sf #{DF_PATH}/aerospace/config.toml #{HOME}/.aerospace.toml`
end
unless Dir.exist?("#{HOME}/.fzf")
Logger.log 'Installing fzf'
`git clone --depth 1 https://github.com/junegunn/fzf.git #{HOME}/.fzf > /dev/null 2>&1`
`#{HOME}/.fzf/install --all > /dev/null 2>&1`
end
Logger.log 'Configuring Git'
`#{DF_PATH}/git/install`
if program_installed? 'docker'
Logger.log 'Installing docker completion'
`mkdir -p #{HOME}/.zsh/completion`
`curl -L https://raw.githubusercontent.com/docker/cli/master/contrib/completion/zsh/_docker > #{HOME}/.zsh/completion/_docker 2>/dev/null`
`curl -L https://raw.githubusercontent.com/docker/compose/master/contrib/completion/zsh/_docker-compose > #{HOME}/.zsh/completion/_docker-compose 2>/dev/null`
end
default_shell = if OS.mac?
`dscl . -read #{HOME}/ UserShell | sed 's/UserShell: //'`
else
`grep ^$(id -un): /etc/passwd | cut -d : -f 7-`
end
if default_shell != `which zsh`
Logger.log 'Setting default shell to zsh' do
`chsh -s $(which zsh)`
Logger.log 'Please notice: In order to use the new shell, you have to logout and back in.'
end
end
post_install_script = "#{DF_LOCAL_PATH}/post-install"
if File.exist?(post_install_script) && File.executable?(post_install_script)
Logger.log 'Running post install script' do
result = `#{post_install_script}`
result.split(/\n/).each { |line| Logger.log line }
end
end
Logger.success 'Setup done.'
end
def uninstall
remove_links '\.dotfiles', '.zshrc'
remove_links '\.fzf', '.zshrc'
remove_links '\.dotfiles', '.vimrc'
remove_links '\.dotfiles', '.tmux.conf'
remove_links '\.dotfiles', '.config/kitty/kitty.conf'
# Remove fzf to ensure that the fzf include is added during install again after all dotfiles zsh includes
Logger.log 'Removing fzf'
`rm -rf #{HOME}/.fzf`
Logger.log 'Removing vim-plug and vim plugins'
`rm -f #{HOME}/.vim/autoload/plug.vim`
`rm -rf #{HOME}/.vim/vim-plug`
cleanup_vim
Logger.log 'Removing bc configuration'
`rm -f #{HOME}/.bc`
Logger.log 'Removing default gems configuration'
`rm -f #{HOME}/.default-gems`
Logger.log 'Removing rubocop configuration'
`rm -f #{HOME}/.rubocop.yml`
Logger.log 'Removing git configuration'
`#{DF_PATH}/git/uninstall`
Logger.log 'Removing dotfiles'
`rm -rf #{DF_PATH}`
Logger.success 'Successfully uninstalled dotfiles'
end
if ARGV[0] == '--uninstall'
uninstall
elsif __FILE__ == $PROGRAM_NAME
# only run installation if script is invoked directly and not by requiring it
variant = ARGV.include?('--vim') ? 'vim' : DF_VARIANT
install(variant)
end