Inkspring / app · inkspring.ai / native-mac / build-app.sh
| 1 | #!/bin/bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | project="$(cd "$(dirname "$0")" && pwd)" |
| 5 | output="${1:-/Volumes/Expand/Projects/Inkspring/native-app}" |
| 6 | case "$output" in |
| 7 | /*) ;; |
| 8 | *) echo "Use an absolute output directory." >&2; exit 2 ;; |
| 9 | esac |
| 10 | if [[ "$output" == /Volumes/Expand/* && ! -d /Volumes/Expand ]]; then |
| 11 | echo "Mount Expand before building Inkspring." >&2 |
| 12 | exit 1 |
| 13 | fi |
| 14 | |
| 15 | mkdir -p "$output/tmp" |
| 16 | export TMPDIR="$output/tmp" |
| 17 | sdk="$(xcrun --sdk macosx --show-sdk-path)" |
| 18 | # Keep the real SDK version in the linked executable for macOS appearance selection. |
| 19 | configuration="${INKSPRING_MAC_CONFIGURATION:-release}" |
| 20 | case "$configuration" in |
| 21 | debug|release) ;; |
| 22 | *) echo "Use debug or release for INKSPRING_MAC_CONFIGURATION." >&2; exit 2 ;; |
| 23 | esac |
| 24 | build_flags=(--package-path "$project" --scratch-path "$output/swift-build" --build-system native --sdk "$sdk" --disable-sandbox -c "$configuration") |
| 25 | swift build "${build_flags[@]}" |
| 26 | binary="$(swift build "${build_flags[@]}" --show-bin-path)" |
| 27 | installed_bundle="$output/Inkspring.app" |
| 28 | staging="$(mktemp -d "$output/.inkspring-build.XXXXXXXX")" |
| 29 | trap 'rm -rf "$staging"' EXIT |
| 30 | bundle="$staging/Inkspring.app" |
| 31 | mkdir -p "$bundle/Contents/MacOS" "$bundle/Contents/Resources" "$bundle/Contents/Library/LaunchAgents" |
| 32 | mkdir -p "$bundle/Contents/Frameworks" |
| 33 | cp "$binary/Inkspring" "$bundle/Contents/MacOS/Inkspring" |
| 34 | cp "$project/Info.plist" "$bundle/Contents/Info.plist" |
| 35 | sparkle="$bundle/Contents/Frameworks/Sparkle.framework" |
| 36 | ditto "$binary/Sparkle.framework" "$sparkle" |
| 37 | if [[ -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" |
| 47 | fi |
| 48 | if [[ -n "${INKSPRING_MAC_BUILD_NUMBER:-}" ]]; then |
| 49 | plutil -replace CFBundleVersion -string "$INKSPRING_MAC_BUILD_NUMBER" "$bundle/Contents/Info.plist" |
| 50 | fi |
| 51 | cp "$project/com.protomota.inkspring.native.daily-backup.plist" "$bundle/Contents/Library/LaunchAgents/" |
| 52 | cp "$project/Resources/models.json" "$bundle/Contents/Resources/models.json" |
| 53 | cp "$project/../build/icon.icns" "$bundle/Contents/Resources/Inkspring.icns" |
| 54 | cp "$project/iPhone/Assets.xcassets/ProjectAtomLogo.imageset/project-atom-logo.png" "$bundle/Contents/Resources/" |
| 55 | cp -R "$project/Resources/PlatformIcons" "$bundle/Contents/Resources/" |
| 56 | cp "$project/../build/installer-background.tiff" "$bundle/Contents/Resources/" |
| 57 | cp -R "$project/Resources/Fonts" "$bundle/Contents/Resources/Fonts" |
| 58 | signature_options=(--force) |
| 59 | embedded_profile="$bundle/Contents/embedded.provisionprofile" |
| 60 | if [[ -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}") |
| 67 | fi |
| 68 | if [[ -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" |
| 80 | else |
| 81 | codesign --force --sign - "$bundle" |
| 82 | fi |
| 83 | codesign --verify --strict --deep "$bundle" |
| 84 | |
| 85 | if [[ "${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" |
| 106 | fi |
| 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. |
| 110 | if [[ -L "$installed_bundle" || -L "$installed_bundle/Contents" ]]; then |
| 111 | echo "Choose an output directory without an Inkspring app symlink." >&2 |
| 112 | exit 1 |
| 113 | fi |
| 114 | xcrun clang -isysroot "$sdk" -x c -o "$staging/install-bundle" - <<'C' |
| 115 | #include <stdio.h> |
| 116 | int 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 | } |
| 123 | C |
| 124 | if [[ -d "$installed_bundle/Contents" ]]; then |
| 125 | "$staging/install-bundle" "$bundle/Contents" "$installed_bundle/Contents" swap |
| 126 | else |
| 127 | "$staging/install-bundle" "$bundle" "$installed_bundle" install |
| 128 | fi |
| 129 | if [[ "${INKSPRING_MAC_NOTARIZE:-0}" == 1 ]]; then |
| 130 | xcrun stapler validate "$installed_bundle" |
| 131 | /usr/sbin/spctl --assess --type execute --verbose=2 "$installed_bundle" |
| 132 | fi |
| 133 | printf '%s\n' "$installed_bundle" |