scripts/build-app.sh

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