-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·234 lines (183 loc) · 6.18 KB
/
run
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
#!/usr/bin/env bash
set -o errexit
set -o pipefail
DC="${DC:-run}"
# If we're running in CI we need to disable TTY allocation for docker compose
# commands that enable it by default, s``uch as exec and run.
TTY=""
if [[ ! -t 1 ]]; then
TTY="-T"
fi
# -----------------------------------------------------------------------------
# Helper functions start with _ and aren't listed in this script's help menu.
# -----------------------------------------------------------------------------
ORANGE='\033[0;33m'
RED='\033[0;31m'
GREEN='\033[0;32m'
ENDCOLOR='\033[0m'
UNSUPPORTED_WITHIN_DEV_CONTAINER="${RED}This command is not supported when running within a dev container.${ENDCOLOR}"
function _within_dev_container {
if [ -n "$REMOTE_CONTAINERS" ]; then
# Running within dev container on the host machine
return 0
else
# Running on the host machine
return 1
fi
}
function _dc {
if _within_dev_container; then
# Do nothing if running within a dev container
echo -e $UNSUPPORTED_WITHIN_DEV_CONTAINER
return 1
fi
docker compose "${DC}" ${TTY} "${@}"
}
function _disabled_pip_command_msg {
DISABLED_COMMAND="${1}"
ALT_COMMAND_NAME="${2}"
echo -e "* ${RED}\`${DISABLED_COMMAND}\` is disabled on this project.${ENDCOLOR}"
echo -e " Please use ${GREEN}\`${ALT_COMMAND_NAME}\`${ENDCOLOR} instead."
echo " The new command will automatically manage \`requirements.in\` and \`requirements.txt\` files for you via pip-tools."
}
function _remove_dependencies_to_requirements_in {
local ADDED_DEPS_COUNT
ADDED_DEPS_COUNT=0
for pkg in "$@"; do
if grep -qxF "${pkg}" requirements.in; then
sed --in-place "/${pkg}=\{0,2\}.*/d" ./requirements.in
echo -e "${ORANGE}* Removed '${pkg}' from './requirements.in'.${ENDCOLOR}"
ADDED_DEPS_COUNT=$((ADDED_DEPS_COUNT+1))
else
echo -e "${RED}• Did not find '${pkg}' in './requirements.in'.${ENDCOLOR}"
fi
done
if [ $ADDED_DEPS_COUNT -gt 0 ]; then
echo -e "${ORANGE}Caution: Please ensure that 'requirements.in' has been correctly modified for you.${ENDCOLOR}"
fi
}
function _add_dependencies_to_requirements_in {
for pkg in "$@"; do
if ! grep -qxF "${pkg}" requirements.in; then
echo "${pkg}" >> requirements.in
echo -e "${GREEN}* Added '${pkg}' to './requirements.in'.${ENDCOLOR}"
else
echo -e "${ORANGE}• Did not add '${pkg}' to './requirements.in'. Dependency already existed.${ENDCOLOR}"
fi
done
}
function _sync_pip_dependencies {
cmd pip-sync # Sync dependencies from 'requirements.in'
cmd pip-compile -q --resolver=backtracking # Write 'requirements.txt'
}
# -----------------------------------------------------------------------------
function build {
# Build the stack
if _within_dev_container; then
echo -e $UNSUPPORTED_WITHIN_DEV_CONTAINER
return 1
fi
docker compose build
}
function cmd {
# Run any command you want in the web container
if _within_dev_container; then
# Execute the commands directly, as we're already within the web container
eval "${@}"
else
_dc web "${@}"
fi
}
function python {
# Run python in the web container
local WATCH_FOR_CHANGES
while getopts ':w' OPTS; do
case "$OPTS" in
w)
WATCH_FOR_CHANGES=true
;;
*)
WATCH_FOR_CHANGES=false
esac
done
if [ "$WATCH_FOR_CHANGES" == true ]; then
shift $((OPTIND-1))
echo -e "${GREEN}Hot Reload is enabled.${ENDCOLOR}"
echo -e "${GREEN}+ Using 'jurigged' with args: ${@}${ENDCOLOR}"
cmd python3 -m jurigged "${@}"
else
cmd python3 "${@}"
fi
}
function listen {
# TODO: tidy
# Open XQuartz on Mac
if [[ "$OSTYPE" =~ ^darwin ]]; then
echo -e "${ORANGE}You're on macOS${ENDCOLOR}"
echo -e "${ORANGE}- Opening XQuartz (make sure you've allowed localhost via \`xhost + localhost\`. See README):${ENDCOLOR}"
open -a XQuartz.app --args :0 -listen tcp
echo -e "${ORANGE}- Forwarding for X11 events from \`/tmp/.X11-unix/X0\` using socat:${ENDCOLOR}"
socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:/tmp/.X11-unix/X0
# >&2
# echo -e "${ORANGE}- Reverting \`localhost\` from X11 allow list:${ENDCOLOR}"
# xhost - localhost
else
echo -e "${ORANGE}You need to run this command from your host macOS machine.${ENDCOLOR}"
fi
}
function app {
# Run the main app script
python "${@}" src/app.py
}
function tests {
python -m pytest tests "${@}"
}
function bash {
# Run bash
if _within_dev_container; then
echo -e $UNSUPPORTED_WITHIN_DEV_CONTAINER
echo "* Hint: Just run the commands on this terminal that you're currently using :)"
return 1
fi
cmd bash
}
function pip {
# Run pip in the web container
if [ "$1" == "install" ]; then
_disabled_pip_command_msg "./run pip install ..." "./run pip:install"
return 1
elif [ "$1" == "uninstall" ]; then
_disabled_pip_command_msg "./run pip uninstall ..." "./run pip:uninstall"
return 1
fi
python -m pip "${@}"
}
function pip:install {
# Install Python dependencies from 'requirements.in' and write the 'requirements.txt' file
# Update requirements.in to add the provided dependencies
_add_dependencies_to_requirements_in "${@}"
# Write 'requirements.txt' and catch any exceptions
if ! cmd pip-compile -q --resolver=backtracking; then
# If it failed to write, perhaps because it could not satisfy any of the provided dependencies
# revert all lines we've previously added to requirements.in.
_remove_dependencies_to_requirements_in "${@}"
return 1
fi
_sync_pip_dependencies
echo -e "${GREEN}Your dependencies are in-sync 🎉.${ENDCOLOR}"
}
function pip:uninstall {
# Remove unwanted deps from reuirements.in, write requirements.txt and sync.
# Update requirements.in to remove the provided dependencies
_remove_dependencies_to_requirements_in "${@}"
_sync_pip_dependencies
echo -e "${GREEN}Your dependencies are in-sync 🎉.${ENDCOLOR}"
}
function help {
printf "%s <task> [args]\n\nTasks:\n" "${0}"
compgen -A function | grep -v "^_" | cat -n
printf "\nExtended help:\n Each task has comments for general usage\n"
}
# This idea is heavily inspired by: https://github.com/adriancooney/Taskfile
TIMEFORMAT=$'\nTask completed in %3lR'
time "${@:-help}"