scripts/build-app.sh

58f3b73e8ff337e5a15111fbb32c0d57cf2346da
orgstar/scripts/build-app.sh history · blame · raw

262 lines · 11501 bytes · executable

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