Files
os/tools/pkg/packages.sh
2026-10-25 14:11:49 +01:00

139 lines
5.2 KiB
Bash

#!/usr/bin/env bash
# Builds, signs and uploads Fennec OS packages (protocol: docs/updates.md).
#
# tools/pkg/packages.sh [--upload] [shell] [app:<id>...] [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:<id> version = "version" in apps/<id>/app.toml, or what the app's
# hook says (tools/pkg/<id>/package.sh: Steam follows Valve's
# official steam-launcher package)
#
# 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 sf-core sf-miracast sf-compositor sf-update)
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 <name> <version> <dir>: 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"
}
# An app with a hook (tools/pkg/<id>/package.sh, e.g. Steam from Valve's
# official package) gets its files and version from it: `--version` prints
# the upstream version, `<dir>` fills the app folder.
do_app() {
local id=$1 src="$ROOT/apps/$1" version hook="$ROOT/tools/pkg/$1/package.sh"
[ -f "$src/app.toml" ] || { echo "no app $id" >&2; return 1; }
if [ -f "$hook" ]; then
version=$(bash "$hook" --version)
else
version=$(toml_version "$src/app.toml")
fi
[ -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"
if [ -f "$hook" ]; then
[ "$(bash "$hook" "$dir" | tail -1)" = "$version" ] || { echo "$id: the hook built another version than $version" >&2; return 1; }
fi
chmod -R u=rwX,go=rX "$dir"
# Git on Windows loses the x bit: programs live in bin/.
[ -d "$dir/bin" ] && chmod -R a+x "$dir/bin"
pack "sf-app-$id" "$version" "$dir"
}
for w in "${WANT[@]}"; do
case "$w" in
shell) do_shell ;;
# A folder with its own .git is a store app developed in place: skipped.
apps) for d in "$ROOT"/apps/*/; do [ -e "$d/.git" ] || do_app "$(basename "$d")"; done ;;
app:*) do_app "${w#app:}" ;;
esac
done