-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
352 lines (268 loc) · 9.35 KB
/
setup.py
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
# set up a mac or linux machine from scratch to awesome
# TODO: should probably refactor into classes
import os
from pathlib import Path
import pkgutil
import subprocess
home = Path.home()
dotfiles = home/'dotfiles'
home_dot_config = home/'.config'
dot_config = dotfiles/'config'
os_name = os.uname().sysname
is_mac = True if os_name == 'Darwin' else False
is_linux = True if os_name == 'Linux' else False
homebrew_prefix = '/usr/local' if is_mac else '/home/linuxbrew/.linuxbrew'
brew_binary = f'{homebrew_prefix}/bin/brew'
# utility
bcolors = {
'header' : '\033[95m',
'blue': '\033[94m',
'green' : '\033[92m',
'warn' : '\033[93m',
'fail' : '\033[91m',
'end' : '\033[0m',
'bold' : '\033[1m',
'underline' : '\033[4m',
'reset': '\x1b[0m'
}
# donger inspired by @atomantic/dotfiles
def robo_says(msg, color='blue'):
print(f'{bcolors["header"]}\\[._.]/ {bcolors[color]} {msg} {bcolors["reset"]}')
########################################################
# directories that should exist
directories = (
home/'.zinit',
home/'.vim',
home/'.vim/tmp',
home/'.config'
)
# symlinks
config_directories_to_symlink = (
'nvim',
'karabiner',
'tridactyl',
'fish',
'kitty'
)
dotfiles_to_symlink = (
'Brewfile',
'gitconfig',
'gitignore',
'npmrc',
'p10k.zsh',
'tmux.conf',
'vimrc.bundles',
'vimrc',
'xonshrc',
'zshenv',
'zshrc'
)
asdf_languages = (
'nodejs',
'ruby',
'rust'
)
# why don't conda packages have a clever name?
conda_packages = (
)
pip_packages = (
'pynvim',
'neovim-remote',
'notedown'
)
gems = (
'json_pure',
'neovim'
)
npm_global_packages = (
'yarn',
'neovim'
)
macos_config_script = 'macos_config.sh'
##################################################3
def create_directory(d):
p = Path(d)
if p.exists():
robo_says(f'dir {d} already exists')
else:
p.mkdir()
robo_says(f'dir {d} created', 'green')
def create_directories():
robo_says('Setting up directories')
for d in directories:
create_directory(d)
# TODO: make these more resillient and recreate symlinks if they are changed
def sym_the_links():
"""symlink directories and dotfiles"""
for d in config_directories_to_symlink:
link = home_dot_config/d
if not link.is_symlink():
link.symlink_to(dot_config/d)
for df in dotfiles_to_symlink:
link = home/f'.{df}'
if not link.is_symlink():
link.symlink_to(dotfiles/df)
def brew_installed():
return Path(brew_binary).is_file()
def git_clone(url=None, path=None):
if url==None or path==None:
raise ValueError('Tried to call git_clone without passing in a repo url or local path')
subprocess.run(['git', 'clone', url, path])
def brew_install():
robo_says(f'installing my true brew')
if brew_installed():
robo_says(f'true brew already installed', 'green')
else:
subprocess.run('/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"', shell=True)
subprocess.run([brew_binary, 'analytics', 'off'])
robo_says('my true brew installed and analytics turned off', 'green')
def brew_update():
if brew_installed:
robo_says('updating brew packages')
subprocess.run([brew_binary, 'update'])
robo_says('brew updated', 'green')
def xcode_cli_install():
if not is_mac:
return
exists = subprocess.run(['xcode-select', '--print-path'])
if not exists.returncode == 0:
subprocess.run(['xcode-select', '--install'])
robo_says('xcode cli tools installed', 'green')
else:
robo_says('xcode cli tools already installed')
def mac_core_software_update():
robo_says('updaing core macos packages')
subprocess.run(['sudo', 'softwareupdate', '-i', '-a'])
robo_says('core macos packages updated', 'green')
def exec_in_fish(cmd):
subprocess.run(cmd, shell=True, executable=f'{homebrew_prefix}/bin/fish')
def conda_setup():
"""Setup conda shells and environments"""
robo_says('adding conda init to shells')
# TODO: Make work on linux ... need to look up where conda is
for sh in ('fish', 'zsh', 'bash', 'xonsh'):
subprocess.run(f'{homebrew_prefix}/anaconda3/bin/conda init {sh}', shell=True, executable=f'{homebrew_prefix}/bin/{sh}')
# Use a conda env that is a clone of base, but maintain base as a clean reference
# Interestingly right now, only from fish can you:
# - calling conda activate, other shells don't see the init block
# - sourcing ~/.{whichever_shell}rc to load a conda env changed in the config, other shells return status 0 but don't register the change
robo_says('creating snake_jazz ... sss ss sss sssss')
exec_in_fish('conda create --name snake_jazz --clone base')
# fish should now pick up snake_jazz as its default when called from subprocess
def conda_packages_install():
"""install conda packages"""
robo_says('installing conda packages')
for pkg in conda_packages:
exec_in_fish(f'conda install --name snake_jazz {pkg}')
robo_says('done installing conda packages', 'green')
def pip_packages_install():
"""install pip packages"""
robo_says('installing pip packages')
for pkg in pip_packages:
exec_in_fish(f'pip install {pkg}')
robo_says('done setting up python! I know snake math!', 'green')
# install gems
def gems_install():
"""Install gems"""
robo_says('installing ruby gems')
for pkg in gems:
exec_in_fish(f'gem install {pkg}')
robo_says('ruby gems installed', 'green')
def npms_install():
"""Install npm packages"""
robo_says('installing npm packages')
for pkg in npm_global_packages:
exec_in_fish(f'npm install -g {pkg}')
robo_says('npm packages installed', 'green')
def asdf_plugins_install():
"""Install asdf plugins"""
robo_says('installing asdf plugins')
for lang in asdf_languages:
subprocess.run(['asdf', 'plugin', 'add', lang])
def asdf_langs_install():
"""Install latest versions of asdf languages"""
# get nodejs gpg keys
robo_says('getting nodejs gpg keys')
subprocess.run('/bin/bash ~/.asdf/plugins/nodejs/bin/import-release-team-keyring', shell=True)
for lang in asdf_languages:
robo_says(f'installing the latest version of {lang}')
subprocess.run(['asdf', 'install', lang, 'latest'])
subprocess.run(f'asdf global {lang} $(asdf latest {lang})', shell=True)
def zsh_setup():
robo_says('setting up zsh')
git_clone('https://github.com/zdharma/zinit.git', home/'.zinit/bin')
robo_says('zinit installed', 'green')
robo_says('sourcing zshrc and installing zsh plugins')
subprocess.run('source ~/.zshrc', shell=True, executable='/usr/local/bin/zsh') # TODO: make this work on linux
robo_says('zsh plugins loaded', 'green')
robo_says('setting the default shell to zsh')
subprocess.run(['chsh', '-s', f'{homebrew_prefix}/bin/zsh'])
def dotfiles_install():
robo_says('fetching dotfiles')
git_clone('https://github.com/bs/dotfiles', home/'dotfiles')
def brewfile_install():
robo_says('installing brew packages')
subprocess.run('brew bundle --file ~/.Brewfile', shell=True, executable='/bin/bash')
def brew_post():
"""All of the things to do after brew packages are installed"""
subprocess.run('$(brew --prefix)/opt/fzf/install', shell=True)
subprocess.run(['brew', 'cleanup'])
subprocess.run(['brew', 'doctor'])
def macos_configs_install():
robo_says('flipping the switches on macOS configs')
subprocess.run(f'/bin/bash ~/dotfiles/{macos_config_script}', shell=True)
robo_says('macos configs done', 'green')
if __name__ == "__main__":
robo_says('Running this dope thing.')
create_directories()
dotfiles_install()
# Core packages
xcode_cli_install()
mac_core_software_update()
# Move things around
sym_the_links()
# All the Homebrew things
brew_install()
brew_update()
brewfile_install() # note: conda is installed here on mac
brew_post()
# shells
zsh_setup()
# programming languages
conda_setup()
asdf_plugins_install()
asdf_langs_install()
conda_packages_install()
pip_packages_install()
gems_install()
npms_install()
robo_says('done setting up programming languages. I know snake math!', 'green')
macos_configs_install()
robo_says('All done! Celebrate!!! (and reboot)', 'green')
### SCRAP
# import a module ... download it if it doesn't exist locally
# def install_and_import(package):
# import importlib
# try:
# importlib.import_module(package)
# except ImportError:
# print(f'pip installing package {package}')
# result = subprocess.run(['pip', 'install', package], capture_output = True)
# if result.returncode == 1:
# raise
# else:
# print(f'results: {result.stdout}')
# finally:
# print(f'importing {package}')
# globals()[package] = importlib.import_module(package)
# def abort_if_false(ctx, param, value):
# if not value:
# print(f'oh no! {ctx}, {param}, {value}')
# print(dir(ctx))
# # TODO: come back and make this shiz work
# ctx.fail('oh noes')
# # ctx.abort()
# @click.command()
# @click.option('--yes', callback=abort_if_false, is_flag=True, default=True, prompt='Ready to get started?')
# def testo(yes):
# click.echo(f'hi dude! {yes}')