|
| 1 | +#!/bin/sh |
| 2 | +# Fails when the public API breaks compared with the last release, unless the |
| 3 | +# break is declared: a pending changeset, or the version being released, bumps |
| 4 | +# zero-swift enough for it (a minor below 1.0, a major from 1.0). |
| 5 | +# |
| 6 | +# scripts/api-breaks.sh [<baseline tag>] default: the mirror's newest tag |
| 7 | +# |
| 8 | +# The digester (`swift package diagnose-api-breaking-changes`) needs the |
| 9 | +# package at the root of a git repository, so this runs it in a scratch |
| 10 | +# repository shaped like the SwiftPM mirror (wevm/zero-swift): the released tag, |
| 11 | +# with this checkout's swift/ committed on top. Its API dumps also hold |
| 12 | +# `package` declarations, which apps can't use, so they're removed from both |
| 13 | +# sides before comparing. On Linux the Apple-only API (ZeroUI, most of |
| 14 | +# ZeroVirtual) isn't built, so swift-macos.yml runs this too. |
| 15 | +set -eu |
| 16 | +cd "$(dirname "$0")/.." |
| 17 | +swift=$(pwd) |
| 18 | +root=$(cd .. && pwd) |
| 19 | +mirror=https://github.com/wevm/zero-swift.git |
| 20 | +products="Zero ZeroUI ZeroVirtual ZeroTesting" |
| 21 | + |
| 22 | +baseline=${1:-$(git ls-remote --tags --refs "$mirror" | sed 's|.*refs/tags/||' | |
| 23 | + sort -V | tail -1)} |
| 24 | + |
| 25 | +work=$(mktemp -d) |
| 26 | +dumps=$(mktemp -d) |
| 27 | +trap 'rm -rf "$work" "$dumps"' EXIT |
| 28 | + |
| 29 | +git -C "$work" init -q |
| 30 | +git -C "$work" fetch -q --depth 1 "$mirror" "refs/tags/$baseline:refs/tags/$baseline" |
| 31 | +git -C "$work" checkout -q "$baseline" |
| 32 | +git -C "$work" rm -rq . |
| 33 | +# This checkout's swift/, with uncommitted changes, as the release would have it. |
| 34 | +git ls-files -z --cached --others --exclude-standard | |
| 35 | + tar --null -cf - -T - | tar -xf - -C "$work" |
| 36 | +git -C "$work" add -A |
| 37 | +git -C "$work" -c user.name=api-breaks -c user.email=api-breaks@localhost \ |
| 38 | + commit -qm current --allow-empty |
| 39 | + |
| 40 | +digester=$(command -v swift-api-digester || xcrun --find swift-api-digester 2>/dev/null || |
| 41 | + echo "$(dirname "$(readlink -f "$(command -v swift)")")/swift-api-digester") |
| 42 | + |
| 43 | +cd "$work" |
| 44 | +# Dumps each side's API, keeps only its public declarations, and compares |
| 45 | +# them. Stripping `package` declarations from both sides matters: one that |
| 46 | +# was public and became `package` reads as removed. |
| 47 | +for side in old:$baseline new:HEAD; do |
| 48 | + name=${side%%:*} rev=${side#*:} |
| 49 | + swift package diagnose-api-breaking-changes "$rev" --products $products \ |
| 50 | + --baseline-dir "$dumps/$name" >"$dumps/$name.log" 2>&1 || true |
| 51 | + if ! ls "$dumps/$name"/*/*.json >/dev/null 2>&1; then |
| 52 | + echo "Couldn't dump the API at $rev:" >&2 |
| 53 | + cat "$dumps/$name.log" >&2 |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | + for dump in "$dumps/$name"/*/*.json; do |
| 57 | + jq 'walk(if type == "object" and has("children") |
| 58 | + then .children |= map(select(.isInternal != true)) else . end)' \ |
| 59 | + "$dump" >"$dumps/$name-$(basename "$dump")" |
| 60 | + done |
| 61 | +done |
| 62 | +breaks= |
| 63 | +for old in "$dumps"/old-*.json; do |
| 64 | + module=$(basename "$old" .json) module=${module#old-} |
| 65 | + new="$dumps/new-$module.json" |
| 66 | + if [ ! -e "$new" ]; then |
| 67 | + breaks="$breaks$module: module has been removed |
| 68 | +" |
| 69 | + continue |
| 70 | + fi |
| 71 | + "$digester" -diagnose-sdk -print-module -input-paths "$old" -input-paths "$new" \ |
| 72 | + -o "$dumps/$module.txt" |
| 73 | + breaks="$breaks$(grep -v -e '^/\*' -e '^$' "$dumps/$module.txt" || true) |
| 74 | +" |
| 75 | +done |
| 76 | +breaks=$(printf '%s' "$breaks" | sed '/^$/d') |
| 77 | +if [ -z "$breaks" ]; then |
| 78 | + echo "No public API breaks since $baseline." |
| 79 | + exit 0 |
| 80 | +fi |
| 81 | + |
| 82 | +echo "Public API breaks since $baseline:" |
| 83 | +printf '%s\n' "$breaks" | sed 's/^/ /' |
| 84 | +case $baseline in |
| 85 | + 0.*) needed=minor levels='minor|major' ;; |
| 86 | + *) needed=major levels=major ;; |
| 87 | +esac |
| 88 | +# A pending changeset that declares the break. |
| 89 | +if grep -lqE "^['\"]zero-swift['\"]: *($levels)\$" "$root"/.changeset/*.md 2>/dev/null; then |
| 90 | + echo "Allowed: a pending changeset bumps zero-swift by a $needed or more." |
| 91 | + exit 0 |
| 92 | +fi |
| 93 | +# The version pull request, or main after it merged: the changesets are gone, |
| 94 | +# and the version being released declares the break. |
| 95 | +version=$(cat "$swift/VERSION") |
| 96 | +old_major=${baseline%%.*} old_minor=${baseline#*.} old_minor=${old_minor%%.*} |
| 97 | +new_major=${version%%.*} new_minor=${version#*.} new_minor=${new_minor%%.*} |
| 98 | +if [ "$new_major" -gt "$old_major" ] || |
| 99 | + { [ "$needed" = minor ] && [ "$new_major" -eq "$old_major" ] && |
| 100 | + [ "$new_minor" -gt "$old_minor" ]; }; then |
| 101 | + echo "Allowed: $version is a $needed or more after $baseline." |
| 102 | + exit 0 |
| 103 | +fi |
| 104 | +echo "These break apps that build against zero-swift $baseline. Keep the old" \ |
| 105 | + "API (deprecate it), or add a changeset that bumps zero-swift by a $needed" \ |
| 106 | + "and tells apps what to change." >&2 |
| 107 | +exit 1 |
0 commit comments