forked from devshorts/EnvZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap
executable file
·306 lines (226 loc) · 6.85 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
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
#!/usr/bin bash
# set the directory where this is installed to
function set_zsh_options(){
# let aliases be re-autocompleted by zsh
#setopt complete_aliases
# dont have directories auto named by their variables
# http://stackoverflow.com/a/17104442/310196
unsetopt auto_name_dirs
}
## ZSH specific stuff
is_zsh=`ps $$ | grep zsh`
if [ "$?" = "0" ]; then
export ENVIRONMENT_LOCATION=`dirname $0`
export IS_ZSH=true
set_zsh_options
fpath=($ENVIRONMENT_LOCATION/zsh_completions/**/ $fpath)
else
# make sure bash gets the env var location
export ENVIRONMENT_LOCATION="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
fi
export USER_MODULES=$ENVIRONMENT_LOCATION/users
export SOURCE_UTILS=$ENVIRONMENT_LOCATION/modules/00_env_tools
no_color='\033[0m' # No Color
yellow='\033[0;33m'
red='\033[0;31m'
cyan='\033[0;36m'
function log-info(){
log-color $cyan "$@"
}
function log-warn(){
log-color $yellow "$@"
}
function log-error(){
log-color $red "$@"
}
function log-color(){
color=$1
shift
echo -e "${color}$@${NC}"
}
_ensure_json_config() {
local config_file=$ENVIRONMENT_LOCATION/.config.json
if [ ! -f $config_file ]; then
echo '{}' > $config_file
fi
}
json_set_pref() {
local config_file=$ENVIRONMENT_LOCATION/.config.json
_ensure_json_config
if [ $# -eq 3 ]; then
local Category=$1
local Setting=$2
local Value=$3
elif [ $# -eq 2 ]; then
local Category='general_settings'
local Setting=$1
local Value=$2
else
log-error 'Not enough args!'
return
fi
settings=$(jq ".\"$Category\".\"$Setting\" = \"$Value\"" $config_file)
if [ $? -eq 0 ]; then
echo $settings >! $config_file
fi
}
_clean_old_config() {
local config_file=$ENVIRONMENT_LOCATION/.config
if [ -f $config_file ]; then
rm $config_file
fi
}
_migrate_old_config() {
local config_file=$ENVIRONMENT_LOCATION/.config
if [ -f $config_file ]; then
log-info 'Found old config, migrating to json config'
_migrate_to_json() {
while read data; do
if [ -z $data ]; then
continue
fi
local -a args
args=(${=data})
log-info "Migrating setting key: '${args[1]}' value: '${args[2]}'"
json_set_pref $args
done
}
awk -F ':' '{ print $1, $2 }' $config_file | _migrate_to_json
json_settings
# _clean_old_config
fi
}
json_settings() {
local config_file=$ENVIRONMENT_LOCATION/.config.json
_ensure_json_config
jq "." $config_file
}
json_get_pref() {
local config_file=$ENVIRONMENT_LOCATION/.config.json
_ensure_json_config
if [ $# -eq 2 ]; then
local Category=$1
local Setting=$2
elif [ $# -eq 1 ]; then
local Category='general_settings'
local Setting=$1
else
log-error 'Not enough args!'
return
fi
jq ".\"$Category\".\"$Setting\" // \"\"" $config_file | sed 's/^"//' | sed 's/"$//'
}
function _reset_pref(){
local pref=$1
json_set_pref "$pref" ""
}
function _setpref(){
json_set_pref $1 $2
}
function _readpref(){
json_get_pref $1
}
function detect-installed(){
type $1 1> /dev/null 2> /dev/null
if [ "$?" = "1" ]; then
echo "Installing $2"
eval $2
fi
}
function ensure_jq() {
# command line json parsing
detect-installed jq "brew install jq"
}
function install-prereqs(){
detect-installed wget "brew install wget"
detect-installed fzf 'brew install fzf && $(brew --prefix)/opt/fzf/install'
detect-installed bat "brew install bat"
detect-installed diff-so-fancy "brew install diff-so-fancy"
detect-installed exa "brew install exa"
detect-installed fd "brew install fd"
detect-installed rg "brew install ripgrep"
detect-installed tig "brew install tig"
detect-installed nvim "brew install nvim"
}
function aliases() {
alias vim='nvim'
alias vi='nvim'
}
function load-basic-modules(){
while IFS= read -r -d $'\0' module_file; do
log-info "Loading $module_file"
source $module_file
done < <(find $ENVIRONMENT_LOCATION/modules -type f -iname '*.zsh' -print0)
while IFS= read -r -d $'\0' module_file; do
log-info "Loading keys"
source $module_file
done < <(find $ENVIRONMENT_LOCATION/modules -type f -iname 'keys' -print0)
}
function load-user-modules(){
userModulePath=$ENVIRONMENT_LOCATION/users
if [ -d $userModulePath ]; then
while IFS= read -r -d $'\0' module_file; do
name=`basename $module_file`
if [[ -f $module_file && $name != "validate.sh" ]]; then
log-info "Loading user module $module_file"
USER_MODULE_PATH=`dirname \`get_abs_filename $module_file\``
source $module_file
fi
done < <(find $userModulePath -type f -iname '*.sh' -follow -print0)
# add zsh completion folder to fpath if it exists
while IFS= read -r -d $'\0' zsh_completion_folder; do
fpath=($zsh_completion_folder $fpath)
log-info "Adding $zsh_completion_folder to fpath"
done < <(find $userModulePath -type d -iname 'zsh_completions' -follow -print0)
# once they are all loaded, run the validation
while IFS= read -r -d $'\0' validation_file; do
if [ -f $validation_file ]; then
log-info "Executing validation on user module $module_file"
USER_MODULE_PATH=`dirname \`get_abs_filename $validation_file\``
source $validation_file
fi
done < <(find $userModulePath -type f -iname 'validate.sh' -follow -print0)
fi
}
function load-source-path(){
if [ -z $SRC_DIR ]; then
srcpath=`_readpref "src-path"`
if [ -z $srcpath ]; then
log-warn "Source path isn't defined!"
log-warn "What is the path where your source code is defined? For example '~/documents/sourecode'?"
read srcpath
srcpath=`echo $srcpath | sed "s;~;$HOME;"`
_setpref "src-path" $srcpath
fi
export SRC_DIR=$srcpath
fi;
}
function set-permissions(){
chmod +x $ENVIRONMENT_LOCATION/modules/scripts/git-diffall
}
function validate(){
log-info "READY"
}
# install jq for config
ensure_jq
# migrate the old .config file to json if needed
_migrate_old_config
# make sure the source code dir is set
load-source-path
# load up any pending required installables
install-prereqs
# load basic modules
load-basic-modules
set-permissions
# load user modules
load-user-modules
# reload compinit if zsh is available now that everything is loaded up
# so that the updated $fpath variable's folders get
# read. this lets the custom zsh_completions folder
# get leveraged
if [ "${IS_ZSH}" = "true" ]; then
reload-compinit
fi
aliases
# final validation
validate