Skip to content

Commit fc94384

Browse files
committed
♻️(project) improve development scripts maintainability
Add a library that is used in all development scripts to ease it's maintainability. Only the library is now supposed to be modified.
1 parent 93a9a80 commit fc94384

File tree

10 files changed

+114
-71
lines changed

10 files changed

+114
-71
lines changed

Makefile

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ default: help
1414
.pre-commit-cache:
1515
mkdir .pre-commit-cache
1616

17+
.git/hooks/_commons.inc.sh:
18+
cp bin/_commons.inc.sh .git/hooks/_commons.inc.sh
19+
1720
.git/hooks/pre-commit:
1821
cp bin/git-pre-commit-hook .git/hooks/pre-commit
1922

@@ -23,17 +26,18 @@ default: help
2326
### BOOTSTRAP
2427
bootstrap: ## setup development environment (build dev service and install git hooks)
2528
bootstrap: \
26-
build \
27-
migrate \
28-
create-superuser \
29-
jupytext--to-ipynb
29+
build \
30+
migrate \
31+
create-superuser \
32+
jupytext--to-ipynb
3033
.PHONY: bootstrap
3134

3235
git-hooks: ## install pre-commit hook
3336
git-hooks: \
34-
.pre-commit-cache \
35-
.git/hooks/pre-commit \
36-
.git/hooks/commit-msg
37+
.pre-commit-cache \
38+
.git/hooks/_commons.inc.sh \
39+
.git/hooks/pre-commit \
40+
.git/hooks/commit-msg
3741
.PHONY: git-hooks
3842

3943
### BUILD

bin/_commons.inc.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#
2+
# Bash library for utility scripts
3+
#
4+
# shellcheck shell=bash
5+
6+
declare DOCKER_USER
7+
declare -i DOCKER_UID
8+
declare -i DOCKER_GID
9+
10+
DOCKER_UID="$(id -u)"
11+
DOCKER_GID="$(id -g)"
12+
# Use a higher GID for MacOS users to prevent permission issues with the default
13+
# GID=20
14+
if [[ "${OSTYPE}" =~ "darwin" ]]; then
15+
DOCKER_GID=1000
16+
fi
17+
DOCKER_USER="${DOCKER_UID}:${DOCKER_GID}"
18+
19+
export DOCKER_UID
20+
export DOCKER_GID
21+
export DOCKER_USER
22+
23+
24+
function compose(){
25+
26+
docker compose "$@"
27+
}
28+
29+
30+
function compose_run(){
31+
32+
if [[ -z "${2}" ]]; then
33+
echo "usage: compose_run [--no-deps] SERVICE COMMAND"
34+
exit 1
35+
fi
36+
37+
# Arguments
38+
declare -i no_deps
39+
if [[ "$1" == "--no-deps" ]]; then
40+
no_deps=1
41+
shift
42+
fi
43+
44+
declare service="${1}"
45+
shift
46+
47+
declare user="${DOCKER_USER}"
48+
declare volume="./src/${service}:/app"
49+
declare -a args=(--rm --user "${user}" --volume "${volume}")
50+
51+
# Prevent 'the input device is not a TTY' error from `docker compose run`
52+
# by disabling TTY when standard output is not a TTY
53+
if [[ "$(tty 2>/dev/null)" == "not a tty" ]]; then
54+
args+=(--no-TTY)
55+
fi
56+
57+
# Do not run related services
58+
if [[ no_deps -eq 1 ]]; then
59+
args+=(--no-deps)
60+
fi
61+
62+
compose run \
63+
"${args[@]}" \
64+
"${service}" \
65+
"$@"
66+
}

bin/compose

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,14 @@
22

33
set -eo pipefail
44

5-
declare DOCKER_USER
6-
DOCKER_UID="$(id -u)"
7-
DOCKER_GID="$(id -g)"
5+
declare cmd
86

7+
source "$(dirname "$(readlink -f "$0")")/_commons.inc.sh"
98

10-
DOCKER_USER="${DOCKER_UID}:${DOCKER_GID}"
11-
12-
extra_args=()
13-
# Prevent 'the input device is not a TTY' error from `docker compose run`
14-
# by disabling TTY when standard output is not a TTY
15-
if [[ "$1" == run && "$(tty 2>/dev/null)" == "not a tty" ]]; then
16-
extra_args=(run --no-TTY)
9+
cmd="compose"
10+
if [[ "$1" == run ]]; then
11+
cmd="compose_run"
1712
shift
1813
fi
1914

