scripts/build-app.sh
174 lines · 6514 bytes · executable
1#!/bin/sh
2# Builds Orgstar.app in .build/app from the release binary, signed ad hoc for local use. The
3# icon is drawn by scripts/make-icon.swift.
4# Usage: scripts/build-app.sh [version]
5set -eu
6
7cd "$(dirname "$0")/.."
8version="${1:-0.1.0}"
9app=".build/app/Orgstar.app"
10
11swift build -c release --product Orgstar
12swift build -c release --product OrgstarQuickLook
13swift build -c release --product OrgstarSpotlight
14binary="$(swift build -c release --show-bin-path)/Orgstar"
15
16rm -rf "$app"
17mkdir -p "$app/Contents/MacOS" "$app/Contents/Resources"
18cp "$binary" "$app/Contents/MacOS/Orgstar"
19iconset="$(mktemp -d)/AppIcon.iconset"
20swift scripts/make-icon.swift "$iconset"
21iconutil -c icns "$iconset" -o "$app/Contents/Resources/AppIcon.icns"
22rm -rf "$(dirname "$iconset")"
23
24cat > "$app/Contents/Info.plist" <<EOF
25<?xml version="1.0" encoding="UTF-8"?>
26<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
27<plist version="1.0">
28<dict>
29 <key>CFBundleIdentifier</key>
30 <string>sh.krz.orgstar</string>
31 <key>CFBundleName</key>
32 <string>Orgstar</string>
33 <key>CFBundleDisplayName</key>
34 <string>Orgstar</string>
35 <key>CFBundleExecutable</key>
36 <string>Orgstar</string>
37 <key>CFBundlePackageType</key>
38 <string>APPL</string>
39 <key>CFBundleIconFile</key>
40 <string>AppIcon</string>
41 <key>UTImportedTypeDeclarations</key>
42 <array>
43 <dict>
44 <key>UTTypeIdentifier</key>
45 <string>org.orgmode.org</string>
46 <key>UTTypeDescription</key>
47 <string>Org document</string>
48 <key>UTTypeConformsTo</key>
49 <array><string>public.plain-text</string></array>
50 <key>UTTypeTagSpecification</key>
51 <dict>
52 <key>public.filename-extension</key>
53 <array><string>org</string><string>org_archive</string></array>
54 </dict>
55 </dict>
56 </array>
57 <key>CFBundleDocumentTypes</key>
58 <array>
59 <dict>
60 <key>CFBundleTypeName</key>
61 <string>Org document</string>
62 <key>CFBundleTypeRole</key>
63 <string>Editor</string>
64 <key>LSHandlerRank</key>
65 <string>Default</string>
66 <key>LSItemContentTypes</key>
67 <array><string>org.orgmode.org</string></array>
68 </dict>
69 <dict>
70 <key>CFBundleTypeName</key>
71 <string>Text</string>
72 <key>CFBundleTypeRole</key>
73 <string>Editor</string>
74 <key>LSHandlerRank</key>
75 <string>Alternate</string>
76 <key>LSItemContentTypes</key>
77 <array><string>public.plain-text</string></array>
78 </dict>
79 </array>
80 <key>CFBundleURLTypes</key>
81 <array>
82 <dict>
83 <key>CFBundleURLName</key>
84 <string>org-protocol</string>
85 <key>CFBundleURLSchemes</key>
86 <array><string>org-protocol</string></array>
87 </dict>
88 </array>
89 <key>CFBundleShortVersionString</key>
90 <string>${version}</string>
91 <key>CFBundleVersion</key>
92 <string>${version}</string>
93 <key>LSMinimumSystemVersion</key>
94 <string>26.0</string>
95 <key>LSApplicationCategoryType</key>
96 <string>public.app-category.productivity</string>
97 <key>NSHighResolutionCapable</key>
98 <true/>
99 <key>NSPrincipalClass</key>
100 <string>NSApplication</string>
101</dict>
102</plist>
103EOF
104
105# App Intents metadata for Shortcuts, from the constant values Swift Build extracted; SwiftPM
106# doesn't run this step itself.
107toolchain="$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain"
108objects=".build/out/Intermediates.noindex/Orgstar.build/Release/Orgstar-p.build/Objects-normal/$(uname -m)"
109work="$(mktemp -d)"
110echo "$PWD/$objects/Orgstar-primary.swiftconstvalues" > "$work/constvalues.txt"
111"$toolchain/usr/bin/appintentsmetadataprocessor" --toolchain-dir "$toolchain" --module-name Orgstar \
112 --sdk-root "$(xcrun --sdk macosx --show-sdk-path)" --xcode-version "$(xcodebuild -version | awk '/Build version/ {print $3}')" \
113 --platform-family macOS --deployment-target 26.0 --bundle-identifier sh.krz.orgstar \
114 --target-triple "$(uname -m)-apple-macos26.0" --binary-file "$binary" --source-file-list "$objects/Orgstar.SwiftFileList" \
115 --swift-const-vals-list "$work/constvalues.txt" --output "$app/Contents/Resources" --force > /dev/null 2>&1
116rm -rf "$work"
117
118# App extensions: Quick Look previews and the Spotlight importer. They run sandboxed.
119appex() { # name, extension point, principal class, attributes
120 dir="$app/Contents/PlugIns/$1.appex/Contents"
121 mkdir -p "$dir/MacOS"
122 cp "$(dirname "$binary")/$1" "$dir/MacOS/$1"
123 cat > "$dir/Info.plist" <<PLIST
124<?xml version="1.0" encoding="UTF-8"?>
125<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
126<plist version="1.0">
127<dict>
128 <key>CFBundleIdentifier</key>
129 <string>sh.krz.orgstar.$(echo "$1" | sed 's/^Orgstar//' | tr 'A-Z' 'a-z')</string>
130 <key>CFBundleName</key>
131 <string>$1</string>
132 <key>CFBundleExecutable</key>
133 <string>$1</string>
134 <key>CFBundlePackageType</key>
135 <string>XPC!</string>
136 <key>CFBundleShortVersionString</key>
137 <string>${version}</string>
138 <key>CFBundleVersion</key>
139 <string>${version}</string>
140 <key>LSMinimumSystemVersion</key>
141 <string>26.0</string>
142 <key>NSExtension</key>
143 <dict>
144 <key>NSExtensionPointIdentifier</key>
145 <string>$2</string>
146 <key>NSExtensionPrincipalClass</key>
147 <string>$3</string>
148 <key>NSExtensionAttributes</key>
149 <dict>$4</dict>
150 </dict>
151</dict>
152</plist>
153PLIST
154 cat > "$work/sandbox.entitlements" <<PLIST
155<?xml version="1.0" encoding="UTF-8"?>
156<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
157<plist version="1.0">
158<dict>
159 <key>com.apple.security.app-sandbox</key>
160 <true/>
161</dict>
162</plist>
163PLIST
164 codesign --force --sign - --entitlements "$work/sandbox.entitlements" "$app/Contents/PlugIns/$1.appex"
165}
166work="$(mktemp -d)"
167appex OrgstarQuickLook com.apple.quicklook.preview PreviewProvider \
168 "<key>QLSupportedContentTypes</key><array><string>org.orgmode.org</string></array><key>QLIsDataBasedPreview</key><true/>"
169appex OrgstarSpotlight com.apple.spotlight.import ImportExtension \
170 "<key>CSSupportedContentTypes</key><array><string>org.orgmode.org</string></array>"
171rm -rf "$work"
172
173codesign --force --sign - "$app"
174echo "$app"