Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

12.1 KB
Raw Blame
#!/bin/zsh
# Tests, builds, signs, notarizes, and checks a release of Peel from a committed tree, then prints the paths and
# SHA-256 checksums of its disk image, for people, and its zip, for Homebrew, and the path of its update feed.
#
# Signing uses Xcode's `archive` and `exportArchive`, which do what notarization requires: they add a secure
# timestamp and remove the `com.apple.security.get-task-allow` entitlement. Nothing is sent to Apple's notary
# service until the app is archived, exported, and checked.
#
# Requires a Developer ID Application certificate in the keychain and a notary profile, stored once with
#   xcrun notarytool store-credentials Peel --apple-id <you> --team-id 6R6J264YA2
# which prompts for the app-specific password, so the password stays out of the shell history, uv
# (`brew install uv`), which runs dmgbuild for the disk image's window, and the update key in the Keychain under the
# account peel, made once with Sparkle's `generate_keys --account peel`.
#
# Usage: zsh Scripts/release.sh [notary-profile]   (default profile: Peel)
set -euo pipefail
cd "$(dirname "$0")/.."

profile="${1:-Peel}"
build="build/Release"
archive="$build/Peel.xcarchive"
export_directory="$build/export"
app="$export_directory/Peel.app"

if ! command -v uv > /dev/null; then
    echo "REFUSED: uv is not installed, and the disk image needs it. Install it with: brew install uv"
    exit 1
fi

echo "== The tree"
if [ -n "$(git status --porcelain)" ]; then
    git status --short
    echo "REFUSED: the working tree has changes. Commit them or set them aside first."
    exit 1
fi

rm -rf "$build"
mkdir -p "$build"

echo "== Testing"
if ! swift test --package-path Packages/PeelCore -Xswiftc -warnings-as-errors > "$build/test.log" 2>&1; then
    grep -E "error:|✘" "$build/test.log" | head -20 || tail -20 "$build/test.log"
    echo "REFUSED: the tests failed. The whole log is $build/test.log"
    exit 1
fi
grep -E "Test run with" "$build/test.log" || true

echo "== Sparkle"
zsh Scripts/build_sparkle.sh

echo "== Archiving"
# The output goes to a log, not through a pipe, so the `if` checks xcodebuild's own exit status. Pointer
# authentication is set here as well as in the project, since Xcode gives the Swift packages only the settings
# of the command line, and every binary imports them. So is macOS 26: a package that names no platform, such as
# swift-argument-parser, would be built for an older macOS, with its class data unsigned, and the linker then
# leaves the class data of the whole `peel` tool unsigned.
if ! xcodebuild archive \
    -project Peel.xcodeproj \
    -scheme Peel \
    -configuration Release \
    -destination 'generic/platform=macOS' \
    -archivePath "$archive" \
    -derivedDataPath "$build/DerivedData" \
    ENABLE_POINTER_AUTHENTICATION=YES \
    MACOSX_DEPLOYMENT_TARGET=26.0 \
    > "$build/archive.log" 2>&1
then
    grep -E "error:" "$build/archive.log" || tail -20 "$build/archive.log"
    echo "REFUSED: the archive failed. The whole log is $build/archive.log"
    exit 1
fi
if grep -q "warning:" "$build/archive.log"; then
    grep "warning:" "$build/archive.log"
    echo "REFUSED: the archive has warnings. The whole log is $build/archive.log"
    exit 1
fi
grep -E "ARCHIVE" "$build/archive.log" || true

echo "== Exporting with Developer ID"
cat > "$build/ExportOptions.plist" <<'PLIST'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>destination</key>
	<string>export</string>
	<key>method</key>
	<string>developer-id</string>
	<key>signingStyle</key>
	<string>automatic</string>
	<key>teamID</key>
	<string>6R6J264YA2</string>
</dict>
</plist>
PLIST
if ! xcodebuild -exportArchive \
    -archivePath "$archive" \
    -exportOptionsPlist "$build/ExportOptions.plist" \
    -exportPath "$export_directory" \
    > "$build/export.log" 2>&1
then
    grep -E "error:" "$build/export.log" || tail -20 "$build/export.log"
    echo "REFUSED: the export failed. The whole log is $build/export.log"
    exit 1
fi
if grep -q "warning:" "$build/export.log"; then
    grep "warning:" "$build/export.log"
    echo "REFUSED: the export has warnings. The whole log is $build/export.log"
    exit 1
