-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
195 lines (166 loc) Β· 5.31 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
require 'io/console'
ENV['HOMEBREW_CASK_OPTS'] = "--appdir=/Applications"
def step(description)
description = "-- #{description} "
description = description.ljust(80, '-')
puts
puts "\e[32m#{description}\e[0m"
end
def app_path(name)
path = "/Applications/#{name}.app"
["~#{path}", path].each do |full_path|
return full_path if File.directory?(full_path)
end
return nil
end
def app?(name)
return !app_path(name).nil?
end
def get_backup_path(path)
number = 1
backup_path = "#{path}.bak"
loop do
if number > 1
backup_path = "#{backup_path}#{number}"
end
if File.exists?(backup_path) || File.symlink?(backup_path)
number += 1
next
end
break
end
backup_path
end
def link_file(original_filename, symlink_filename)
original_path = File.expand_path(original_filename)
symlink_path = File.expand_path(symlink_filename)
if File.exists?(symlink_path) || File.symlink?(symlink_path)
if File.symlink?(symlink_path)
symlink_points_to_path = File.readlink(symlink_path)
return if symlink_points_to_path == original_path
# Symlinks can't just be moved like regular files. Recreate old one, and
# remove it.
ln_s symlink_points_to_path, get_backup_path(symlink_path), :verbose => true
rm symlink_path
else
# Never move user's files without creating backups first
mv symlink_path, get_backup_path(symlink_path), :verbose => true
end
end
ln_s original_path, symlink_path, :verbose => true
end
def unlink_file(original_filename, symlink_filename)
original_path = File.expand_path(original_filename)
symlink_path = File.expand_path(symlink_filename)
if File.symlink?(symlink_path)
symlink_points_to_path = File.readlink(symlink_path)
if symlink_points_to_path == original_path
# the symlink is installed, so we should uninstall it
rm_f symlink_path, :verbose => true
backups = Dir["#{symlink_path}*.bak"]
case backups.size
when 0
# nothing to do
when 1
mv backups.first, symlink_path, :verbose => true
else
$stderr.puts "found #{backups.size} backups for #{symlink_path}, please restore the one you want."
end
else
$stderr.puts "#{symlink_path} does not point to #{original_path}, skipping."
end
else
$stderr.puts "#{symlink_path} is not a symlink, skipping."
end
end
def filemap(map)
map.inject({}) do |result, (key, value)|
result[File.expand_path(key)] = File.expand_path(value)
result
end.freeze
end
namespace :install do
desc 'Welcome message'
task :intro_msg do
step '@adiq dotfiles'
puts 'This tool, will install/update all apps specified in Brew, Cask and Mas files.'
puts 'It will also setup basic aliases and zshrc to get you up and running ASAP π'
puts ''
puts 'Press ENTER to continue...'
STDIN.gets.chomp
end
desc 'Prompt user to login in App Store app'
task :get_credentials do
step 'Mac App Store'
puts 'Login in App Store app, then press ENTER to continue...'
STDIN.gets.chomp
end
desc 'Homebrew'
task :brew do
step 'Homebrew'
if system('which brew > /dev/null') then
puts 'Homebrew detected in system. All good πΊ'
else
puts 'πΊ Homebrew not detected. Installing...'
unless system('ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"')
raise "Installation failed. Consider doing a manual install of Homebrew."
end
end
end
desc 'Install applications with Homebrew'
task :brew_bundle do
step 'Installing / Updating Homebrew Apps'
system('brew tap Homebrew/bundle && brew bundle --file=./Brew-bundle')
end
desc 'Install MacOS applications with Homebrew Cask'
task :brew_cask_bundle do
step 'Installing / Updating Cask Apps'
system('brew bundle --file=./Cask-bundle')
end
desc 'Install App Store apps'
task :mas_bundle do
step 'Installing Mac Store Apps'
system('brew bundle --file=./Mas-bundle')
end
end
LINKED_FILES = filemap(
'zshrc' => '~/.zshrc',
'zsh_aliases' => '~/.zsh_aliases',
'starship.toml' => '~/.config/starship.toml'
)
desc 'Install these config files.'
task :install do
Rake::Task['install:intro_msg'].invoke
Rake::Task['install:get_credentials'].invoke
Rake::Task['install:brew'].invoke
Rake::Task['install:brew_bundle'].invoke
Rake::Task['install:brew_cask_bundle'].invoke
Rake::Task['install:mas_bundle'].invoke
step 'symlink'
system('mkdir -p ~/.config')
LINKED_FILES.each do |orig, link|
link_file orig, link
end
step 'localrc'
localRc = File.expand_path('~/.zshrc-local')
if File.exist?(localRc)
puts 'Your local ~/.zshrc-local file already exist π€'
else
File.open(localRc, "w") { |file| file.puts "# Put any custom scripts here, this file won't be overwritten or deleted"}
puts 'Created a ~/.zshrc-local file for your custom scripts π€'
end
step 'Enjoy'
puts 'Installation completed! Enjoy your awesome setup π'
end
desc 'Uninstall these config files.'
task :uninstall do
step 'un-symlink'
# un-symlink files that still point to the installed locations
LINKED_FILES.each do |orig, link|
unlink_file orig, link
end
step 'homebrew'
puts
puts 'Manually uninstall homebrew if you wish: https://gist.github.com/mxcl/1173223.'
end
task :default => :install