scripts/build-app.sh

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