266 lines
13 KiB
Bash
266 lines
13 KiB
Bash
#!/usr/bin/env bash
|
|||
|
|
# Functions shared by the kit scripts. To be sourced: `. "$(dirname "$0")/common.sh"`.
|
||
|
|
#
|
||
|
|
# Parameters read from the environment (default values below); crate.mk exports
|
||
|
|
# them, the scripts also remain usable on their own from the root of a crate.
|
||
|
|
|
||
|
|
CARGO="${CARGO:-cargo}"
|
||
|
|
CARGO_TOML="${CARGO_TOML:-Cargo.toml}"
|
||
|
|
API_DIR="${API_DIR:-api}"
|
||
|
|
RELEASE_NOTES_DIR="${RELEASE_NOTES_DIR:-ReleaseNotes}"
|
||
|
|
# Private cargo registry and its sparse index: provided by the organisation overlay (org.mk), empty otherwise.
|
||
|
|
REGISTRY="${REGISTRY:-}"
|
||
|
|
REGISTRY_INDEX="${REGISTRY_INDEX:-}"
|
||
|
|
|
||
|
|
info() { printf '\033[1;34m%s\033[0m\n' "$*" >&2; }
|
||
|
|
warn() { printf '\033[1;33m%s\033[0m\n' "$*" >&2; }
|
||
|
|
ok() { printf '\033[0;32m%s\033[0m\n' "$*" >&2; }
|
||
|
|
err() { printf '\033[0;31m%s\033[0m\n' "$*" >&2; }
|
||
|
|
|
||
|
|
# API call: `forge_call <operation> <curl args…>`. Returns the response body on standard output;
|
||
|
|
# on an HTTP response ≥ 400 (or a transport failure), states the operation, the code and the body on standard
|
||
|
|
# error and returns 1. Replaces `curl -sf`, which fails silently (code 22).
|
||
|
|
forge_call() {
|
||
|
|
local operation="$1" body code
|
||
|
|
shift
|
||
|
|
body=$(mktemp)
|
||
|
|
code=$(curl -s -o "$body" -w '%{http_code}' "$@") || code="000"
|
||
|
|
if [ "$code" -ge 400 ] || [ "$code" = "000" ]; then
|
||
|
|
err "✗ $operation : HTTP $code : $(head -c 500 "$body")"
|
||
|
|
rm -f "$body"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
cat "$body"
|
||
|
|
rm -f "$body"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Directory of the forge tokens of a workstation: `secrets/` of the cluster, i.e. next to the repositories
|
||
|
|
# (the parent of the current repository), outside every repository. FORGE_SECRETS_DIR overrides it.
|
||
|
|
forge_secrets_dir() {
|
||
|
|
if [ -n "${FORGE_SECRETS_DIR:-}" ]; then
|
||
|
|
printf '%s' "$FORGE_SECRETS_DIR"
|
||
|
|
return 0
|
||
|
|
fi
|
||
|
|
local root
|
||
|
|
root=$(jj root 2>/dev/null) || root=$(pwd)
|
||
|
|
printf '%s/secrets' "$(dirname "$root")"
|
||
|
|
}
|
||
|
|
|
||
|
|
# "host owner repo" of a git or registry URL: ssh://git@host[:port]/owner/repo.git, git@host:owner/repo.git,
|
||
|
|
# https://host/owner/repo, sparse+https://host/api/packages/owner/cargo/ (owner of the registry, no repo).
|
||
|
|
# Fails if the URL has no known form. Fields are empty when absent.
|
||
|
|
forge_parse_url() {
|
||
|
|
local u="${1#sparse+}" host path owner repo
|
||
|
|
case "$u" in
|
||
|
|
*://*) u="${u#*://}"; u="${u#*@}"; host="${u%%/*}"; path="${u#*/}" ;;
|
||
|
|
*@*:*) u="${u#*@}"; host="${u%%:*}"; path="${u#*:}" ;;
|
||
|
|
*) return 1 ;;
|
||
|
|
esac
|
||
|
|
[ "$path" = "$u" ] && path=""
|
||
|
|
host="${host%%:*}"
|
||
|
|
path="${path%/}"; path="${path%.git}"
|
||
|
|
case "$path" in
|
||
|
|
api/packages/*) owner="${path#api/packages/}"; owner="${owner%%/*}"; repo="" ;;
|
||
|
|
*) owner="${path%%/*}"; repo="${path#*/}"; [ "$repo" = "$path" ] && repo="" ;;
|
||
|
|
esac
|
||
|
|
[ -n "$host" ] || return 1
|
||
|
|
printf '%s %s %s\n' "$host" "$owner" "$repo"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Forge token. `forge_token [url]`: the forge is that of the URL (registry index, repository), by default that of
|
||
|
|
# the `origin` remote. Order, the first that exists wins:
|
||
|
|
# 1. $FORGETOKEN: the CI, where each forge provides its own secret to its own jobs;
|
||
|
|
# 2. the token files of the cluster, most specific first (one line, mode 0600; see forge_secrets_dir):
|
||
|
|
# <secrets>/<host>/<owner>/<repo>.token, <secrets>/<host>/<owner>.token, <secrets>/<host>.token;
|
||
|
|
# 3. the file $FORGE_TOKEN_FILE (former single-token layout).
|
||
|
|
# Nothing on standard output if there is none; the caller then says where to put it. Never displayed.
|
||
|
|
forge_token() {
|
||
|
|
if [ -n "${FORGETOKEN:-}" ]; then
|
||
|
|
printf '%s' "$FORGETOKEN"
|
||
|
|
return 0
|
||
|
|
fi
|
||
|
|
local url="${1:-}" host owner repo dir f
|
||
|
|
[ -n "$url" ] || url=$(jj git remote list 2>/dev/null | awk '$1=="origin"{print $2; exit}')
|
||
|
|
if [ -n "$url" ] && read -r host owner repo < <(forge_parse_url "$url"); then
|
||
|
|
dir=$(forge_secrets_dir)
|
||
|
|
for f in ${repo:+"$dir/$host/$owner/$repo.token"} ${owner:+"$dir/$host/$owner.token"} "$dir/$host.token"; do
|
||
|
|
if [ -f "$f" ]; then
|
||
|
|
head -n 1 "$f" | tr -d '[:space:]'
|
||
|
|
return 0
|
||
|
|
fi
|
||
|
|
done
|
||
|
|
fi
|
||
|
|
if [ -n "${FORGE_TOKEN_FILE:-}" ] && [ -f "$FORGE_TOKEN_FILE" ]; then
|
||
|
|
head -n 1 "$FORGE_TOKEN_FILE" | tr -d '[:space:]'
|
||
|
|
fi
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
# cargo authentication against the private registry: CARGO_REGISTRIES_<REGISTRY>_TOKEN ("Bearer <token>"),
|
||
|
|
# built from the forge token if not already defined. Needed by any cargo call that
|
||
|
|
# resolves registry dependencies (publish, wiki doc). For crates.io (REGISTRY = crates-io) nothing
|
||
|
|
# is built: the crates.io token is CARGO_REGISTRY_TOKEN, supplied by the user.
|
||
|
|
if [ -n "$REGISTRY" ] && [ "$REGISTRY" != crates-io ]; then
|
||
|
|
reg_var="CARGO_REGISTRIES_$(printf '%s' "$REGISTRY" | tr 'a-z-' 'A-Z_')_TOKEN"
|
||
|
|
if [ -z "${!reg_var:-}" ] && [ -n "$(forge_token "$REGISTRY_INDEX")" ]; then
|
||
|
|
export "$reg_var=Bearer $(forge_token "$REGISTRY_INDEX")"
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Version: content of $VERSION_FILE if set (repositories that are not crates, like
|
||
|
|
# crate-kit itself), otherwise first occurrence in the crate's Cargo.toml.
|
||
|
|
current_version() {
|
||
|
|
# Source of the version: VERSION_FILE (exported by the Makefiles of the kits), else the Cargo.toml of a crate.
|
||
|
|
# Without either (script run by hand at the root of a kit, not through make): kit/VERSION (crate-kit) or
|
||
|
|
# overlay/kit/VERSION (extension kit), if present.
|
||
|
|
local file="${VERSION_FILE:-}" candidate
|
||
|
|
if [ -z "$file" ] && [ ! -f "$CARGO_TOML" ]; then
|
||
|
|
for candidate in kit/VERSION overlay/kit/VERSION; do
|
||
|
|
[ -f "$candidate" ] && { file="$candidate"; break; }
|
||
|
|
done
|
||
|
|
fi
|
||
|
|
if [ -n "$file" ]; then
|
||
|
|
head -n 1 "$file"
|
||
|
|
elif [ -f "$CARGO_TOML" ]; then
|
||
|
|
sed -n 's/^version = "\(.*\)"/\1/p' "$CARGO_TOML" | head -n 1
|
||
|
|
else
|
||
|
|
err "✗ no source for the version: no VERSION_FILE, no $CARGO_TOML, no kit/VERSION nor overlay/kit/VERSION here"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
# Package name: first occurrence in the crate's Cargo.toml.
|
||
|
|
# Abandons the empty commits without a description between trunk() and @ (excluding @ itself): jj refuses to
|
||
|
|
# push a commit without a description, and these carry nothing (leftovers of `jj new`). Descendants
|
||
|
|
# are rebased automatically; neither work nor an already pushed (immutable) commit is touched.
|
||
|
|
abandon_blank_commits() {
|
||
|
|
jj abandon 'trunk()..@ & mutable() & empty() & description(exact:"") ~ @' >/dev/null 2>&1 || true
|
||
|
|
}
|
||
|
|
|
||
|
|
# The commit to publish: @, unless it is empty and without a description (the working commit that `jj new`
|
||
|
|
# leaves after a release or a push); we then go up to the first ancestor that carries work.
|
||
|
|
# Such a commit is not pushed (jj refuses a commit without a description) and deserves neither tag nor main.
|
||
|
|
release_rev() {
|
||
|
|
local rev='@' blank
|
||
|
|
while :; do
|
||
|
|
blank=$(jj log -r "$rev" --no-graph -T 'if(empty && description == "", "1", "0")')
|
||
|
|
[ "$blank" = 1 ] || break
|
||
|
|
rev="$rev-"
|
||
|
|
done
|
||
|
|
echo "$rev"
|
||
|
|
}
|
||
|
|
|
||
|
|
crate_name() { sed -n 's/^name = "\(.*\)"/\1/p' "$CARGO_TOML" | head -n 1; }
|
||
|
|
|
||
|
|
# Last version having an API snapshot (sorted by version number), empty if there is none.
|
||
|
|
latest_snapshot() {
|
||
|
|
ls "$API_DIR"/*.json.gz 2>/dev/null | sed 's|.*/||; s|\.json\.gz$||' | sort -V | tail -n 1 || true
|
||
|
|
}
|
||
|
|
|
||
|
|
# Fingerprint of the tools that produce a snapshot. The rustdoc JSON (format number) and the text
|
||
|
|
# rendering of cargo public-api change with rustc and cargo-public-api: a snapshot written with other
|
||
|
|
# tools than today's is no longer comparable (cargo-semver-checks crashes, or the text diff
|
||
|
|
# announces a break where only the rendering changed).
|
||
|
|
toolchain_stamp() {
|
||
|
|
printf '%s | %s\n' "$(rustc -V)" "$("$CARGO" public-api --version)"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Writes the snapshot of version <version> of the code in the current directory (root of a
|
||
|
|
# crate), into <api_dir>: <version>.txt, <version>.json.gz and <version>.stamp (toolchain_stamp).
|
||
|
|
# Parameters: version, snapshot directory (absolute if the current directory is not the crate).
|
||
|
|
emit_snapshot() {
|
||
|
|
local version="$1" api_dir="$2" name target_dir
|
||
|
|
name=$(crate_name)
|
||
|
|
mkdir -p "$api_dir"
|
||
|
|
|
||
|
|
# -s: without the blanket implementations (`impl<T> Into<U> for Type`…), added by rustc to
|
||
|
|
# every public type and unrelated to its API (they made up half of the file).
|
||
|
|
"$CARGO" public-api -s >"$api_dir/$version.txt"
|
||
|
|
# The rustdoc JSON is unstable; RUSTC_BOOTSTRAP makes it possible to produce it with the stable toolchain,
|
||
|
|
# the same one cargo-semver-checks uses to build the current state.
|
||
|
|
# --document-private-items: cargo-semver-checks builds the current state with private items
|
||
|
|
# (private fields included); a reference without them makes a struct with a private field look like
|
||
|
|
# it "gains a private field" and gives a break wrongly.
|
||
|
|
RUSTC_BOOTSTRAP=1 "$CARGO" rustdoc --lib -q -- -Zunstable-options --output-format json --document-private-items
|
||
|
|
|
||
|
|
target_dir=$("$CARGO" metadata --format-version 1 --no-deps |
|
||
|
|
sed -n 's/.*"target_directory":"\([^"]*\)".*/\1/p')
|
||
|
|
# Compressed (gzip -n: reproducible output): the JSON exceeds 1 MB, jj's limit for a new file.
|
||
|
|
gzip -9 -n -c "$target_dir/doc/${name//-/_}.json" >"$api_dir/$version.json.gz"
|
||
|
|
toolchain_stamp >"$api_dir/$version.stamp"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Regenerates the snapshot of the published version <version> with the current tools, from the code of
|
||
|
|
# its tag `version-<version>` (read-only: `jj file list/show`, no modification of the repository).
|
||
|
|
# The tag is exported into a sibling folder of the crate (`<crate>.baseline.tmp`): the relative paths
|
||
|
|
# of `.cargo/config.toml` (`[patch.<registry>]` towards the neighbours) remain valid there. Only `.cargo/` (ignored
|
||
|
|
# by the repository, generated by `make`) is copied in addition.
|
||
|
|
regenerate_snapshot() {
|
||
|
|
local version="$1" api_dir root tmp rev="version-$1" f
|
||
|
|
api_dir="$(pwd)/$API_DIR"
|
||
|
|
root="$(pwd)"
|
||
|
|
tmp="$root.baseline.tmp"
|
||
|
|
if ! jj file list -r "$rev" >/dev/null 2>&1; then
|
||
|
|
err "✗ tag $rev not found: cannot regenerate the snapshot of $version"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
rm -rf "$tmp"
|
||
|
|
mkdir -p "$tmp"
|
||
|
|
while IFS= read -r f; do
|
||
|
|
mkdir -p "$tmp/$(dirname "$f")"
|
||
|
|
jj file show -r "$rev" "$f" >"$tmp/$f"
|
||
|
|
done < <(jj file list -r "$rev")
|
||
|
|
[ -d "$root/.cargo" ] && cp -R "$root/.cargo" "$tmp/.cargo"
|
||
|
|
(
|
||
|
|
cd "$tmp"
|
||
|
|
CARGO_TARGET_DIR="$tmp/target" emit_snapshot "$version" "$api_dir"
|
||
|
|
) || { rm -rf "$tmp"; return 1; }
|
||
|
|
rm -rf "$tmp"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Fails with an installation message if a cargo tool is missing.
|
||
|
|
require_cargo_tool() {
|
||
|
|
local sub="$1" crate="$2"
|
||
|
|
if ! command -v "cargo-$sub" >/dev/null 2>&1; then
|
||
|
|
err "✗ cargo-$sub is required: cargo install $crate --locked"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
# Lines that are not the crate's API and that the text diff must not see:
|
||
|
|
# - `impl core::marker::Freeze`: unstable auto trait, invisible on stable; it appears or
|
||
|
|
# disappears as soon as a field goes under `Mutex`/`Atomic*`, with no effect for a caller;
|
||
|
|
# - the blanket implementations (`impl<T> other_crate::Trait for Type`) of traits of OTHER crates
|
||
|
|
# and their associated items: rustdoc lists them for every trait of the dependency graph, so
|
||
|
|
# they appear or disappear when a dependency changes (e.g. `crossbeam_epoch::Pointable`,
|
||
|
|
# `either::IntoEither`), without the crate's API having moved. Those of `core`,
|
||
|
|
# `alloc`, `std` and of the crate itself are kept.
|
||
|
|
filter_unstable() {
|
||
|
|
grep -v '^impl core::marker::Freeze for ' "$1" | awk -v crate="$(crate_name)" '
|
||
|
|
/^impl</ {
|
||
|
|
skip = 0
|
||
|
|
rest = $0
|
||
|
|
sub(/^impl<[^>]*> /, "", rest)
|
||
|
|
split(rest, parts, " ")
|
||
|
|
trait = parts[1]
|
||
|
|
if (trait !~ /^(core|alloc|std)::/ && index(trait, crate "::") != 1 && match($0, / for [^ ]+/)) {
|
||
|
|
self = substr($0, RSTART + 5, RLENGTH - 5)
|
||
|
|
skip = 1
|
||
|
|
next
|
||
|
|
}
|
||
|
|
print
|
||
|
|
next
|
||
|
|
}
|
||
|
|
skip && (index($0, "pub fn " self "::") == 1 || index($0, "pub unsafe fn " self "::") == 1 ||
|
||
|
|
index($0, "pub type " self "::") == 1 || index($0, "pub const " self "::") == 1) { next }
|
||
|
|
{ skip = 0; print }
|
||
|
|
' || true
|
||
|
|
}
|
||
|
|
|
||
|
|
# API text for the release notes: `filter_unstable`, then without the auto traits
|
||
|
|
# (`Send`, `Sync`, `Unpin`, `UnwindSafe`, `RefUnwindSafe`, `Freeze`, `Forget`, `Move`, `UnsafeUnpin`,
|
||
|
|
# `StructuralPartialEq`) that cargo public-api lists for every type: a new type adds nine
|
||
|
|
# lines that drown the API. They remain in the snapshot, hence in the verdict of `make api-level`
|
||
|
|
# (losing `Send` is a break); only the readable notes omit them.
|
||
|
|
filter_notes() {
|
||
|
|
filter_unstable "$1" | grep -v -E '^impl core::(marker::(Forget|Freeze|Move|Send|Sync|Unpin|UnsafeUnpin|StructuralPartialEq)|panic::unwind_safe::(RefUnwindSafe|UnwindSafe)) for ' || true
|
||
|
|
}
|