fenilsonani / menu-browser / build.sh

#!/bin/bash
set -euo pipefail

APP_NAME="MenuBrowser"
BUILD_DIR="build"
APP_BUNDLE="${BUILD_DIR}/${APP_NAME}.app"
CONTENTS="${APP_BUNDLE}/Contents"
MACOS="${CONTENTS}/MacOS"
source "$(dirname "$0")/scripts/common.sh"
ARCHS="${ARCHS:-arm64 x86_64}"
VERSION="$(menubrowser_version)"
read -r -a BUILD_ARCHS <<< "${ARCHS}"

echo "Cleaning build directory..."
rm -rf "${BUILD_DIR}"
mkdir -p "${MACOS}"

echo "Compiling universal binary for: ${BUILD_ARCHS[*]}"
SOURCE_FILES=(
    Sources/main.swift
    Sources/BrowserManager.swift
    Sources/GlobalShortcut.swift
    Sources/HotkeyManager.swift
    Sources/URLRuleManager.swift
    Sources/OnboardingView.swift
    Sources/HelpView.swift
    Sources/PreferencesView.swift
    Sources/AppDelegate.swift
)
FRAMEWORKS=(
    -framework AppKit
    -framework SwiftUI
    -framework ServiceManagement
    -framework CoreServices
    -framework Carbon
)
SLICE_BINARIES=()

for ARCH in "${BUILD_ARCHS[@]}"; do
    SLICE_PATH="${BUILD_DIR}/${APP_NAME}-${ARCH}"
    echo "  - ${ARCH}"
    xcrun swiftc \
        -o "${SLICE_PATH}" \
        "${FRAMEWORKS[@]}" \
        -target "${ARCH}-apple-macos${MIN_MACOS_VERSION}" \
        "${SOURCE_FILES[@]}"
    SLICE_BINARIES+=("${SLICE_PATH}")
done

lipo -create "${SLICE_BINARIES[@]}" -output "${MACOS}/${APP_NAME}"
rm -f "${SLICE_BINARIES[@]}"

echo "Copying Info.plist (version: ${VERSION})..."
cp Info.plist "${CONTENTS}/Info.plist"
plutil -replace CFBundleShortVersionString -string "${VERSION}" "${CONTENTS}/Info.plist"
plutil -replace CFBundleVersion -string "${VERSION}" "${CONTENTS}/Info.plist"
plutil -replace LSMinimumSystemVersion -string "${MIN_MACOS_VERSION}" "${CONTENTS}/Info.plist"

echo "Copying app icon..."
mkdir -p "${CONTENTS}/Resources"
cp AppIcon.icns "${CONTENTS}/Resources/AppIcon.icns"

echo "Code signing..."
# Sign by SHA-1 fingerprint instead of name so duplicate certificates (two "Apple
# Development: <name>" entries in the keychain) don't make codesign abort with
# "ambiguous". Also keeps the signature stable across rebuilds — important because
# TCC binds Accessibility grants to the code-signing identity.
#
# Only ever accept a 40-char hex fingerprint. `security find-identity` prints
# "0 valid identities found" on a machine with no certificates (CI), and matching
# field 2 of the first line there yields the literal string "valid", which then
# reaches codesign as an identity and fails the build instead of falling through
# to ad-hoc signing.
IDENTITIES=$(security find-identity -v -p codesigning 2>/dev/null || true)
SIGN_SHA=$(printf '%s\n' "$IDENTITIES" \
    | awk '/Apple Development:/ && $2 ~ /^[0-9A-Fa-f]{40}$/ {print $2; exit}')
if [ -z "$SIGN_SHA" ]; then
    SIGN_SHA=$(printf '%s\n' "$IDENTITIES" \
        | awk '$2 ~ /^[0-9A-Fa-f]{40}$/ {print $2; exit}')
fi
if [ -n "$SIGN_SHA" ]; then
    codesign --force --sign "$SIGN_SHA" "${APP_BUNDLE}"
    echo "Signed with identity SHA: $SIGN_SHA"
else
    codesign --force --sign - "${APP_BUNDLE}"
    echo "Ad-hoc signed (no identity found)"
fi

codesign --verify --strict "${APP_BUNDLE}"

echo "Build complete: ${APP_BUNDLE}"
echo "Run with: open ${APP_BUNDLE}"