# Distribution and updates

## Distributing the authoring app (Liminalia Studio)

Everything up to here is about exported games. Studio (`packages/studio-desktop`) uses the same signing module (`packages/cli/src/sign`) and the same update format (`liminalia-update`,
`packages/desktop/update.cjs`). The settings are passed not in `liminalia.json` but as environment variables to `npm run studio:package`. If something is missing
or a value can't be used, it stops before building. If nothing is passed, it works as before (ad hoc signing on Mac, no signing on Windows, and no update checks).

### Signing and notarization

| Environment variable | Meaning |
|---|---|
| `LIMINALIA_MAC_SIGN_IDENTITY` | The name or SHA-1 of the Developer ID certificate. Stops if it isn't in the keychain. `-` or empty means ad hoc signing |
| `LIMINALIA_NOTARY_PROFILE` and others | Notarization credentials (the same as in "Mac (Developer ID and notarization)" above: `LIMINALIA_NOTARY_KEY` `_KEY_ID` `_ISSUER`, `LIMINALIA_NOTARY_APPLE_ID` `_PASSWORD` `_TEAM_ID`). Without them, the app is only signed |
| `LIMINALIA_NOTARIZE=0` | Don't notarize even if credentials are available |
| `LIMINALIA_WIN_PFX`, `LIMINALIA_WIN_PFX_PASSWORD` | The Windows PFX (signed with `osslsigncode` on Mac and with `signtool` on Windows) |
| `LIMINALIA_WIN_SIGN_COMMAND` | An external signing command for Windows (`{file}` is replaced with `Liminalia Studio.exe`) |

```bash
LIMINALIA_MAC_SIGN_IDENTITY="Developer ID Application: Example Inc. (ABCDE12345)" LIMINALIA_NOTARY_PROFILE=liminalia-notary \
  npm run studio:package -- mac
```

- On Mac, `@electron/osx-sign` signs from the inside out with the hardened runtime, entitlements and a timestamp. Studio contains many images and fonts,
  so only the executables and native libraries (Mach-O) are signed. Since the app loads the code of project plugins, library validation is turned off in the entitlements
