-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
340 lines (300 loc) · 7.74 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
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
require 'rake'
require 'fileutils'
require 'yaml'
require 'uri'
HOME = ENV['HOME']
DOTDIR = "#{HOME}/dotfiles"
desc 'Do the best.'
task :install => [
:install_homebrew,
:install_ruby,
:init_submodules,
:update_submodules,
:brew_essentials,
:orgmode,
:deploy,
:chsh ] do
my_ln("#{DOTDIR}/macfiles/Library/Application\\ Support/Karabiner/private.xml", "#{HOME}/Library/Application\\ Support/Karabiner/private.xml")
puts 'run `rake bundle_up` after logging in with zsh'
end
desc 'Do the best for unix.'
task :install_unix => [
:init_submodules,
:update_submodules,
:orgmode,
:deploy,
:chsh ] do
puts 'run `rake bundle_up_unix` after logging in with zsh'
end
desc 'Install softwares'
task :bundle_up => [
:brew_optionals,
:go_get,
:cargo_install,
:ghq_get,
:npm_install,
:bundle_install,
:install_rbenv_plugins,
:setup_osx] do
end
desc 'Install softwares'
task :bundle_up_unix => [
:go_get,
:cargo_install,
:ghq_get,
:npm_install,
:bundle_install,
:install_rbenv_plugins] do
end
desc 'Change login shell'
task :chsh do
puts 'Changing login shell...'
shell = '/usr/local/bin/zsh'
run %{
grep #{shell} /etc/shells > /dev/null || echo #{shell} | sudo tee -a /etc/shells > /dev/null
}
run %{ chsh -s #{shell} }
end
desc 'install homebrew'
task :install_homebrew do
puts 'Installing Homebrew...'
run %{ which brew || ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" }
run %{ brew doctor }
end
desc 'install essential brew packages'
task :brew_essentials do
run %{ brew tap Homebrew/bundle }
run %{ brew bundle --file=Brewfile }
end
desc 'install optional brew packages'
task :brew_optionals do
run %{ brew bundle --file=Brewfile.optional }
end
desc 'setup osx'
task :setup_osx do
run %{ ./setup/setup_osx }
end
desc 'install ruby'
task :install_ruby do
puts 'Installing Ruby...'
run %{ brew install rbenv ruby-build }
run %{ rbenv install 2.1.3 } # TODO: ask version to user
run %{ rbenv rehash }
run %{ rbenv global 2.1.3 }
end
desc 'Deploy dotfiles and other files.'
task :deploy do
directories = YAML.load_file('config/directory_structure.yml')
dotfiles = YAML.load_file('config/dotfiles.yml')
Dir.chdir(HOME) do
mkdir_hierarchy(directories)
end
Dir.chdir(DOTDIR) do
deploy_dotfiles(dotfiles)
end
# TODO: Do this better ($HOME/.config is becoming the standard recently)
my_ln("#{DOTDIR}/miscfiles/get-shit-done.ini", "#{HOME}/.config/get-shit-done.ini")
my_ln("#{DOTDIR}/miscfiles/dunstrc", "#{HOME}/.config/dunst/dunstrc")
my_ln("#{DOTDIR}/nvim", "#{HOME}/.config/nvim")
my_ln("#{DOTDIR}/miscfiles/picom", "#{HOME}/.config/picom")
my_ln("#{DOTDIR}/miscfiles/pet", "#{HOME}/.config/pet")
my_ln("#{DOTDIR}/miscfiles/pipewire", "#{HOME}/.config/pipewire")
my_ln("#{DOTDIR}/systemd-user", "#{HOME}/.config/systemd/user")
end
desc 'Update submodules'
task :update_submodules do
run %{
git submodule update --recursive
}
# neobundle is treated specially (to manage itself via neobundle)
unless File.directory?("#{DOTDIR}/vimfiles/vim/bundles/neobundle.vim")
run %{ git clone https://github.com/Shougo/neobundle.vim vimfiles/vim/bundles/neobundle.vim }
end
end
task :init_submodules do
run %{ git submodule update --init --recursive }
end
desc 'Prepare Orgmode'
task :orgmode do
my_ln("#{HOME}/Dropbox/org", "#{HOME}/org")
end
desc 'Prepare Go packages'
task :go_get do
# There must be a better way, but it works
File.read("#{DOTDIR}/Gofile").each_line do |gocmd|
run %{ go #{gocmd} }
end
end
desc 'Prepare Rust packages'
task :cargo_install do
# There must be a better way, but it works
File.read("#{DOTDIR}/Cargofile").each_line do |cargocmd|
run %{ cargo #{cargocmd} }
end
end
desc 'Clone repositories'
task :ghq_get do
repos = YAML.load_file("#{DOTDIR}/config/ghq.yml")
repos.each do |repo|
run %{ ghq get #{repo} }
end
end
desc 'Install npm packages'
task :npm_install do
Dir.chdir(DOTDIR) do
run %{ npm install }
end
end
desc 'Install ruby gems'
task :bundle_install do
Dir.chdir(DOTDIR) do
run %{ ./setup/install-rbenv-plugins.sh }
end
end
desc 'Install rbenv plugins'
task :install_rbenv_plugins do
if ENV['RBENV_ROOT'].to_s.empty?
warn '$RBENV_ROOT is not defined'
return
end
run %{ #{DOTDIR}/setup/install-rbenv-plugins.sh }
end
desc 'Generate ctags files for ghq repositories'
task :ghq_ctags do
repos = YAML.load_file("#{DOTDIR}/config/ghq.yml")
repos.each do |repo|
run %{ cd `ghq list -e -p #{repo}` && ctags -R . }
end
end
desc 'Generate dictionary files (for vim completion) for ghq repositories'
task :ghq_dict do
repos = YAML.load_file("#{DOTDIR}/config/ghq.yml")
repos.each do |repo|
run %{ cd `ghq list -e -p #{repo}` && cat tags | cut -f1 | uniq > tags.dict }
end
end
# FIXME: use $RBENV_ROOT
desc 'Generate dictionary files (for vim completion) for ruby'
task :ruby_dict do
# Ref: http://henry.animeo.jp/blog/2013/08/24/ruby-dict/
# Assumes rbenv is used.
# Creates ~/.rbenv/versions/*/ruby.dict
Dir.glob("#{HOME}/.rbenv/versions/*").each do |ruby_dir|
Dir.chdir(ruby_dir) do
methods = []
Dir.glob("#{Dir.pwd}/**/*.ri").each do |file|
method = URI.decode(File.basename(file))
if /^(.*)-\w*\.ri$/ =~ method
methods << $1
end
end
File.open('ruby.dict', 'w') do |file|
file.write(methods.uniq.sort.join("\n"))
end
end
end
end
desc 'Mac settings'
task :mac do
puts "Deploying plist files"
Dir.glob("#{DOTDIR}/macfiles/LaunchAgents/*") do |plist|
fname = File.basename(plist)
my_ln(plist, "#{HOME}/Library/LaunchAgents/#{fname}")
end
Dir.glob("#{DOTDIR}/macfiles/LaunchDaemons/*") do |plist|
fname = File.basename(plist)
my_ln(plist, "/Library/LaunchDaemons/#{fname}", true)
end
end
task :generate_global_tags do
run %{
ctags \
-R \
-f ~/tags_ruby \
--sort=yes \
--exclude=*.js \
--exclude=*.h \
--exclude=log \
--exclude=*.yml \
--exclude=.git \
--langmap=RUBY:.rb \
$rvm_path/rubies/default
ctags \
-R \
-f ~/tags_gem \
--sort=yes \
--exclude=*.js \
--exclude=*.exp \
--exclude=*.am \
--exclude=*.in \
--exclude=*.m4 \
--exclude=*.o \
--exclude=*.h \
--exclude=log \
--exclude=*.yml \
--exclude=.git \
--langmap=RUBY:.rb \
$GEM_HOME/gems
}
end
private
def run(cmd)
puts "[Running] #{cmd} (#{Dir.pwd})"
ret = `#{cmd}` unless ENV['DEBUG']
raise $?.to_s unless $?.success?
ret
end
def mkdir_hierarchy(obj)
case obj
when String
run %{ mkdir -p #{obj} }
when Array
obj.each{ |o| mkdir_hierarchy(o) }
when Hash
obj.each do |k, v|
run %{ mkdir -p #{k} }
Dir.chdir(k) do
mkdir_hierarchy(v)
end
end
end
end
def deploy_dotfiles(obj)
case obj
when String
my_ln("#{Dir.pwd}/#{obj}", "#{HOME}/.#{obj}")
when Array
obj.each{ |o| deploy_dotfiles(o) }
when Hash
obj.each do |k, v|
Dir.chdir(k) do
deploy_dotfiles(v)
end
end
end
end
def my_ln(src, dst, sudo = false)
run %{ rm #{dst} } if File.symlink?(dst)
raise %{File "#{dst}" exists and not a symlink.} if File.exists?(dst)
dir = File.dirname(dst)
FileUtils.mkdir_p(dir) unless File.directory?(dir)
run %{ #{sudo ? 'sudo' : ''} ln -s #{src} #{dst} }
end
def warn(msg)
STDERR.puts msg
end
# OS check (http://stackoverflow.com/questions/170956/how-can-i-find-which-operating-system-my-ruby-program-is-running-on)
module OS
def OS.windows?
(/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
end
def OS.mac?
(/darwin/ =~ RUBY_PLATFORM) != nil
end
def OS.unix?
!OS.windows?
end
def OS.linux?
OS.unix? and not OS.mac?
end
end