-
Notifications
You must be signed in to change notification settings - Fork 1
/
run
executable file
·179 lines (140 loc) · 4.3 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
#!/usr/bin/env bash
set -o errexit
set -o pipefail
DC="${DC:-exec}"
# 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 _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 rails {
# Run any Rails commands
cmd rails "${@}"
}
function test {
# Run your Rails tests, use `test -b` to first rebuild your JS and CSS
local run_build="${1:-}"
local test_command="rails test"
if [ "${run_build}" = "-b" ]; then
test_command="yarn build && yarn build:css && ${test_command}"
fi
_dc -e "RAILS_ENV=test" js bash -c "${test_command}"
}
function shell {
# Start a shell session in the web container
cmd bash "${@}"
}
function psql {
# Connect to PostgreSQL with psql
# shellcheck disable=SC1091
. .env
_dc postgres psql -U "${POSTGRES_USER}" "${@}"
}
function redis-cli {
# Connect to Redis with redis-cli
_dc redis redis-cli "${@}"
}
function hadolint {
# Lint Dockerfile with hadolint
docker container run --rm -i \
hadolint/hadolint hadolint --ignore DL3008 -t style "${@}" - < Dockerfile
}
function bundle:install {
# Install Ruby dependencies and write out a lock file
_build_run_down web bundle install
}
function bundle:outdated {
# List any installed gems that are outdated
cmd bundle outdated
}
function bundle:update {
# Update any installed gems that are outdated
cmd bundle update
bundle:install
}
function yarn:install {
# Install Yarn dependencies and write out a lock file
_build_run_down js yarn install
}
function yarn:outdated {
# Install yarn dependencies and write lock file
_dc js yarn outdated
}
function yarn:build {
# Build JS assets, this is only meant to be referenced from your package.json
local args=()
if [ "${NODE_ENV:-}" == "production" ]; then
args=(--minify)
elif [ "${RAILS_ENV:-}" == "development" ]; then
args=(--sourcemap --watch)
fi
esbuild app/javascript/*.* --outdir=app/assets/builds --bundle "${args[@]}"
}
function yarn:build:css {
# Build CSS assets, this is only meant to be referenced from your package.json
local args=()
if [ "${NODE_ENV:-}" == "production" ]; then
args=(--minify)
elif [ "${RAILS_ENV:-}" == "development" ]; then
args=(--watch)
fi
tailwindcss --postcss \
-i ./app/assets/stylesheets/application.tailwind.css \
-o ./app/assets/builds/application.css "${args[@]}"
}
function clean {
# Remove cache and other machine generates files
rm -rf node_modules/ app/assets/builds/* public/assets tmp/* .byebug_history
}
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.1.2/wait-until \
-o /usr/local/bin/wait-until && sudo chmod +x /usr/local/bin/wait-until
}
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/docker-entrypoint-web
hadolint "${@}"
cp --no-clobber .env.example .env
docker compose build
docker compose up -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 logs
rails db:setup
test -b
}
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}"