20-
21-
DOCKER_USER=${DOCKER_USER} \
22-
DOCKER_UID=${DOCKER_UID} \
23-
DOCKER_GID=${DOCKER_GID} \
24-
docker compose "${extra_args[@]}" "$@"
15+
eval $cmd "$@"

bin/cz

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#!/usr/bin/env bash
22

3+
set -eo pipefail
4+
35
cz --config dev/pyproject.toml "$@"

bin/git-commit-msg-hook

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env bash
22

3+
set -eo pipefail
4+
35
# read commit message
46
MESSAGE=$(cat "$1")
57

bin/git-pre-commit-hook

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#!/usr/bin/env bash
22

3-
declare DOCKER_USER
4-
DOCKER_USER="$(id -u):$(id -g)"
3+
set -eo pipefail
54

6-
DOCKER_USER=${DOCKER_USER} docker compose \
7-
run \
8-
--no-TTY \
9-
--rm \
10-
dev \
5+
source "$(dirname "$(readlink -f "$0")")/_commons.inc.sh"
6+
7+
compose_run --no-deps \
8+
dev \
119
bash -c "cd dev && uv run pre-commit run"

bin/jupytext

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env bash
22

3-
declare DOCKER_USER
4-
DOCKER_USER="$(id -u):$(id -g)"
3+
set -eo pipefail
54

6-
DOCKER_USER=${DOCKER_USER} docker compose run --rm notebook uv run jupytext "$@"
5+
source "$(dirname "$(readlink -f "$0")")/_commons.inc.sh"
6+
7+
compose_run \
8+
notebook \
9+
uv run jupytext "$@"

bin/manage

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22

33
set -eo pipefail
44

5-
declare DOCKER_USER
6-
DOCKER_UID="$(id -u)"
7-
DOCKER_GID="$(id -g)"
8-
DOCKER_USER="${DOCKER_UID}:${DOCKER_GID}"
9-
10-
DOCKER_USER=${DOCKER_USER} \
11-
DOCKER_UID=${DOCKER_UID} \
12-
DOCKER_GID=${DOCKER_GID} \
13-
docker compose run -it \
14-
tycho \
5+
source "$(dirname "$(readlink -f "$0")")/_commons.inc.sh"
6+
7+
declare service=${SERVICE:-tycho}
8+
9+
compose_run \
10+
"${service}" \
1511
uv run python manage.py "$@"

bin/pytest

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,10 @@
22

33
set -eo pipefail
44

5-
declare TEST=true
6-
declare service=${SERVICE:-tycho}
7-
declare cmd="uv run pytest"
8-
declare volume="./src/${service}:/app"
9-
declare DOCKER_USER
10-
DOCKER_UID="$(id -u)"
11-
DOCKER_GID="$(id -g)"
12-
DOCKER_USER="${DOCKER_UID}:${DOCKER_GID}"
5+
source "$(dirname "$(readlink -f "$0")")/_commons.inc.sh"
136

7+
declare service=${SERVICE:-tycho}
148

15-
DOCKER_USER=${DOCKER_USER} \
16-
DOCKER_UID=${DOCKER_UID} \
17-
DOCKER_GID=${DOCKER_GID} \
18-
docker compose run --rm \
19-
-e TEST="${TEST}" \
20-
-v "${volume}" \
21-
"${service}" \
22-
${cmd} "$@"
9+
compose_run \
10+
"${service}" \
11+
uv run pytest "$@"

bin/uv

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,10 @@
22

33
set -eo pipefail
44

5+
source "$(dirname "$(readlink -f "$0")")/_commons.inc.sh"
6+
57
declare service="${SERVICE:-tycho}"
6-
declare volume="./src/${service}:/app"
7-
declare DOCKER_USER
8-
DOCKER_UID="$(id -u)"
9-
DOCKER_GID="$(id -g)"
10-
DOCKER_USER="${DOCKER_UID}:${DOCKER_GID}"
118

12-
DOCKER_USER=${DOCKER_USER} \
13-
DOCKER_UID=${DOCKER_UID} \
14-
DOCKER_GID=${DOCKER_GID} \
15-
docker compose run --rm --no-deps \
16-
-u "uv:uv" \
17-
-v "${volume}" \
18-
"${service}" \
19-
uv "$@"
9+
compose_run --no-deps \
10+
"${service}" \
11+
uv "$@"

0 commit comments

Comments
 (0)