-
Notifications
You must be signed in to change notification settings - Fork 151
/
dev-env-cli.sh
executable file
·317 lines (278 loc) · 8.62 KB
/
dev-env-cli.sh
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
#!/usr/bin/env bash
set -o nounset # exit if a variable is not set
set -o errexit # exit for any command failure"
# text color escape codes (\033 == \e but OSX doesn't respect the \e)
blue_text='\033[94m'
green_text='\033[32m'
red_text='\033[31m'
default_text='\033[39m'
# set -x/xtrace uses PS4 for more info
PS4="$blue_text""${0}:${LINENO}: ""$default_text"
debug() {
if [ "$opt_verbose" = true ]; then
echo $1
fi
}
check_dependencies() {
if ! command -v python3 &> /dev/null; then
echo "$red_text""python3 not found. Exiting.""$default_text"
exit 1
fi
if ! command -v docker compose &> /dev/null; then
echo "$red_text""docker not found. Exiting.""$default_text"
exit 1
fi
# For 'docker compose' vs 'docker-compose', see https://stackoverflow.com/a/66526176.
if command -v docker compose &> /dev/null; then
docker_compose_cmd="docker compose"
elif command -v docker-compose &> /dev/null; then
docker_compose_cmd="docker-compose"
else
echo "$red_text""Both 'docker compose' and 'docker-compose' not found. Exiting.""$default_text"
exit 1
fi
if ! command -v pdm &> /dev/null; then
echo "$red_text""pdm not found. Exiting.""$default_text"
exit 1
fi
}
display_banner() {
# Make sure the console is huge
if test $(tput cols) -ge 64; then
echo " █████ █████"
echo "░░███ ░░███ "
echo " ░███ ░███ "
echo " ░███ ░███ "
echo " ░███ ░███ "
echo " ░███ ░███ "
echo " ░░█████████ >UNSTRACT COMMUNITY EDITION"
echo " ░░░░░░░░░ "
echo ""
sleep 1
fi
}
display_help() {
printf "Dev environment CLI for Unstract platform services\n"
echo
echo -e "Syntax: $0 [options] -s service"
echo -e "Options:"
echo -e " -h, --help Display help information"
echo -e " -e, --setup-venv Setup venv (requires service)"
echo -e " -a, --activate-venv Activate venv (requires service)"
echo -e " -i, --install-deps Install dependencies in venv (requires service)"
echo -e " -d, --destroy-venv Destroy venv (requires service)"
echo -e " -p, --install-pre-commit-hook Install Git pre-commit hook"
echo -e " -r, --run-pre-commit-hook Run Git pre-commit hook"
echo -e " -f, --force Force operation"
echo -e " -s, --service Service name"
echo -e " -x, --trace Enables trace mode"
echo -e " -V, --verbose Print verbose logs"
echo -e ""
}
parse_args() {
while [[ $# -gt 0 ]]; do
arg="$1"
case $arg in
-h | --help)
display_help
exit
;;
-e | --setup-venv)
opt_setup_venv=true
;;
-a | --activate-venv)
opt_activate_venv=true
;;
-i | --install-deps)
opt_install_deps=true
;;
-d | --destroy-venv)
opt_destroy_venv=true
;;
-p | --install-pre-commit-hook)
opt_install_pre_commit_hook=true
;;
-r | --run-pre-commit-hook)
opt_run_pre_commit_hook=true
;;
-f | --force)
opt_force="--force"
;;
-s | --service)
if [ -z "${2-}" ]; then
echo "No service specified."
echo
display_help
exit
else
opt_service="$2"
fi
shift
;;
-x | --trace)
set -o xtrace # display every line before execution; enables PS4
;;
-V | --verbose)
opt_verbose=true
;;
*)
echo "'$1' is not a known command."
echo
display_help
exit
;;
esac
shift
done
if [ "$opt_install_pre_commit_hook" = false ] && [ "$opt_run_pre_commit_hook" = false ]; then
if [ "$opt_service" = "" ]; then
echo "No service specified."
echo
display_help
exit
fi
ret=$(echo ${services[@]} | grep -ow "$opt_service" | wc -w)
if [ $ret -eq 0 ]; then
echo "Unknown service '$opt_service'."
echo
display_help
exit
fi
fi
if [ "$opt_setup_venv" = false ] && [ "$opt_activate_venv" = false ] &&
[ "$opt_install_deps" = false ] && [ "$opt_destroy_venv" = false ] &&
[ "$opt_install_pre_commit_hook" = false ] && [ "$opt_run_pre_commit_hook" = false ]; then
echo "One of -e,-a,-i,-d,-p,-r options should be specified."
echo
display_help
exit
fi
debug "OPTION setup venv: $opt_setup_venv"
debug "OPTION activate venv: $opt_activate_venv"
debug "OPTION install deps: $opt_install_deps"
debug "OPTION destroy venv: $opt_destroy_venv"
debug "OPTION install pre-commit hook: $opt_install_pre_commit_hook"
debug "OPTION run pre-commit hook: $opt_run_pre_commit_hook"
debug "OPTION service: $opt_service"
debug "OPTION verbose: $opt_verbose"
}
setup_venv() {
if [ "$opt_setup_venv" = false ]; then
return
fi
pushd ${script_dir}/$opt_service 1>/dev/null
if [ -e "package.json" ]; then
echo -e "Nothing to do for ""$blue_text""$opt_service""$default_text"
elif [ -e "pyproject.toml" ]; then
echo -e "Setting up ""$blue_text""Python venv""$default_text"" for ""$blue_text""$opt_service""$default_text"
pdm venv create -w virtualenv --with-pip $opt_force
fi
popd 1>/dev/null
}
activate_venv() {
if [ "$opt_activate_venv" = false ]; then
return
fi
pushd ${script_dir}/$opt_service 1>/dev/null
if [ -e "package.json" ]; then
echo -e "Nothing to do for ""$blue_text""$opt_service""$default_text"
elif [ -e "pyproject.toml" ]; then
if [ ! -e ".venv" ]; then
echo -e "venv not found for ""$blue_text""$opt_service""$default_text"". Please run setup first."
return
fi
echo -e "Run the following in a ""$blue_text""new terminal""$default_text"" to activate venv for ""$blue_text""$opt_service""$default_text"":"
echo ""
echo "cd ${script_dir}/$opt_service"
echo "eval \"$(pdm venv activate in-project)\""
fi
popd 1>/dev/null
}
install_deps() {
if [ "$opt_install_deps" = false ]; then
return
fi
pushd ${script_dir}/$opt_service 1>/dev/null
if [ -e "package.json" ]; then
echo -e "Installing dependencies for ""$blue_text""$opt_service""$default_text"
npm ci
elif [ -e "pyproject.toml" ]; then
if [ ! -e ".venv" ]; then
echo -e "venv not found for ""$blue_text""$opt_service""$default_text"". Please run setup first."
return
fi
echo -e "Installing dependencies in venv for ""$blue_text""$opt_service""$default_text"
eval "$(pdm venv activate in-project)"
pdm sync
fi
popd 1>/dev/null
}
destroy_venv() {
if [ "$opt_destroy_venv" = false ]; then
return
fi
pushd ${script_dir}/$opt_service 1>/dev/null
if [ -e "package.json" ]; then
echo -e "$blue_text""Nothing to do for $opt_service""$default_text"
elif [ -e "pyproject.toml" ]; then
if [ ! -e ".venv" ]; then
echo -e "venv not found for ""$blue_text""$opt_service""$default_text"". Please run setup first."
return
fi
echo -e "Destroying venv for ""$blue_text""$opt_service""$default_text"
pdm venv remove in-project
fi
popd 1>/dev/null
}
install_pre_commit_hook() {
if [ "$opt_install_pre_commit_hook" = false ]; then
return
fi
pushd ${script_dir} 1>/dev/null
echo -e "Installing ""$blue_text""Git pre-commit hook""$default_text"
pdm venv create -w virtualenv --with-pip $opt_force
eval $(pdm venv activate in-project)
pdm sync --dev -G lint -G hook-check-django-migrations
pre-commit install
popd 1>/dev/null
}
run_pre_commit_hook() {
if [ "$opt_run_pre_commit_hook" = false ]; then
return
fi
pushd ${script_dir} 1>/dev/null
if [ ! -e ".venv" ]; then
echo -e "$blue_text""Git pre-commit hook""$default_text"" not found. Please run install first."
return
fi
eval "$(pdm venv activate in-project)"
pre-commit run
popd 1>/dev/null
}
#
# Run Unstract platform - BEGIN
#
check_dependencies
opt_setup_venv=false
opt_activate_venv=false
opt_install_deps=false
opt_destroy_venv=false
opt_install_pre_commit_hook=false
opt_run_pre_commit_hook=false
opt_force=""
opt_service=""
opt_verbose=false
script_dir=$(dirname "$(readlink -f "$BASH_SOURCE")")
# Extract service names from docker compose file.
services=($(VERSION="latest" $docker_compose_cmd -f $script_dir/docker/docker-compose.build.yaml config --services))
display_banner
parse_args $*
setup_venv
activate_venv
install_deps
destroy_venv
install_pre_commit_hook
run_pre_commit_hook
#
# Run Unstract platform - END
#