generated from aifrak/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·227 lines (186 loc) · 5.44 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
#!/usr/bin/env bash
set -euo pipefail
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
. "${current_dir}/scripts/run/css.sh"
. "${current_dir}/scripts/run/docker_file.sh"
. "${current_dir}/scripts/run/elixir.sh"
. "${current_dir}/scripts/run/git.sh"
. "${current_dir}/scripts/run/html.sh"
. "${current_dir}/scripts/run/javascript.sh"
. "${current_dir}/scripts/run/markdown.sh"
. "${current_dir}/scripts/run/prettier.sh"
. "${current_dir}/scripts/run/release.sh"
. "${current_dir}/scripts/run/shell.sh"
. "${current_dir}/scripts/run/spellcheck.sh"
. "${current_dir}/scripts/lib/utils.sh"
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
function help {
cat <<EOF
$0 [command] [OPTIONS] [sub-command]
Example: $0 --dc-dev dc:exec ./run test
Options:
-h, --help Print this message
--dc-ci Use "ci" file when docker compose
--dc-dev Use "dev" file when docker compose
Main commands:
help Print this message
format Format code of the whole project
install:deps Install all dependencies
lint Lint the whole project
lint:staged Lint staged files
pre-commit Lint staged files and test the whole project
test Test the whole project
docker compose commands:
dc:build Build all docker services
dc:console Build, start, up all services and console of "app" docker service
dc:exec Execute a command in a "app" docker service
dc:run Run a command inside the "app" docker service and remove stopped containers
dc:up Starts all docker services
EOF
help:release
help:elixir
help:css
help:html
help:js
help:markdown
help:prettier
help:shell
help:dockerfile
help:spellcheck
help:git
}
# —————————————————————————————————————————————— #
# Helper functions #
# —————————————————————————————————————————————— #
# Check if the command is from the docker container or the host.
function is_inside_docker {
[[ "${INSIDE_DOCKER:-false}" == true ]]
}
function is_ci {
[[ "${CI:-false}" == true ]]
}
# 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.
# Idea from: https://github.com/nickjj/docker-flask-example/blob/main/run
TTY=""
if [[ ! -t 1 ]] || is_ci; then
TTY="-T"
fi
# —————————————————————————————————————————————— #
# Application #
# —————————————————————————————————————————————— #
function test {
if is_inside_docker; then
mix check \
--only ex_unit \
--only release_test
else
dc:exec ./run test
fi
}
function lint {
if is_inside_docker; then
mix check \
--except ex_unit \
--except lint_staged \
--except release_test
else
dc:exec ./run lint
fi
}
function format {
if is_inside_docker; then
mix format.all
else
dc:exec ./run format
fi
}
function pre-commit {
if is_inside_docker; then
mix check \
--only ex_unit \
--only lint_staged
else
dc:exec ./run pre-commit
fi
}
function lint:staged {
npx lint-staged
}
function install:deps {
if is_inside_docker; then
mix setup.deps.dev
success "✔ Dependencies installed"
else
dc:exec ./run install:deps
fi
}
# —————————————————————————————————————————————— #
# docker compose #
# —————————————————————————————————————————————— #
dc_options=()
ci_dc_options=(
-f docker-compose.yml
-f docker-compose.ci.yml
)
dev_dc_options=(
-f docker-compose.yml
-f docker-compose.dev.yml
)
function dc {
docker compose "${dc_options[@]}" "${@}"
}
function dc:build {
docker buildx bake "${dc_options[@]}"
}
function dc:exec {
dc exec ${TTY} app "${@}"
}
function dc:run {
dc run ${TTY} app "${@}"
dc rm -f -v
}
function dc:up {
dc up -d --remove-orphans
}
function dc:console {
dc:build
dc:up
dc:exec bash
}
# —————————————————————————————————————————————— #
# run #
# —————————————————————————————————————————————— #
opt_dc_ci=
opt_dc_dev=
# Options
for opt in "${@}"; do
case ${opt} in
--dc-ci)
if [[ -n ${opt_dc_dev} ]]; then
alert_die "--dc-ci is not compatible with --dc-dev"
fi
dc_options=("${ci_dc_options[@]}")
opt_dc_ci=1
shift
;;
--dc-dev)
if [[ -n ${opt_dc_ci} ]]; then
alert_die "--dc-dev is not compatible with --dc-ci"
fi
dc_options=("${dev_dc_options[@]}")
opt_dc_dev=1
shift
;;
-h | --help)
if [[ "${#}" -eq 1 ]]; then
help
close
fi
;;
esac
done
# Idea from: https://github.com/adriancooney/Taskfile
TIMEFORMAT=$'\nTask completed in %3lR'
time "${@:-help}"