OpenTalon / app · opentalon.ai / native / build.sh
| 1 | #!/bin/bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | source_dir="$(cd "$(dirname "$0")" && pwd)" |
| 5 | repo="$(dirname "$source_dir")" |
| 6 | output="${OPENTALON_NATIVE_OUTPUT:-/Volumes/Expand/Projects/OpenTalon/native-app/native}" |
| 7 | configuration="${OPENTALON_NATIVE_CONFIGURATION:-release}" |
| 8 | signing="${OPENTALON_NATIVE_SIGNING:-developer-id}" |
| 9 | notarize="${OPENTALON_NATIVE_NOTARIZE:-0}" |
| 10 | bundle_version="${OPENTALON_NATIVE_BUILD_NUMBER:-}" |
| 11 | update_feed="${OPENTALON_NATIVE_UPDATE_FEED:-}" |
| 12 | icloud_profile="${OPENTALON_NATIVE_ICLOUD_PROFILE:-}" |
| 13 | previous_profile="$output/OpenTalon.app/Contents/embedded.provisionprofile" |
| 14 | if [[ -z "$icloud_profile" && "$signing" == developer-id && -f "$previous_profile" ]]; then |
| 15 | icloud_profile="$previous_profile" |
| 16 | fi |
| 17 | version=25.9.0 |
| 18 | arch="$(uname -m)" |
| 19 | case "$arch" in |
| 20 | arm64) runtime_arch=arm64; checksum=e479f3c469d3d9303a44f00a8ea37a3788395d171bb8059c48a4bbbd2e371b59 ;; |
| 21 | *) echo "This local build script currently packages Apple Silicon." >&2; exit 1 ;; |
| 22 | esac |
| 23 | if [[ "$output" == /Volumes/Expand/* && ! -d /Volumes/Expand ]]; then |
| 24 | echo "Mount Expand before building." >&2 |
| 25 | exit 1 |
| 26 | fi |
| 27 | case "$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 | ;; |
| 52 | esac |
| 53 | if [[ "$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 |
| 57 | fi |
| 58 | command -v magick >/dev/null || { echo "ImageMagick is required to prepare the app artwork." >&2; exit 1; } |
| 59 | mkdir -p "$output"/{runtime,tmp,cache,build} |
| 60 | export TMPDIR="$output/tmp" |
| 61 | export CLANG_MODULE_CACHE_PATH="$output/cache/clang" |
| 62 | export SWIFTPM_MODULECACHE_OVERRIDE="$output/cache/swift" |
| 63 | runtime="node-v$version-darwin-$runtime_arch" |
| 64 | archive="$output/runtime/$runtime.tar.gz" |
| 65 | if [[ ! -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" |
| 69 | fi |
| 70 | printf '%s %s\n' "$checksum" "$archive" | shasum -a 256 -c - |
| 71 | tar -xzf "$archive" -C "$output/runtime" "$runtime/bin/node" "$runtime/LICENSE" |
| 72 | |
| 73 | swift build --package-path "$source_dir" --scratch-path "$output/build" \ |
| 74 | --cache-path "$output/cache" --disable-dependency-cache -c "$configuration" |
| 75 | binary_dir="$(swift build --package-path "$source_dir" --scratch-path "$output/build" \ |
| 76 | --cache-path "$output/cache" --disable-dependency-cache -c "$configuration" --show-bin-path)" |
| 77 | staging="$(mktemp -d "$output/.bundle.XXXXXX")" |
| 78 | trap 'rm -rf "$staging"' EXIT |
| 79 | bundle="$staging/OpenTalon.app" |
| 80 | contents="$bundle/Contents" |
| 81 | resources="$contents/Resources" |
| 82 | mkdir -p "$contents/Library/LaunchAgents" "$contents/MacOS" "$contents/Helpers" "$resources/service" "$resources/assets" |
| 83 | install -m 755 "$binary_dir/OpenTalon" "$contents/MacOS/OpenTalon" |
| 84 | install -m 755 "$output/runtime/$runtime/bin/node" "$contents/Helpers/node" |
| 85 | install -m 644 "$output/runtime/$runtime/LICENSE" "$resources/Node-LICENSE" |
| 86 | install -m 644 "$source_dir/Info.plist" "$contents/Info.plist" |
| 87 | if [[ -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" |
| 90 | fi |
| 91 | if [[ "$signing" == adhoc ]]; then |
| 92 | /usr/libexec/PlistBuddy -c "Delete SUFeedURL" "$contents/Info.plist" |
| 93 | elif [[ -n "$update_feed" ]]; then |
| 94 | /usr/libexec/PlistBuddy -c "Set SUFeedURL $update_feed" "$contents/Info.plist" |
| 95 | fi |
| 96 | sparkle="$(find "$output/build/artifacts" -type d -path '*/macos-arm64_x86_64/Sparkle.framework' -print -quit)" |
| 97 | if [[ -z "$sparkle" ]]; then |
| 98 | echo "The resolved Sparkle framework was not found." >&2 |
| 99 | exit 1 |
| 100 | fi |
| 101 | framework="$contents/Frameworks/Sparkle.framework" |
| 102 | mkdir -p "$contents/Frameworks" |
| 103 | /usr/bin/ditto "$sparkle" "$framework" |
| 104 | install -m 644 "$repo/assets/opentalon.icns" "$resources/opentalon.icns" |
| 105 | install -m 644 "$source_dir/assets/installer-background.tiff" "$resources/installer-background.tiff" |
| 106 | |
| 107 | install -m 644 "$source_dir/assets/opentalon-logo-1024.png" "$resources/assets/opentalon-logo-1024.png" |
| 108 | install -m 644 "$repo/assets/claude-mark.png" "$repo/assets/openai-mark.png" "$repo/assets/gemini-mark.png" "$resources/" |
| 109 | magick "$repo/assets/opentalon-wordmark.png" -resize 720x "$resources/wordmark.png" |
| 110 | for 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" |
| 112 | done |
| 113 | for module in service daily-backup backup-once; do |
| 114 | install -m 644 "$source_dir/$module.cjs" "$resources/service/$module.cjs" |
| 115 | done |
| 116 | install -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" |
| 119 | for helper in Autoupdate Updater.app XPCServices/Downloader.xpc XPCServices/Installer.xpc; do |
| 120 | /usr/bin/codesign --force "${signature[@]}" "$framework/Versions/Current/$helper" |
| 121 | done |
| 122 | /usr/bin/codesign --force "${signature[@]}" "$framework" |
| 123 | if [[ -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}") |
| 130 | fi |
| 131 | /usr/bin/codesign --force "${signature[@]}" "$bundle" |
| 132 | /usr/bin/codesign --verify --deep --strict "$bundle" |
| 133 | if [[ "$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" |
| 147 | fi |
| 148 | # Swap only a previously built artifact under this output directory. |
| 149 | if [[ -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 |
| 155 | else |
| 156 | mv "$bundle" "$output/OpenTalon.app" |
| 157 | fi |
| 158 | if [[ "$notarize" == 1 ]]; then |
| 159 | install -m 644 "$staging/notarization.json" "$output/notarization.json" |
| 160 | fi |
| 161 | printf 'Built %s\n' "$output/OpenTalon.app" |