Branchwell

Inkspring / app · inkspring.ai / native-mac / build-app.sh

1#!/bin/bash
2set -euo pipefail
3
4project="$(cd "$(dirname "$0")" && pwd)"
5output="${1:-/Volumes/Expand/Projects/Inkspring/native-app}"
6case "$output" in
7 /*) ;;
8 *) echo "Use an absolute output directory." >&2; exit 2 ;;
9esac
10if [[ "$output" == /Volumes/Expand/* && ! -d /Volumes/Expand ]]; then
11 echo "Mount Expand before building Inkspring." >&2
12 exit 1
13fi
14
15mkdir -p "$output/tmp"
16export TMPDIR="$output/tmp"
17sdk="$(xcrun --sdk macosx --show-sdk-path)"
18# Keep the real SDK version in the linked executable for macOS appearance selection.
19configuration="${INKSPRING_MAC_CONFIGURATION:-release}"
20case "$configuration" in
21 debug|release) ;;
22 *) echo "Use debug or release for INKSPRING_MAC_CONFIGURATION." >&2; exit 2 ;;
23esac
24build_flags=(--package-path "$project" --scratch-path "$output/swift-build" --build-system native --sdk "$sdk" --disable-sandbox -c "$configuration")
25swift build "${build_flags[@]}"
26binary="$(swift build "${build_flags[@]}" --show-bin-path)"
27installed_bundle="$output/Inkspring.app"
28staging="$(mktemp -d "$output/.inkspring-build.XXXXXXXX")"
29trap 'rm -rf "$staging"' EXIT
30bundle="$staging/Inkspring.app"
31mkdir -p "$bundle/Contents/MacOS" "$bundle/Contents/Resources" "$bundle/Contents/Library/LaunchAgents"
32mkdir -p "$bundle/Contents/Frameworks"
33cp "$binary/Inkspring" "$bundle/Contents/MacOS/Inkspring"
34cp "$project/Info.plist" "$bundle/Contents/Info.plist"
35sparkle="$bundle/Contents/Frameworks/Sparkle.framework"
36ditto "$binary/Sparkle.framework" "$sparkle"
37if [[ -n "${INKSPRING_UPDATE_FEED:-}" || -n "${INKSPRING_UPDATE_PUBLIC_KEY:-}" ]]; then
38 : "${INKSPRING_UPDATE_FEED:?Set the Sparkle appcast URL.}"
39 : "${INKSPRING_UPDATE_PUBLIC_KEY:?Set the Sparkle Ed25519 public key.}"
40 : "${INKSPRING_MAC_SIGNING_IDENTITY:?Sparkle releases require Developer ID signing.}"
41 [[ "$INKSPRING_UPDATE_FEED" == https://* ]] || { echo "Use an HTTPS Sparkle appcast." >&2; exit 2; }
42 plutil -insert SUFeedURL -string "$INKSPRING_UPDATE_FEED" "$bundle/Contents/Info.plist"
43 plutil -insert SUPublicEDKey -string "$INKSPRING_UPDATE_PUBLIC_KEY" "$bundle/Contents/Info.plist"
44 plutil -insert SUVerifyUpdateBeforeExtraction -bool YES "$bundle/Contents/Info.plist"
45 plutil -insert SURequireSignedFeed -bool YES "$bundle/Contents/Info.plist"
46 plutil -insert SUAllowsAutomaticUpdates -bool NO "$bundle/Contents/Info.plist"
47fi
48if [[ -n "${INKSPRING_MAC_BUILD_NUMBER:-}" ]]; then
49 plutil -replace CFBundleVersion -string "$INKSPRING_MAC_BUILD_NUMBER" "$bundle/Contents/Info.plist"
50fi
51cp "$project/com.protomota.inkspring.native.daily-backup.plist" "$bundle/Contents/Library/LaunchAgents/"
52cp "$project/Resources/models.json" "$bundle/Contents/Resources/models.json"
53cp "$project/../build/icon.icns" "$bundle/Contents/Resources/Inkspring.icns"
54cp "$project/iPhone/Assets.xcassets/ProjectAtomLogo.imageset/project-atom-logo.png" "$bundle/Contents/Resources/"
55cp -R "$project/Resources/PlatformIcons" "$bundle/Contents/Resources/"
56cp "$project/../build/installer-background.tiff" "$bundle/Contents/Resources/"
57cp -R "$project/Resources/Fonts" "$bundle/Contents/Resources/Fonts"
58signature_options=(--force)
59embedded_profile="$bundle/Contents/embedded.provisionprofile"
60if [[ -n "${INKSPRING_MAC_ICLOUD_PROFILE:-}" ]]; then
61 if [[ -z "${INKSPRING_MAC_SIGNING_IDENTITY:-}" ]]; then
62 echo "Set INKSPRING_MAC_SIGNING_IDENTITY for the iCloud-provisioned build." >&2
63 exit 2
64 fi
65 cp "$INKSPRING_MAC_ICLOUD_PROFILE" "$embedded_profile"
66 signature_options+=(--entitlements "${INKSPRING_MAC_ICLOUD_ENTITLEMENTS:-$project/macOSCloudKit.entitlements}")
67fi
68if [[ -n "${INKSPRING_MAC_SIGNING_IDENTITY:-}" ]]; then
69 sparkle_sign=(--force --sign "$INKSPRING_MAC_SIGNING_IDENTITY" --options runtime --timestamp
70 --keychain "$HOME/Library/Keychains/login.keychain-db")
71 sparkle_version="$sparkle/Versions/Current"
72 codesign "${sparkle_sign[@]}" "$sparkle_version/Autoupdate"
73 codesign "${sparkle_sign[@]}" "$sparkle_version/Updater.app"
74 for service in "$sparkle_version"/XPCServices/*.xpc; do
75 codesign "${sparkle_sign[@]}" "$service"
76 done
77 codesign "${sparkle_sign[@]}" "$sparkle"
78 codesign --sign "$INKSPRING_MAC_SIGNING_IDENTITY" --options runtime --timestamp \
79 --keychain "$HOME/Library/Keychains/login.keychain-db" "${signature_options[@]}" "$bundle"
80else
81 codesign --force --sign - "$bundle"
82fi
83codesign --verify --strict --deep "$bundle"
84
85if [[ "${INKSPRING_MAC_NOTARIZE:-0}" == 1 ]]; then
86 : "${INKSPRING_MAC_SIGNING_IDENTITY:?Set the Developer ID signing identity for notarization.}"
87 : "${APPLE_API_KEY:?Set the App Store Connect private key path.}"
88 : "${APPLE_API_KEY_ID:?Set the App Store Connect key ID.}"
89 : "${APPLE_API_ISSUER:?Set the App Store Connect issuer ID.}"
90 credentials=(--key "$APPLE_API_KEY" --key-id "$APPLE_API_KEY_ID" --issuer "$APPLE_API_ISSUER")
91 archive="$staging/Inkspring.zip"
92 /usr/bin/ditto -c -k --keepParent "$bundle" "$archive"
93 xcrun notarytool submit "$archive" "${credentials[@]}" --output-format json > "$staging/submission.json"
94 submission="$(/usr/bin/plutil -extract id raw -o - "$staging/submission.json")"
95 printf 'Apple notarization submission %s\n' "$submission"
96 xcrun notarytool wait "$submission" "${credentials[@]}" --timeout 20m --output-format json > "$staging/result.json"
97 status="$(/usr/bin/plutil -extract status raw -o - "$staging/result.json")"
98 if [[ "$status" != Accepted ]]; then
99 xcrun notarytool log "$submission" "${credentials[@]}"
100 exit 1
101 fi
102 printf 'Apple notarization status: %s\n' "$status"
103 xcrun stapler staple "$bundle"
104 xcrun stapler validate "$bundle"
105 /usr/sbin/spctl --assess --type execute --verbose=2 "$bundle"
106fi
107
108# Swap the signed contents in one filesystem operation. The app path stays
109# registered and MCP and launchd can keep opening the previous build until then.
110if [[ -L "$installed_bundle" || -L "$installed_bundle/Contents" ]]; then
111 echo "Choose an output directory without an Inkspring app symlink." >&2
112 exit 1
113fi
114xcrun clang -isysroot "$sdk" -x c -o "$staging/install-bundle" - <<'C'
115#include <stdio.h>
116int main(int argc, char **argv) {
117 if (argc != 4) return 2;
118 unsigned flags = argv[3][0] == 's' ? RENAME_SWAP : RENAME_EXCL;
119 if (renamex_np(argv[1], argv[2], flags) == 0) return 0;
120 perror("Could not install the signed Inkspring build");
121 return 1;
122}
123C
124if [[ -d "$installed_bundle/Contents" ]]; then
125 "$staging/install-bundle" "$bundle/Contents" "$installed_bundle/Contents" swap
126else
127 "$staging/install-bundle" "$bundle" "$installed_bundle" install
128fi
129if [[ "${INKSPRING_MAC_NOTARIZE:-0}" == 1 ]]; then
130 xcrun stapler validate "$installed_bundle"
131 /usr/sbin/spctl --assess --type execute --verbose=2 "$installed_bundle"
132fi
133printf '%s\n' "$installed_bundle"