#!/bin/bash
set +x
set +v
set -euo pipefail
cd "$(dirname "$0")/.."
# shellcheck source=Scripts/product.env
source Scripts/product.env
source Scripts/version.sh
source Scripts/packaging-paths.sh
value() { local name="${PRODUCT_ENV_PREFIX}_$1"; printf '%s' "${!name:-}"; }
fail() { printf '%s\n' "$*" >&2; exit 1; }
[[ ${GITHUB_ACTIONS:-} == true && ${RUNNER_ENVIRONMENT:-} == github-hosted ]] || fail 'CI signing requires a GitHub-hosted runner.'
[[ $(git rev-parse HEAD) == "$(git rev-parse "origin/$PRODUCT_DEFAULT_BRANCH")" ]] || fail 'Signing scripts must come from the default branch.'
seed=$(/bin/cat)
[[ -n $seed ]] || fail 'Provide the update seed on stdin.'
app=$(value SIGNING_APP)
identity=$(value SIGN_IDENTITY)
version_name="${PRODUCT_ENV_PREFIX}_VERSION"
build_name="${PRODUCT_ENV_PREFIX}_BUILD_NUMBER"
version=${!version_name-}
build=${!build_name-}
validate_release_version "$version" "$build"
output=$(value RELEASE_DIR)
public=$(value UPDATE_PUBLIC_KEY)
[[ -n $identity && $identity != - && -n $output && -n $public && $public == "$PRODUCT_UPDATE_PUBLIC_KEY" ]] || fail 'Invalid signing inputs.'
app=$(packaging_app_path "$app")
output=$(packaging_output_path "$output")
Scripts/bundle-preflight.sh "$app"
mkdir -p "$(dirname "$output")"
work=$(mktemp -d "$(dirname "$output")/.sign-release.XXXXXX")
work=$(cd "$work" && pwd)
trap 'rm -rf "$work"' EXIT
framework="$app/Contents/Frameworks/Sparkle.framework"
Scripts/parcel-dev.sh release-bundle-identity "$app/Contents/Info.plist" "$PRODUCT_BUNDLE_ID" "$version" "$build"
Scripts/parcel-dev.sh update-metadata configure "$app/Contents/Info.plist" release
# The credential umask must not make the installed app unreadable to other users.
Scripts/parcel-dev.sh release-bundle-permissions "$app"
printf '%s\n' '' > "$work/empty.entitlements"
sign() {
local args=(--force --sign "$identity" --options runtime --timestamp)
[[ ${1:-} == --entitlements ]] || args+=(--entitlements "$work/empty.entitlements")
if ! codesign "${args[@]}" "$@" > "$work/sign.log" 2>&1; then
/bin/cat "$work/sign.log" >&2
fail 'Could not sign the release.'
fi
}
# Keep the downloader sandboxed without trusting entitlements from the archive.
printf '%s\n' 'com.apple.security.app-sandboxcom.apple.security.network.client' > "$work/downloader.entitlements"
sign "$framework/Versions/B/XPCServices/Installer.xpc"
sign --entitlements "$work/downloader.entitlements" "$framework/Versions/B/XPCServices/Downloader.xpc"
sign "$framework/Versions/B/Autoupdate"
sign "$framework/Versions/B/Updater.app"
sign "$framework"
sign "$app/Contents/Resources/bin/$PRODUCT_CLI"
sign "$app"
Scripts/check-bundle.sh --no-launch "$app"
mkdir "$work/artifacts"
artifact="$work/artifacts/$PRODUCT_NAME-$version.dmg"
export "${PRODUCT_ENV_PREFIX}_DMG_PATH=$artifact"
Scripts/notarize.sh
signature=$(printf '%s' "$seed" | Scripts/parcel-dev.sh update-crypto sign "$artifact")
unset seed
Scripts/parcel-dev.sh update-crypto verify "$artifact" "$signature" "$public"
Scripts/parcel-dev.sh update-metadata release "$artifact" "$signature" "$work/artifacts" "Packaging/$PRODUCT_CASK.rb"
mv -n "$work/artifacts" "$output"
[[ ! -d $work/artifacts ]] || fail "Refusing to replace: $output"
printf 'Signed release artifacts: %s\n' "$output"