forked from veelkoov/fuzzrake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolbox
More file actions
executable file
·285 lines (219 loc) · 9.59 KB
/
toolbox
File metadata and controls
executable file
·285 lines (219 loc) · 9.59 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env bash
set -euo pipefail
PROJECT_NAME="${FUZZRAKE_DEV_PROJECT_NAME:-fuzzrake}"
SYMFONY_DIR_REL_PATH='symfony'
KOTLIN_DIR_REL_PATH='kotlin'
SUBMISSIONS_DIR_REL_PATH='symfony/var/submissions' # grep-code-submissions-dir-path
SNAPSHOTS_REL_PATH='symfony/var/snapshots' # grep-code-snapshots-dir-path
DATABASE_REL_PATH='symfony/var/db.sqlite' # grep-code-database-path
TMP_DATABASE_REL_PATH="$DATABASE_REL_PATH.tmp"
DB_DUMP_DIR_REL_PATH='db_dump'
DB_DUMP_PRV_COPY_PATH="$DB_DUMP_DIR_REL_PATH/artisans_private_data-$(date -u '+%Y-%m-%d_%H-%M-%S').sql"
PROD_ENV_SSH_PATH='getfursu.it:/var/www/prod'
BETA_ENV_SSH_PATH='getfursu.it:/var/www/beta'
# Volatile information, easily reproducible
DB_IGNORED_TABLES=(artisans_commissions_statuses artisans_volatile_data artisans_urls_states submissions)
function run_command() {
echo "Executing: $*"
"$@"
}
function run_docker_compose() {
run_command docker compose --project-directory docker --project-name "$PROJECT_NAME" "$@"
}
function run_docker_compose_exec() {
run_docker_compose exec --user "$(echo -n "$(id -u):www-data")" -ti php "$@"
}
function run_composer() {
run_docker_compose_exec composer "$@"
}
function run_console() {
run_docker_compose_exec ./bin/console "$@"
}
function run_yarn() {
# Known issue: paths in additional arguments will contain symfony/ prefix
# Not using --cwd yarn switch because eslint ignored that
# Known issue: requires local installation of Yarn
pushd "$SYMFONY_DIR_REL_PATH"
run_command yarn "$@"
popd
}
function run_gradle() {
run_command "$KOTLIN_DIR_REL_PATH/gradlew" -p "$KOTLIN_DIR_REL_PATH" "$@"
}
function assure_line_in_file() {
local filepath="$1"
local pattern="$2"
local default="$3"
grep -q "$pattern" "$filepath" || {
echo "Appending '$default' to '$filepath'"
echo "$default" >> "$filepath"
}
}
function action_run_setup() { # TODO: Ansible this
assure_line_in_file "$SYMFONY_DIR_REL_PATH/.env.local" '^GOOGLE_RECAPTCHA_SITE_KEY=' 'GOOGLE_RECAPTCHA_SITE_KEY=__TODO_PROVIDE_THIS__'
assure_line_in_file "$SYMFONY_DIR_REL_PATH/.env.local" '^GOOGLE_RECAPTCHA_SECRET=' 'GOOGLE_RECAPTCHA_SECRET=__TODO_PROVIDE_THIS__'
assure_line_in_file "$SYMFONY_DIR_REL_PATH/.env.test.local" '^GOOGLE_RECAPTCHA_SITE_KEY=' 'GOOGLE_RECAPTCHA_SITE_KEY=__TODO_PROVIDE_THIS__'
assure_line_in_file "$SYMFONY_DIR_REL_PATH/.env.test.local" '^GOOGLE_RECAPTCHA_SECRET=' 'GOOGLE_RECAPTCHA_SECRET=__TODO_PROVIDE_THIS__'
}
function backup_private_data() {
run_command sqlite3 "$DATABASE_REL_PATH" ".output $DB_DUMP_PRV_COPY_PATH" '.dump artisans_private_data'
}
function action_release_prod() {
run_command git checkout main
run_command git merge --no-edit develop
run_command git push
run_command git checkout develop
run_command git merge main
run_command git push
run_command ansible/setup_envs.yaml --limit prod_env
}
function action_release_beta() {
PREVIOUS_BRANCH="$(git branch --show-current)"
run_command git branch -D beta
run_command git checkout -b beta
run_command git push --force origin beta
run_command ansible/setup_envs.yaml --limit beta_env --diff
git checkout "$PREVIOUS_BRANCH"
}
function action_get_snapshots() {
run_command rsync --recursive --progress --human-readable --compress --checksum \
"$PROD_ENV_SSH_PATH/$SNAPSHOTS_REL_PATH/" "$SNAPSHOTS_REL_PATH"
}
function action_get_submissions() {
run_command rsync --recursive --progress --human-readable --compress --checksum \
"$PROD_ENV_SSH_PATH/$SUBMISSIONS_DIR_REL_PATH/" "$SUBMISSIONS_DIR_REL_PATH/"
}
function action_dbcommit() {
pushd "$DB_DUMP_DIR_REL_PATH"
run_command git reset HEAD
run_command git commit -m 'Updated DB dump' -p
run_command git push
run_command git show -q
popd
}
function action_dbpull() {
run_command scp -p "$PROD_ENV_SSH_PATH/$DATABASE_REL_PATH" "$TMP_DATABASE_REL_PATH"
backup_private_data
run_command sqlite3 "$TMP_DATABASE_REL_PATH" 'DROP TABLE artisans_private_data;'
run_command sqlite3 "$TMP_DATABASE_REL_PATH" ".read $DB_DUMP_PRV_COPY_PATH"
run_command chmod a+w "$TMP_DATABASE_REL_PATH"
run_command mv "$TMP_DATABASE_REL_PATH" "$DATABASE_REL_PATH"
}
function action_dbpush() {
run_command cp "$DATABASE_REL_PATH" "$TMP_DATABASE_REL_PATH"
run_command sqlite3 "$TMP_DATABASE_REL_PATH" "UPDATE artisans_private_data SET original_contact_info = '', contact_address = '', notes = '';"
run_command scp -p "$TMP_DATABASE_REL_PATH" "$PROD_ENV_SSH_PATH/$DATABASE_REL_PATH"
run_command scp -p "$TMP_DATABASE_REL_PATH" "$BETA_ENV_SSH_PATH/$DATABASE_REL_PATH"
run_command rm "$TMP_DATABASE_REL_PATH"
}
function action_dbdump() {
# shellcheck disable=SC2207 # Yes, split by whitespace
TABLE_NAMES=($(sqlite3 "$DATABASE_REL_PATH" .tables))
for TABLE_NAME in "${DB_IGNORED_TABLES[@]}"; do # Sanity check
if ! printf '%s\0' "${DB_IGNORED_TABLES[@]}" | grep -Fxqz -- "$TABLE_NAME"; then
error "$TABLE_NAME does not exist in the DB $DATABASE_REL_PATH"
fi
done
backup_private_data
for TABLE_NAME in "${TABLE_NAMES[@]}"; do
if ! printf '%s\0' "${DB_IGNORED_TABLES[@]}" | grep -Fxqz -- "$TABLE_NAME"; then
run_command sqlite3 "$DATABASE_REL_PATH" ".output $DB_DUMP_DIR_REL_PATH/$TABLE_NAME.sql" ".dump $TABLE_NAME"
echo 'Dumping to JSON...'
if grep 'CREATE TABLE' "$DB_DUMP_DIR_REL_PATH/$TABLE_NAME.sql" | grep -q 'artisan_id'; then
order_by='artisan_id'
else
order_by='id'
fi
sqlite3 -json "$DATABASE_REL_PATH" "SELECT * FROM $TABLE_NAME ORDER BY $order_by" | jq > "$DB_DUMP_DIR_REL_PATH/$TABLE_NAME.json"
fi
done
}
function error() {
local message="$1"
echo "ERROR: $message" >&2
echo ''
usage
exit 1
}
function usage() {
cat << EOF
Usage:
$0 ACTION [arguments ...]
Available actions:
setup Symfony: setup and/or fix required filesystem items and/or settings
branch Symfony:
composer install
yarn install
yep
docker-up Symfony: "ups" the Docker Compose project
docker-down Symfony: "downs" the Docker Compose project
composer Symfony: run Composer
yarn Symfony: run Yarn (needs to be installed locally)
yep Symfony: execute 'yarn encore production'
console Symfony: run Symfony console command
cc Symfony: clear cache
pu Symfony: run PHPUnit tests
pus Symfony: run PHPUnit tests, "small" group
pum Symfony: run PHPUnit tests, "medium" group
pul Symfony: run PHPUnit tests, "large" group
pcf Symfony: run PHP CS Fixer
tcf Symfony: run Twig CS Fixer
ps Symfony: run PHPStan
rector Symfony: run Rector
el Symfony: run ESLint
prettier Symfony: run Prettier
gradle Kotlin: run Gradle command
species Kotlin: update creators' species
filters Kotlin: update filters data
miniatures Kotlin: set miniatures data for changed photos
EOF
}
function action() {
[[ $# -ge 1 ]] || error 'Not enough arguments'
local action="$1"
shift
case $action in
'setup') action_run_setup ;;
'branch')
run_composer install
run_yarn install
run_yarn encore production
;;
'docker-up') run_docker_compose up --detach --build ;;
'docker-down') run_docker_compose down ;;
'yep') pushd symfony ; yarn encore production ; popd ;;
'composer') run_composer "$@" ;;
'yarn') run_yarn "$@" ;;
'console') run_console "$@" ;;
'cc') run_console cache:clear ;;
'cc-beta') run_command ssh getfursu.it docker exec fuzzrake-beta bin/console cache:clear ;;
'cc-prod') run_command ssh getfursu.it docker exec fuzzrake-prod bin/console cache:clear ;;
'pu') run_docker_compose_exec ./bin/phpunit --testdox "$@" ;;
'pus') action pu --group small "$@" ;;
'pum') action pu --group medium "$@" ;;
'pul') action pu --group large "$@" ;;
'pcf') run_docker_compose_exec ./vendor/bin/php-cs-fixer fix "$@" ;;
'tcf') run_docker_compose_exec ./vendor/bin/twig-cs-fixer lint --fix "$@" ;;
'ps') run_docker_compose_exec ./vendor/bin/phpstan analyse -c phpstan.neon "$@" ;;
'rector') run_docker_compose_exec ./vendor/bin/rector process "$@" ;;
'el') run_yarn eslint assets/scripts/ --ext js,ts "$@" ;;
'prettier') run_yarn prettier . --write "$@" ;;
'tidy') run_console app:data:tidy "$@" ;;
'tidyc') action tidy --commit "$@" ;;
'dbcommit') action_dbcommit ;;
'dbpull') action_dbpull ;;
'dbpush') action_dbpush ;;
'dbdump') action_dbdump ;;
'release-beta') action_release_beta ;;
'release-prod') action_release_prod ;;
'get-snapshots') action_get_snapshots ;;
'get-submissions') action_get_submissions ;;
'gradle') run_gradle "$@" ;;
'species') run_gradle run --args sync-species "$@" ;;
'filters') run_gradle run --args update-filters "$@" ;;
'miniatures') run_gradle run --args update-miniatures "$@" ;;
*) error "Unknown action: '$action'" ;;
esac
}
pushd "$(dirname "$(realpath "$0")")"
action "$@"