#!/bin/sh
# Builds orgstar.app in .build/app from the release binary. The icon is drawn by
# scripts/make-icon.swift.
# Usage: scripts/build-app.sh [version] [build]
# ORGSTAR_SIGN signing identity; ad hoc ("-") for local use by default. Any other
# identity also turns on the hardened runtime and a secure timestamp.
# ORGSTAR_ARCHS architectures, e.g. "arm64 x86_64" for a universal app; this Mac's by default.
set -eu
cd "$(dirname "$0")/.."
version="${1:-1.2.0}"
build="${2:-$version}"
sign="${ORGSTAR_SIGN:--}"
app=".build/app/orgstar.app"
arch_flags=""
for arch in ${ORGSTAR_ARCHS:-$(uname -m)}; do arch_flags="$arch_flags --arch $arch"; done
if [ "$sign" = "-" ]; then
sign_flags=""
else
sign_flags="--options runtime --timestamp"
fi
# shellcheck disable=SC2086
swift build -c release $arch_flags --product Orgstar
# shellcheck disable=SC2086
swift build -c release $arch_flags --product OrgstarQuickLook
# shellcheck disable=SC2086
swift build -c release $arch_flags --product OrgstarSpotlight
# shellcheck disable=SC2086
swift build -c release $arch_flags --product OrgstarWidgets
# shellcheck disable=SC2086
binary="$(swift build -c release $arch_flags --show-bin-path)/Orgstar"
rm -rf "$app"
mkdir -p "$app/Contents/MacOS" "$app/Contents/Resources" "$app/Contents/Frameworks"
cp "$binary" "$app/Contents/MacOS/Orgstar"
# Sparkle, from its SwiftPM artifact: the framework beside the binary, found through an rpath.
sparkle="$(find .build/artifacts -path '*Sparkle.xcframework/macos-*/Sparkle.framework' -maxdepth 7 | head -1)"
[ -n "$sparkle" ] || { echo "Sparkle.framework not in .build/artifacts; swift build fetches it." >&2; exit 1; }
ditto "$sparkle" "$app/Contents/Frameworks/Sparkle.framework"
otool -l "$app/Contents/MacOS/Orgstar" | grep -q '@executable_path/../Frameworks' \
|| install_name_tool -add_rpath @executable_path/../Frameworks "$app/Contents/MacOS/Orgstar"
# SwiftMath's math fonts, which it finds in the app's resources.
cp -R "$(dirname "$binary")/SwiftMath_SwiftMath.bundle" "$app/Contents/Resources/"
# MathJax, for PDF export.
cp -R "$(dirname "$binary")/Orgstar_OrgMathJax.bundle" "$app/Contents/Resources/"
# Updates: the appcast and its signing key, for signed builds only; a dev build never checks.
if [ "$sign" != "-" ]; then
feed_keys=" SUFeedURL
https://files.krz.sh/orgstar/appcast.xml
SUPublicEDKey
${ORGSTAR_ED_PUBLIC_KEY:?set ORGSTAR_ED_PUBLIC_KEY to the key generate_keys printed}"
else
feed_keys=""
fi
# The UI's translations, from l10n/Localizable.xcstrings and l10n/DeveloperID.xcstrings.
scripts/localize.sh --developer-id "$app/Contents/Resources"
# The icon (macOS derives its dark and tinted styles), and the Dock icons Settings offers.
work="$(mktemp -d)"
xcrun actool packaging/AppIcons.xcassets --compile "$app/Contents/Resources" --platform macosx \
--minimum-deployment-target 26.0 --app-icon AppIcon --output-partial-info-plist "$work/icon.plist" > /dev/null
rm -rf "$work"
mkdir -p "$app/Contents/Resources/IconPreviews"
cp packaging/IconPreviews/*.png "$app/Contents/Resources/IconPreviews/"
cat > "$app/Contents/Info.plist" <
CFBundleIdentifier
sh.krz.orgstar
CFBundleDevelopmentRegion
en
CFBundleLocalizations
endefrjaespt-BR
CFBundleName
orgstar
CFBundleDisplayName
orgstar
CFBundleExecutable
Orgstar
CFBundlePackageType
APPL
CFBundleIconName
AppIcon
UTImportedTypeDeclarations
UTTypeIdentifier
org.orgmode.org
UTTypeDescription
Org document
UTTypeConformsTo
public.plain-text
UTTypeTagSpecification
public.filename-extension
orgorg_archive
CFBundleDocumentTypes
CFBundleTypeName
Org document
CFBundleTypeRole
Editor
LSHandlerRank
Default
LSItemContentTypes
org.orgmode.org
CFBundleTypeName
Text
CFBundleTypeRole
Editor
LSHandlerRank
Alternate
LSItemContentTypes
public.plain-text
CFBundleURLTypes
CFBundleURLName
org-protocol
CFBundleURLSchemes
org-protocolorgstar
CFBundleShortVersionString
${version}
CFBundleVersion
${build}
LSMinimumSystemVersion
26.0
${feed_keys}
LSApplicationCategoryType
public.app-category.productivity
NSCalendarsFullAccessUsageDescription
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.
NSRemindersFullAccessUsageDescription
The agenda shows your reminders alongside your Org entries. orgstar only reads them.
NSHighResolutionCapable
NSPrincipalClass
NSApplication
EOF
# App Intents metadata for Shortcuts, from the constant values Swift Build extracted; SwiftPM
# doesn't run this step itself.
toolchain="$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain"
objects=".build/out/Intermediates.noindex/Orgstar.build/Release/Orgstar-p.build/Objects-normal/$(uname -m)"
work="$(mktemp -d)"
echo "$PWD/$objects/Orgstar-primary.swiftconstvalues" > "$work/constvalues.txt"
"$toolchain/usr/bin/appintentsmetadataprocessor" --toolchain-dir "$toolchain" --module-name Orgstar \
--sdk-root "$(xcrun --sdk macosx --show-sdk-path)" --xcode-version "$(xcodebuild -version | awk '/Build version/ {print $3}')" \
--platform-family macOS --deployment-target 26.0 --bundle-identifier sh.krz.orgstar \
--target-triple "$(uname -m)-apple-macos26.0" --binary-file "$binary" --source-file-list "$objects/Orgstar.SwiftFileList" \
--swift-const-vals-list "$work/constvalues.txt" --output "$app/Contents/Resources" --force > /dev/null 2>&1
rm -rf "$work"
# App extensions: Quick Look previews, the Spotlight importer and the widgets. They run
# sandboxed.
appex() { # name, extension point, principal class (or ""), attributes, extra entitlements
dir="$app/Contents/PlugIns/$1.appex/Contents"
mkdir -p "$dir/MacOS" "$dir/Resources"
cp "$(dirname "$binary")/$1" "$dir/MacOS/$1"
scripts/localize.sh "$dir/Resources"
cat > "$dir/Info.plist" <
CFBundleIdentifier
sh.krz.orgstar.$(echo "$1" | sed 's/^Orgstar//' | tr 'A-Z' 'a-z')
CFBundleName
$1
CFBundleExecutable
$1
CFBundlePackageType
XPC!
CFBundleShortVersionString
${version}
CFBundleVersion
${build}
LSMinimumSystemVersion
26.0
NSExtension
NSExtensionPointIdentifier
$2
$([ -n "$3" ] && printf ' NSExtensionPrincipalClass\n %s\n' "$3")
NSExtensionAttributes
$4
PLIST
cat > "$work/sandbox.entitlements" <
com.apple.security.app-sandbox
${5:-}
PLIST
# shellcheck disable=SC2086
codesign --force --sign "$sign" $sign_flags --entitlements "$work/sandbox.entitlements" "$app/Contents/PlugIns/$1.appex"
}
work="$(mktemp -d)"
appex OrgstarQuickLook com.apple.quicklook.preview PreviewProvider \
"QLSupportedContentTypesorg.orgmode.orgQLIsDataBasedPreview"
appex OrgstarSpotlight com.apple.spotlight.import ImportExtension \
"CSSupportedContentTypesorg.orgmode.org"
# The widgets read what the app writes in ~/Library/Application Support/Orgstar/Widgets.
appex OrgstarWidgets com.apple.widgetkit-extension "" "" \
" com.apple.security.temporary-exception.files.home-relative-path.read-only
/Library/Application Support/Orgstar/Widgets/"
# Sparkle's inner pieces are signed first, as its documentation has it, then the framework.
fw="$app/Contents/Frameworks/Sparkle.framework/Versions/B"
for item in "$fw/XPCServices/Installer.xpc" "$fw/XPCServices/Downloader.xpc" "$fw/Autoupdate" "$fw/Updater.app" "$app/Contents/Frameworks/Sparkle.framework"; do
# shellcheck disable=SC2086
codesign --force --sign "$sign" $sign_flags "$item"
done
# The hardened runtime needs Calendar access declared for the agenda's events.
cat > "$work/app.entitlements" <
com.apple.security.personal-information.calendars
PLIST
# shellcheck disable=SC2086
codesign --force --sign "$sign" $sign_flags --entitlements "$work/app.entitlements" "$app"
rm -rf "$work"
echo "$app"