scripts/build-app.sh

a15753a827b5ab7a0ccf3a0873890771c2979ee7
orgstar/scripts/build-app.sh

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

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form