264 lines
8.3 KiB
Bash
264 lines
8.3 KiB
Bash
#!/bin/bash
|
|
# Refresh font source checksums and regenerate package metadata.
|
|
set -euo pipefail
|
|
|
|
REPO_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
|
|
readonly REPO_DIR
|
|
source "${REPO_DIR}/package-data.sh"
|
|
|
|
declare -gA API_HASHES=()
|
|
API_COUNT=0
|
|
LOCAL_COUNT=0
|
|
DOWNLOAD_COUNT=0
|
|
FONT_HASHES_FILE=''
|
|
|
|
# Download an archive atomically so an interrupted transfer cannot replace cache.
|
|
# Arguments: Temporary directory, URL, and destination path.
|
|
# Returns: Non-zero if curl fails.
|
|
download_archive() {
|
|
local temporary_dir="$1"
|
|
local url="$2"
|
|
local destination="$3"
|
|
local temporary_file
|
|
|
|
temporary_file="${temporary_dir}/$(basename "$destination").download"
|
|
mkdir -p "$(dirname "$destination")"
|
|
curl --fail --silent --show-error --location --retry 3 \
|
|
"$url" -o "$temporary_file"
|
|
mv -- "$temporary_file" "$destination"
|
|
}
|
|
|
|
# Read Release asset SHA-256 values when GitHub provides them.
|
|
# Globals: API_HASHES.
|
|
# Arguments: Release tag, temporary directory.
|
|
# Outputs: Warnings about API fallback to STDERR.
|
|
fetch_api_hashes() {
|
|
local release_tag="$1"
|
|
local temporary_dir="$2"
|
|
local api_url token_config escaped_token
|
|
local -a curl_arguments=(--header 'Accept: application/vnd.github+json')
|
|
|
|
api_url="https://api.github.com/repos/jonz94/Sarasa-Gothic-Nerd-Fonts/releases/tags/${release_tag}"
|
|
if [[ -n "${GITHUB_TOKEN:-}" ]]; then
|
|
[[ "$GITHUB_TOKEN" != *$'\n'* ]] || {
|
|
printf 'GITHUB_TOKEN must not contain a newline.\n' >&2
|
|
return 2
|
|
}
|
|
token_config="${temporary_dir}/curl-auth.conf"
|
|
escaped_token=${GITHUB_TOKEN//\\/\\\\}
|
|
escaped_token=${escaped_token//\"/\\\"}
|
|
(
|
|
umask 077
|
|
printf 'header = "Authorization: Bearer %s"\n' "$escaped_token" > "$token_config"
|
|
)
|
|
curl_arguments+=(--config "$token_config")
|
|
fi
|
|
|
|
if ! curl --fail --silent --show-error --location \
|
|
"${curl_arguments[@]}" "$api_url" \
|
|
-o "${temporary_dir}/release.json" 2> /dev/null; then
|
|
printf 'GitHub API unavailable; will use local ZIP files.\n' >&2
|
|
return 0
|
|
fi
|
|
|
|
if ! python3 - "${temporary_dir}/release.json" > "${temporary_dir}/api-hashes.tsv" << 'PY'; then
|
|
import json
|
|
import re
|
|
import sys
|
|
|
|
with open(sys.argv[1], encoding="utf-8") as release_file:
|
|
release = json.load(release_file)
|
|
|
|
for asset in release.get("assets", []):
|
|
name = asset.get("name", "")
|
|
digest = asset.get("digest") or ""
|
|
if re.fullmatch(r"sarasa-.+-nerd-font\.zip", name) and re.fullmatch(
|
|
r"sha256:[0-9a-fA-F]{64}", digest
|
|
):
|
|
print(f"{name}\t{digest.removeprefix('sha256:').lower()}")
|
|
PY
|
|
printf 'Could not parse GitHub API response; using local ZIP files.\n' >&2
|
|
return 0
|
|
fi
|
|
|
|
while IFS=$'\t' read -r asset_name digest; do
|
|
API_HASHES["$asset_name"]="$digest"
|
|
done < "${temporary_dir}/api-hashes.tsv"
|
|
}
|
|
|
|
# Check cached archive metadata against .SRCINFO and refresh stale sources.
|
|
# Globals: API_HASHES, FONT_HASHES_FILE, API_COUNT, LOCAL_COUNT, DOWNLOAD_COUNT.
|
|
# Arguments: Release tag, temporary directory, force-download flag.
|
|
# Returns: Non-zero for download or checksum failures.
|
|
collect_font_hashes() {
|
|
local release_tag="$1"
|
|
local temporary_dir="$2"
|
|
local download_all="$3"
|
|
local style orthography package_name package_dir asset_name archive_path
|
|
local srcinfo_path archive_url digest local_digest srcinfo_metadata
|
|
local srcinfo_version srcinfo_digest archive_is_current
|
|
local -i source_index=0
|
|
|
|
FONT_HASHES_FILE="${temporary_dir}/font-hashes.txt"
|
|
: > "$FONT_HASHES_FILE"
|
|
|
|
for style in "${STYLES[@]}"; do
|
|
for orthography in "${ORTHOGRAPHIES[@]}"; do
|
|
package_name="ttf-sarasa-${style}-${orthography}-nerd-font"
|
|
package_dir="${REPO_DIR}/aur-packages/${package_name}"
|
|
asset_name="sarasa-${style}-${orthography}-nerd-font.zip"
|
|
archive_path="${package_dir}/${package_name}.zip"
|
|
srcinfo_path="${package_dir}/.SRCINFO"
|
|
archive_url="${UPSTREAM_URL}/releases/download/${release_tag}/${asset_name}"
|
|
digest="${API_HASHES[$asset_name]:-}"
|
|
local_digest=''
|
|
srcinfo_metadata=''
|
|
srcinfo_version=''
|
|
srcinfo_digest=''
|
|
archive_is_current=false
|
|
|
|
if [[ -f "$archive_path" ]]; then
|
|
local_digest=$(sha256sum "$archive_path" | cut -d ' ' -f 1)
|
|
fi
|
|
if [[ -f "$srcinfo_path" ]]; then
|
|
srcinfo_metadata=$(awk '
|
|
$1 == "pkgver" && !version { version = $3 }
|
|
$1 == "sha256sums" && ++checksum_count == 1 { font_sha256 = $3 }
|
|
END {
|
|
if (version != "" && font_sha256 != "") {
|
|
printf "%s\t%s", version, font_sha256
|
|
} else {
|
|
exit 1
|
|
}
|
|
}
|
|
' "$srcinfo_path") || srcinfo_metadata=''
|
|
fi
|
|
srcinfo_version=${srcinfo_metadata%%$'\t'*}
|
|
srcinfo_digest=${srcinfo_metadata#*$'\t'}
|
|
|
|
if [[ -n "$local_digest" ]]; then
|
|
if [[ -n "$digest" && "$local_digest" == "$digest" ]]; then
|
|
archive_is_current=true
|
|
elif [[ "$srcinfo_version" == "$PKGVER" && "$srcinfo_digest" == "$local_digest" ]]; then
|
|
archive_is_current=true
|
|
fi
|
|
fi
|
|
|
|
if $download_all || ! $archive_is_current; then
|
|
printf 'Downloading %s...\n' "$asset_name"
|
|
download_archive "$temporary_dir" "$archive_url" "$archive_path"
|
|
((DOWNLOAD_COUNT += 1))
|
|
local_digest=$(sha256sum "$archive_path" | cut -d ' ' -f 1)
|
|
fi
|
|
|
|
if [[ -n "$digest" ]]; then
|
|
if [[ "$local_digest" != "$digest" ]]; then
|
|
printf 'Downloaded checksum does not match GitHub digest: %s\n' "$asset_name" >&2
|
|
return 1
|
|
fi
|
|
((API_COUNT += 1))
|
|
else
|
|
digest="$local_digest"
|
|
((LOCAL_COUNT += 1))
|
|
fi
|
|
|
|
printf '%s\n' "$digest" >> "$FONT_HASHES_FILE"
|
|
((source_index += 1))
|
|
done
|
|
done
|
|
|
|
if ((source_index != ${#FONT_SHA256S[@]})); then
|
|
printf 'Expected %d font checksums, collected %d.\n' \
|
|
"${#FONT_SHA256S[@]}" "$source_index" >&2
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Replace canonical hashes and regenerate all PKGBUILD/.SRCINFO files.
|
|
# Globals: REPO_DIR, FONT_LICENSE_SHA256, API_COUNT, LOCAL_COUNT, DOWNLOAD_COUNT.
|
|
# Arguments: Temporary directory and collected font hash file.
|
|
update_package_metadata() {
|
|
local temporary_dir="$1"
|
|
local font_hashes_file="$2"
|
|
local license_hash data_file updated_data
|
|
|
|
license_hash=$(sha256sum "${REPO_DIR}/aur-packages/LICENSE" | cut -d ' ' -f 1)
|
|
data_file="${REPO_DIR}/package-data.sh"
|
|
updated_data="${temporary_dir}/package-data.sh"
|
|
|
|
awk -v license_hash="$license_hash" -v hash_file="$font_hashes_file" '
|
|
/^readonly FONT_LICENSE_SHA256=/ {
|
|
printf "readonly FONT_LICENSE_SHA256=\047%s\047\n", license_hash
|
|
license_found = 1
|
|
next
|
|
}
|
|
/^declare -ar FONT_SHA256S=\(/ {
|
|
print
|
|
while ((getline hash < hash_file) > 0) {
|
|
printf " %s\n", hash
|
|
hash_count++
|
|
}
|
|
close(hash_file)
|
|
in_hashes = 1
|
|
hashes_found = 1
|
|
next
|
|
}
|
|
in_hashes && /^\)/ {
|
|
print
|
|
in_hashes = 0
|
|
next
|
|
}
|
|
!in_hashes { print }
|
|
END {
|
|
if (!license_found || !hashes_found || in_hashes || hash_count != 48) {
|
|
exit 1
|
|
}
|
|
}
|
|
' "$data_file" > "$updated_data"
|
|
|
|
bash -n "$updated_data"
|
|
chmod --reference="$data_file" "$updated_data"
|
|
mv -- "$updated_data" "$data_file"
|
|
bash "${REPO_DIR}/tools/generate-packages.sh" --force
|
|
}
|
|
|
|
# Parse options and coordinate checksum refresh.
|
|
# Arguments: Optional --api and --download flags.
|
|
main() {
|
|
local api_mode=false
|
|
local download_all=false
|
|
local argument release_tag temporary_dir
|
|
|
|
for argument in "$@"; do
|
|
case "$argument" in
|
|
--api) api_mode=true ;;
|
|
--download) download_all=true ;;
|
|
*)
|
|
printf 'Usage: %s [--api] [--download]\n' "$0" >&2
|
|
return 2
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if ((${#FONT_SHA256S[@]} != ${#STYLES[@]} * ${#ORTHOGRAPHIES[@]})); then
|
|
printf 'Font checksum count does not match style/orthography combinations.\n' >&2
|
|
return 1
|
|
fi
|
|
|
|
release_tag="v${PKGVER//_/-}"
|
|
temporary_dir=$(mktemp -d)
|
|
|
|
if $api_mode; then
|
|
fetch_api_hashes "$release_tag" "$temporary_dir"
|
|
fi
|
|
|
|
collect_font_hashes "$release_tag" "$temporary_dir" "$download_all"
|
|
update_package_metadata "$temporary_dir" "$FONT_HASHES_FILE"
|
|
rm -rf "$temporary_dir"
|
|
|
|
printf 'Updated 48 font checksums (%d from GitHub API, %d from local ZIPs); downloaded %d ZIPs. Font LICENSE checksum updated locally.\n' \
|
|
"$API_COUNT" "$LOCAL_COUNT" "$DOWNLOAD_COUNT"
|
|
}
|
|
|
|
main "$@"
|