Branchwell

OpenTalon / app · opentalon.ai / native / build.sh

1#!/bin/bash
2set -euo pipefail
3
4source_dir="$(cd "$(dirname "$0")" && pwd)"
5repo="$(dirname "$source_dir")"
6output="${OPENTALON_NATIVE_OUTPUT:-/Volumes/Expand/Projects/OpenTalon/native-app/native}"
7configuration="${OPENTALON_NATIVE_CONFIGURATION:-release}"
8signing="${OPENTALON_NATIVE_SIGNING:-developer-id}"
9notarize="${OPENTALON_NATIVE_NOTARIZE:-0}"
10bundle_version="${OPENTALON_NATIVE_BUILD_NUMBER:-}"
11update_feed="${OPENTALON_NATIVE_UPDATE_FEED:-}"
12icloud_profile="${OPENTALON_NATIVE_ICLOUD_PROFILE:-}"
13previous_profile="$output/OpenTalon.app/Contents/embedded.provisionprofile"
14if [[ -z "$icloud_profile" && "$signing" == developer-id && -f "$previous_profile" ]]; then
15 icloud_profile="$previous_profile"
16fi
17version=25.9.0
18arch="$(uname -m)"
19case "$arch" in
20 arm64) runtime_arch=arm64; checksum=e479f3c469d3d9303a44f00a8ea37a3788395d171bb8059c48a4bbbd2e371b59 ;;
21 *) echo "This local build script currently packages Apple Silicon." >&2; exit 1 ;;
22esac
23if [[ "$output" == /Volumes/Expand/* && ! -d /Volumes/Expand ]]; then
24 echo "Mount Expand before building." >&2
25 exit 1
26fi
27case "$signing" in
28 developer-id)
29 keychain="${CSC_KEYCHAIN:-$HOME/Library/Keychains/login.keychain-db}"
30 identity="${CSC_NAME:-Developer ID Application: Protomota, LLC (22HV69L32T)}"
31 if [[ ! -f "$keychain" || "$keychain" == /Library/Keychains/System.keychain ]]; then
32 echo "Select your login or dedicated user signing keychain. See docs/SIGNING.md." >&2
33 exit 1
34 fi
35 identities="$(/usr/bin/security find-identity -v -p codesigning "$keychain")"
36 fingerprint="$(awk -v identity="$identity" 'index($0, "\"" identity "\"") && !/CSSMERR_/ { print $2; exit }' <<< "$identities")"
37 if [[ -z "$fingerprint" ]]; then
38 echo "Developer ID signing identity not found in $keychain. See docs/SIGNING.md." >&2
39 exit 1
40 fi
41 signature=(--sign "$fingerprint" --keychain "$keychain" --timestamp --options runtime)
42 node_signature=("${signature[@]}" --entitlements "$repo/build/entitlements.mac.plist")
43 ;;
44 adhoc)
45 signature=(--sign -)
46 node_signature=("${signature[@]}")
47 ;;
48 *)
49 echo "Use developer-id or adhoc for OPENTALON_NATIVE_SIGNING." >&2
50 exit 1
51 ;;
52esac
53if [[ "$notarize" == 1 && ( "$signing" != developer-id || ! -f "${APPLE_API_KEY:-}" ||
54 -z "${APPLE_API_KEY_ID:-}" || -z "${APPLE_API_ISSUER:-}" ) ]]; then
55 echo "Notarization requires Developer ID and the App Store Connect API credentials. See docs/SIGNING.md." >&2
56 exit 1
57fi
58command -v magick >/dev/null || { echo "ImageMagick is required to prepare the app artwork." >&2; exit 1; }
59mkdir -p "$output"/{runtime,tmp,cache,build}
60export TMPDIR="$output/tmp"
61export CLANG_MODULE_CACHE_PATH="$output/cache/clang"
62export SWIFTPM_MODULECACHE_OVERRIDE="$output/cache/swift"
63runtime="node-v$version-darwin-$runtime_arch"
64archive="$output/runtime/$runtime.tar.gz"
65if [[ ! -f "$archive" ]]; then
66 curl --fail --silent --show-error --location --max-time 120 \
67 "https://nodejs.org/dist/v$version/$runtime.tar.gz" -o "$archive.partial"
68 mv "$archive.partial" "$archive"
69fi
70printf '%s %s\n' "$checksum" "$archive" | shasum -a 256 -c -
71tar -xzf "$archive" -C "$output/runtime" "$runtime/bin/node" "$runtime/LICENSE"
72
73swift build --package-path "$source_dir" --scratch-path "$output/build" \
74 --cache-path "$output/cache" --disable-dependency-cache -c "$configuration"
75binary_dir="$(swift build --package-path "$source_dir" --scratch-path "$output/build" \
76 --cache-path "$output/cache" --disable-dependency-cache -c "$configuration" --show-bin-path)"
77staging="$(mktemp -d "$output/.bundle.XXXXXX")"
78trap 'rm -rf "$staging"' EXIT
79bundle="$staging/OpenTalon.app"
80contents="$bundle/Contents"
81resources="$contents/Resources"
82mkdir -p "$contents/Library/LaunchAgents" "$contents/MacOS" "$contents/Helpers" "$resources/service" "$resources/assets"
83install -m 755 "$binary_dir/OpenTalon" "$contents/MacOS/OpenTalon"
84install -m 755 "$output/runtime/$runtime/bin/node" "$contents/Helpers/node"
85install -m 644 "$output/runtime/$runtime/LICENSE" "$resources/Node-LICENSE"
86install -m 644 "$source_dir/Info.plist" "$contents/Info.plist"
87if [[ -n "$bundle_version" ]]; then
88 [[ "$bundle_version" =~ ^[1-9][0-9]*$ ]] || { echo "Use a positive Mac build number." >&2; exit 1; }
89 /usr/libexec/PlistBuddy -c "Set CFBundleVersion $bundle_version" "$contents/Info.plist"
90fi
91if [[ "$signing" == adhoc ]]; then
92 /usr/libexec/PlistBuddy -c "Delete SUFeedURL" "$contents/Info.plist"
93elif [[ -n "$update_feed" ]]; then
94 /usr/libexec/PlistBuddy -c "Set SUFeedURL $update_feed" "$contents/Info.plist"
95fi
96sparkle="$(find "$output/build/artifacts" -type d -path '*/macos-arm64_x86_64/Sparkle.framework' -print -quit)"
97if [[ -z "$sparkle" ]]; then
98 echo "The resolved Sparkle framework was not found." >&2
99 exit 1
100fi
101framework="$contents/Frameworks/Sparkle.framework"
102mkdir -p "$contents/Frameworks"
103/usr/bin/ditto "$sparkle" "$framework"
104install -m 644 "$repo/assets/opentalon.icns" "$resources/opentalon.icns"
105install -m 644 "$source_dir/assets/installer-background.tiff" "$resources/installer-background.tiff"
106
107install -m 644 "$source_dir/assets/opentalon-logo-1024.png" "$resources/assets/opentalon-logo-1024.png"
108install -m 644 "$repo/assets/claude-mark.png" "$repo/assets/openai-mark.png" "$repo/assets/gemini-mark.png" "$resources/"
109magick "$repo/assets/opentalon-wordmark.png" -resize 720x "$resources/wordmark.png"
110for module in task-activity task-history task-files store contracts comments mcp mcp-bridge mcp-extension agent-readiness agent-runner agent-setup agent-models gemini-agent; do
111 install -m 644 "$source_dir/runtime/$module.cjs" "$resources/service/$module.cjs"
112done
113for module in service daily-backup backup-once; do
114 install -m 644 "$source_dir/$module.cjs" "$resources/service/$module.cjs"
115done
116install -m 644 "$source_dir/com.protomota.opentalon.native.daily-backup.plist" "$contents/Library/LaunchAgents/"
117
118/usr/bin/codesign --force "${node_signature[@]}" "$contents/Helpers/node"
119for helper in Autoupdate Updater.app XPCServices/Downloader.xpc XPCServices/Installer.xpc; do
120 /usr/bin/codesign --force "${signature[@]}" "$framework/Versions/Current/$helper"
121done
122/usr/bin/codesign --force "${signature[@]}" "$framework"
123if [[ -n "$icloud_profile" ]]; then
124 if [[ "$signing" != developer-id ]]; then
125 echo "Use Developer ID signing with the OpenTalon iCloud provisioning profile." >&2
126 exit 1
127 fi
128 install -m 644 "$icloud_profile" "$contents/embedded.provisionprofile"
129 signature+=(--entitlements "${OPENTALON_NATIVE_ICLOUD_ENTITLEMENTS:-$source_dir/CloudKit.entitlements}")
130fi
131/usr/bin/codesign --force "${signature[@]}" "$bundle"
132/usr/bin/codesign --verify --deep --strict "$bundle"
133if [[ "$notarize" == 1 ]]; then
134 /usr/bin/ditto -c -k --keepParent "$bundle" "$staging/OpenTalon.zip"
135 xcrun notarytool submit "$staging/OpenTalon.zip" --key "$APPLE_API_KEY" \
136 --key-id "$APPLE_API_KEY_ID" --issuer "$APPLE_API_ISSUER" --wait --timeout 20m \
137 --output-format json | tee "$staging/notarization.json"
138 status="$(/usr/bin/plutil -extract status raw -o - "$staging/notarization.json")"
139 if [[ "$status" != Accepted ]]; then
140 echo "Apple notarization did not succeed ($status)." >&2
141 exit 1
142 fi
143 xcrun stapler staple "$bundle"
144 xcrun stapler validate "$bundle"
145 /usr/bin/codesign --verify --deep --strict "$bundle"
146 /usr/sbin/spctl --assess --type execute --verbose=2 "$bundle"
147fi
148# Swap only a previously built artifact under this output directory.
149if [[ -e "$output/OpenTalon.app" ]]; then
150 mv "$output/OpenTalon.app" "$staging/previous.app"
151 if ! mv "$bundle" "$output/OpenTalon.app"; then
152 mv "$staging/previous.app" "$output/OpenTalon.app"
153 exit 1
154 fi
155else
156 mv "$bundle" "$output/OpenTalon.app"
157fi
158if [[ "$notarize" == 1 ]]; then
159 install -m 644 "$staging/notarization.json" "$output/notarization.json"
160fi
161printf 'Built %s\n' "$output/OpenTalon.app"