feat: add macOS build and app bundle script
@@ -0,0 +1,58 @@
1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
<plist version="1.0">
4
<dict>
5
<key>CFBundleDevelopmentRegion</key>
6
<string>en</string>
7
<key>CFBundleDisplayName</key>
8
<string>POS Print</string>
9
<key>CFBundleExecutable</key>
10
<string>applet</string>
11
<key>CFBundleIconFile</key>
12
<string>applet</string>
13
<key>CFBundleIdentifier</key>
14
<string>com.timfa.posprint</string>
15
<key>CFBundleInfoDictionaryVersion</key>
16
<string>6.0</string>
17
<key>CFBundleName</key>
18
<string>Posprint</string>
19
<key>CFBundlePackageType</key>
20
<string>APPL</string>
21
<key>CFBundleShortVersionString</key>
22
<string>@VERSION@</string>
23
<key>CFBundleSignature</key>
24
<string>aplt</string>
25
<key>CFBundleVersion</key>
26
<string>@VERSION@</string>
27
<key>LSMinimumSystemVersion</key>
28
<string>11.0</string>
29
<key>NSHighResolutionCapable</key>
30
<true/>
31
<key>CFBundleURLTypes</key>
32
<array>
33
<dict>
34
<key>CFBundleURLName</key>
35
<string>POS Print URL</string>
36
<key>CFBundleURLSchemes</key>
37
<array>
38
<string>posprint</string>
39
</array>
40
</dict>
41
</array>
42
<key>CFBundleDocumentTypes</key>
43
<array>
44
<dict>
45
<key>CFBundleTypeExtensions</key>
46
<array>
47
<string>pos</string>
48
</array>
49
<key>CFBundleTypeName</key>
50
<string>POS Print Template</string>
51
<key>CFBundleTypeRole</key>
52
<string>Editor</string>
53
<key>LSHandlerRank</key>
54
<string>Owner</string>
55
</dict>
56
</array>
57
</dict>
58
</plist>
@@ -0,0 +1,58 @@1
<?xml version="1.0" encoding="UTF-8"?>2
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">3
<plist version="1.0">4
<dict>5
<key>CFBundleDevelopmentRegion</key>6
<string>en</string>7
<key>CFBundleDisplayName</key>8
<string>POS Print</string>9
<key>CFBundleExecutable</key>10
<string>applet</string>11
<key>CFBundleIconFile</key>12
<string>applet</string>13
<key>CFBundleIdentifier</key>14
<string>com.timfa.posprint</string>15
<key>CFBundleInfoDictionaryVersion</key>16
<string>6.0</string>17
<key>CFBundleName</key>18
<string>Posprint</string>19
<key>CFBundlePackageType</key>20
<string>APPL</string>21
<key>CFBundleShortVersionString</key>22
<string>@VERSION@</string>23
<key>CFBundleSignature</key>24
<string>aplt</string>25
<key>CFBundleVersion</key>26
<string>@VERSION@</string>27
<key>LSMinimumSystemVersion</key>28
<string>11.0</string>29
<key>NSHighResolutionCapable</key>30
<true/>31
<key>CFBundleURLTypes</key>32
<array>33
<dict>34
<key>CFBundleURLName</key>35
<string>POS Print URL</string>36
<key>CFBundleURLSchemes</key>37
<array>38
<string>posprint</string>39
</array>40
</dict>41
</array>42
<key>CFBundleDocumentTypes</key>43
<array>44
<dict>45
<key>CFBundleTypeExtensions</key>46
<array>47
<string>pos</string>48
</array>49
<key>CFBundleTypeName</key>50
<string>POS Print Template</string>51
<key>CFBundleTypeRole</key>52
<string>Editor</string>53
<key>LSHandlerRank</key>54
<string>Owner</string>55
</dict>56
</array>57
</dict>58
</plist>@@ -0,0 +1,185 @@
1
#!/usr/bin/env bash
2
set -euo pipefail
3
4
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
5
cd "$ROOT"
6
7
BUILD_APP=false
8
BUILD_DMG=false
9
SKIP_TESTS=false
10
INSTALL=false
11
12
for 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
30
done
31
32
is_interactive() {
33
[[ -t 0 ]]
34
}
35
36
ask_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
46
want_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.
57
if [[ -f "$HOME/.cargo/env" ]]; then
58
# shellcheck source=/dev/null
59
source "$HOME/.cargo/env"
60
fi
61
if [[ -d "/opt/homebrew/bin" && ":$PATH:" != *":/opt/homebrew/bin:"* ]]; then
62
export PATH="/opt/homebrew/bin:$PATH"
63
fi
64
65
ensure_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
86
ensure_rust
87
88
ARCH="$(uname -m)"
89
echo "Building posprint for macOS ($ARCH)..."
90
91
if [[ "$SKIP_TESTS" != "true" ]]; then
92
cargo test
93
fi
94
95
cargo build --release
96
BIN="$ROOT/target/release/posprint"
97
echo "OK: $BIN"
98
ls -lh "$BIN"
99
100
VERSION="$(grep '^version = ' "$ROOT/Cargo.toml" | head -n1 | tr -d '\r' | sed -E 's/.*"([^"]+)"/\1/')"
101
102
if [[ "$BUILD_APP" == "true" ]]; then
103
APP_DIR="$ROOT/target/macos/Posprint.app"
104
rm -rf "$APP_DIR"
105
mkdir -p "$ROOT/target/macos"
106
107
# Compile an AppleEvent launcher bundle so macOS delivers GURL (posprint://)
108
# and odoc (.pos files) events and forwards them to the embedded Rust binary.
109
APPLESCRIPT_SRC="$(mktemp /tmp/posprint-applet.XXXXXX.applescript)"
110
trap 'rm -f "$APPLESCRIPT_SRC"' EXIT
111
cat > "$APPLESCRIPT_SRC" <<'APPLESCRIPT'
112
on resolve_bin()
113
set bundlePath to POSIX path of (path to me)
114
return bundlePath & "Contents/MacOS/posprint-bin"
115
end resolve_bin
116
117
on open location this_URL
118
set binPath to resolve_bin()
119
do shell script quoted form of binPath & " " & quoted form of this_URL & " >/dev/null 2>&1 &"
120
end open location
121
122
on open these_items
123
set binPath to resolve_bin()
124
repeat with an_item in these_items
125
set itemPath to POSIX path of an_item
126
do shell script quoted form of binPath & " " & quoted form of itemPath & " >/dev/null 2>&1 &"
127
end repeat
128
end open
129
130
on run
131
set binPath to resolve_bin()
132
do shell script quoted form of binPath & " >/dev/null 2>&1 &"
133
end run
134
APPLESCRIPT
135
136
osacompile -o "$APP_DIR" "$APPLESCRIPT_SRC"
137
cp "$BIN" "$APP_DIR/Contents/MacOS/posprint-bin"
138
chmod +x "$APP_DIR/Contents/MacOS/posprint-bin"
139
140
sed "s/@VERSION@/${VERSION}/g" "$ROOT/packaging/macos/Info.plist" > "$APP_DIR/Contents/Info.plist"
141
142
# Build applet.icns from pos-logo.png if sips and iconutil are available.
143
if command -v sips &>/dev/null && command -v iconutil &>/dev/null && [[ -f "$ROOT/pos-logo.png" ]]; then
144
ICONSET_DIR="$(mktemp -d /tmp/posprint-icon.XXXXXX.iconset)"
145
for size in 16 32 128 256 512; do
146
sips -z "$size" "$size" "$ROOT/pos-logo.png" --out "$ICONSET_DIR/icon_${size}x${size}.png" >/dev/null 2>&1 || true
147
size2=$((size * 2))
148
sips -z "$size2" "$size2" "$ROOT/pos-logo.png" --out "$ICONSET_DIR/icon_${size}x${size}@2x.png" >/dev/null 2>&1 || true
149
done
150
iconutil -c icns "$ICONSET_DIR" -o "$APP_DIR/Contents/Resources/applet.icns" >/dev/null 2>&1 || true
151
rm -rf "$ICONSET_DIR"
152
fi
153
154
codesign --force --deep --sign - "$APP_DIR"
155
echo "macOS app bundle: $APP_DIR"
156
157
if [[ "$BUILD_DMG" == "true" ]]; then
158
DMG_PATH="$ROOT/target/macos/Posprint-${VERSION}-macos-${ARCH}.dmg"
159
rm -f "$DMG_PATH"
160
hdiutil create -volname "Posprint" -srcfolder "$APP_DIR" -ov -format UDZO "$DMG_PATH" >/dev/null
161
echo "macOS disk image: $DMG_PATH"
162
ls -lh "$DMG_PATH"
163
fi
164
165
if want_install; then
166
echo "Installing Posprint.app to /Applications/Posprint.app..."
167
rm -rf /Applications/Posprint.app
168
cp -R "$APP_DIR" /Applications/Posprint.app
169
codesign --force --deep --sign - /Applications/Posprint.app
170
171
if [[ -d "/opt/homebrew/bin" && -w "/opt/homebrew/bin" ]]; then
172
cp "$BIN" /opt/homebrew/bin/posprint
173
echo "Installed CLI binary: /opt/homebrew/bin/posprint"
174
elif [[ -d "/usr/local/bin" && -w "/usr/local/bin" ]]; then
175
cp "$BIN" /usr/local/bin/posprint
176
echo "Installed CLI binary: /usr/local/bin/posprint"
177
fi
178
179
LSREGISTER="/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister"
180
if [[ -x "$LSREGISTER" ]]; then
181
"$LSREGISTER" -f /Applications/Posprint.app || true
182
fi
183
echo "Installed and registered posprint:// URL handler."
184
fi
185
fi
@@ -0,0 +1,185 @@1
#!/usr/bin/env bash2
set -euo pipefail3
4
ROOT="$(cd "$(dirname "$0")/.." && pwd)"5
cd "$ROOT"6
7
BUILD_APP=false8
BUILD_DMG=false9
SKIP_TESTS=false10
INSTALL=false11
12
for arg in "$@"; do13
case "$arg" in14
--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 027
;;28
*) echo "Unknown option: $arg"; exit 1 ;;29
esac30
done31
32
is_interactive() {33
[[ -t 0 ]]34
}35
36
ask_yes_no() {37
local prompt="$1"38
if ! is_interactive; then39
return 140
fi41
local answer42
read -r -p "$prompt [y/N] " answer43
[[ "$answer" =~ ^[Yy]([Ee][Ss])?$ ]]44
}45
46
want_install() {47
if [[ "$INSTALL" == "true" ]]; then48
return 049
fi50
if ! is_interactive; then51
return 152
fi53
ask_yes_no "Install Posprint.app to /Applications and posprint CLI now?"54
}55
56
# Make rustup / Homebrew toolchains visible when installed.57
if [[ -f "$HOME/.cargo/env" ]]; then58
# shellcheck source=/dev/null59
source "$HOME/.cargo/env"60
fi61
if [[ -d "/opt/homebrew/bin" && ":$PATH:" != *":/opt/homebrew/bin:"* ]]; then62
export PATH="/opt/homebrew/bin:$PATH"63
fi64
65
ensure_rust() {66
if command -v cargo &>/dev/null && command -v rustc &>/dev/null; then67
return 068
fi69
70
echo "cargo/rustc not found."71
if ask_yes_no "Install Rust via rustup now?"; then72
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y73
# shellcheck source=/dev/null74
source "$HOME/.cargo/env"75
else76
echo "Install Rust from https://rustup.rs (or 'brew install rust'), then re-run."77
exit 178
fi79
80
if ! command -v cargo &>/dev/null || ! command -v rustc &>/dev/null; then81
echo "Rust install finished but cargo/rustc still not on PATH. Open a new shell or: source \"\$HOME/.cargo/env\""82
exit 183
fi84
}85
86
ensure_rust87
88
ARCH="$(uname -m)"89
echo "Building posprint for macOS ($ARCH)..."90
91
if [[ "$SKIP_TESTS" != "true" ]]; then92
cargo test93
fi94
95
cargo build --release96
BIN="$ROOT/target/release/posprint"97
echo "OK: $BIN"98
ls -lh "$BIN"99
100
VERSION="$(grep '^version = ' "$ROOT/Cargo.toml" | head -n1 | tr -d '\r' | sed -E 's/.*"([^"]+)"/\1/')"101
102
if [[ "$BUILD_APP" == "true" ]]; then103
APP_DIR="$ROOT/target/macos/Posprint.app"104
rm -rf "$APP_DIR"105
mkdir -p "$ROOT/target/macos"106
107
# Compile an AppleEvent launcher bundle so macOS delivers GURL (posprint://)108
# and odoc (.pos files) events and forwards them to the embedded Rust binary.109
APPLESCRIPT_SRC="$(mktemp /tmp/posprint-applet.XXXXXX.applescript)"110
trap 'rm -f "$APPLESCRIPT_SRC"' EXIT111
cat > "$APPLESCRIPT_SRC" <<'APPLESCRIPT'112
on resolve_bin()113
set bundlePath to POSIX path of (path to me)114
return bundlePath & "Contents/MacOS/posprint-bin"115
end resolve_bin116
117
on open location this_URL118
set binPath to resolve_bin()119
do shell script quoted form of binPath & " " & quoted form of this_URL & " >/dev/null 2>&1 &"120
end open location121
122
on open these_items123
set binPath to resolve_bin()124
repeat with an_item in these_items125
set itemPath to POSIX path of an_item126
do shell script quoted form of binPath & " " & quoted form of itemPath & " >/dev/null 2>&1 &"127
end repeat128
end open129
130
on run131
set binPath to resolve_bin()132
do shell script quoted form of binPath & " >/dev/null 2>&1 &"133
end run134
APPLESCRIPT135
136
osacompile -o "$APP_DIR" "$APPLESCRIPT_SRC"137
cp "$BIN" "$APP_DIR/Contents/MacOS/posprint-bin"138
chmod +x "$APP_DIR/Contents/MacOS/posprint-bin"139
140
sed "s/@VERSION@/${VERSION}/g" "$ROOT/packaging/macos/Info.plist" > "$APP_DIR/Contents/Info.plist"141
142
# Build applet.icns from pos-logo.png if sips and iconutil are available.143
if command -v sips &>/dev/null && command -v iconutil &>/dev/null && [[ -f "$ROOT/pos-logo.png" ]]; then144
ICONSET_DIR="$(mktemp -d /tmp/posprint-icon.XXXXXX.iconset)"145
for size in 16 32 128 256 512; do146
sips -z "$size" "$size" "$ROOT/pos-logo.png" --out "$ICONSET_DIR/icon_${size}x${size}.png" >/dev/null 2>&1 || true147
size2=$((size * 2))148
sips -z "$size2" "$size2" "$ROOT/pos-logo.png" --out "$ICONSET_DIR/icon_${size}x${size}@2x.png" >/dev/null 2>&1 || true149
done150
iconutil -c icns "$ICONSET_DIR" -o "$APP_DIR/Contents/Resources/applet.icns" >/dev/null 2>&1 || true151
rm -rf "$ICONSET_DIR"152
fi153
154
codesign --force --deep --sign - "$APP_DIR"155
echo "macOS app bundle: $APP_DIR"156
157
if [[ "$BUILD_DMG" == "true" ]]; then158
DMG_PATH="$ROOT/target/macos/Posprint-${VERSION}-macos-${ARCH}.dmg"159
rm -f "$DMG_PATH"160
hdiutil create -volname "Posprint" -srcfolder "$APP_DIR" -ov -format UDZO "$DMG_PATH" >/dev/null161
echo "macOS disk image: $DMG_PATH"162
ls -lh "$DMG_PATH"163
fi164
165
if want_install; then166
echo "Installing Posprint.app to /Applications/Posprint.app..."167
rm -rf /Applications/Posprint.app168
cp -R "$APP_DIR" /Applications/Posprint.app169
codesign --force --deep --sign - /Applications/Posprint.app170
171
if [[ -d "/opt/homebrew/bin" && -w "/opt/homebrew/bin" ]]; then172
cp "$BIN" /opt/homebrew/bin/posprint173
echo "Installed CLI binary: /opt/homebrew/bin/posprint"174
elif [[ -d "/usr/local/bin" && -w "/usr/local/bin" ]]; then175
cp "$BIN" /usr/local/bin/posprint176
echo "Installed CLI binary: /usr/local/bin/posprint"177
fi178
179
LSREGISTER="/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister"180
if [[ -x "$LSREGISTER" ]]; then181
"$LSREGISTER" -f /Applications/Posprint.app || true182
fi183
echo "Installed and registered posprint:// URL handler."184
fi185
fi@@ -201,6 +201,21 @@ mod unix_scripts {
201
201
"expected missing-tarball error, got: {stderr}"
202
202
);
203
203
}
204
205
#[test]
206
fn macos_build_script_help_succeeds() {
207
let script = repo_root().join("scripts/build-macos.sh");
208
let output = Command::new("bash")
209
.arg(&script)
210
.arg("--help")
211
.output()
212
.expect("run build-macos.sh --help");
213
assert!(output.status.success());
214
let stdout = String::from_utf8_lossy(&output.stdout);
215
assert!(stdout.contains("--app"));
216
assert!(stdout.contains("--dmg"));
217
assert!(stdout.contains("--install"));
218
}
204
219
}
205
220
206
221
#[test]
@@ -274,3 +289,12 @@ fn expected_release_filenames_match_packaging_tools() {
274
289
"posprint-0.5.7-x86_64.tar.gz"
275
290
);
276
291
}
292
293
#[test]
294
fn macos_info_plist_registers_posprint_url_scheme_and_pos_extension() {
295
let plist = include_str!("../packaging/macos/Info.plist");
296
assert!(plist.contains("<string>com.timfa.posprint</string>"));
297
assert!(plist.contains("<string>posprint</string>"));
298
assert!(plist.contains("<string>pos</string>"));
299
assert!(plist.contains("@VERSION@"));
300
}
@@ -201,6 +201,21 @@ mod unix_scripts {201
"expected missing-tarball error, got: {stderr}"201
"expected missing-tarball error, got: {stderr}"202
);202
);203
}203
}204
205
#[test]206
fn macos_build_script_help_succeeds() {207
let script = repo_root().join("scripts/build-macos.sh");208
let output = Command::new("bash")209
.arg(&script)210
.arg("--help")211
.output()212
.expect("run build-macos.sh --help");213
assert!(output.status.success());214
let stdout = String::from_utf8_lossy(&output.stdout);215
assert!(stdout.contains("--app"));216
assert!(stdout.contains("--dmg"));217
assert!(stdout.contains("--install"));218
}204
}219
}205
220
206
#[test]221
#[test]@@ -274,3 +289,12 @@ fn expected_release_filenames_match_packaging_tools() {274
"posprint-0.5.7-x86_64.tar.gz"289
"posprint-0.5.7-x86_64.tar.gz"275
);290
);276
}291
}292
293
#[test]294
fn macos_info_plist_registers_posprint_url_scheme_and_pos_extension() {295
let plist = include_str!("../packaging/macos/Info.plist");296
assert!(plist.contains("<string>com.timfa.posprint</string>"));297
assert!(plist.contains("<string>posprint</string>"));298
assert!(plist.contains("<string>pos</string>"));299
assert!(plist.contains("@VERSION@"));300
}