- After notarization passes, the app is stapled, and then the zip and dmg are made (the dmg itself isn't signed; the app inside is stapled). If notarization fails, it stops
- For a notarized app, `liminaliaStudio.notarized` is embedded in `package.json`. The help in a notarized app doesn't show "Opening it for the first time on a Mac".
  On Windows, a signed app (`codeSigned`) doesn't show "Opening it for the first time on Windows"
- On Windows, only `Liminalia Studio.exe` is signed (`esbuild.exe` and the others inside are only run as child processes, so they aren't signed)

### Windows installer

`npm run studio:package -- windows` makes `Liminalia-Studio-<version>-windows-x64-Setup.exe` in addition to the folder and zip (with the same NSIS module as game exports,
`packages/cli/src/installer/nsis.ts`. If makensis is missing, the installer isn't made and the output shows how to install makensis. `LIMINALIA_INSTALLER=0` skips it).

- It installs for the current user only (`%LOCALAPPDATA%\Programs\Liminalia Studio`; no administrator rights are needed, and the app's updater can replace the app itself). The screens are in Japanese and English.
  It creates Start menu and desktop shortcuts and an uninstall entry in Apps & features (key `LiminaliaStudio`). If signing is set up, the installer is signed too
- App updates replace the whole folder, so the swap script (`WINDOWS_SWAP_SCRIPT` in `app-update.ts`) copies `Uninstall.exe` into the new version.
  Uninstalling deletes the installed files plus the items of Electron and the app (`resources`, `locales` and so on, with their contents) (nothing outside the install folder is touched)
- It can be built on a Mac too (`brew install makensis`). makensis on Mac and Linux can't read scripts containing Japanese unless the locale is UTF-8,
  so it's run with `LC_ALL` set to UTF-8 (game exports do the same)
- `.github/workflows/studio-windows.yml` checks on `windows-latest` that the app launches and that silent install and uninstall (`/S`) work, and keeps `Setup.exe` as an artifact (studio-windows-setup, for 7 days)
  (on pushes to main and PRs that change `packages/studio-desktop`, the installer build, or the dependencies, once a week, and manually).
  To build it by hand, run `npm ci` and then `npm run studio:package -- windows` on Windows (get NSIS from https://nsis.sourceforge.io/Download or with `choco install nsis`)

### App updates

Shortly after launch, and from "Check for Updates…" in the menu (the app menu on Mac, Help on Windows), Studio reads `update.json` from the update server. If there's a new version, a small window
shows the version and what changed, and if the user chooses to update, the update proceeds in this order.

1. Download the zip to `userData/updates/` and check its SHA-256 and size (if they don't match, discard it and stop)
2. Extract it into `.liminalia-studio-update/` next to the app, and check that it's the same app at the expected version. On Mac, it must also pass `codesign --verify --deep --strict`,
   and if the current app has a Developer ID signature, only a signature from the same Team is accepted
3. Close the project windows the usual way (finishing any pending saves. If the user cancels closing, the update is canceled too)
4. Run a small swap script (`sh` on Mac, PowerShell on Windows; `userData/updates/swap.*`, logged to `swap.log`) detached, and quit the app.
   The script waits for the app to exit, moves the old app aside, puts the new app in place and launches it. If something fails along the way, it gives the old app back its original name and launches it
5. The script doesn't delete the old app; it waits up to 60 seconds for a marker showing that the new app started (`userData/updates/started`). If it appears, the old app is deleted. If not, the script stops the new app, goes back to the old one and opens it
6. The launched app checks the result of the previous swap and cleans up (if it's the new version, it creates the marker). If the swap failed or the previous version was restored, it tells the user that this is the previous version and where the downloaded file is

`electron-updater` and Squirrel aren't used (so that updates work even for apps without signing or notarization). When the app is in a place that can't be written to (a read-only volume, when Gatekeeper
is running it from a moved location, Program Files and so on), it isn't replaced, and the download location is shown instead. On Windows, the whole folder of the exe is replaced, so if there's even one item directly inside it
that isn't recognizably the app's (one that's neither among Electron's items nor in the list of top-level items in the new zip), the app again isn't replaced, and the reason and the download location are shown.
Updates don't start during an export. If an export starts after the download, the update stops before the project is closed and the user is told (the downloaded file is used next time).

#### Making the update files

The repository is private and GitHub Releases can't be downloaded anonymously, so the update URL is embedded at packaging time. Without it, Studio doesn't check for updates, and the menu has no item for it.
When building in CI, putting it in the variable `LIMINALIA_STUDIO_UPDATE_URL` embeds it ("Release CI" below).

```bash
# 1. Package with the update URL embedded (an environment variable, or "liminaliaStudio": { "updateUrl": "…" } in packages/studio-desktop/package.json; the environment variable comes first)
LIMINALIA_STUDIO_UPDATE_URL=https://example.com/studio/update.json npm run studio:package -- all

# 2. Prepare update.json and the zips in out/<version>/update/ (--notes or --notes-file: a string, or JSON such as { "ja": "…", "en": "…" })
npm run studio:update-manifest -- --notes-file notes.json

# 3. Put the contents of out/<version>/update/ on the update server
```

- The format is `liminalia-update`, the same as in "Updates" above. Mac builds are distributed per CPU, so `app.files` gets the extra keys `mac-arm64` and `mac-x64`
  (`mac` if built with `--universal`). Windows is `windows`. Studio looks for the CPU key first and uses the OS key if there's none. Games don't pass the CPU, so they read `mac` as before
- Zip URLs are written relative to the location of `update.json` (`studio:update-manifest` does this. Absolute URLs are checked the same way)
- Only https is used. For testing, http is allowed only for `http://127.0.0.1` (stricter than games; `localhost` isn't accepted either). Redirects aren't followed automatically, and the same restrictions apply to each hop (up to 5)
- Signing the manifest works the same way as for games ("Signing the manifest" above). Make a key pair with `lim update-key --out <file>` (pass a project folder),
  and run `studio:package` with `LIMINALIA_STUDIO_UPDATE_PUBLIC_KEY=<public key>`; the app then only uses an `update.json` whose signature verifies.
  `studio:update-manifest` signs with `LIMINALIA_STUDIO_UPDATE_SIGN_KEY=<private key .pem>` (and `LIMINALIA_STUDIO_UPDATE_PUBLIC_KEY` to check that the keys pair)
- If `app.files` has no file for this OS and there's an `app.page`, the update window only opens the download page

To test with real packaged apps, run `npx tsx packages/studio-desktop/scripts/update-check.ts [--shots folder] [--keep]` (Mac only).
It makes an app with a lower version and one with a higher version in `out/`, serves them on 127.0.0.1, and goes through the redirect limits, a hash mismatch, a read-only volume, an export in progress, canceling the close, swapping and relaunching, and going back from a version that crashes on launch to the previous one.

### Release CI

When a `v1.2.3` tag is pushed (and when it's run by hand), `.github/workflows/release.yml` builds and signs the authoring app's Mac version (zips and dmgs for Apple Silicon and Intel) and
Windows version (a zip and the installer), makes the update manifest (`update.json`) if the update URL has been decided, builds and checks the npm package (the npm package section of the README;
`lim` installed into an empty folder runs `new`, `check`, `test`, `build browser` and `dev`), and puts them in a draft GitHub Release. Check the draft, then publish it by hand.
The tarballs of the two packages that go to npm (`@liminalia/sdk`, the plugin contract, and `@liminalia/cli`, which provides `lim`) are also put in the distribution repository's Release.
They're published to npm from the public repository (`skyt-a/liminalia-sdk`; see "Publishing to npm" below).

All jobs run on the development Mac's self-hosted runner (label `liminalia-mac`, Apple Silicon, installed in `~/actions-runner-liminalia` as a service that starts at login),
and use neither GitHub's runner minutes nor its artifact storage. Files are passed between jobs in `~/liminalia-release-stage/<run_id>` on that Mac (removed after 14 days).
The Windows version is built from the Mac, so launching on Windows isn't checked (check it with `studio-windows.yml` or on a real machine). Keep that Mac awake when pushing a tag.

1. Set the version with `npm run engine-version -- set <version>` and commit (the root `package.json` is the source, and the authoring app, `lim --version` and the site's download page show this version too.
   If it differs from the tag's version, CI stops. [versioning.md](../versioning.md))
2. Create an annotated tag (`git tag -a v1.2.3 -m "What changed"`; the annotation becomes "What's new" in the update window) and push it
3. Check the draft Release and publish it. Publishing it makes `publish-vscode.yml` publish the VS Code extension ("Publishing to the Marketplace and Open VSX" in [vscode.md](../vscode.md))
4. Publish to npm ("Publishing to npm" below)
5. Put the update on the update server (run `deploy-studio-update.yml` with the version)

#### Publishing to npm

`.github/workflows/publish-npm.yml` in the public repository `skyt-a/liminalia-sdk` (the source is `scripts/public/publish-npm.yml`, copied by `scripts/public-mirror.mjs`)
publishes to npm with OIDC (trusted publishing). No npm token is used.

- It builds from that repository's source (`node scripts/npm-pack-public.mjs`) and checks that the unpacked contents are the same as the tarballs that `release.yml` built, checked and put
  in the distribution repository before publishing (if anything differs, nothing is published). It adds a provenance statement
- It stages the packages with `npm stage publish`. Review them under Staged Packages on npmjs.com and approve them with 2FA to publish them. sdk and cli have the same version, so approve both together
- A version with `-` (`1.2.3-beta.1`) gets the `next` dist-tag. Versions that are already on npm are skipped

Steps (after publishing the draft Release):

1. Copy into a checkout of the public repository: `node scripts/public-mirror.mjs --repo <checkout> --check` (it also checks, in the copy, that the npm tarballs are the same as the ones built from the source repository)
2. Push the copied commit, then create an annotated tag `v<version>` and push it. Pushing the tag runs `publish-npm.yml` (when running it by hand, enter the version)
3. Check the two packages under Staged Packages on npmjs.com and approve them

In each npm package's settings (Settings → Trusted publishing), set GitHub Actions, `skyt-a`, `liminalia-sdk` and `publish-npm.yml`.
If nothing is published within 2 days of setting it, the setting expires (an npm rule). A package that doesn't exist yet has no settings page, so for a package published for the first time,
stage an earlier version with `npm stage publish` from your own machine to create the package (a placeholder version `0.0.0-stage`), and set it while that version is still staged.
Reject the version you staged by hand only after approving the version that CI published (rejecting it while there's no other version removes the package along with the placeholder; checked on 2026-11-22).
Trusted publishing can't be used from self-hosted runners, so this workflow runs on GitHub's runners (it's a public repository, so it doesn't use the minutes).

Keys come from Secrets (Settings → Secrets and variables → Actions). If one is missing, that signing is skipped and the build continues, and what was skipped is shown in the job summary and annotations.
Incomplete combinations (a certificate without its password, only the notarization key, and so on) stop at that step (`scripts/release.mjs`).

For the first release, enroll in the Apple Developer Program and obtain a Developer ID Application certificate and notarization credentials. Until then, use a manual build with draft creation unchecked. Tags and draft releases require Mac signing and notarization. For unsigned Windows builds leave the Windows certificate, password and `LIMINALIA_RELEASE_REQUIRE_SIGNING` unset. Windows may display a SmartScreen warning. Configuring an update URL for distribution also requires the Ed25519 signing key pair.

| Secret | Contents | If missing |
|---|---|---|
| `LIMINALIA_MAC_CERT_P12`, `LIMINALIA_MAC_CERT_PASSWORD` | The .p12 exported from the Developer ID Application certificate and its private key, in base64 (`base64 -i cert.p12`), and its password. They're imported into a temporary keychain in CI, which is deleted afterward | Ad hoc signing |
| `LIMINALIA_NOTARY_KEY_P8`, `LIMINALIA_NOTARY_KEY_ID`, `LIMINALIA_NOTARY_ISSUER` | The App Store Connect API key used for notarization (the contents of the .p8), the key ID and the Issuer ID | Signed only (not notarized) |
| `LIMINALIA_WIN_PFX_BASE64`, `LIMINALIA_WIN_PFX_PASSWORD` | The Windows code signing certificate (the .pfx in base64) and its password. `signtool` signs `Liminalia Studio.exe` and the installer | Not signed |
| `LIMINALIA_STUDIO_UPDATE_SIGN_KEY_PEM` | The private key for signing the update manifest (the contents of the .pem made with `lim update-key`). Used together with the variable `LIMINALIA_STUDIO_UPDATE_PUBLIC_KEY` | The manifest isn't signed (if only the public key variable is set, it stops) |
| `LIMINALIA_STUDIO_UPDATE_DEPLOY_TOKEN` | The key used by the command that puts the update on the update server (`deploy-studio-update.yml`) | The command runs without a key |

| Variable | Contents |
|---|---|
| `LIMINALIA_STUDIO_UPDATE_URL` | Where the authoring app's updates are hosted (the https URL of `update.json`). It's embedded in the app that's built. Without it, the app doesn't check for updates, and `update.json` isn't made |
| `LIMINALIA_STUDIO_UPDATE_PUBLIC_KEY` | The public key that verifies the update manifest's signature (embedded in the app) |
| `LIMINALIA_MAC_SIGN_IDENTITY` | The name or SHA-1 of the certificate used for signing (without it, the Developer ID Application certificate that was imported is used) |
| `LIMINALIA_RELEASE_REQUIRE_SIGNING` | If `true`, stops when the credentials for Mac signing and notarization or for Windows signing are missing. Set it once you have the keys, so that no unsigned release goes out |
| `LIMINALIA_STUDIO_UPDATE_DEPLOY_COMMAND` | The command (bash) that puts the update on the update server. `$LIMINALIA_STUDIO_UPDATE_DIR` contains `update.json` and the zips |

Where updates are hosted is for a person to decide. Once it's decided, set `LIMINALIA_STUDIO_UPDATE_URL` and put the files there in one of these ways.

- GitHub Releases: they can be downloaded anonymously only if the repository is public. If you set `https://github.com/<repository>/releases/latest/download/update.json`,
  the files are in place as soon as the Release is published (zip URLs are relative to `update.json`, so they point to the zips in the same Release. Up to 5 redirects are followed)
- Another host (Cloudflare R2 and so on): write the command that puts the files there in `LIMINALIA_STUDIO_UPDATE_DEPLOY_COMMAND`. For example (R2; the key is an API token that can write to Workers R2):
  ```bash
  for f in "$LIMINALIA_STUDIO_UPDATE_DIR"/*; do CLOUDFLARE_API_TOKEN="$LIMINALIA_STUDIO_UPDATE_DEPLOY_TOKEN" npx wrangler r2 object put "<bucket>/studio/$(basename "$f")" --file "$f" --remote; done
  ```

Not yet verified: signing and notarization in CI with real certificates (including importing into the temporary keychain), signing with `signtool`, publishing to npm (`publish-npm.yml` in the public repository), and `deploy-studio-update.yml`.
Skipping and stopping when keys are missing, and the version check, are tested by `scripts/test/release.test.mjs`, the npm package by `node scripts/npm-pack.mjs --verify`,
and the npm publishing order, skipping versions that are already there and comparing contents by `scripts/test/npm-publish.test.mjs` and `node scripts/npm-publish.mjs <tarball folder> --version <version> --dry-run`.

### Where problem reports are received

"Help → Report a Problem…" in the authoring app puts what happened, the app version and OS, an excerpt of the frontmost project's settings, and the end of the log into one report,
and before anything is sent, it shows the contents and lets the user choose what to include (the home folder is shown as `~`). Once you've decided where to receive reports (GitHub Issues or Discussions, a form and so on),
write its https URL in `LIMINALIA_STUDIO_REPORT_URL` (or `"liminaliaStudio": { "reportUrl": "…" }` in `packages/studio-desktop/package.json`) when packaging.
`{title}` and `{body}` in the URL are replaced with the title and the contents (Markdown).

```bash
LIMINALIA_STUDIO_REPORT_URL='https://github.com/<owner>/<repository>/issues/new?title={title}&body={body}' npm run studio:package
```

When the contents are too long for the URL (about 6,000 characters), or the URL has no `{body}`, the contents are copied to the clipboard before the page opens, and the user pastes them in.
Without a place to receive reports, "Open the report page" isn't shown, and users pass reports on by copying them or saving them to a file (`.md`).

### What hasn't been verified

Signing and notarization with a real Developer ID certificate, updating a notarized app (including how macOS's App Management treats it in `/Applications`), and swapping on a real Windows machine
can't be verified on the development Mac. Launching on Windows and the swap script (PowerShell) are run on `windows-latest` by `.github/workflows/studio-windows.yml` (once a week when files the app depends on have changed, and manually). Game exports (Mac, Windows, Linux, and Android) and launching the Mac build are checked by running `.github/workflows/platform-builds.yml` manually.
