feat: add nix for formatting
All checks were successful
/ check (treefmt) (push) Successful in 3s
/ check-renovaterc (push) Successful in 2s
/ report-size (push) Successful in 2s

Also adds CI tests!
This commit is contained in:
Jalil David Salamé Messina 2025-03-10 22:58:18 +01:00
parent 58249e9ad0
commit 73142cfab8
Signed by: jalil
GPG key ID: F016B9E770737A0B
9 changed files with 314 additions and 121 deletions

View file

@ -11,34 +11,34 @@ util_path="${GITHUB_ACTION_PATH:-.}/utils.sh"
#
# JSON_FILE can be piped from stdin
json_to_md_rows() {
jq --raw-output \
".$1[]"' | "| `\(.name)` | \(.size) | \(.narSize) |"' "$2" |
numfmt --suffix=B --to=iec-i --field=4,6
jq --raw-output \
".$1[]"' | "| `\(.name)` | \(.size) | \(.narSize) |"' "$2" |
numfmt --suffix=B --to=iec-i --field=4,6
}
# USAGE: json_to_md_rows <FIELD> [JSON_FILE]
#
# JSON_FILE can be piped from stdin
json_to_md_rows_and_change() {
jq --raw-output \
".$1[]"' | "| `\(.name)` | \(.size) | \(.sizeChange) | \(.narSize) | \(.narSizeChange) |"' |
numfmt --suffix=B --to=iec-i --field=4,6,8,10
jq --raw-output \
".$1[]"' | "| `\(.name)` | \(.size) | \(.sizeChange) | \(.narSize) | \(.narSizeChange) |"' |
numfmt --suffix=B --to=iec-i --field=4,6,8,10
}
# USAGE: has_elements <FIELD> <JSON_FILE>
has_elements() {
if [ "${2+set}" = 'set' ]; then
[ "$(jq ".$1 != []" "$2")" = 'true' ]
else
[ "$(jq ".$1 != []")" = 'true' ]
fi
if [ "${2+set}" = 'set' ]; then
[ "$(jq ".$1 != []" "$2")" = 'true' ]
else
[ "$(jq ".$1 != []")" = 'true' ]
fi
}
# USAGE: markdown_from_report <REPORT> [BASE_REPORT]
#
# If BASE_REPORT is provided, a comparison will be made
markdown_from_report() {
cat <<-"EOF"
cat <<-"EOF"
<!-- AUTOGENERATED by nix-flake-outputs-size action -->
# Flake output sizes
@ -49,12 +49,12 @@ markdown_from_report() {
- `Size`: the closure size (size on disk/NAR size + all transitive dependencies).
- `NAR Size`: the size of the build output (package without the dependencies).
EOF
if [ "${2+set}" = "set" ]; then
cat <<-"EOF"
if [ "${2+set}" = "set" ]; then
cat <<-"EOF"
- `[NAR] Size Change`: the amount changed compared to the main branch.
EOF
fi
cat <<-"EOF"
fi
cat <<-"EOF"
**Tips on reading this data:**
@ -66,62 +66,62 @@ markdown_from_report() {
- Don't worry too much about size, some dependencies are deduplicated, e.g. `glibc` adds ~40MiB to the `Size`, but is generally shared by ~every binary on the system, so, chances are, you are already including it from somewhere else and statically linking with e.g. `musl` is not gonna improve things.
EOF
if [ "${2+set}" = "set" ]; then
compare=$(jq --slurp --from-file "${GITHUB_ACTION_PATH:-.}/compare.jq" "$1" "$2")
if echo "$compare" | has_elements 'nixosConfigurations'; then
cat <<-"EOF"
if [ "${2+set}" = "set" ]; then
compare=$(jq --slurp --from-file "${GITHUB_ACTION_PATH:-.}/compare.jq" "$1" "$2")
if echo "$compare" | has_elements 'nixosConfigurations'; then
cat <<-"EOF"
# NixOS Configurations
| Name | Size | Size Change | NAR Size | NAR Size Change |
|------|-----:|------------:|---------:|----------------:|
EOF
echo "$compare" | json_to_md_rows_and_change "nixosConfigurations"
echo
fi
if echo "$compare" | has_elements 'packages'; then
cat <<-"EOF"
echo "$compare" | json_to_md_rows_and_change "nixosConfigurations"
echo
fi
if echo "$compare" | has_elements 'packages'; then
cat <<-"EOF"
# Packages
| Name | Size | Size Change | NAR Size | NAR Size Change |
|------|-----:|------------:|---------:|----------------:|
EOF
echo "$compare" | json_to_md_rows_and_change "packages"
echo
fi
else
if has_elements 'nixosConfigurations' "$1"; then
cat <<-"EOF"
echo "$compare" | json_to_md_rows_and_change "packages"
echo
fi
else
if has_elements 'nixosConfigurations' "$1"; then
cat <<-"EOF"
# NixOS Configurations
| Name | Size | NAR Size |
|------|-----:|---------:|
EOF
json_to_md_rows "nixosConfigurations" "$1"
echo
fi
if has_elements 'packages' "$1"; then
cat <<-"EOF"
json_to_md_rows "nixosConfigurations" "$1"
echo
fi
if has_elements 'packages' "$1"; then
cat <<-"EOF"
# Packages
| Name | Size | NAR Size |
|------|-----:|---------:|
EOF
json_to_md_rows "packages" "$1"
echo
fi
fi
json_to_md_rows "packages" "$1"
echo
fi
fi
}
# Test outside CI
if [ "${CI:-false}" != 'true' ]; then
markdown_from_report "$@"
exit 0
markdown_from_report "$@"
exit 0
fi
# Protect against running before a PR is made or if it is triggered on the main branch
if [ -z "$PR_ID" ]; then
warn "No PR created for this commit"
exit 0
warn "No PR created for this commit"
exit 0
fi
log 'Generating comment body'
@ -137,19 +137,19 @@ log "$data"
endgroup
if [ -z "$COMMENT_ID" ]; then
log 'Posting new comment'
curl -o - -X 'POST' \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$PR_ID/comments" \
-H 'accept: application/json' \
-H "Authorization: token $GITHUB_TOKEN" \
-H 'Content-Type: application/json' \
-d "$data"
log 'Posting new comment'
curl -o - -X 'POST' \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$PR_ID/comments" \
-H 'accept: application/json' \
-H "Authorization: token $GITHUB_TOKEN" \
-H 'Content-Type: application/json' \
-d "$data"
else
log "Editing comment $COMMENT_ID"
curl -o - -X 'PATCH' \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$PR_ID/comments/$COMMENT_ID" \
-H 'accept: application/json' \
-H "Authorization: token $GITHUB_TOKEN" \
-H 'Content-Type: application/json' \
-d "$data"
log "Editing comment $COMMENT_ID"
curl -o - -X 'PATCH' \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$PR_ID/comments/$COMMENT_ID" \
-H 'accept: application/json' \
-H "Authorization: token $GITHUB_TOKEN" \
-H 'Content-Type: application/json' \
-d "$data"
fi