#!/usr/bin/env bash # Builds, signs and uploads SmartFox packages (protocol: docs/updates.md). # # tools/pkg/packages.sh [--upload] [shell] [app:...] [apps] # # With no package names it does the shell and every app. A package whose # version is already in the registry is skipped (bump the version to publish): # shell version = [workspace.package] version in Cargo.toml # app: version = "version" in apps//app.toml # # Environment: # SF_SIGNING_KEY Ed25519 private key: the PEM file base64-encoded on one # line (so CI logs mask it), or SF_SIGNING_KEY_FILE = the PEM # SF_PACKAGE_TOKEN "user:token", or a token of SF_OWNER, with write:package (upload only) # SF_GITEA default https://gitea.bluefox.support # SF_OWNER default bluefox # SF_OUT output folder, default target/packages set -euo pipefail ROOT="$(cd "$(dirname "$0")/../.." && pwd)" GITEA="${SF_GITEA:-https://gitea.bluefox.support}" OWNER="${SF_OWNER:-bluefox}" OUT="${SF_OUT:-$ROOT/target/packages}" # Binaries in the shell package (built with cargo --release). SHELL_BINS=(sf-launcher sf-remote) UPLOAD=0 WANT=() for a in "$@"; do case "$a" in --upload) UPLOAD=1 ;; shell | apps | app:*) WANT+=("$a") ;; *) echo "unknown argument: $a" >&2; exit 2 ;; esac done [ ${#WANT[@]} -gt 0 ] || WANT=(shell apps) KEY_FILE="${SF_SIGNING_KEY_FILE:-}" TMP=$(mktemp -d) trap 'rm -rf "$TMP"' EXIT if [ -z "$KEY_FILE" ]; then [ -n "${SF_SIGNING_KEY:-}" ] || { echo "SF_SIGNING_KEY or SF_SIGNING_KEY_FILE is needed" >&2; exit 2; } KEY_FILE="$TMP/key.pem" (umask 077; printf '%s' "$SF_SIGNING_KEY" | base64 -d >"$KEY_FILE") fi auth() { case "${SF_PACKAGE_TOKEN:-}" in *:*) printf -- '-u\n%s\n' "$SF_PACKAGE_TOKEN" ;; ?*) printf -- '-u\n%s:%s\n' "$OWNER" "$SF_PACKAGE_TOKEN" ;; esac } mapfile -t AUTH < <(auth) url() { echo "$GITEA/api/packages/$OWNER/generic/$1/$2/$1-$2.tar.zst"; } published() { [ "$UPLOAD" = 1 ] || return 1 local code code=$(curl -s -o /dev/null -w '%{http_code}' -I "${AUTH[@]}" "$(url "$1" "$2")") [ "$code" = 200 ] } # pack : tar.zst + .sig in $OUT, uploaded with --upload. pack() { local name=$1 version=$2 dir=$3 file printf 'name = "%s"\nversion = "%s"\n' "$name" "$version" >"$dir/pkg.toml" mkdir -p "$OUT" file="$OUT/$name-$version.tar.zst" tar --sort=name --owner=0 --group=0 --numeric-owner --mtime=@0 -C "$dir" -cf - . | zstd -q -19 -T0 -f -o "$file" openssl pkeyutl -sign -inkey "$KEY_FILE" -rawin -in "$file" -out "$file.sig" echo "built $(basename "$file") ($(du -h "$file" | cut -f1))" if [ "$UPLOAD" = 1 ]; then for f in "$file" "$file.sig"; do curl -fsS "${AUTH[@]}" --upload-file "$f" "$GITEA/api/packages/$OWNER/generic/$name/$version/$(basename "$f")" >/dev/null done echo "uploaded $name $version" fi } toml_version() { sed -n 's/^version *= *"\(.*\)".*/\1/p' "$1" | head -1; } do_shell() { local version version=$(sed -n '/^\[workspace.package\]/,/^\[/ s/^version *= *"\(.*\)"/\1/p' "$ROOT/Cargo.toml") if published sf-shell "$version"; then echo "sf-shell $version: already published" return fi local args=() for b in "${SHELL_BINS[@]}"; do args+=(-p "$b"); done (cd "$ROOT" && cargo build --release --locked "${args[@]}") local dir="$TMP/sf-shell" rm -rf "$dir" mkdir -p "$dir/bin" for b in "${SHELL_BINS[@]}"; do install -m755 "${CARGO_TARGET_DIR:-$ROOT/target}/release/$b" "$dir/bin/$b"; done install -m755 "$ROOT/tools/siri-remote/fastpair.py" "$dir/bin/sf-pair" pack sf-shell "$version" "$dir" } do_app() { local id=$1 src="$ROOT/apps/$1" version [ -f "$src/app.toml" ] || { echo "no app $id" >&2; return 1; } version=$(toml_version "$src/app.toml") [ -n "$version" ] || { echo "apps/$id/app.toml has no version, skipped"; return; } if published "sf-app-$id" "$version"; then echo "sf-app-$id $version: already published" return fi local dir="$TMP/sf-app-$id" rm -rf "$dir" cp -r "$src" "$dir" chmod -R u=rwX,go=rX "$dir" pack "sf-app-$id" "$version" "$dir" } for w in "${WANT[@]}"; do case "$w" in shell) do_shell ;; apps) for d in "$ROOT"/apps/*/; do do_app "$(basename "$d")"; done ;; app:*) do_app "${w#app:}" ;; esac done