-
Notifications
You must be signed in to change notification settings - Fork 0
/
justfile
executable file
·459 lines (359 loc) · 14.2 KB
/
justfile
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#!/usr/bin/env -S just --working-directory . --justfile
# Load project-specific properties from the `.env` file
set dotenv-load := true
# Whether to run ignored tests (set to "true" to run ignored tests)
ignored := "false"
# The output directory for documentation artifacts
output_dir := "output"
# Runs all checks and tests. Since this is the first recipe it is run by default.
run-pre-commit-hook: check test
# Runs all check targets
check: check-spelling check-formatting lint check-unused-deps check-dependencies check-licenses check-links
# Faster checks need to be executed first for better UX. For example
# codespell is very fast. cargo fmt does not need to download crates etc.
# Installs all tools required for development
dev-install: install-pacman-dev-packages install-rust-dev-tools
# Installs development packages using pacman
install-pacman-dev-packages:
# All packages are set in the `.env` file
pacman -Syu --needed --noconfirm $PACMAN_DEV_PACKAGES
# Installs all Rust tools required for development
install-rust-dev-tools:
rustup default stable
rustup component add clippy
rustup toolchain install nightly
rustup component add --toolchain nightly rustfmt
# Checks commit messages for correctness
check-commits:
#!/usr/bin/env bash
set -euo pipefail
just ensure-command codespell cog committed rg
readonly default_branch="${CI_DEFAULT_BRANCH:-main}"
if ! git rev-parse --verify "origin/$default_branch" > /dev/null 2>&1; then
printf "The default branch '%s' does not exist!\n" "$default_branch" >&2
exit 1
fi
tmpdir="$(mktemp --dry-run --directory)"
readonly check_tmpdir="$tmpdir"
mkdir -p "$check_tmpdir"
# remove temporary dir on exit
cleanup() (
if [[ -n "${check_tmpdir:-}" ]]; then
rm -rf "${check_tmpdir}"
fi
)
trap cleanup EXIT
for commit in $(git rev-list "origin/${default_branch}.."); do
printf "Checking commit %s\n" "$commit"
commit_message="$(git show -s --format=%B "$commit")"
codespell_config="$(mktemp --tmpdir="$check_tmpdir")"
# either use the commit's .codespellrc or create one
if git show "$commit:.codespellrc" > /dev/null 2>&1; then
git show "$commit:.codespellrc" > "$codespell_config"
else
printf "[codespell]\nskip = .cargo,.git,target,.env,Cargo.lock\nignore-words-list = crate\n" > "$codespell_config"
fi
if ! rg -q "Signed-off-by: " <<< "$commit_message"; then
printf "Commit %s ❌️\n" "$commit" >&2
printf "The commit message lacks a \"Signed-off-by\" line.\n" >&2
printf "%s\n" \
" Please use:" \
" git rebase --signoff main && git push --force-with-lease" \
" See https://developercertificate.org/ for more details." >&2
exit 1
elif ! codespell --config "$codespell_config" - <<< "$commit_message"; then
printf "Commit %s ❌️\n" "$commit" >&2
printf "The spelling of the commit message needs improvement.\n" >&2
exit 1
elif ! cog verify "$commit_message"; then
printf "Commit %s ❌️\n" "$commit" >&2
printf "%s\n" \
"The commit message is not a conventional commit message:" \
"$commit_message" \
"See https://www.conventionalcommits.org/en/v1.0.0/ for more details." >&2
exit 1
elif ! committed "$commit"; then
printf "Commit %s ❌️\n" "$commit" >&2
printf "%s\n" \
"The commit message does not meet the required standards:" \
"$commit_message" \
exit 1
else
printf "Commit %s ✅️\n\n" "$commit"
fi
done
# Runs checks before pushing commits to remote repository.
run-pre-push-hook: check-commits
# Checks common spelling mistakes
check-spelling:
codespell
# Gets names of all workspace members
get-workspace-members:
cargo metadata --format-version=1 |jq -r '.workspace_members[] | capture("/(?<name>[a-z-]+)#.*").name'
# Checks if a string matches a workspace member exactly
is-workspace-member package:
#!/usr/bin/env bash
set -euo pipefail
mapfile -t workspace_members < <(just get-workspace-members 2>/dev/null)
for name in "${workspace_members[@]}"; do
if [[ "$name" == {{ package }} ]]; then
exit 0
fi
done
exit 1
# Gets metadata version of a workspace member
get-workspace-member-version package:
#!/usr/bin/env bash
set -euo pipefail
readonly version="$(cargo metadata --format-version=1 |jq -r --arg pkg {{ package }} '.workspace_members[] | capture("/(?<name>[a-z-]+)#(?<version>[0-9.]+)") | select(.name == $pkg).version')"
if [[ -z "$version" ]]; then
printf "No version found for package %s\n" {{ package }} >&2
exit 1
fi
printf "$version\n"
# Checks for unused dependencies
check-unused-deps:
#!/usr/bin/env bash
set -euxo pipefail
just ensure-command cargo-machete
for name in $(just get-workspace-members); do
cargo machete "$name"
done
# Checks source code formatting
check-formatting:
just ensure-command taplo
just --unstable --fmt --check
# We're using nightly to properly group imports, see rustfmt.toml
cargo +nightly fmt -- --check
taplo format --check
# Updates the local cargo index and displays which crates would be updated
dry-update:
cargo update --dry-run --verbose
# Lints the source code
lint:
just ensure-command shellcheck tangler
tangler bash < alpm-buildinfo/README.md | shellcheck --shell bash -
tangler bash < alpm-mtree/README.md | shellcheck --shell bash -
just lint-recipe 'test-readme alpm-buildinfo'
just lint-recipe build-book
just lint-recipe check-commits
just lint-recipe check-unused-deps
just lint-recipe ci-publish
just lint-recipe 'generate shell_completions alpm-buildinfo'
just lint-recipe 'is-workspace-member alpm-buildinfo'
just lint-recipe 'release alpm-buildinfo'
just lint-recipe flaky
just lint-recipe test
just lint-recipe 'ensure-command test'
cargo clippy --tests --all -- -D warnings
just docs
# Check justfile recipe for shell issues
lint-recipe recipe:
just -vv -n {{ recipe }} 2>&1 | rg -v '===> Running recipe' | shellcheck -
# Build local documentation
docs:
RUSTDOCFLAGS='-D warnings' cargo doc --document-private-items --no-deps
# Checks for issues with dependencies
check-dependencies: dry-update
cargo deny --all-features check
# Checks licensing status
check-licenses:
just ensure-command reuse
reuse lint
# Runs all unit tests. By default ignored tests are not run. Run with `ignored=true` to run only ignored tests
test:
#!/usr/bin/env bash
set -euxo pipefail
readonly ignored="{{ ignored }}"
if [[ "${ignored}" == "true" ]]; then
cargo test --all -- --ignored
cargo test --doc
else
cargo test --all
just docs
fi
# Runs per project end-to-end tests found in a project README.md
test-readme project:
#!/usr/bin/env bash
set -euxo pipefail
CARGO_HOME="${CARGO_HOME:-$HOME/.cargo}"
install_executables() {
printf "Installing executables of %s...\n" "{{ project }}"
cargo install --locked --path {{ project }}
}
install_executables
PATH="$CARGO_HOME/bin:$PATH"
printf "PATH=%s\n" "$PATH"
cd {{ project }} && PATH="$PATH" tangler bash < README.md | bash -euxo pipefail -
# Adds needed git configuration for the local repository
configure-git:
# Enforce gpg signed keys for this repository
git config commit.gpgsign true
just add-hooks
# Adds pre-commit and pre-push git hooks
add-hooks:
#!/usr/bin/env bash
set -euo pipefail
echo just run-pre-commit-hook > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
echo just run-pre-push-hook > .git/hooks/pre-push
chmod +x .git/hooks/pre-push
cat > .git/hooks/prepare-commit-msg <<'EOL'
#!/bin/sh
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE"
if test -z "$COMMIT_SOURCE"; then
/usr/bin/perl -i.bak -pe 'print "\n" if !$first_line++' "$COMMIT_MSG_FILE"
fi
EOL
chmod +x .git/hooks/prepare-commit-msg
# Check for stale links in documentation
check-links:
just ensure-command lychee
lychee .
# Fixes common issues. Files need to be git add'ed
fix:
#!/usr/bin/env bash
set -euo pipefail
if ! git diff-files --quiet ; then
echo "Working tree has changes. Please stage them: git add ."
exit 1
fi
codespell --write-changes
just --unstable --fmt
cargo clippy --fix --allow-staged
# fmt must be last as clippy's changes may break formatting
cargo +nightly fmt
# Render `manpages`, `shell_completions` or `specifications` (`kind`) of a given package (`pkg`).
generate kind pkg:
#!/usr/bin/bash
set -Eeuo pipefail
readonly output_dir="{{ output_dir }}"
mkdir --parents "$output_dir"
kind="{{ kind }}"
script="$(mktemp --suffix=.ers)"
readonly script="$script"
# remove temporary script file on exit
cleanup() (
if [[ -n "${script:-}" ]]; then
rm -f "$script"
fi
)
trap cleanup EXIT
case "$kind" in
manpages|shell_completions)
sed "s/PKG/{{ pkg }}/;s#PATH#$PWD/{{ pkg }}#g;s/KIND/{{ kind }}/g" > "$script" < .rust-script/allgen.ers
;;
specifications)
sed "s/PKG/{{ pkg }}/;s#PATH#$PWD/{{ pkg }}#g;s/KIND/{{ kind }}/g" > "$script" < .rust-script/mandown.ers
# override kind, as we are in fact generating manpages
kind="manpages"
;;
*)
printf 'Only "manpages", "shell_completions" or "specifications" are supported targets.\n'
exit 1
esac
chmod +x "$script"
"$script" "$output_dir/$kind"
# Continuously run integration tests for a given number of rounds
flaky test='just test-readme alpm-buildinfo' rounds='999999999999':
#!/usr/bin/bash
set -euo pipefail
seq 1 {{ rounds }} | while read -r counter; do
printf "Running flaky tests (%d/{{ rounds }})...\n" "$counter"
sleep 1
{{ test }}
echo
done
# Prepares the release of a crate by updating dependencies, incrementing the crate version and creating a changelog entry
prepare-release package:
release-plz update -u -p {{ package }}
# Creates a release of a crate in the workspace by creating a tag and pushing it
release package:
#!/usr/bin/env bash
set -euo pipefail
package_version="$(just get-workspace-member-version {{ package }})"
readonly package_version="$package_version"
if [[ -z "$package_version" ]]; then
exit 1
fi
readonly current_version="{{ package }}/$package_version"
if [[ -n "$(git tag -l "$current_version")" ]]; then
printf "The tag %s exists already!\n" "$current_version" >&2
exit 1
fi
printf "Creating tag %s...\n" "$current_version"
git tag -s "$current_version" -m "$current_version"
printf "Pushing tag %s...\n" "$current_version"
git push origin refs/tags/"$current_version"
# Publishes a crate in the workspace from GitLab CI in a pipeline for tags
ci-publish:
#!/usr/bin/env bash
set -euo pipefail
# an auth token with publishing capabilities is expected to be set in GitLab project settings
readonly token="${CARGO_REGISTRY_TOKEN:-}"
# rely on predefined variable to retrieve git tag: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
readonly tag="${CI_COMMIT_TAG:-}"
readonly crate="${tag//\/*/}"
readonly version="${tag#*/}"
if [[ -z "$tag" ]]; then
printf "There is no tag!\n" >&2
exit 1
fi
if [[ -z "$token" ]]; then
printf "There is no token for crates.io!\n" >&2
exit 1
fi
if ! just is-workspace-member "$crate" &>/dev/null; then
printf "The crate %s is not a workspace member of the project!\n" "$crate" >&2
exit 1
fi
current_member_version="$(just get-workspace-member-version "$crate" 2>/dev/null)"
readonly current_member_version="$current_member_version"
if [[ "$version" != "$current_member_version" ]]; then
printf "Current version in metadata of crate %s (%s) does not match the version from the tag (%s)!\n" "$crate" "$current_member_version" "$version"
exit 1
fi
printf "Found tag %s (crate %s in version %s).\n" "$tag" "$crate" "$version"
cargo publish -p "$crate"
# Ensures that one or more required commands are installed
ensure-command +command:
#!/usr/bin/env bash
set -euo pipefail
read -r -a commands <<< "{{ command }}"
for cmd in "${commands[@]}"; do
if ! command -v "$cmd" > /dev/null 2>&1 ; then
printf "Couldn't find required executable '%s'\n" "$cmd" >&2
exit 1
fi
done
# Builds the documentation book using mdbook and stages all necessary rustdocs alongside
build-book:
#!/usr/bin/env bash
set -euo pipefail
just ensure-command cargo jq mdbook mdbook-mermaid
target_dir="$(cargo metadata --format-version 1 | jq -r .target_directory)"
readonly target_dir="$target_dir"
readonly output_dir="{{ output_dir }}"
readonly rustdoc_dir="$output_dir/docs/rustdoc/"
mapfile -t workspace_members < <(just get-workspace-members 2>/dev/null)
just docs
mdbook-mermaid install resources/docs/
mdbook build resources/docs/
# move rust docs to their own namespaced dir
mkdir -p "$rustdoc_dir"
for name in "${workspace_members[@]}"; do
cp -r "$target_dir/doc/${name//-/_}" "$rustdoc_dir"
done
cp -r "$target_dir/doc/"{search.desc,src,static.files,trait.impl,type.impl} "$rustdoc_dir"
cp -r "$target_dir/doc/"*.{js,html} "$rustdoc_dir"
# Serves the documentation book using miniserve
serve-book: build-book
just ensure-command miniserve
miniserve --index=index.html {{ output_dir }}/docs
# Watches the documentation book contents and rebuilds on change using mdbook (useful for development)
watch-book:
just ensure-command watchexec
watchexec --exts md,toml,js --delay-run 5s -- just build-book