4.4 KiB
4.4 KiB
| name | description |
|---|---|
| macos-selfcontained-webapp | Build or refactor a project so a web app runs as a self-contained macOS app: embedded local backend binary + SwiftUI/WKWebView shell + packaging scripts. Use when a user has web logic and wants no external browser, no separate web-code install, local execution from inside .app, and repeatable DMG packaging. |
macOS Self-Contained Web App
Outcome
Deliver one installable macOS app that:
- launches an embedded backend executable from app resources
- opens the web UI only inside
WKWebView - persists settings locally and supports native-to-web sync
- provides native app-level help/onboarding (not only in the web UI)
- can be packaged as a DMG
Layout Standard
Use this layout for new projects:
web/src/for web backend source (app.py, modules, requirements)mac/src/for Xcode project and SwiftUI shell sourcesscripts/for build and packaging automationdocs/for architecture and onboarding docs
For existing repos, avoid destructive renames unless explicitly requested. Add compatibility paths or migration commits in small steps.
Detailed naming guidance: references/layout-and-naming.md
Workflow
- Inventory existing architecture.
- Identify backend entrypoint, runtime dependencies, and static/resource files.
- Confirm current launch mode and where browser auto-open is happening.
- Create embedded backend launcher.
- Add a thin launcher (
backend_embedded_launcher.pyor equivalent) that starts the web server on127.0.0.1and reads port from env (for exampleMANESH_TRADER_PORT). - Ensure browser auto-open is disabled.
- Build backend into a single executable.
- Add
scripts/build_embedded_backend.shthat compiles backend into a one-file executable and copies it into the mac target resource folder. - Use generic discovery:
- discover
*.xcodeprojundermac/src - discover scheme via
xcodebuild -list -json(fallback to project name) - discover/create
EmbeddedBackendfolder under selected project sources
- discover
- Include all required web files/modules in build inputs.
- Default backend name should be stable (for example
WebBackend) and configurable via env.
- Implement SwiftUI host shell.
- Use
@Observablehost state (noObservableObject/Combine). - Start/stop backend process, detect available local port, and handle retries.
- Render URL in
WKWebViewonly. - Keep app responsive when backend fails and surface actionable status.
- Resolve backend executable by checking both current and legacy names during migration.
- Add a native toolbar
Helpbutton that opens bundled help content in-app.
- Define settings sync contract.
- Use shared settings file (for example
~/.<app>/settings.json) as source of truth. - Normalize settings both in web app and native app.
- Pass effective settings via URL query params on launch/reload/restart.
- Keep onboarding limited to starter fields; preserve advanced fields.
- Add web-only fallback access to help/onboarding (for users not using the mac wrapper).
- Automate packaging.
- Add
scripts/build_selfcontained_mac_app.shto build embedded backend then Xcode app. - Add
scripts/create_installer_dmg.shfor distributable DMG. - Use generic app bundle discovery for DMG defaults where possible.
- Standardize installer artifacts under
build/dmg/(not repo root). - Ensure
.gitignoreexcludes generated artifacts (build/,*.dmg, temprw.*.dmg).
- Validate.
- Python syntax:
python -m py_compileon web entrypoint. - Tests:
PYTHONPATH=web/src pytest -q web/src/tests. - Xcode build:
xcodebuild ... build. - Runtime check: no external browser opens, webview loads locally, settings persist across relaunch.
- Verify help works in both modes:
- native toolbar help popup in mac app
- sidebar help fallback in web-only mode
Required Deliverables
- Embedded backend build script
- macOS app host that launches backend + WKWebView
- Shared settings sync path
- Native help/onboarding popup + web fallback help entry
- README section for local build + DMG workflow
- Verified build commands and final artifact locations
.gitignoreupdated for build/installer outputs
References
- Layout and migration rules:
references/layout-and-naming.md - Implementation blueprint and command templates:
references/implementation-blueprint.md
Bundled Script
Use scripts/scaffold_web_mac_layout.sh to create a new standardized folder skeleton for fresh projects.