scripts/build-app.sh

253 lines · 10749 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.8.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
 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
 60# The icon (macOS derives its dark and tinted styles), and the Dock icons Settings offers.
 61work="$(mktemp -d)"
 62xcrun actool packaging/AppIcons.xcassets --compile "$app/Contents/Resources" --platform macosx \
 63    --minimum-deployment-target 26.0 --app-icon AppIcon --output-partial-info-plist "$work/icon.plist" > /dev/null
 64rm -rf "$work"
 65mkdir -p "$app/Contents/Resources/IconPreviews"
 66cp packaging/IconPreviews/*.png "$app/Contents/Resources/IconPreviews/"
 67
 68cat > "$app/Contents/Info.plist" <<EOF
 69<?xml version="1.0" encoding="UTF-8"?>
 70<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 71<plist version="1.0">
 72<dict>
 73    <key>CFBundleIdentifier</key>
 74    <string>sh.krz.orgstar</string>
 75    <key>CFBundleDevelopmentRegion</key>
 76    <string>en</string>
 77    <key>CFBundleLocalizations</key>
 78    <array><string>en</string><string>de</string></array>
 79    <key>CFBundleName</key>
 80    <string>orgstar</string>
 81    <key>CFBundleDisplayName</key>
 82    <string>orgstar</string>
 83    <key>CFBundleExecutable</key>
 84    <string>Orgstar</string>
 85    <key>CFBundlePackageType</key>
 86    <string>APPL</string>
 87    <key>CFBundleIconName</key>
 88    <string>AppIcon</string>
 89    <key>UTImportedTypeDeclarations</key>
 90    <array>
 91        <dict>
 92            <key>UTTypeIdentifier</key>
 93            <string>org.orgmode.org</string>
 94            <key>UTTypeDescription</key>
 95            <string>Org document</string>
 96            <key>UTTypeConformsTo</key>
 97            <array><string>public.plain-text</string></array>
 98            <key>UTTypeTagSpecification</key>
 99            <dict>
100                <key>public.filename-extension</key>
101                <array><string>org</string><string>org_archive</string></array>
102            </dict>
103        </dict>
104    </array>
105    <key>CFBundleDocumentTypes</key>
106    <array>
107        <dict>
108            <key>CFBundleTypeName</key>
109            <string>Org document</string>
110            <key>CFBundleTypeRole</key>
111            <string>Editor</string>
112            <key>LSHandlerRank</key>
113            <string>Default</string>
114            <key>LSItemContentTypes</key>
115            <array><string>org.orgmode.org</string></array>
116        </dict>
117        <dict>
118            <key>CFBundleTypeName</key>
119            <string>Text</string>
120            <key>CFBundleTypeRole</key>
121            <string>Editor</string>
122            <key>LSHandlerRank</key>
123            <string>Alternate</string>
124            <key>LSItemContentTypes</key>
125            <array><string>public.plain-text</string></array>
126        </dict>
127    </array>
128    <key>CFBundleURLTypes</key>
129    <array>
130        <dict>
131            <key>CFBundleURLName</key>
132            <string>org-protocol</string>
133            <key>CFBundleURLSchemes</key>
134            <array><string>org-protocol</string><string>orgstar</string></array>
135        </dict>
136    </array>
137    <key>CFBundleShortVersionString</key>
138    <string>${version}</string>
139    <key>CFBundleVersion</key>
140    <string>${build}</string>
141    <key>LSMinimumSystemVersion</key>
142    <string>26.0</string>
143${feed_keys}
144    <key>LSApplicationCategoryType</key>
145    <string>public.app-category.productivity</string>
146    <key>NSCalendarsFullAccessUsageDescription</key>
147    <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>
148    <key>NSRemindersFullAccessUsageDescription</key>
149    <string>The agenda shows your reminders alongside your Org entries. orgstar only reads them.</string>
150    <key>NSHighResolutionCapable</key>
151    <true/>
152    <key>NSPrincipalClass</key>
153    <string>NSApplication</string>
154</dict>
155</plist>
156EOF
157
158# App Intents metadata for Shortcuts, from the constant values Swift Build extracted; SwiftPM
159# doesn't run this step itself.
160toolchain="$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain"
161objects=".build/out/Intermediates.noindex/Orgstar.build/Release/Orgstar-p.build/Objects-normal/$(uname -m)"
162work="$(mktemp -d)"
163echo "$PWD/$objects/Orgstar-primary.swiftconstvalues" > "$work/constvalues.txt"
164"$toolchain/usr/bin/appintentsmetadataprocessor" --toolchain-dir "$toolchain" --module-name Orgstar \
165    --sdk-root "$(xcrun --sdk macosx --show-sdk-path)" --xcode-version "$(xcodebuild -version | awk '/Build version/ {print $3}')" \
166    --platform-family macOS --deployment-target 26.0 --bundle-identifier sh.krz.orgstar \
167    --target-triple "$(uname -m)-apple-macos26.0" --binary-file "$binary" --source-file-list "$objects/Orgstar.SwiftFileList" \
168    --swift-const-vals-list "$work/constvalues.txt" --output "$app/Contents/Resources" --force > /dev/null 2>&1
169rm -rf "$work"
170
171# App extensions: Quick Look previews, the Spotlight importer and the widgets. They run
172# sandboxed.
173appex() { # name, extension point, principal class (or ""), attributes, extra entitlements
174    dir="$app/Contents/PlugIns/$1.appex/Contents"
175    mkdir -p "$dir/MacOS" "$dir/Resources"
176    cp "$(dirname "$binary")/$1" "$dir/MacOS/$1"
177    scripts/localize.sh "$dir/Resources"
178    cat > "$dir/Info.plist" <<PLIST
179<?xml version="1.0" encoding="UTF-8"?>
180<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
181<plist version="1.0">
182<dict>
183    <key>CFBundleIdentifier</key>
184    <string>sh.krz.orgstar.$(echo "$1" | sed 's/^Orgstar//' | tr 'A-Z' 'a-z')</string>
185    <key>CFBundleName</key>
186    <string>$1</string>
187    <key>CFBundleExecutable</key>
188    <string>$1</string>
189    <key>CFBundlePackageType</key>
190    <string>XPC!</string>
191    <key>CFBundleShortVersionString</key>
192    <string>${version}</string>
193    <key>CFBundleVersion</key>
194    <string>${build}</string>
195    <key>LSMinimumSystemVersion</key>
196    <string>26.0</string>
197    <key>NSExtension</key>
198    <dict>
199        <key>NSExtensionPointIdentifier</key>
200        <string>$2</string>
201$([ -n "$3" ] && printf '        <key>NSExtensionPrincipalClass</key>\n        <string>%s</string>\n' "$3")
202        <key>NSExtensionAttributes</key>
203        <dict>$4</dict>
204    </dict>
205</dict>
206</plist>
207PLIST
208    cat > "$work/sandbox.entitlements" <<PLIST
209<?xml version="1.0" encoding="UTF-8"?>
210<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
211<plist version="1.0">
212<dict>
213    <key>com.apple.security.app-sandbox</key>
214    <true/>
215${5:-}
216</dict>
217</plist>
218PLIST
219    # shellcheck disable=SC2086
220    codesign --force --sign "$sign" $sign_flags --entitlements "$work/sandbox.entitlements" "$app/Contents/PlugIns/$1.appex"
221}
222work="$(mktemp -d)"
223appex OrgstarQuickLook com.apple.quicklook.preview PreviewProvider \
224    "<key>QLSupportedContentTypes</key><array><string>org.orgmode.org</string></array><key>QLIsDataBasedPreview</key><true/>"
225appex OrgstarSpotlight com.apple.spotlight.import ImportExtension \
226    "<key>CSSupportedContentTypes</key><array><string>org.orgmode.org</string></array>"
227# The widgets read what the app writes in ~/Library/Application Support/Orgstar/Widgets.
228appex OrgstarWidgets com.apple.widgetkit-extension "" "" \
229    "    <key>com.apple.security.temporary-exception.files.home-relative-path.read-only</key>
230    <array><string>/Library/Application Support/Orgstar/Widgets/</string></array>"
231
232# Sparkle's inner pieces are signed first, as its documentation has it, then the framework.
233fw="$app/Contents/Frameworks/Sparkle.framework/Versions/B"
234for item in "$fw/XPCServices/Installer.xpc" "$fw/XPCServices/Downloader.xpc" "$fw/Autoupdate" "$fw/Updater.app" "$app/Contents/Frameworks/Sparkle.framework"; do
235    # shellcheck disable=SC2086
236    codesign --force --sign "$sign" $sign_flags "$item"
237done
238
239# The hardened runtime needs Calendar access declared for the agenda's events.
240cat > "$work/app.entitlements" <<PLIST
241<?xml version="1.0" encoding="UTF-8"?>
242<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
243<plist version="1.0">
244<dict>
245    <key>com.apple.security.personal-information.calendars</key>
246    <true/>
247</dict>
248</plist>
249PLIST
250# shellcheck disable=SC2086
251codesign --force --sign "$sign" $sign_flags --entitlements "$work/app.entitlements" "$app"
252rm -rf "$work"
253echo "$app"