scripts/build-app.sh

06941b2cf25c6ccc1629723cd7951910ce15e67a
orgstar/scripts/build-app.sh history · blame · raw

233 lines · 9410 bytes · executable

  1#!/bin/sh
  2# Builds orgstar.app in .build/app from the release binary. The icon is drawn by
  3# scripts/make-icon.swift.
  4# Usage: scripts/build-app.sh [version] [build]
  5#   ORGSTAR_SIGN   signing identity; ad hoc ("-") for local use by default. Any other
  6#                  identity also turns on the hardened runtime and a secure timestamp.
  7#   ORGSTAR_ARCHS  architectures, e.g. "arm64 x86_64" for a universal app; this Mac's by default.
  8set -eu
  9
 10cd "$(dirname "$0")/.."
 11version="${1:-0.1.0}"
 12build="${2:-$version}"
 13sign="${ORGSTAR_SIGN:--}"
 14app=".build/app/orgstar.app"
 15arch_flags=""
 16for arch in ${ORGSTAR_ARCHS:-$(uname -m)}; do arch_flags="$arch_flags --arch $arch"; done
 17if [ "$sign" = "-" ]; then
 18    sign_flags=""
 19else
 20    sign_flags="--options runtime --timestamp"
 21fi
 22
 23# shellcheck disable=SC2086
 24swift build -c release $arch_flags --product Orgstar
 25# shellcheck disable=SC2086
 26swift build -c release $arch_flags --product OrgstarQuickLook
 27# shellcheck disable=SC2086
 28swift build -c release $arch_flags --product OrgstarSpotlight
 29# shellcheck disable=SC2086
 30binary="$(swift build -c release $arch_flags --show-bin-path)/Orgstar"
 31
 32rm -rf "$app"
 33mkdir -p "$app/Contents/MacOS" "$app/Contents/Resources" "$app/Contents/Frameworks"
 34cp "$binary" "$app/Contents/MacOS/Orgstar"
 35
 36# Sparkle, from its SwiftPM artifact: the framework beside the binary, found through an rpath.
 37sparkle="$(find .build/artifacts -path '*Sparkle.xcframework/macos-*/Sparkle.framework' -maxdepth 7 | head -1)"
 38[ -n "$sparkle" ] || { echo "Sparkle.framework not in .build/artifacts; swift build fetches it." >&2; exit 1; }
 39ditto "$sparkle" "$app/Contents/Frameworks/Sparkle.framework"
 40otool -l "$app/Contents/MacOS/Orgstar" | grep -q '@executable_path/../Frameworks' \
 41    || install_name_tool -add_rpath @executable_path/../Frameworks "$app/Contents/MacOS/Orgstar"
 42
 43# SwiftMath's math fonts, which it finds in the app's resources.
 44cp -R "$(dirname "$binary")/SwiftMath_SwiftMath.bundle" "$app/Contents/Resources/"
 45
 46# Updates: the appcast and its signing key, for signed builds only; a dev build never checks.
 47if [ "$sign" != "-" ]; then
 48    feed_keys="    <key>SUFeedURL</key>
 49    <string>https://krz.gitbay.site/orgstar/appcast.xml</string>
 50    <key>SUPublicEDKey</key>
 51    <string>${ORGSTAR_ED_PUBLIC_KEY:?set ORGSTAR_ED_PUBLIC_KEY to the key generate_keys printed}</string>"
 52else
 53    feed_keys=""
 54fi
 55iconset="$(mktemp -d)/AppIcon.iconset"
 56swift scripts/make-icon.swift "$iconset"
 57iconutil -c icns "$iconset" -o "$app/Contents/Resources/AppIcon.icns"
 58rm -rf "$(dirname "$iconset")"
 59
 60cat > "$app/Contents/Info.plist" <<EOF
 61<?xml version="1.0" encoding="UTF-8"?>
 62<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 63<plist version="1.0">
 64<dict>
 65    <key>CFBundleIdentifier</key>
 66    <string>sh.krz.orgstar</string>
 67    <key>CFBundleName</key>
 68    <string>orgstar</string>
 69    <key>CFBundleDisplayName</key>
 70    <string>orgstar</string>
 71    <key>CFBundleExecutable</key>
 72    <string>Orgstar</string>
 73    <key>CFBundlePackageType</key>
 74    <string>APPL</string>
 75    <key>CFBundleIconFile</key>
 76    <string>AppIcon</string>
 77    <key>UTImportedTypeDeclarations</key>
 78    <array>
 79        <dict>
 80            <key>UTTypeIdentifier</key>
 81            <string>org.orgmode.org</string>
 82            <key>UTTypeDescription</key>
 83            <string>Org document</string>
 84            <key>UTTypeConformsTo</key>
 85            <array><string>public.plain-text</string></array>
 86            <key>UTTypeTagSpecification</key>
 87            <dict>
 88                <key>public.filename-extension</key>
 89                <array><string>org</string><string>org_archive</string></array>
 90            </dict>
 91        </dict>
 92    </array>
 93    <key>CFBundleDocumentTypes</key>
 94    <array>
 95        <dict>
 96            <key>CFBundleTypeName</key>
 97            <string>Org document</string>
 98            <key>CFBundleTypeRole</key>
 99            <string>Editor</string>
