scripts/build-app.sh
280 lines · 12477 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.
8# ORGSTAR_PROFILE a Developer ID provisioning profile to embed, with its Keychain access group,
9# so passphrases can be kept in the data protection keychain. Needs ORGSTAR_SIGN.
10set -eu
11
12cd "$(dirname "$0")/.."
13version="${1:-1.3.1}"
14build="${2:-$version}"
15sign="${ORGSTAR_SIGN:--}"
16app=".build/app/orgstar.app"
17arch_flags=""
18for arch in ${ORGSTAR_ARCHS:-$(uname -m)}; do arch_flags="$arch_flags --arch $arch"; done
19if [ "$sign" = "-" ]; then
20 sign_flags=""
21else
22 sign_flags="--options runtime --timestamp"
23fi
24
25# shellcheck disable=SC2086
26swift build -c release $arch_flags --product Orgstar
27# shellcheck disable=SC2086
28swift build -c release $arch_flags --product OrgstarQuickLook
29# shellcheck disable=SC2086
30swift build -c release $arch_flags --product OrgstarSpotlight
31# shellcheck disable=SC2086
32swift build -c release $arch_flags --product OrgstarWidgets
33# shellcheck disable=SC2086
34binary="$(swift build -c release $arch_flags --show-bin-path)/Orgstar"
35
36rm -rf "$app" .build/app/dSYMs
37mkdir -p "$app/Contents/MacOS" "$app/Contents/Resources" "$app/Contents/Frameworks" .build/app/dSYMs
38cp "$binary" "$app/Contents/MacOS/Orgstar"
39# The build's debug symbols, for symbolicating crash reports; the executables ship stripped.
40for dsym in "$(dirname "$binary")"/*.dSYM; do ditto "$dsym" ".build/app/dSYMs/$(basename "$dsym")"; done
41
42# Sparkle, from its SwiftPM artifact: the framework beside the binary, found through an rpath.
43sparkle="$(find .build/artifacts -path '*Sparkle.xcframework/macos-*/Sparkle.framework' -maxdepth 7 | head -1)"
44[ -n "$sparkle" ] || { echo "Sparkle.framework not in .build/artifacts; swift build fetches it." >&2; exit 1; }
45ditto "$sparkle" "$app/Contents/Frameworks/Sparkle.framework"
46otool -l "$app/Contents/MacOS/Orgstar" | grep -q '@executable_path/../Frameworks' \
47 || install_name_tool -add_rpath @executable_path/../Frameworks "$app/Contents/MacOS/Orgstar"
48strip -x "$app/Contents/MacOS/Orgstar"
49
50# SwiftMath's math fonts, which it finds in the app's resources. LatexPreview uses only the
51# default, Latin Modern.
52cp -R "$(dirname "$binary")/SwiftMath_SwiftMath.bundle" "$app/Contents/Resources/"
53find "$app/Contents/Resources/SwiftMath_SwiftMath.bundle" -path '*/mathFonts.bundle/*' \
54 \( -name '*.otf' -o -name '*.plist' \) ! -name 'latinmodern-math.*' -delete
55# MathJax, for PDF export.
56cp -R "$(dirname "$binary")/Orgstar_OrgMathJax.bundle" "$app/Contents/Resources/"
57
58# Updates: the appcast and its signing key, for signed builds only; a dev build never checks.
59if [ "$sign" != "-" ]; then
60 feed_keys=" <key>SUFeedURL</key>
61 <string>https://files.krz.sh/orgstar/appcast.xml</string>
62 <key>SUPublicEDKey</key>
63 <string>${ORGSTAR_ED_PUBLIC_KEY:?set ORGSTAR_ED_PUBLIC_KEY to the key generate_keys printed}</string>"
64else
65 feed_keys=""
66fi
67# The UI's translations, from l10n/Localizable.xcstrings and l10n/DeveloperID.xcstrings.
68scripts/localize.sh --developer-id "$app/Contents/Resources"
69
70# The icon (macOS derives its dark and tinted styles), and the Dock icons Settings offers.
71work="$(mktemp -d)"
72xcrun actool packaging/AppIcons.xcassets --compile "$app/Contents/Resources" --platform macosx \
73 --minimum-deployment-target 26.0 --app-icon AppIcon --output-partial-info-plist "$work/icon.plist" > /dev/null
74rm -rf "$work"
75mkdir -p "$app/Contents/Resources/IconPreviews"
76cp packaging/IconPreviews/*.png "$app/Contents/Resources/IconPreviews/"
77
78cat > "$app/Contents/Info.plist" <<EOF
79<?xml version="1.0" encoding="UTF-8"?>
80<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
81<plist version="1.0">
82<dict>
83 <key>CFBundleIdentifier</key>
84 <string>sh.krz.orgstar</string>
85 <key>CFBundleDevelopmentRegion</key>
86 <string>en</string>
87 <key>CFBundleLocalizations</key>
88 <array><string>en</string><string>de</string><string>fr</string><string>ja</string><string>es</string><string>pt-BR</string></array>
89 <key>CFBundleName</key>
90 <string>orgstar</string>
91 <key>CFBundleDisplayName</key>
92 <string>orgstar</string>
93 <key>CFBundleExecutable</key>
94 <string>Orgstar</string>
95 <key>CFBundlePackageType</key>
96 <string>APPL</string>
97 <key>CFBundleIconName</key>
98 <string>AppIcon</string>
99 <key>UTImportedTypeDeclarations</key>
100 <array>
101 <dict>
102 <key>UTTypeIdentifier</key>
103 <string>org.orgmode.org</string>
104 <key>UTTypeDescription</key>
105 <string>Org document</string>
106 <key>UTTypeConformsTo</key>
107 <array><string>public.plain-text</string></array>
108 <key>UTTypeTagSpecification</key>
109 <dict>
110 <key>public.filename-extension</key>
111 <array><string>org</string><string>org_archive</string></array>
112 </dict>
113 </dict>
114 </array>
115 <key>CFBundleDocumentTypes</key>
116 <array>
117 <dict>
118 <key>CFBundleTypeName</key>
119 <string>Org document</string>
120 <key>CFBundleTypeRole</key>
121 <string>Editor</string>
122 <key>LSHandlerRank</key>
123 <string>Default</string>
124 <key>LSItemContentTypes</key>
125 <array><string>org.orgmode.org</string></array>
126 </dict>
127 <dict>
128 <key>CFBundleTypeName</key>
129 <string>Text</string>
130 <key>CFBundleTypeRole</key>
131 <string>Editor</string>
132 <key>LSHandlerRank</key>
133 <string>Alternate</string>
134 <key>LSItemContentTypes</key>
135 <array><string>public.plain-text</string></array>
136 </dict>
137 </array>
138 <key>CFBundleURLTypes</key>
139 <array>
140 <dict>
141 <key>CFBundleURLName</key>
142 <string>org-protocol</string>
143 <key>CFBundleURLSchemes</key>
144 <array><string>org-protocol</string><string>orgstar</string></array>
145 </dict>
146 </array>
147 <key>CFBundleShortVersionString</key>
148 <string>${version}</string>
149 <key>CFBundleVersion</key>
150 <string>${build}</string>
151 <key>LSMinimumSystemVersion</key>
152 <string>26.0</string>
153${feed_keys}
154 <key>LSApplicationCategoryType</key>
155 <string>public.app-category.productivity</string>
156 <key>NSCalendarsFullAccessUsageDescription</key>
157 <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>
158 <key>NSRemindersFullAccessUsageDescription</key>
159 <string>The agenda shows your reminders alongside your Org entries. orgstar only reads them.</string>
160 <key>NSHighResolutionCapable</key>
161 <true/>
162 <key>NSPrincipalClass</key>
163 <string>NSApplication</string>
164 <key>NSUserActivityTypes</key>
165 <array><string>sh.krz.orgstar.editing</string></array>
166</dict>
167</plist>
168EOF
169
170# App Intents metadata for Shortcuts, from the constant values Swift Build extracted; SwiftPM
171# doesn't run this step itself.
172toolchain="$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain"
173objects=".build/out/Intermediates.noindex/Orgstar.build/Release/Orgstar-p.build/Objects-normal/$(uname -m)"
174work="$(mktemp -d)"
175echo "$PWD/$objects/Orgstar-primary.swiftconstvalues" > "$work/constvalues.txt"
176"$toolchain/usr/bin/appintentsmetadataprocessor" --toolchain-dir "$toolchain" --module-name Orgstar \
177 --sdk-root "$(xcrun --sdk macosx --show-sdk-path)" --xcode-version "$(xcodebuild -version | awk '/Build version/ {print $3}')" \
178 --platform-family macOS --deployment-target 26.0 --bundle-identifier sh.krz.orgstar \
179 --target-triple "$(uname -m)-apple-macos26.0" --binary-file "$binary" --source-file-list "$objects/Orgstar.SwiftFileList" \
180 --swift-const-vals-list "$work/constvalues.txt" --output "$app/Contents/Resources" --force > /dev/null 2>&1
181rm -rf "$work"
182
183# App extensions: Quick Look previews, the Spotlight importer and the widgets. They run
184# sandboxed.
185appex() { # name, extension point, principal class (or ""), attributes, extra entitlements
186 dir="$app/Contents/PlugIns/$1.appex/Contents"
187 mkdir -p "$dir/MacOS" "$dir/Resources"
188 cp "$(dirname "$binary")/$1" "$dir/MacOS/$1"
189 strip -x "$dir/MacOS/$1"
190 scripts/localize.sh "$dir/Resources"
191 cat > "$dir/Info.plist" <<PLIST
192<?xml version="1.0" encoding="UTF-8"?>
193<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
194<plist version="1.0">
195<dict>
196 <key>CFBundleIdentifier</key>
197 <string>sh.krz.orgstar.$(echo "$1" | sed 's/^Orgstar//' | tr 'A-Z' 'a-z')</string>
198 <key>CFBundleName</key>
199 <string>$1</string>
200 <key>CFBundleExecutable</key>
201 <string>$1</string>
202 <key>CFBundlePackageType</key>
203 <string>XPC!</string>
204 <key>CFBundleShortVersionString</key>
205 <string>${version}</string>
206 <key>CFBundleVersion</key>
207 <string>${build}</string>
208 <key>LSMinimumSystemVersion</key>
209 <string>26.0</string>
210 <key>NSExtension</key>
211 <dict>
212 <key>NSExtensionPointIdentifier</key>
213 <string>$2</string>
214$([ -n "$3" ] && printf ' <key>NSExtensionPrincipalClass</key>\n <string>%s</string>\n' "$3")
215 <key>NSExtensionAttributes</key>
216 <dict>$4</dict>
217 </dict>
218</dict>
219</plist>
220PLIST
221 cat > "$work/sandbox.entitlements" <<PLIST
222<?xml version="1.0" encoding="UTF-8"?>
223<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
224<plist version="1.0">
225<dict>
226 <key>com.apple.security.app-sandbox</key>
227 <true/>
228${5:-}
229</dict>
230</plist>
231PLIST
232 # shellcheck disable=SC2086
233 codesign --force --sign "$sign" $sign_flags --entitlements "$work/sandbox.entitlements" "$app/Contents/PlugIns/$1.appex"
234}
235work="$(mktemp -d)"
236appex OrgstarQuickLook com.apple.quicklook.preview PreviewProvider \
237 "<key>QLSupportedContentTypes</key><array><string>org.orgmode.org</string></array><key>QLIsDataBasedPreview</key><true/>"
238appex OrgstarSpotlight com.apple.spotlight.import ImportExtension \
239 "<key>CSSupportedContentTypes</key><array><string>org.orgmode.org</string></array>"
240# The widgets read what the app writes in ~/Library/Application Support/Orgstar/Widgets.
241appex OrgstarWidgets com.apple.widgetkit-extension "" "" \
242 " <key>com.apple.security.temporary-exception.files.home-relative-path.read-only</key>
243 <array><string>/Library/Application Support/Orgstar/Widgets/</string></array>"
244
245# Sparkle's inner pieces are signed first, as its documentation has it, then the framework.
246fw="$app/Contents/Frameworks/Sparkle.framework/Versions/B"
247for item in "$fw/XPCServices/Installer.xpc" "$fw/XPCServices/Downloader.xpc" "$fw/Autoupdate" "$fw/Updater.app" "$app/Contents/Frameworks/Sparkle.framework"; do
248 # shellcheck disable=SC2086
249 codesign --force --sign "$sign" $sign_flags "$item"
250done
251
252# The hardened runtime needs Calendar access declared for the agenda's events.
253keychain=""
254if [ -n "${ORGSTAR_PROFILE:-}" ]; then
255 [ "$sign" != "-" ] || { echo "ORGSTAR_PROFILE needs ORGSTAR_SIGN." >&2; exit 1; }
256 cp "$ORGSTAR_PROFILE" "$app/Contents/embedded.provisionprofile"
257 security cms -D -i "$ORGSTAR_PROFILE" -o "$work/profile.plist"
258 team="$(/usr/libexec/PlistBuddy -c 'Print :Entitlements:com.apple.developer.team-identifier' "$work/profile.plist")"
259 keychain="
260 <key>com.apple.application-identifier</key>
261 <string>$team.sh.krz.orgstar</string>
262 <key>com.apple.developer.team-identifier</key>
263 <string>$team</string>
264 <key>keychain-access-groups</key>
265 <array><string>$team.sh.krz.orgstar</string></array>"
266fi
267cat > "$work/app.entitlements" <<PLIST
268<?xml version="1.0" encoding="UTF-8"?>
269<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
270<plist version="1.0">
271<dict>
272 <key>com.apple.security.personal-information.calendars</key>
273 <true/>$keychain
274</dict>
275</plist>
276PLIST
277# shellcheck disable=SC2086
278codesign --force --sign "$sign" $sign_flags --entitlements "$work/app.entitlements" "$app"
279rm -rf "$work"
280echo "$app"