-
Notifications
You must be signed in to change notification settings - Fork 17
/
installer.rb
executable file
·213 lines (174 loc) · 4.76 KB
/
installer.rb
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
# frozen_string_literal: true
require 'fileutils'
require 'pathname'
require 'net/http'
require 'cgi'
require_relative 'installer/string'
require_relative 'installer/request'
Warning[:experimental] = false
GEMS = [
'bundler', # manage gem bundles for a project
'pry', # ruby debugger
].freeze
TASKS = [
{
name: 'Default Shell',
sync: false,
confirmation: 'Make zsh default?',
run_if: proc { !shell_already_zsh? },
callback: proc { change_shell },
},
{
name: 'Fonts',
sync: false,
confirmation: 'Install fonts?',
callback: proc { install_fonts },
},
{
name: 'ZSH Plugin Manager',
sync: false,
confirmation: 'Install zinit?',
callback: proc { install_zinit },
},
{
name: 'Extrenal Packages',
sync: true,
confirmation: 'Install external packages (gems)?',
callback:
proc do
install_rubygems
end,
},
].freeze
# Installs dotfiles and configures the machine to my preferences
# This is an idempotent script (or at least should be)
class Installer
def install
trap('SIGINT') do
puts ''
puts 'Cancelled! Exiting...'.red
exit!
end
print_title
return unless sync? || confirm('Run installer?')
TASKS.each { |task| run_task(task) }
puts ''
puts '===== ALL DONE! ====='.green
end
private
def run_task(task)
task in { name: name, callback: callback, sync: sync }
# skip non "sync" tasks when in sync mode
# sync mode is used to just sync commonly changed files and directories
return if sync? && !sync
puts ''
puts "=== TASK: #{name}".pink
puts ''
# skip if run_if condition is not met
if task[:run_if] && !instance_eval(&task[:run_if])
puts 'Skipping...'.yellow + ' (run_if: false)'.light_blue
return
end
# ask for confirmation (automatically confirmed in `force` mode)
if task[:confirmation] && !confirm(task[:confirmation])
puts 'Skipping...'.yellow
return
end
# execute the task
instance_eval(&callback)
end
def print_title
file = File.open('installer/title.txt')
title = file.read
puts title.red
end
def install_zinit
puts '===== Installing zinit'.blue
IO.popen(<<-BASH.chomp)
ZINIT_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}/zinit/zinit.git" && \
mkdir -p "$(dirname $ZINIT_HOME)" && \
rm -rf "$ZINIT_HOME" && \
git clone https://github.com/zdharma-continuum/zinit.git "$ZINIT_HOME"
BASH
end
def install_fonts
puts '==== Installing fonts'.blue
# download_fonts
[
# Regular
'https://github.com/ryanoasis/nerd-fonts/raw/master/patched-fonts/CascadiaCode/Regular/CaskaydiaCoveNerdFontMono-Regular.ttf',
# Italic
'https://github.com/ryanoasis/nerd-fonts/raw/master/patched-fonts/CascadiaCode/Regular/CaskaydiaCoveNerdFontMono-Italic.ttf',
# Bold
'https://github.com/ryanoasis/nerd-fonts/raw/master/patched-fonts/CascadiaCode/Bold/CaskaydiaCoveNerdFontMono-Bold.ttf',
# Bold Italic
'https://github.com/ryanoasis/nerd-fonts/raw/master/patched-fonts/CascadiaCode/Bold/CaskaydiaCoveNerdFontMono-BoldItalic.ttf',
].each do |url|
filename = CGI.unescape(File.basename(url))
dir =
if mac?
'~/Library/Fonts'
elsif linux?
'~/.fonts'
else
raise 'WTF? not a mac or linux.. who are you?'
end
target_path = File.expand_path(File.join(dir, filename))
print(
'Downloading '.light_blue + filename + ' -> '.light_blue + target_path +
'...'.light_blue,
)
response = Request.new(url).resolve
File.binwrite(target_path, response.body)
puts 'Done'.green
puts ''
end
end
def install_rubygems
puts '===== Installing necessary RubyGems'.blue
popen("gem install #{GEMS.join(' ')}")
end
def git_install(repo_url, install_dir)
install_dir = File.expand_path(install_dir)
if Dir.exist?(install_dir)
puts '...already installed.'.pink
else
popen("git clone #{repo_url} #{install_dir}")
end
end
# changes the default shell to zsh
def change_shell
popen('chsh -s zsh')
end
def popen(cmd, silent: false)
puts('Running: '.light_blue + cmd.to_s) unless silent
IO.popen(cmd) do |io|
while (line = io.gets)
puts line
end
end
end
def confirm(msg)
return true if force?
print "#{msg} [Y/n] "
resp = gets.strip.downcase
puts ''
%w[y yes].include?(resp) || resp == ''
end
def mac?
RUBY_PLATFORM.include?('darwin')
end
def linux?
RUBY_PLATFORM.include?('linux')
end
def force?
ARGV.include?('--force')
end
def sync?
ARGV.include?('--sync')
end
def shell_already_zsh?
ENV['SHELL'].end_with?('zsh')
end
end
Installer.new.install