Merge branch 'feature/macos-support' into 'master'
feat: add macOS support, Posprint.app bundle, and CLI USB printer lookup See merge request Timfa/posprint!3
@@ -2,7 +2,7 @@
2
2
3
3
Cross-platform designer for POS (point-of-sale) receipt documents. Edit templates in a markdown-like syntax, preview output live, substitute runtime parameters, and print via raw ESC/POS (USB printer-class, Windows spooler, USB-serial, or Classic Bluetooth SPP after OS pairing).
4
4
5
**Target platforms:** Windows and Linux (x64).
5
**Target platforms:** Windows, Linux (x64), and macOS (Apple Silicon / Intel).
6
6
7
7
## Status
8
8
@@ -18,7 +18,7 @@ cargo run # launch the designer
18
18
cargo build --release
19
19
```
20
20
21
**Scripts:** `scripts/build-windows.ps1`, `scripts/build-linux.sh`, `scripts/build-arch.sh`
21
**Scripts:** `scripts/build-windows.ps1`, `scripts/build-linux.sh`, `scripts/build-arch.sh`, `scripts/build-macos.sh`
22
22
(Interactive shells offer to install missing dependencies.)
23
23
24
24
### Command-line printing
@@ -30,7 +30,8 @@ posprint list
30
30
# Windows (serial, Bluetooth COM after pairing, or spooler queue name)
31
31
posprint print receipt.pos -p COM3 --set StoreName="Shop" --set DocumentNumber=42
32
32
33
# Linux (USB-serial or RFCOMM after pairing)
33
# Linux / macOS (USB-direct, 'auto', USB-serial, or RFCOMM/Bluetooth SPP)
34
posprint print receipt.pos -p auto --set StoreName="Shop" --set DocumentNumber=42
34
35
posprint print receipt.pos -p /dev/ttyUSB0 --set StoreName="Shop" --set DocumentNumber=42
35
36
36
37
# Embedded CLI help for this OS
@@ -2,7 +2,7 @@2
2
3
Cross-platform designer for POS (point-of-sale) receipt documents. Edit templates in a markdown-like syntax, preview output live, substitute runtime parameters, and print via raw ESC/POS (USB printer-class, Windows spooler, USB-serial, or Classic Bluetooth SPP after OS pairing).3
Cross-platform designer for POS (point-of-sale) receipt documents. Edit templates in a markdown-like syntax, preview output live, substitute runtime parameters, and print via raw ESC/POS (USB printer-class, Windows spooler, USB-serial, or Classic Bluetooth SPP after OS pairing).4
4
5
**Target platforms:** Windows and Linux (x64).5
**Target platforms:** Windows, Linux (x64), and macOS (Apple Silicon / Intel).6
6
7
## Status7
## Status8
8
@@ -18,7 +18,7 @@ cargo run # launch the designer18
cargo build --release18
cargo build --release19
```19
```20
20
21
**Scripts:** `scripts/build-windows.ps1`, `scripts/build-linux.sh`, `scripts/build-arch.sh` 21
**Scripts:** `scripts/build-windows.ps1`, `scripts/build-linux.sh`, `scripts/build-arch.sh`, `scripts/build-macos.sh` 22
(Interactive shells offer to install missing dependencies.)22
(Interactive shells offer to install missing dependencies.)23
23
24
### Command-line printing24
### Command-line printing@@ -30,7 +30,8 @@ posprint list30
# Windows (serial, Bluetooth COM after pairing, or spooler queue name)30
# Windows (serial, Bluetooth COM after pairing, or spooler queue name)31
posprint print receipt.pos -p COM3 --set StoreName="Shop" --set DocumentNumber=4231
posprint print receipt.pos -p COM3 --set StoreName="Shop" --set DocumentNumber=4232
32
33
# Linux (USB-serial or RFCOMM after pairing)33
# Linux / macOS (USB-direct, 'auto', USB-serial, or RFCOMM/Bluetooth SPP)34
posprint print receipt.pos -p auto --set StoreName="Shop" --set DocumentNumber=4234
posprint print receipt.pos -p /dev/ttyUSB0 --set StoreName="Shop" --set DocumentNumber=4235
posprint print receipt.pos -p /dev/ttyUSB0 --set StoreName="Shop" --set DocumentNumber=4235
36
36
# Embedded CLI help for this OS37
# Embedded CLI help for this OS@@ -1,6 +1,6 @@
1
1
# Build scripts
2
2
3
Local release builds for Windows and Linux.
3
Local release builds for Windows, Linux, and macOS.
4
4
5
5
## Toolchain
6
6
@@ -35,6 +35,18 @@ Options:
35
35
.\scripts\build-windows.ps1 -SkipTests
36
36
```
37
37
38
## macOS
39
40
```bash
41
chmod +x scripts/build-macos.sh
42
./scripts/build-macos.sh # release binary (target/release/posprint)
43
./scripts/build-macos.sh --app # binary + target/macos/Posprint.app
44
./scripts/build-macos.sh --dmg # binary + Posprint.app + target/macos/Posprint-<ver>-macos-<arch>.dmg
45
./scripts/build-macos.sh --install # build Posprint.app, install to /Applications & PATH, register posprint://
46
```
47
48
`Posprint.app` bundles the native `posprint` binary together with an AppleEvent launcher (`packaging/macos/Info.plist`) so clicking `posprint://` deep links in Safari/Chrome or opening `.pos` files in Finder launches `posprint` automatically.
49
38
50
## Linux
39
51
40
52
```bash
@@ -1,6 +1,6 @@1
# Build scripts1
# Build scripts2
2
3
Local release builds for Windows and Linux.3
Local release builds for Windows, Linux, and macOS.4
4
5
## Toolchain5
## Toolchain6
6
@@ -35,6 +35,18 @@ Options:35
.\scripts\build-windows.ps1 -SkipTests35
.\scripts\build-windows.ps1 -SkipTests36
```36
```37
37
38
## macOS39
40
```bash41
chmod +x scripts/build-macos.sh42
./scripts/build-macos.sh # release binary (target/release/posprint)43
./scripts/build-macos.sh --app # binary + target/macos/Posprint.app44
./scripts/build-macos.sh --dmg # binary + Posprint.app + target/macos/Posprint-<ver>-macos-<arch>.dmg45
./scripts/build-macos.sh --install # build Posprint.app, install to /Applications & PATH, register posprint://46
```47
48
`Posprint.app` bundles the native `posprint` binary together with an AppleEvent launcher (`packaging/macos/Info.plist`) so clicking `posprint://` deep links in Safari/Chrome or opening `.pos` files in Finder launches `posprint` automatically.49
38
## Linux50
## Linux39
51
40
```bash52
```bash@@ -1,4 +1,4 @@
1
# Command-line printing (Linux)
1
# Command-line printing (Linux & macOS)
2
2
3
3
Print a `.pos` template without opening the GUI.
4
4
@@ -24,7 +24,7 @@ posprint link receipt.pos
24
24
## Rules
25
25
26
26
- **`posprint list`**: print discovered printers (USB, USB-serial, paired Classic Bluetooth SPP). Use the device path from the list as `-p`.
27
- **`--printer` / `-p`** (required): serial device path (e.g. `/dev/ttyUSB0`, `/dev/ttyACM0`, `/dev/rfcomm0` after OS pairing), or another printer id accepted by discovery.
27
- **`--printer` / `-p`** (required): printer id from `posprint list` (e.g. `usb:28e9:0289:0:1` or `usb:28e9:0289`), `auto` (first discovered printer), or serial device path (e.g. `/dev/ttyUSB0`, `/dev/ttyACM0`, `/dev/rfcomm0`, `/dev/cu.usbserial-*`).
28
28
- **`--set NAME=VALUE`** (required for every parameter): one flag per `${Name}` or `$[Name]` in the template.
29
29
- Checkbox params (`$[Name]`): use `true`/`false` (also `1`/`0`, `yes`/`no`).
30
30
- If any text `${Name}` is missing or empty, or a `$[Name]` is omitted / not a boolean, the command **fails**.
@@ -1,4 +1,4 @@1
# Command-line printing (Linux)1
# Command-line printing (Linux & macOS)2
2
3
Print a `.pos` template without opening the GUI.3
Print a `.pos` template without opening the GUI.4
4
@@ -24,7 +24,7 @@ posprint link receipt.pos24
## Rules24
## Rules25
25
26
- **`posprint list`**: print discovered printers (USB, USB-serial, paired Classic Bluetooth SPP). Use the device path from the list as `-p`.26
- **`posprint list`**: print discovered printers (USB, USB-serial, paired Classic Bluetooth SPP). Use the device path from the list as `-p`.27
- **`--printer` / `-p`** (required): serial device path (e.g. `/dev/ttyUSB0`, `/dev/ttyACM0`, `/dev/rfcomm0` after OS pairing), or another printer id accepted by discovery.27
- **`--printer` / `-p`** (required): printer id from `posprint list` (e.g. `usb:28e9:0289:0:1` or `usb:28e9:0289`), `auto` (first discovered printer), or serial device path (e.g. `/dev/ttyUSB0`, `/dev/ttyACM0`, `/dev/rfcomm0`, `/dev/cu.usbserial-*`).28
- **`--set NAME=VALUE`** (required for every parameter): one flag per `${Name}` or `$[Name]` in the template.28
- **`--set NAME=VALUE`** (required for every parameter): one flag per `${Name}` or `$[Name]` in the template.29
- Checkbox params (`$[Name]`): use `true`/`false` (also `1`/`0`, `yes`/`no`).29
- Checkbox params (`$[Name]`): use `true`/`false` (also `1`/`0`, `yes`/`no`).30
- If any text `${Name}` is missing or empty, or a `$[Name]` is omitted / not a boolean, the command **fails**.30
- If any text `${Name}` is missing or empty, or a `$[Name]` is omitted / not a boolean, the command **fails**.@@ -143,6 +143,7 @@ const auto = posprintUrl(pos, {
143
143
|----------|-----|
144
144
| **Windows** | On each launch, posprint registers `HKCU\Software\Classes\posprint` → `"<exe>" "%1"` |
145
145
| **Linux (deb/Arch)** | Ships `posprint-url.desktop` with `MimeType=x-scheme-handler/posprint` |
146
| **macOS (`Posprint.app`)** | `Info.plist` registers `CFBundleURLSchemes = ["posprint"]` with LaunchServices and forwards `open location` AppleEvents to `posprint` |
146
147
147
148
After a Linux package install, if links do not open:
148
149
@@ -143,6 +143,7 @@ const auto = posprintUrl(pos, {143
|----------|-----|143
|----------|-----|144
| **Windows** | On each launch, posprint registers `HKCU\Software\Classes\posprint` → `"<exe>" "%1"` |144
| **Windows** | On each launch, posprint registers `HKCU\Software\Classes\posprint` → `"<exe>" "%1"` |145
| **Linux (deb/Arch)** | Ships `posprint-url.desktop` with `MimeType=x-scheme-handler/posprint` |145
| **Linux (deb/Arch)** | Ships `posprint-url.desktop` with `MimeType=x-scheme-handler/posprint` |146
| **macOS (`Posprint.app`)** | `Info.plist` registers `CFBundleURLSchemes = ["posprint"]` with LaunchServices and forwards `open location` AppleEvents to `posprint` |146
147
147
After a Linux package install, if links do not open:148
After a Linux package install, if links do not open:148
149
@@ -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>posprint-launcher</string>
11
<key>CFBundleIconFile</key>
12
<string>AppIcon</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>CFBundleVersion</key>
24
<string>@VERSION@</string>
25
<key>LSMinimumSystemVersion</key>
26
<string>11.0</string>
27
<key>LSUIElement</key>
28
<true/>
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>posprint-launcher</string>11
<key>CFBundleIconFile</key>12
<string>AppIcon</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>CFBundleVersion</key>24
<string>@VERSION@</string>25
<key>LSMinimumSystemVersion</key>26
<string>11.0</string>27
<key>LSUIElement</key>28
<true/>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,76 @@
1
#import <Cocoa/Cocoa.h>
2
#include <spawn.h>
3
#include <stdlib.h>
4
#include <unistd.h>
5
6
extern char **environ;
7
8
@interface AppDelegate : NSObject <NSApplicationDelegate>
9
@property (nonatomic, assign) BOOL handledOpen;
10
@end
11
12
@implementation AppDelegate
13
14
- (void)spawnPosprintWithArg:(NSString *)arg {
15
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
16
NSString *binPath = [bundlePath stringByAppendingPathComponent:@"Contents/MacOS/posprint-bin"];
17
const char *bin = [binPath fileSystemRepresentation];
18
19
pid_t pid;
20
if (arg != nil) {
21
const char *cArg = [arg UTF8String];
22
char *const argv[] = { (char *)bin, (char *)cArg, NULL };
23
posix_spawn(&pid, bin, NULL, NULL, argv, environ);
24
} else {
25
char *const argv[] = { (char *)bin, NULL };
26
posix_spawn(&pid, bin, NULL, NULL, argv, environ);
27
}
28
}
29
30
- (void)application:(NSApplication *)application openURLs:(NSArray<NSURL *> *)urls {
31
self.handledOpen = YES;
32
for (NSURL *url in urls) {
33
if ([url isFileURL]) {
34
[self spawnPosprintWithArg:[url path]];
35
} else {
36
[self spawnPosprintWithArg:[url absoluteString]];
37
}
38
}
39
_exit(0);
40
}
41
42
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
43
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(80 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
44
if (!self.handledOpen) {
45
[self spawnPosprintWithArg:nil];
46
}
47
_exit(0);
48
});
49
}
50
51
@end
52
53
int main(int argc, const char * argv[]) {
54
@autoreleasepool {
55
if (argc > 1) {
56
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
57
NSString *binPath = [bundlePath stringByAppendingPathComponent:@"Contents/MacOS/posprint-bin"];
58
const char *bin = [binPath fileSystemRepresentation];
59
char **newArgv = calloc((size_t)argc + 1, sizeof(char *));
60
newArgv[0] = (char *)bin;
61
for (int i = 1; i < argc; i++) {
62
newArgv[i] = (char *)argv[i];
63
}
64
newArgv[argc] = NULL;
65
pid_t pid;
66
posix_spawn(&pid, bin, NULL, NULL, newArgv, environ);
67
free(newArgv);
68
return 0;
69
}
70
NSApplication *app = [NSApplication sharedApplication];
71
AppDelegate *delegate = [[AppDelegate alloc] init];
72
[app setDelegate:delegate];
73
[app run];
74
}
75
return 0;
76
}
@@ -0,0 +1,76 @@1
#import <Cocoa/Cocoa.h>2
#include <spawn.h>3
#include <stdlib.h>4
#include <unistd.h>5
6
extern char **environ;7
8
@interface AppDelegate : NSObject <NSApplicationDelegate>9
@property (nonatomic, assign) BOOL handledOpen;10
@end11
12
@implementation AppDelegate13
14
- (void)spawnPosprintWithArg:(NSString *)arg {15
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];16
NSString *binPath = [bundlePath stringByAppendingPathComponent:@"Contents/MacOS/posprint-bin"];17
const char *bin = [binPath fileSystemRepresentation];18
19
pid_t pid;20
if (arg != nil) {21
const char *cArg = [arg UTF8String];22
char *const argv[] = { (char *)bin, (char *)cArg, NULL };23
posix_spawn(&pid, bin, NULL, NULL, argv, environ);24
} else {25
char *const argv[] = { (char *)bin, NULL };26
posix_spawn(&pid, bin, NULL, NULL, argv, environ);27
}28
}29
30
- (void)application:(NSApplication *)application openURLs:(NSArray<NSURL *> *)urls {31
self.handledOpen = YES;32
for (NSURL *url in urls) {33
if ([url isFileURL]) {34
[self spawnPosprintWithArg:[url path]];35
} else {36
[self spawnPosprintWithArg:[url absoluteString]];37
}38
}39
_exit(0);40
}41
42
- (void)applicationDidFinishLaunching:(NSNotification *)notification {43
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(80 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{44
if (!self.handledOpen) {45
[self spawnPosprintWithArg:nil];46
}47
_exit(0);48
});49
}50
51
@end52
53
int main(int argc, const char * argv[]) {54
@autoreleasepool {55
if (argc > 1) {56
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];57
NSString *binPath = [bundlePath stringByAppendingPathComponent:@"Contents/MacOS/posprint-bin"];58
const char *bin = [binPath fileSystemRepresentation];59
char **newArgv = calloc((size_t)argc + 1, sizeof(char *));60
newArgv[0] = (char *)bin;61
for (int i = 1; i < argc; i++) {62
newArgv[i] = (char *)argv[i];63
}64
newArgv[argc] = NULL;65
pid_t pid;66
posix_spawn(&pid, bin, NULL, NULL, newArgv, environ);67
free(newArgv);68
return 0;69
}70
NSApplication *app = [NSApplication sharedApplication];71
AppDelegate *delegate = [[AppDelegate alloc] init];72
[app setDelegate:delegate];73
[app run];74
}75
return 0;76
}@@ -0,0 +1,162 @@
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 "$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
162
fi
@@ -0,0 +1,162 @@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 "$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" ]]; then121
ICONSET_DIR="$(mktemp -d /tmp/posprint-icon.XXXXXX.iconset)"122
for size in 16 32 128 256 512; do123
sips -z "$size" "$size" "$ROOT/pos-logo.png" --out "$ICONSET_DIR/icon_${size}x${size}.png" >/dev/null 2>&1 || true124
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 || true126
done127
iconutil -c icns "$ICONSET_DIR" -o "$APP_DIR/Contents/Resources/AppIcon.icns" >/dev/null 2>&1 || true128
rm -rf "$ICONSET_DIR"129
fi130
131
codesign --force --deep --sign - "$APP_DIR"132
echo "macOS app bundle: $APP_DIR"133
134
if [[ "$BUILD_DMG" == "true" ]]; then135
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/null138
echo "macOS disk image: $DMG_PATH"139
ls -lh "$DMG_PATH"140
fi141
142
if want_install; then143
echo "Installing Posprint.app to /Applications/Posprint.app..."144
rm -rf /Applications/Posprint.app145
cp -R "$APP_DIR" /Applications/Posprint.app146
codesign --force --deep --sign - /Applications/Posprint.app147
148
if [[ -d "/opt/homebrew/bin" && -w "/opt/homebrew/bin" ]]; then149
cp "$BIN" /opt/homebrew/bin/posprint150
echo "Installed CLI binary: /opt/homebrew/bin/posprint"151
elif [[ -d "/usr/local/bin" && -w "/usr/local/bin" ]]; then152
cp "$BIN" /opt/homebrew/bin/posprint153
echo "Installed CLI binary: /opt/homebrew/bin/posprint"154
fi155
156
LSREGISTER="/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister"157
if [[ -x "$LSREGISTER" ]]; then158
"$LSREGISTER" -f /Applications/Posprint.app || true159
fi160
echo "Installed and registered posprint:// URL handler."161
fi162
fi@@ -72,7 +72,9 @@ fn datetime_to_tm(dt: &chrono::DateTime<Local>) -> tm {
72
72
tm_isdst: -1,
73
73
#[cfg(unix)]
74
74
tm_gmtoff: dt.offset().local_minus_utc() as libc::c_long,
75
#[cfg(unix)]
75
#[cfg(all(unix, target_os = "macos"))]
76
tm_zone: std::ptr::null_mut(),
77
#[cfg(all(unix, not(target_os = "macos")))]
76
78
tm_zone: std::ptr::null(),
77
79
}
78
80
}
@@ -72,7 +72,9 @@ fn datetime_to_tm(dt: &chrono::DateTime<Local>) -> tm {72
tm_isdst: -1,
72
tm_isdst: -1,
73
#[cfg(unix)]
73
#[cfg(unix)]
74
tm_gmtoff: dt.offset().local_minus_utc() as libc::c_long,
74
tm_gmtoff: dt.offset().local_minus_utc() as libc::c_long,
75
#[cfg(unix)]
75
#[cfg(all(unix, target_os = "macos"))]
76
tm_zone: std::ptr::null_mut(),
77
#[cfg(all(unix, not(target_os = "macos")))]
76
tm_zone: std::ptr::null(),
78
tm_zone: std::ptr::null(),
77
}
79
}
78
}
80
}
@@ -186,14 +186,38 @@ pub fn open_printer(target: &PrinterTarget) -> Result<Box<dyn PrinterTransport>,
186
186
}
187
187
}
188
188
189
/// Open by manual name: tries serial/COM first, then Windows spooler queue name.
189
/// Match a printer from `list_printers()` by `"auto"`, device `id`, preference key (`usb:vid:pid`), or `display_name`.
190
pub fn find_matching_printer<'a>(
191
printers: &'a [PrinterDevice],
192
name: &str,
193
) -> Option<&'a PrinterDevice> {
194
let name = name.trim();
195
if name.is_empty() {
196
return None;
197
}
198
if name.eq_ignore_ascii_case("auto") {
199
return printers.first();
200
}
201
printers.iter().find(|p| {
202
p.id == name || printer_preference_key(&p.target) == name || p.display_name == name
203
})
204
}
205
206
/// Open by manual name: tries discovered printers (`id`, `usb:vid:pid`, `"auto"`), then serial/COM, then Windows spooler queue name.
190
207
pub fn open_printer_by_name(name: &str) -> Result<Box<dyn PrinterTransport>, PrintError> {
191
208
let name = name.trim();
192
209
if name.is_empty() {
193
210
return Err(PrintError::NotFound);
194
211
}
195
212
196
if let Ok(transport) = SerialTransport::open(name) {
213
if let Ok(printers) = list_printers() {
214
if let Some(matched) = find_matching_printer(&printers, name) {
215
return open_printer(&matched.target);
216
}
217
}
218
219
let serial_name = name.strip_prefix("serial:").unwrap_or(name);
220
if let Ok(transport) = SerialTransport::open(serial_name) {
197
221
return Ok(Box::new(transport));
198
222
}
199
223
@@ -425,4 +449,43 @@ mod tests {
425
449
None
426
450
);
427
451
}
452
453
#[test]
454
fn matches_discovered_printer_by_id_key_or_auto() {
455
let printers = vec![
456
PrinterDevice {
457
id: "usb:28e9:0289:0:1".into(),
458
display_name: "micro-printer — USB 28e9:0289 (direct)".into(),
459
target: PrinterTarget::UsbDirect {
460
vid: 0x28e9,
461
pid: 0x0289,
462
bus: 0,
463
address: 1,
464
},
465
},
466
PrinterDevice {
467
id: "serial:/dev/cu.usbserial-1410".into(),
468
display_name: "/dev/cu.usbserial-1410 — POS-58 (serial 0416:5011)".into(),
469
target: PrinterTarget::Serial("/dev/cu.usbserial-1410".into()),
470
},
471
];
472
assert_eq!(
473
find_matching_printer(&printers, "auto").map(|p| p.id.as_str()),
474
Some("usb:28e9:0289:0:1")
475
);
476
assert_eq!(
477
find_matching_printer(&printers, "usb:28e9:0289:0:1").map(|p| p.id.as_str()),
478
Some("usb:28e9:0289:0:1")
479
);
480
assert_eq!(
481
find_matching_printer(&printers, "usb:28e9:0289").map(|p| p.id.as_str()),
482
Some("usb:28e9:0289:0:1")
483
);
484
assert_eq!(
485
find_matching_printer(&printers, "serial:/dev/cu.usbserial-1410").map(|p| p.id.as_str()),
486
Some("serial:/dev/cu.usbserial-1410")
487
);
488
assert!(find_matching_printer(&printers, "usb:9999:9999").is_none());
489
assert!(find_matching_printer(&[], "auto").is_none());
490
}
428
491
}
@@ -186,14 +186,38 @@ pub fn open_printer(target: &PrinterTarget) -> Result<Box<dyn PrinterTransport>,186
}
186
}
187
}
187
}
188
188
189
/// Open by manual name: tries serial/COM first, then Windows spooler queue name.
189
/// Match a printer from `list_printers()` by `"auto"`, device `id`, preference key (`usb:vid:pid`), or `display_name`.
190
pub fn find_matching_printer<'a>(
191
printers: &'a [PrinterDevice],
192
name: &str,
193
) -> Option<&'a PrinterDevice> {
194
let name = name.trim();
195
if name.is_empty() {
196
return None;
197
}
198
if name.eq_ignore_ascii_case("auto") {
199
return printers.first();
200
}
201
printers.iter().find(|p| {
202
p.id == name || printer_preference_key(&p.target) == name || p.display_name == name
203
})
204
}
205
206
/// Open by manual name: tries discovered printers (`id`, `usb:vid:pid`, `"auto"`), then serial/COM, then Windows spooler queue name.
190
pub fn open_printer_by_name(name: &str) -> Result<Box<dyn PrinterTransport>, PrintError> {
207
pub fn open_printer_by_name(name: &str) -> Result<Box<dyn PrinterTransport>, PrintError> {
191
let name = name.trim();
208
let name = name.trim();
192
if name.is_empty() {
209
if name.is_empty() {
193
return Err(PrintError::NotFound);
210
return Err(PrintError::NotFound);
194
}
211
}
195
212
196
if let Ok(transport) = SerialTransport::open(name) {
213
if let Ok(printers) = list_printers() {
214
if let Some(matched) = find_matching_printer(&printers, name) {
215
return open_printer(&matched.target);
216
}
217
}
218
219
let serial_name = name.strip_prefix("serial:").unwrap_or(name);
220
if let Ok(transport) = SerialTransport::open(serial_name) {
197
return Ok(Box::new(transport));
221
return Ok(Box::new(transport));
198
}
222
}
199
223
@@ -425,4 +449,43 @@ mod tests {425
None
449
None
426
);
450
);
427
}
451
}
452
453
#[test]
454
fn matches_discovered_printer_by_id_key_or_auto() {
455
let printers = vec![
456
PrinterDevice {
457
id: "usb:28e9:0289:0:1".into(),
458
display_name: "micro-printer — USB 28e9:0289 (direct)".into(),
459
target: PrinterTarget::UsbDirect {
460
vid: 0x28e9,
461
pid: 0x0289,
462
bus: 0,
463
address: 1,
464
},
465
},
466
PrinterDevice {
467
id: "serial:/dev/cu.usbserial-1410".into(),
468
display_name: "/dev/cu.usbserial-1410 — POS-58 (serial 0416:5011)".into(),
469
target: PrinterTarget::Serial("/dev/cu.usbserial-1410".into()),
470
},
471
];
472
assert_eq!(
473
find_matching_printer(&printers, "auto").map(|p| p.id.as_str()),
474
Some("usb:28e9:0289:0:1")
475
);
476
assert_eq!(
477
find_matching_printer(&printers, "usb:28e9:0289:0:1").map(|p| p.id.as_str()),
478
Some("usb:28e9:0289:0:1")
479
);
480
assert_eq!(
481
find_matching_printer(&printers, "usb:28e9:0289").map(|p| p.id.as_str()),
482
Some("usb:28e9:0289:0:1")
483
);
484
assert_eq!(
485
find_matching_printer(&printers, "serial:/dev/cu.usbserial-1410").map(|p| p.id.as_str()),
486
Some("serial:/dev/cu.usbserial-1410")
487
);
488
assert!(find_matching_printer(&printers, "usb:9999:9999").is_none());
489
assert!(find_matching_printer(&[], "auto").is_none());
490
}
428
}
491
}
@@ -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,13 @@ 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-launcher</string>"));
298
assert!(plist.contains("<string>posprint</string>"));
299
assert!(plist.contains("<string>pos</string>"));
300
assert!(plist.contains("@VERSION@"));
301
}
@@ -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,13 @@ 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-launcher</string>"));298
assert!(plist.contains("<string>posprint</string>"));299
assert!(plist.contains("<string>pos</string>"));300
assert!(plist.contains("@VERSION@"));301
}