fi
grep -E "EXPORT" "$build/export.log" || true

echo "== What every program is signed with"
sparkle="$app/Contents/Frameworks/Sparkle.framework"
for binary in \
    "$app" \
    "$app/Contents/MacOS/PeelHelper" \
    "$app/Contents/PlugIns/PeelFinder.appex" \
    "$app/Contents/Helpers/peel" \
    "$sparkle" \
    "$sparkle/Versions/B/Autoupdate" \
    "$sparkle/Versions/B/Updater.app"
do
    echo "-- $binary"
    [ -e "$binary" ] || { echo "   REFUSED: $binary is not there"; exit 1; }
    signature="$(codesign -d --verbose=2 "$binary" 2>&1)" \
        || { echo "   REFUSED: codesign cannot read $binary"; exit 1; }
    grep -E "Authority|TeamIdentifier|Timestamp|flags" <<< "$signature" || true
    # Notarization needs each of these, and an update Sparkle installs is checked against the same signature.
    [[ "$signature" == *"Authority=Developer ID Application: "*"(6R6J264YA2)"* ]] \
        || { echo "   REFUSED: $binary is not signed with Peel's Developer ID"; exit 1; }
    [[ "$signature" == *"flags="*"runtime"* ]] || { echo "   REFUSED: $binary lacks the hardened runtime"; exit 1; }
    [[ "$signature" == *"Timestamp="* ]] || { echo "   REFUSED: $binary has no secure timestamp"; exit 1; }
    # Stop if `codesign` cannot read the binary, since its entitlements then cannot be checked.
    entitlements="$(codesign -d --entitlements - --xml "$binary" 2>/dev/null)" \
        || { echo "   REFUSED: codesign cannot read $binary"; exit 1; }
    # Nothing that ships may be debuggable: `get-task-allow` lets any process of the same user take control of it.
    # Read whole before it is searched: with `pipefail`, a `grep -q` that stops at its match could end `plutil` with
    # SIGPIPE and turn the match into a pass. A binary with no entitlements prints nothing, which passes.
    printed="$(plutil -p - <<< "$entitlements" 2>/dev/null)" || true
    if [[ "$printed" == *get-task-allow* ]]; then
        echo "   REFUSED: $binary carries com.apple.security.get-task-allow"
        exit 1
    fi
done

# Sparkle's XPC services are for sandboxed apps; Peel's build leaves them out, and nothing may bring them back.
[ -z "$(find "$sparkle" -name '*.xpc')" ] || { echo "REFUSED: Sparkle.framework carries XPC services"; exit 1; }

# Every program and library in the app, found rather than listed, so one added later is checked too.
executables=()
for file in ${(f)"$(find "$app" -type f -perm +111)"}; do
    [[ "$(file -b "$file")" == Mach-O* ]] && executables+=("$file")
done
for expected in MacOS/Peel MacOS/PeelHelper PlugIns/PeelFinder.appex/Contents/MacOS/PeelFinder Helpers/peel \
    Frameworks/Sparkle.framework/Versions/B/Sparkle Frameworks/Sparkle.framework/Versions/B/Autoupdate \
    Frameworks/Sparkle.framework/Versions/B/Updater.app/Contents/MacOS/Updater
do
    (( ${executables[(Ie)$app/Contents/$expected]} )) || { echo "REFUSED: $expected was not found"; exit 1; }
done

echo "== Architectures"
# arm64e is the slice with pointer authentication, which Apple silicon runs; arm64 and x86_64 are for the rest.
for executable in $executables
do
    archs="$(lipo -archs "$executable")" || { echo "REFUSED: lipo cannot read $executable"; exit 1; }
    for arch in arm64e arm64 x86_64; do
        [[ " $archs " == *" $arch "* ]] || { echo "REFUSED: $executable is built for $archs, without $arch"; exit 1; }
    done
done
echo "Every binary: arm64e, arm64, x86_64"

