scripts/build-app.sh

ecedad8fac1deba0bacd11a5a65f20ba1659029d
orgstar/scripts/build-app.sh

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