BlueGit

timfa posprint

Files

posprint / scripts / build-macos.sh

build-macos.sh

build-macos.sh 5.3 KiB Download Raw
1#!/usr/bin/env bash
2set -euo pipefail
3
4ROOT="$(cd "$(dirname "$0")/.." && pwd)"
5cd "$ROOT"
6
7BUILD_APP=false
8BUILD_DMG=false
9SKIP_TESTS=false
10INSTALL=false
11
12for arg in "$@"; do
13 case "$arg" in
14 --app) BUILD_APP=true ;;
15 --dmg) BUILD_APP=true; BUILD_DMG=true ;;
16 --skip-tests) SKIP_TESTS=true ;;
17 --install) BUILD_APP=true; INSTALL=true ;;
18 -h|--help)
19 echo "Usage: $0 [--app] [--dmg] [--skip-tests] [--install]"
20 echo "Builds target/release/posprint (and optionally Posprint.app / .dmg on macOS)."
21 echo " --app also bundle target/macos/Posprint.app (registers posprint:// handler)"
22 echo " --dmg also build target/macos/Posprint-<ver>-macos-<arch>.dmg (implies --app)"
23 echo " --install install Posprint.app to /Applications and posprint CLI on PATH (implies --app)"
24 echo " --skip-tests skip cargo test before building"
25 echo "On a TTY, missing Rust toolchain is offered for automatic install."
26 exit 0
27 ;;
28 *) echo "Unknown option: $arg"; exit 1 ;;
29 esac
30done
31
32is_interactive() {
33 [[ -t 0 ]]
34}
35
36ask_yes_no() {
37 local prompt="$1"
38 if ! is_interactive; then
39 return 1
40 fi
41 local answer
42 read -r -p "$prompt [y/N] " answer
43 [[ "$answer" =~ ^[Yy]([Ee][Ss])?$ ]]
44}
45
46want_install() {
47 if [[ "$INSTALL" == "true" ]]; then
48 return 0
49 fi
50 if ! is_interactive; then
51 return 1
52 fi
53 ask_yes_no "Install Posprint.app to /Applications and posprint CLI now?"
54}
55
56# Make rustup / Homebrew toolchains visible when installed.
57if [[ -f "$HOME/.cargo/env" ]]; then
58 # shellcheck source=/dev/null
59 source "$HOME/.cargo/env"
60fi
61if [[ -d "/opt/homebrew/bin" && ":$PATH:" != *":/opt/homebrew/bin:"* ]]; then
62 export PATH="/opt/homebrew/bin:$PATH"
63fi
64
65ensure_rust() {
66 if command -v cargo &>/dev/null && command -v rustc &>/dev/null; then
67 return 0
68 fi
69
70 echo "cargo/rustc not found."
71 if ask_yes_no "Install Rust via rustup now?"; then
72 curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
73 # shellcheck source=/dev/null
74 source "$HOME/.cargo/env"
75 else
76 echo "Install Rust from https://rustup.rs (or 'brew install rust'), then re-run."
77 exit 1
78 fi
79
80 if ! command -v cargo &>/dev/null || ! command -v rustc &>/dev/null; then
81 echo "Rust install finished but cargo/rustc still not on PATH. Open a new shell or: source \"\$HOME/.cargo/env\""
82 exit 1
83 fi
84}
85
86ensure_rust
87
88ARCH="$(uname -m)"
89echo "Building posprint for macOS ($ARCH)..."
90
91if [[ "$SKIP_TESTS" != "true" ]]; then
92 cargo test
93fi
94
95cargo build --release
96BIN="$ROOT/target/release/posprint"
97echo "OK: $BIN"
98ls -lh "$BIN"
99
100VERSION="$(grep '^version = ' "$ROOT/Cargo.toml" | head -n1 | tr -d '\r' | sed -E 's/.*"([^"]+)"/\1/')"
101
102if [[ "$BUILD_APP" == "true" ]]; then
103 APP_DIR="$ROOT/target/macos/Posprint.app"
104 rm -rf "$APP_DIR"
105 mkdir -p "$APP_DIR/Contents/MacOS" "$APP_DIR/Contents/Resources"
106
107 # Compile native Cocoa launcher that handles posprint:// URLs, .pos files,
108 # and direct app launches by spawning Contents/MacOS/posprint-bin.
109 cc -O2 -framework Cocoa \
110 -o "$APP_DIR/Contents/MacOS/posprint-launcher" \
111 "$ROOT/packaging/macos/launcher.m"
112 chmod +x "$APP_DIR/Contents/MacOS/posprint-launcher"
113
114 cp "$BIN" "$APP_DIR/Contents/MacOS/posprint-bin"
115 chmod +x "$APP_DIR/Contents/MacOS/posprint-bin"
116
117 sed "s/@VERSION@/${VERSION}/g" "$ROOT/packaging/macos/Info.plist" > "$APP_DIR/Contents/Info.plist"
118
119 # Build AppIcon.icns from pos-logo.png if sips and iconutil are available.
120 if command -v sips &>/dev/null && command -v iconutil &>/dev/null && [[ -f "$ROOT/pos-logo.png" ]]; then
121 ICONSET_DIR="$(mktemp -d /tmp/posprint-icon.XXXXXX.iconset)"
122 for size in 16 32 128 256 512; do
123 sips -z "$size" "$size" "$ROOT/pos-logo.png" --out "$ICONSET_DIR/icon_${size}x${size}.png" >/dev/null 2>&1 || true
124 size2=$((size * 2))
125 sips -z "$size2" "$size2" "$ROOT/pos-logo.png" --out "$ICONSET_DIR/icon_${size}x${size}@2x.png" >/dev/null 2>&1 || true
126 done
127 iconutil -c icns "$ICONSET_DIR" -o "$APP_DIR/Contents/Resources/AppIcon.icns" >/dev/null 2>&1 || true
128 rm -rf "$ICONSET_DIR"
129 fi
130
131 codesign --force --deep --sign - "$APP_DIR"
132 echo "macOS app bundle: $APP_DIR"
133
134 if [[ "$BUILD_DMG" == "true" ]]; then
135 DMG_PATH="$ROOT/target/macos/Posprint-${VERSION}-macos-${ARCH}.dmg"
136 rm -f "$DMG_PATH"
137 hdiutil create -volname "Posprint" -srcfolder "$APP_DIR" -ov -format UDZO "$DMG_PATH" >/dev/null
138 echo "macOS disk image: $DMG_PATH"
139 ls -lh "$DMG_PATH"
140 fi
141
142 if want_install; then
143 echo "Installing Posprint.app to /Applications/Posprint.app..."
144 rm -rf /Applications/Posprint.app
145 cp -R "$APP_DIR" /Applications/Posprint.app
146 codesign --force --deep --sign - /Applications/Posprint.app
147
148 if [[ -d "/opt/homebrew/bin" && -w "/opt/homebrew/bin" ]]; then
149 cp "$BIN" /opt/homebrew/bin/posprint
150 echo "Installed CLI binary: /opt/homebrew/bin/posprint"
151 elif [[ -d "/usr/local/bin" && -w "/usr/local/bin" ]]; then
152 cp "$BIN" /opt/homebrew/bin/posprint
153 echo "Installed CLI binary: /opt/homebrew/bin/posprint"
154 fi
155
156 LSREGISTER="/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister"
157 if [[ -x "$LSREGISTER" ]]; then
158 "$LSREGISTER" -f /Applications/Posprint.app || true
159 fi
160 echo "Installed and registered posprint:// URL handler."
161 fi
162fi