100            <key>LSHandlerRank</key>
101            <string>Default</string>
102            <key>LSItemContentTypes</key>
103            <array><string>org.orgmode.org</string></array>
104        </dict>
105        <dict>
106            <key>CFBundleTypeName</key>
107            <string>Text</string>
108            <key>CFBundleTypeRole</key>
109            <string>Editor</string>
110            <key>LSHandlerRank</key>
111            <string>Alternate</string>
112            <key>LSItemContentTypes</key>
113            <array><string>public.plain-text</string></array>
114        </dict>
115    </array>
116    <key>CFBundleURLTypes</key>
117    <array>
118        <dict>
119            <key>CFBundleURLName</key>
120            <string>org-protocol</string>
121            <key>CFBundleURLSchemes</key>
122            <array><string>org-protocol</string></array>
123        </dict>
124    </array>
125    <key>CFBundleShortVersionString</key>
126    <string>${version}</string>
127    <key>CFBundleVersion</key>
128    <string>${build}</string>
129    <key>LSMinimumSystemVersion</key>
130    <string>26.0</string>
131${feed_keys}
132    <key>LSApplicationCategoryType</key>
133    <string>public.app-category.productivity</string>
134    <key>NSCalendarsFullAccessUsageDescription</key>
135    <string>The agenda shows your calendar events alongside your Org entries. orgstar only reads them.</string>
136    <key>NSHighResolutionCapable</key>
137    <true/>
138    <key>NSPrincipalClass</key>
139    <string>NSApplication</string>
140</dict>
141</plist>
142EOF
143
144# App Intents metadata for Shortcuts, from the constant values Swift Build extracted; SwiftPM
145# doesn't run this step itself.
146toolchain="$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain"
147objects=".build/out/Intermediates.noindex/Orgstar.build/Release/Orgstar-p.build/Objects-normal/$(uname -m)"
148work="$(mktemp -d)"
149echo "$PWD/$objects/Orgstar-primary.swiftconstvalues" > "$work/constvalues.txt"
150"$toolchain/usr/bin/appintentsmetadataprocessor" --toolchain-dir "$toolchain" --module-name Orgstar \
151    --sdk-root "$(xcrun --sdk macosx --show-sdk-path)" --xcode-version "$(xcodebuild -version | awk '/Build version/ {print $3}')" \
152    --platform-family macOS --deployment-target 26.0 --bundle-identifier sh.krz.orgstar \
153    --target-triple "$(uname -m)-apple-macos26.0" --binary-file "$binary" --source-file-list "$objects/Orgstar.SwiftFileList" \
154    --swift-const-vals-list "$work/constvalues.txt" --output "$app/Contents/Resources" --force > /dev/null 2>&1
155rm -rf "$work"
156
157# App extensions: Quick Look previews and the Spotlight importer. They run sandboxed.
158appex() { # name, extension point, principal class, attributes
159    dir="$app/Contents/PlugIns/$1.appex/Contents"
160    mkdir -p "$dir/MacOS"
161    cp "$(dirname "$binary")/$1" "$dir/MacOS/$1"
162    cat > "$dir/Info.plist" <<PLIST
163<?xml version="1.0" encoding="UTF-8"?>
164<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
165<plist version="1.0">
166<dict>
167    <key>CFBundleIdentifier</key>
168    <string>sh.krz.orgstar.$(echo "$1" | sed 's/^Orgstar//' | tr 'A-Z' 'a-z')</string>
169    <key>CFBundleName</key>
170    <string>$1</string>
171    <key>CFBundleExecutable</key>
172    <string>$1</string>
173    <key>CFBundlePackageType</key>
174    <string>XPC!</string>
175    <key>CFBundleShortVersionString</key>
176    <string>${version}</string>
177    <key>CFBundleVersion</key>
178    <string>${build}</string>
179    <key>LSMinimumSystemVersion</key>
180    <string>26.0</string>
181    <key>NSExtension</key>
182    <dict>
183        <key>NSExtensionPointIdentifier</key>
184        <string>$2</string>
185        <key>NSExtensionPrincipalClass</key>
186        <string>$3</string>
187        <key>NSExtensionAttributes</key>
188        <dict>$4</dict>
189    </dict>
190</dict>
191</plist>
192PLIST
193    cat > "$work/sandbox.entitlements" <<PLIST
194<?xml version="1.0" encoding="UTF-8"?>
195<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
196<plist version="1.0">
197<dict>
198    <key>com.apple.security.app-sandbox</key>
199    <true/>
200</dict>
201</plist>
202PLIST
203    # shellcheck disable=SC2086
204    codesign --force --sign "$sign" $sign_flags --entitlements "$work/sandbox.entitlements" "$app/Contents/PlugIns/$1.appex"
205}
206work="$(mktemp -d)"
207appex OrgstarQuickLook com.apple.quicklook.preview PreviewProvider \
208    "<key>QLSupportedContentTypes</key><array><string>org.orgmode.org</string></array><key>QLIsDataBasedPreview</key><true/>"
209appex OrgstarSpotlight com.apple.spotlight.import ImportExtension \
210    "<key>CSSupportedContentTypes</key><array><string>org.orgmode.org</string></array>"
211
212# Sparkle's inner pieces are signed first, as its documentation has it, then the framework.
213fw="$app/Contents/Frameworks/Sparkle.framework/Versions/B"
214for item in "$fw/XPCServices/Installer.xpc" "$fw/XPCServices/Downloader.xpc" "$fw/Autoupdate" "$fw/Updater.app" "$app/Contents/Frameworks/Sparkle.framework"; do
215    # shellcheck disable=SC2086
216    codesign --force --sign "$sign" $sign_flags "$item"
217done
218
219# The hardened runtime needs Calendar access declared for the agenda's events.
220cat > "$work/app.entitlements" <<PLIST
221<?xml version="1.0" encoding="UTF-8"?>
222<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
223<plist version="1.0">
224<dict>
225    <key>com.apple.security.personal-information.calendars</key>
226    <true/>
227</dict>
228</plist>
229PLIST
230# shellcheck disable=SC2086
231codesign --force --sign "$sign" $sign_flags --entitlements "$work/app.entitlements" "$app"
232rm -rf "$work"
233echo "$app"