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