-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·269 lines (218 loc) · 6.58 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
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
#!/usr/bin/env bash
set -o errexit
set -o pipefail
DC="${DC:-run}"
DC_FILE_TEST="docker-compose.test.yml"
# If we're running in CI we need to disable TTY allocation for docker compose
# commands that enable it by default, such 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.
# -----------------------------------------------------------------------------
function _dc {
docker compose "${DC}" ${TTY} "${@}"
}
function _dc-test-rm {
docker compose -f ${DC_FILE_TEST} "${DC}" --rm ${TTY} "${@}"
}
function _build_run_down {
docker compose build
docker compose run ${TTY} "${@}"
docker compose down
}
# -----------------------------------------------------------------------------
function cmd {
# Run any command you want in the web container
_dc web "${@}"
}
function cmd-test {
# Run any command you want in the web container
_dc-test-rm web-test "${@}"
}
function cmd-js {
# Run any command you want in the js container
_dc js "${@}"
}
function manage-test {
# Run any manage.py commands
# We need to collectstatic before we run our tests.
if [ "${1-''}" == "test" ]; then
test-create-env
cmd-test python3 manage.py collectstatic --no-input
cmd-test python3 manage.py "${@}"
docker compose down --remove-orphans
else
cmd-test python3 manage.py "${@}"
fi
}
function manage {
# Run any manage.py commands
# We need to collectstatic before we run our tests.
if [ "${1-''}" == "test" ]; then
test-create-env
cmd-test python3 manage.py collectstatic --no-input
cmd-test python3 manage.py "${@}" || docker compose down --remove-orphans
docker compose down --remove-orphans
else
cmd python3 manage.py "${@}"
fi
}
function lint:dockerfile {
# Lint Dockerfile
docker container run --rm -i \
hadolint/hadolint hadolint --ignore DL3008 -t style "${@}" - < Dockerfile
}
function lint {
# Lint Python code
cmd flake8 "${@}"
}
function lint-js {
if [ "$1" == "fix" ]; then
cmd-js yarn lint:fix
else
cmd-js yarn lint
fi
}
function format:imports {
# Sort Python imports
cmd isort . "${@}"
}
function format {
# Format Python code
cmd black . "${@}"
}
function quality {
# Perform all code quality commands together
format:imports
format
lint
lint-js fix
}
function secret {
# Generate a random secret that can be used for your SECRET_KEY and more
cmd python3 -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())"
}
function shell {
# Start a shell session in the web container
cmd bash "${@}"
}
function psql {
# Connect to PostgreSQL
# shellcheck disable=SC1091
. .env
_dc postgres psql -U "${POSTGRES_USER}" "${@}"
}
function redis-cli {
# Connect to Redis
_dc redis redis-cli "${@}"
}
function pip3:install {
# Install pip3 dependencies and write lock file
_build_run_down web bash -c "cd .. && bin/pip3-install"
}
function mkdocs {
# Run an mkdocs command
cmd mkdocs "${@}"
}
function pip3:outdated {
# List any installed packages that are outdated
cmd pip3 list --outdated
}
function yarn:install {
# Install yarn dependencies and write lock file
_build_run_down js yarn install
}
function yarn:outdated {
# List any installed packages that are outdated
_dc js yarn outdated
}
function yarn:build:js {
# Build JS assets, this is meant to be run from within the assets container
mkdir -p ../public/js
node esbuild.config.mjs
}
function yarn:build:css {
# Build CSS assets, this is meant to be run from within the assets container
local args=()
if [ "${NODE_ENV:-}" == "production" ]; then
args=(--minify)
else
args=(--watch)
fi
mkdir -p ../public/css
DEBUG=0 tailwindcss --postcss -i css/app.css -o ../public/css/app.css "${args[@]}"
}
function clean {
# Remove cache and other machine generates files
rm -rf public/*.* public/admin public/js public/css public/images public/fonts \
public_collected/*.* public_collected/admin public_collected/js \
public_collected/css public_collected/images public_collected/fonts \
.pytest_cache/ .coverage celerybeat-schedule
touch public/.keep public_collected/.keep
}
function ci:install-deps {
# Install Continuous Integration (CI) dependencies
sudo apt-get install -y curl shellcheck
sudo curl \
-L https://raw.githubusercontent.com/nickjj/wait-until/v0.2.0/wait-until \
-o /usr/local/bin/wait-until && sudo chmod +x /usr/local/bin/wait-until
}
function test-create-env {
if [ ! -f .env.test ]; then
cp -n .env.example .env.test
# Delete existing vars.
sed -i'' -e '/REDIS_URL=/d' .env.test
sed -i'' -e '/POSTGRES_HOST=/d' .env.test
sed -i'' -e '/STRIPE_SECRET_KEY=/d' .env.test
sed -i'' -e '/STRIPE_API_BASE=/d' .env.test
sed -i'' -e '/STRIPE_WEBHOOK_SECRET=/d' .env.test
sed -i'' -e '/REDIS_URL=/d' .env.test
sed -i'' -e '/SITE_PORT==/d' .env.test
# Append vars.
echo 'export REDIS_URL=redis://redis-test:6379/0' >> .env.test
echo 'export POSTGRES_HOST=postgres-test' >> .env.test
echo 'export STRIPE_SECRET_KEY=sk_test_123' >> .env.test
echo 'export STRIPE_API_BASE=http://stripe-mock:12111' >> .env.test
echo 'export STRIPE_WEBHOOK_SECRET=sk_test_123' >> .env.test
echo 'export SITE_PORT=8000' >> .env.test
# Delete unused produced file on MacOS.
rm -f .env.test-e
fi
}
function ci:test {
# Execute Continuous Integration (CI) pipeline
#
# It's expected that your CI environment has these tools available:
# - https://github.com/koalaman/shellcheck
# - https://github.com/nickjj/wait-until
# shellcheck run bin/*
# lint:dockerfile "${@}"
# create .env if not exists.
test-create-env
cp -n .env.test .env || echo "skipping .env.test create"
docker compose -f ${DC_FILE_TEST} build
docker compose -f ${DC_FILE_TEST} up web-test -d
# shellcheck disable=SC1091
. .env
# wait-until "docker compose exec -T \
# -e PGPASSWORD=${POSTGRES_PASSWORD} postgres \
# psql -U ${POSTGRES_USER} ${POSTGRES_USER} -c 'SELECT 1'"
docker compose -f ${DC_FILE_TEST} logs
lint "${@}"
lint-js
format:imports --check --diff
format --check --diff
manage-test migrate
manage-test test
}
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}"