echo "== Signed class data"
# In the arm64e slice, pointer authentication also signs each class's read-only data, which the Objective-C image
# info marks with its flag 0x10; one object built without it turns it off for the whole binary.
for executable in $executables
do
    flags="$(otool -arch arm64e -s __DATA_CONST __objc_imageinfo "$executable" | awk 'END { print $3 }')"
    [[ -n "$flags" ]] && (( 16#$flags & 16#10 )) || { echo "REFUSED: $executable has its class data unsigned"; exit 1; }
done
echo "Every binary: class data signed"

echo "== Languages"
# Every language in the project's `knownRegions` (besides `en` and `Base`) must ship in the app and in the Finder
# extension. Without this check, a language missing from the build would quietly fall back to English.
languages=(${(f)"$(sed -n '/knownRegions = (/,/);/p' Peel.xcodeproj/project.pbxproj | tr -d ' \t,;"' \
    | grep -vE '^(knownRegions=\(|\)|en|Base)$')"})
for language in $languages; do
    for bundle in "$app" "$app/Contents/PlugIns/PeelFinder.appex"; do
        [ -d "$bundle/Contents/Resources/$language.lproj" ] \
            || { echo "REFUSED: $bundle has no $language.lproj"; exit 1; }
    done
done
echo "${#languages} languages besides English: $languages"

echo "== Notarizing"
zip="$build/Peel.zip"
ditto -c -k --keepParent "$app" "$zip"
xcrun notarytool submit "$zip" --keychain-profile "$profile" --wait
xcrun stapler staple "$app"

echo "== Checks"
codesign --verify --deep --strict --verbose=2 "$app"
spctl -a -vvv -t exec "$app"
xcrun stapler validate "$app"
if ! syspolicy_check distribution "$app"; then
    echo "REFUSED: macOS would not open this app as it is."
    exit 1
fi

version="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' "$app/Contents/Info.plist")"
build_number="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleVersion' "$app/Contents/Info.plist")"
# Zip the app again after stapling, so the download carries its notarization ticket.
final="$build/Peel-$version.zip"
rm -f "$final"
ditto -c -k --keepParent "$app" "$final"

echo "== Disk image"
# Signed by the very certificate that signed the app, named by its SHA-1: `codesign` refuses a name that two
# certificates in the keychain share (`man codesign`), as an old Developer ID and its renewal do. Then notarized and
# stapled itself, so it opens without a warning.
certificates="$(mktemp -d)"
codesign -d --extract-certificates="$certificates/leaf" "$app" 2> /dev/null || true
identity="$(shasum -a 1 "$certificates/leaf0" 2> /dev/null | cut -d ' ' -f 1)" || true
rm -rf "$certificates"
[[ "$identity" =~ '^[0-9a-f]{40}$' ]] \
    || { echo "REFUSED: the certificate that signed the app could not be read"; exit 1; }
image="$build/Peel-$version.dmg"
zsh Scripts/make_dmg.sh "$app" "$image"
codesign --sign "$identity" --timestamp "$image"
xcrun notarytool submit "$image" --keychain-profile "$profile" --wait
xcrun stapler staple "$image"
xcrun stapler validate "$image"
spctl -a -vvv -t open --context context:primary-signature "$image"

echo "== The update feed"
# Every copy of Peel reads appcast.xml from the latest release. It offers the disk image, signed with the key in the
# Keychain, whose prompt is the person's to answer; every update is critical, so Sparkle offers no Skip This Version;
# and its release notes are the changelog's entry, which Sparkle shows as Markdown, GitHub's alerts made bold lines.
feed="$build/feed"
download="https://github.com/TuguiDragos/Peel/releases/download/v$version/"
mkdir -p "$feed"
cp "$image" "$feed/"
awk -v heading="## $version (" '
    index($0, heading) == 1 { found = 1; next }
    found && /^## / { exit }
    found && /^> \[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]$/ {
        kind = substr($0, 5, length($0) - 5)
        $0 = "> **" substr(kind, 1, 1) tolower(substr(kind, 2)) "**"
    }
    found && (started || NF) { started = 1; lines[++count] = $0; if (NF) last = count }
    END { for (line = 1; line <= last; line++) print lines[line] }
' CHANGELOG.md > "$feed/Peel-$version.md"
[ -s "$feed/Peel-$version.md" ] || { echo "REFUSED: CHANGELOG.md has no entry for $version"; exit 1; }
build/SparkleTools/Build/Products/Release/generate_appcast --account peel --critical-update-version "" \
    --embed-release-notes --download-url-prefix "$download" "$feed"
swift Scripts/check_appcast.swift "$feed/appcast.xml" "$app" "$image" "$download"

echo "== Made"
echo "Peel $version ($build_number)"
for made in "$image" "$final"; do
    echo "$made"
    shasum -a 256 "$made"
done
echo "$feed/appcast.xml"
echo "Once these are on the release, on its draft and again once it is published:"
echo "swift Scripts/check_release.swift $version $build"