-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap
executable file
·245 lines (195 loc) · 7.92 KB
/
bootstrap
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
#!/usr/bin/env python3
from __future__ import print_function
from __future__ import unicode_literals
import os
import sys
import argparse
import glob
from os.path import expanduser
import platform
import shutil
from subprocess import call
HOME = expanduser('~')
OS = platform.system()
CURRENTPATH = os.path.dirname(os.path.realpath(__file__))
DOTFILEDIR = os.path.realpath('%s/../' % CURRENTPATH)
BREWPATH = "%s/brew/path.zsh" % DOTFILEDIR
PY2 = sys.version_info[0] == 2
if PY2 is False:
raw_input = input
def installGitConfiguration(name, email, backup, overwrite):
print("Add git configuration...")
# we want to use the git version installed by brew if available
returnCode = call(['bash', '-c', 'source %s && git --version' % BREWPATH]) # check git version
if OS == "Darwin" and returnCode == 69:
call(['open', '-a', 'xcode'])
exit(69)
return
filePath = os.path.join(HOME, '.gitconfig')
fileExists = os.path.exists(filePath)
if ( overwrite is False and backup is False) and fileExists:
step = raw_input('git configuration already exists, what do you want to do? [e]xit, [b]ackup all or [o]verride all? ')
if step.lower() == 'b':
backup = True
elif step.lower() == 'e':
exit(1)
elif step.lower() == "o":
overwrite = True
backupFilePath = '%s.bk' % filePath
backupFilePathExists = os.path.exists(filePath)
if backupFilePathExists == False and fileExists and backup:
print("Creating backup copy of %s" % filePath)
os.rename(filePath, '%s.bk' % filePath)
elif fileExists:
if os.path.isdir(filePath):
os.rmdir(filePath)
else:
print("Deleting %s" % filePath)
os.remove(filePath)
configPath = os.path.join(DOTFILEDIR, 'git', 'config')
call(['bash', '-c', 'source %s && git config --global include.path %s' % (BREWPATH, configPath)])
while name == None:
name = raw_input('Please enter your name: ')
call(['bash', '-c', 'source %s && git config --global user.name "%s"' % (BREWPATH, name)])
while email == None:
email = raw_input('Please enter your email: ')
call(['bash', '-c', 'source %s && git config --global user.email "%s"' % (BREWPATH, email)])
def installSymlinks(backup, overwrite=False):
"""
Install all *.symlink inside of dotfile
"""
print("Create symlinks...")
os.chdir(DOTFILEDIR)
for file in glob.glob('*/*.symlink'):
fileName, fileExtension = os.path.splitext(os.path.basename(file))
filePath = os.path.join(HOME, '.%s' % fileName)
fileExists = os.path.exists(filePath)
if ( overwrite is False and backup is False) and fileExists:
step = raw_input('File already exists, what do you want to do? [e]xit, [b]ackup all or [o]verride all? ')
if step.lower() == 'b':
backup = True
elif step.lower() == 'e':
exit(1)
elif step.lower() == "o":
overwrite = True
backupFilePath = '%s.bk' % filePath
backupFilePathExists = os.path.exists(filePath)
if backupFilePathExists == False and backup and fileExists:
print("Creating backup copy of %s" % filePath)
os.rename(filePath, '%s.bk' % filePath)
elif fileExists:
print("Deleting %s" % filePath)
os.remove(filePath)
homePath = os.path.join(DOTFILEDIR, file)
print("Creating a symlink to %s from %s" % (filePath, homePath))
os.symlink(homePath, filePath)
def initZsh():
localFile = os.path.join(HOME, '.zshinit')
exist = os.path.exists(localFile)
if exist:
os.remove(localFile)
with open(localFile, "a+") as file:
if "DOTFILEDIR" not in file.read():
print("Updating .zshinit file...")
file.write('export DOTFILEDIR=%s\n' % DOTFILEDIR)
file.write('export ZSH=%s/zsh\n' % DOTFILEDIR)
file.write('eval "$($DOTFILEDIR/bin/dotfile-env)"\n')
def runInstall():
for file in glob.glob('*/install'):
os.system(file)
# install any extra dep needed by
# package.
for file in glob.glob('*/extra_deps'):
os.system(file)
def restore():
step = raw_input('Are you sure you want to uninstall dotfiles [e]xit, [c]ontinue? ')
if step.lower() != 'c':
exit(1)
print("Uninstalling")
filePath = os.path.join(HOME, '.gitconfig')
fileExists = os.path.exists(filePath)
if fileExists:
print("removing %s file" % filePath)
os.remove(filePath)
backupFile = "%s.bk" % filePath
backupFileExists = os.path.exists("%s.bk" % filePath)
if backupFileExists:
print("restoring %s file" % backupFile)
os.rename(backupFile, filePath)
for file in glob.glob('*/*.symlink'):
fileName, fileExtension = os.path.splitext(os.path.basename(file))
filePath = os.path.join(HOME, '.%s' % fileName)
fileExists = os.path.exists(filePath)
if fileExists:
print("removing %s file" % filePath)
os.remove(filePath)
backupFile = "%s.bk" % filePath
backupFileExists = os.path.exists("%s.bk" % filePath)
if backupFileExists:
print("restoring %s file" % backupFile)
os.rename(backupFile, filePath)
def update_dotfile_bash(migrate=True):
bash_file = os.path.join(HOME, '.bashrc')
dotfiles_bash = os.path.join(HOME, '.dotfiles.bash')
if migrate:
migrate_bash(bash_file)
content = """
[ -f ~/.dotfiles.bash ] && source ~/.dotfiles.bash
"""
with open(bash_file, 'a+') as f:
if content not in f.read():
print("Updating .bashrc file...")
f.write('%s' % content)
with open(dotfiles_bash, 'w') as f:
f.write("""
if [[ $- == *i* ]] && [ -d "%s" ]
then
source %s/brew/path.zsh
export SHELL=`which zsh`
[ -z "$ZSH_VERSION" ] && exec "$SHELL" -l
fi
""" % (DOTFILEDIR, DOTFILEDIR))
def migrate_bash(bash_file):
"""
Remove old bash configuration lines
"""
lines = []
remove_lines = ["source %s/brew/path.zsh" % DOTFILEDIR, "export SHELL=`which zsh`", '[ -z "$ZSH_VERSION" ] && exec "$SHELL" -l']
print(remove_lines)
with open(bash_file) as f:
for line in f.readlines():
if line.strip() not in remove_lines:
lines.append(line)
with open(bash_file, 'w') as f:
content = "".join(lines)
f.write(content)
def main():
parser = argparse.ArgumentParser(description='Install dotfiles')
parser.add_argument('--backup', help='Backup files', action='store_false')
parser.add_argument('--restore', help='restore to backup file', action='store_true')
parser.add_argument('--bash', help='Enable zsh shell via bash', action='store_true')
parser.add_argument('--name', help='Name to use for git', type=str)
parser.add_argument('--email', help='Email to use for git', type=str)
parser.add_argument('-s','--link', help='Don\'t run install symlink', action='store_false')
parser.add_argument('-c','--conf', help='Don\'t run git configuration', action='store_false')
parser.add_argument('-i','--install', help='Don\'t run */install files', action='store_false')
parser.add_argument('-o','--overwrite', help='Overwrite all files', action='store_true')
args = parser.parse_args()
if args.restore:
restore()
return
if args.bash:
update_dotfile_bash()
return
if args.link:
update_dotfile_bash(False)
installSymlinks(args.backup, args.overwrite)
initZsh()
if args.install:
runInstall()
if args.conf:
installGitConfiguration(args.name, args.email, args.backup, args.overwrite)
if args.bash != False and args.link:
print("Run chsh -s $(which zsh) or script/bootstrap --bash to set zsh as default shell")
if __name__ == '__main__':
main()