diff options
| -rw-r--r-- | README.md | 26 | ||||
| -rw-r--r-- | mise.toml | 205 | ||||
| -rw-r--r-- | scripts/test-release-task.sh | 87 |
3 files changed, 314 insertions, 4 deletions
@@ -13,17 +13,24 @@ idea in Zig and AppKit, written without his code. The behavior and the colors fo [joeysim/write-on-replica](https://github.com/joeysim/write-on-replica), whose design notes describe his clips in detail. It is not connected with him or with 37signals. +## Install + +Releases are zips on Codeberg for Apple silicon, built for macOS 13 or later (only tested on macOS 26). Unzip one and move `Write_Away.app` to `/Applications`. The app is not notarized, so macOS refuses it the first time: try to open it, then choose **System Settings → Privacy & Security → Open Anyway**. Each zip's checksum is in the release's `SHA256SUMS`. **Write_Away → About Write_Away** shows the version. + ## Build and run -Use Zig **0.16.0**, the Xcode Command Line Tools, and a sibling checkout of `libpantograph` at `../libpantograph`. Tested on Apple silicon. If Homebrew supplies a newer Zig, use `export PATH=/opt/zig:$PATH` with the 0.16 installation here. +Use [mise](https://mise.jdx.dev), which supplies Zig **0.16.0**, and the Xcode Command Line Tools, on Apple silicon. The first build fetches [libpantograph](https://codeberg.org/teepark/libpantograph) at the revision pinned in `build.zig.zon`. ```sh -zig build # zig-out/Write_Away.app +mise trust # once: let mise read this repo's tasks +mise run build # zig-out/Write_Away.app open zig-out/Write_Away.app -zig build run -- piece.md # open a piece, or start one at that path -zig build test # the document model, then the app driven with no window +mise run run -- piece.md # open a piece, or start one at that path +mise run test # the document model, then the app driven with no window ``` +`build:local` and `test:local` use a libpantograph checkout at `../libpantograph` instead. `mise run pin:libpanto` moves the pin. `mise tasks` lists the rest. + A normal launch reopens the last file you had open, using its current contents on disk. On the first launch, after **File → New**, or if the file can’t be read, you get an empty **Untitled** draft instead. Opening a file explicitly takes precedence. Unsaved text is not restored. ### Launch from the terminal @@ -132,6 +139,17 @@ Legacy Overflow trailers and v1 records are supported. For drafts saved under an A separate Markdown view, source-stripped public export, sharing/posting, deletion animations, crash recovery, and multiple windows are not implemented. +## Releasing + +The version is the `.version` in `build.zig.zon`; `mise run version x.y.z` rewrites it and the build copies it into `Info.plist`. In panto, `/workflow:ship` does everything: commits the work, bumps the version, pushes `main`, and releases. By hand, with the work committed, `main` at `@-` and pushed, and nothing in the working copy: + +```sh +DRY_RUN=1 mise run release # test, build and check the zip, and stop there +mise run release # and tag, push the tag, and publish on Codeberg +``` + +Both need a remote named `codeberg`, and a `CODEBERG_TOKEN` with repository write access in `mise.local.toml` (ignored by version control). A release stays a draft until its zip and checksum are uploaded; if it fails partway, run it again. + ## The code - `src/doc.zig` and `src/ghost_io.zig`: variants, recoverable ghosts, and the Markdown format. diff --git a/mise.toml b/mise.toml new file mode 100644 index 0000000..2bc1323 --- /dev/null +++ b/mise.toml @@ -0,0 +1,205 @@ +[tools] +zig = "0.16.0" + +# The plain tasks build against the libpantograph revision pinned in build.zig.zon; +# the :local ones against the checkout beside this repo. +[tasks.build] +description = "Build zig-out/Write_Away.app" +run = "zig build" + +[tasks."build:local"] +description = "Build zig-out/Write_Away.app against the sibling libpantograph checkout" +run = "zig build --fork=../libpantograph" + +[tasks.run] +description = "Run the editor (mise run run -- file.md)" +run = "zig build run --" + +[tasks.test] +description = "Run the checks" +run = "zig build test" + +[tasks."test:local"] +description = "Run the checks against the sibling libpantograph checkout" +run = "zig build test --fork=../libpantograph" + +[tasks."test:release-task"] +description = "Test release publication against a local fake Forgejo" +run = "sh scripts/test-release-task.sh" + +[tasks."build:release"] +description = "Build the signed (ad hoc), zipped app and its checksum in zig-out/release" +run = ''' +set -eu +VER="$(sed -n 's/^ \.version = "\([^"]*\)",$/\1/p' build.zig.zon)" +[ -n "$VER" ] || { echo "could not read version from build.zig.zon" >&2; exit 1; } +OUT=zig-out/release +rm -rf "$OUT" +zig build -Doptimize=ReleaseSafe --prefix "$OUT" +# Zig signs the executable alone. The bundle needs a seal of its own, or a +# downloaded copy is reported as damaged. +codesign --force --sign - "$OUT/Write_Away.app" +ditto -c -k --keepParent "$OUT/Write_Away.app" "$OUT/Write_Away-$VER-macos-arm64.zip" +rm -rf "$OUT/Write_Away.app" +cd "$OUT" +shasum -a 256 ./*.zip | sed 's#\./##' > SHA256SUMS +''' + +[tasks."check:release"] +description = "Check the release zip: checksum, signature, version, and the app's own checks" +run = ''' +set -eu +VER="$(sed -n 's/^ \.version = "\([^"]*\)",$/\1/p' build.zig.zon)" +TMP="$(mktemp -d)" +trap 'rm -rf "$TMP"' 0 +( + cd zig-out/release + shasum -a 256 -c SHA256SUMS +) +ditto -x -k "zig-out/release/Write_Away-$VER-macos-arm64.zip" "$TMP" +APP="$TMP/Write_Away.app/Contents" +BIN="$APP/MacOS/write_away" +codesign --verify --deep --strict "$TMP/Write_Away.app" +GOT="$(plutil -extract CFBundleShortVersionString raw "$APP/Info.plist")" +[ "$GOT" = "$VER" ] || { echo "Info.plist says $GOT, build.zig.zon says $VER" >&2; exit 1; } +[ "$(lipo -archs "$BIN")" = arm64 ] || { echo "not an arm64 binary" >&2; exit 1; } +MIN="$(plutil -extract LSMinimumSystemVersion raw "$APP/Info.plist")" +GOT="$(otool -l "$BIN" | awk '$1 == "minos" { print $2 }')" +[ "$GOT.0" = "$MIN" ] || { echo "the binary needs macOS $GOT, Info.plist says $MIN" >&2; exit 1; } +# Outside the bundle, so it cannot reach the installed app's preferences. +cp "$BIN" "$TMP/write_away" +WRITE_AWAY_SELFTEST=1 "$TMP/write_away" +''' + +[tasks.version] +description = "Rewrite build.zig.zon for a new version" +usage = 'arg "<version>" help="x.y.z"' +run = ''' +set -eu +VERSION="$usage_version" +if ! printf '%s\n' "$VERSION" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$'; then + echo "usage: mise run version <x.y.z>" >&2 + exit 1 +fi +sed -i '' "s/^ \.version = \"[^\"]*\",$/ .version = \"$VERSION\",/" build.zig.zon +if ! grep -Fqx " .version = \"$VERSION\"," build.zig.zon; then + echo "could not rewrite .version in build.zig.zon" >&2 + exit 1 +fi +echo "wrote .version = \"$VERSION\" -- commit it, then: mise run release" +''' + +[tasks."pin:libpanto"] +description = "Pin libpantograph to a revision (default: the sibling checkout's main@codeberg)" +usage = 'arg "[revision]" default="main@codeberg"' +run = ''' +set -eu +REV="$(jj log -R ../libpantograph -r "$usage_revision" --no-graph -T commit_id)" +zig fetch --save=libpantograph "git+https://codeberg.org/teepark/libpantograph.git#$REV" +''' + +[tasks.release] +description = "Test, build, tag, push the tag, and publish on Codeberg" +run = ''' +set -eu +: "${CODEBERG_TOKEN:?set CODEBERG_TOKEN in mise.local.toml}" + +VER="$(sed -n 's/^ \.version = "\([^"]*\)",$/\1/p' build.zig.zon)" +[ -n "$VER" ] || { echo "could not read version from build.zig.zon" >&2; exit 1; } +TAG="v$VER" + +# The working copy is what gets built, so it must be exactly what gets tagged. +if [ -n "$(jj diff -r @ --name-only)" ]; then + echo "the working copy has changes -- commit them, or jj new main" >&2 + exit 1 +fi +REV="$(jj log --no-graph -r 'main & @-' -T commit_id)" +[ -n "$REV" ] || { echo "main is not @-" >&2; exit 1; } +# A tag already on this commit is a release that failed partway: carry on with it. +TAGGED="$(jj log --no-graph -r "tags(exact:'$TAG')" -T commit_id)" +if [ -n "$TAGGED" ] && [ "$TAGGED" != "$REV" ]; then + echo "$TAG is already on another commit -- bump the version first" >&2 + exit 1 +fi +REMOTES="$(jj git remote list | awk '{ print $1 }')" +printf '%s\n' "$REMOTES" | grep -Fqx codeberg || { echo "no remote named codeberg" >&2; exit 1; } + +# One command per line: under `set -e` a failure inside an `&&` chain does +# not exit, and a failed build must never reach the tag below. +mise run test +mise run build:release +mise run check:release + +if [ -n "${DRY_RUN:-}" ]; then + echo "DRY_RUN set; stopping before tag/push/publish" + exit 0 +fi + +[ -n "$TAGGED" ] || jj tag set "$TAG" -r "$REV" +for r in $REMOTES; do + # jj tracks a new tag by itself on the first remote only, and refuses the others. + jj tag track "$TAG@$r" + jj git push --remote "$r" --tag "exact:$TAG" +done + +mise run release:publish +''' + +[tasks."release:publish"] +description = "Create a draft Codeberg release, upload the zip and checksum, then publish it" +run = ''' +set -eu +: "${CODEBERG_TOKEN:?set CODEBERG_TOKEN in mise.local.toml}" + +VER="$(sed -n 's/^ \.version = "\([^"]*\)",$/\1/p' build.zig.zon)" +[ -n "$VER" ] || { echo "could not read version from build.zig.zon" >&2; exit 1; } +TAG="v$VER" +API="${WRITE_AWAY_API:-}" +if [ -z "$API" ]; then + REPO="$(jj git remote list | sed -n 's#^codeberg .*codeberg\.org[:/]\(.*\)$#\1#p' | sed 's#\.git$##')" + [ -n "$REPO" ] || { echo "no codeberg.org remote named codeberg" >&2; exit 1; } + API="https://codeberg.org/api/v1/repos/$REPO" +fi +# Both assets must exist before anything is created. +ZIP="zig-out/release/Write_Away-$VER-macos-arm64.zip" +SUMS="zig-out/release/SHA256SUMS" +for f in "$ZIP" "$SUMS"; do + [ -f "$f" ] || { echo "missing release asset: $f" >&2; exit 1; } +done + +BODY="$(mktemp)" +trap 'rm -f "$BODY"' 0 +# call <expected status> <curl args...>: leaves the response in $BODY. +call() { + WANT="$1" + shift + STATUS="$(curl -sS -o "$BODY" -w '%{http_code}' -H "Authorization: token $CODEBERG_TOKEN" "$@")" + case " $WANT " in + *" $STATUS "*) ;; + *) echo "HTTP $STATUS, expected $WANT: $(cat "$BODY")" >&2; exit 1 ;; + esac +} + +# A published release is never touched. A draft is what a failed run left +# behind: its assets may be from another build, so start it over. +call "200 404" "$API/releases/tags/$TAG" +if [ "$STATUS" = 200 ]; then + if [ "$(jq .draft "$BODY")" != true ]; then + echo "$TAG is already published" >&2 + exit 1 + fi + call 204 -X DELETE "$API/releases/$(jq -er .id "$BODY")" +fi + +call 201 -H "Content-Type: application/json" \ + -d "{\"tag_name\":\"$TAG\",\"name\":\"$TAG\",\"draft\":true,\"prerelease\":false}" "$API/releases" +ID="$(jq -er .id "$BODY")" +for f in "$ZIP" "$SUMS"; do + call 201 -F "attachment=@$f" "$API/releases/$ID/assets?name=$(basename "$f")" +done +call 200 -X PATCH -H "Content-Type: application/json" -d '{"draft":false}' "$API/releases/$ID" + +call 200 "$API/releases/latest" +[ "$(jq -r .tag_name "$BODY")" = "$TAG" ] || { echo "published, but the latest release is not $TAG" >&2; exit 1; } +jq -r .html_url "$BODY" +''' diff --git a/scripts/test-release-task.sh b/scripts/test-release-task.sh new file mode 100644 index 0000000..fa0da33 --- /dev/null +++ b/scripts/test-release-task.sh @@ -0,0 +1,87 @@ +#!/bin/sh +# Drive `mise run release:publish` against a fake Forgejo. Leaves zig-out/release as it was. +set -eu +cd "$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)" +TMP="$(mktemp -d)" +VER="$(sed -n 's/^ \.version = "\([^"]*\)",$/\1/p' build.zig.zon)" +cleanup() { + if [ -n "${SERVER_PID:-}" ]; then kill "$SERVER_PID" 2>/dev/null || :; fi + if [ -n "${FAKES:-}" ]; then rm -f $FAKES; fi + rm -rf "$TMP" +} +trap cleanup 0 1 2 3 15 +mkdir -p zig-out/release +FAKES="" +for f in "zig-out/release/Write_Away-$VER-macos-arm64.zip" zig-out/release/SHA256SUMS; do + [ -f "$f" ] || { echo fake > "$f"; FAKES="$FAKES $f"; } +done + +# One release, as Forgejo keeps it: state is absent, draft or published. GET /fail/<n> +# makes the nth upload from now answer 500; GET /state reports. +/usr/bin/python3 - "$TMP/port" "v$VER" <<'PY' 2> "$TMP/server.log" & +import json, sys +from http.server import BaseHTTPRequestHandler, HTTPServer +from urllib.parse import parse_qs, urlsplit +port_file, tag = sys.argv[1:] +state, assets, fail = "absent", [], 0 +class Handler(BaseHTTPRequestHandler): + def log_message(self, *a): pass + def out(self, code, value=None): + body = b"" if value is None else (value if isinstance(value, str) else json.dumps(value)).encode() + self.send_response(code); self.send_header("Content-Length", str(len(body))); self.end_headers(); self.wfile.write(body) + def release(self): + return {"author": {"id": 7}, "id": 42, "tag_name": tag, "draft": state == "draft", "html_url": "fake://release"} + def handle_any(self): + global state, assets, fail + url = urlsplit(self.path); body = self.rfile.read(int(self.headers.get("Content-Length", "0"))) + call = (self.command, url.path) + if call[1].startswith("/fail/"): fail = int(call[1][6:]); return self.out(200) + if call == ("GET", "/state"): return self.out(200, " ".join([state] + assets)) + if self.headers.get("Authorization") != "token fake": return self.out(401) + if call == ("GET", "/releases/tags/" + tag): return self.out(404) if state == "absent" else self.out(200, self.release()) + if call == ("GET", "/releases/latest"): return self.out(200, self.release()) if state == "published" else self.out(404) + if call == ("POST", "/releases"): + if state != "absent": return self.out(409) + if json.loads(body) != {"tag_name": tag, "name": tag, "draft": True, "prerelease": False}: return self.out(422) + state = "draft"; return self.out(201, self.release()) + if call == ("DELETE", "/releases/42") and state == "draft": state, assets = "absent", []; return self.out(204) + if call == ("POST", "/releases/42/assets") and state == "draft": + if not self.headers.get("Content-Type", "").startswith("multipart/form-data;") or b'name="attachment"' not in body: return self.out(400) + fail -= 1 + if fail == 0: return self.out(500) + assets.append(parse_qs(url.query)["name"][0]); return self.out(201, {}) + if call == ("PATCH", "/releases/42") and state == "draft" and json.loads(body) == {"draft": False}: + state = "published"; return self.out(200, self.release()) + self.out(404) + do_GET = do_POST = do_PATCH = do_DELETE = handle_any +server = HTTPServer(("127.0.0.1", 0), Handler) +open(port_file, "w").write(str(server.server_address[1])) +server.serve_forever() +PY +SERVER_PID=$! +tries=0 +while [ ! -s "$TMP/port" ]; do + tries=$((tries + 1)) + [ "$tries" -lt 100 ] || { cat "$TMP/server.log" >&2; exit 1; } + sleep 0.05 +done +API="http://127.0.0.1:$(cat "$TMP/port")" +ASSETS="Write_Away-$VER-macos-arm64.zip SHA256SUMS" + +publish() { CODEBERG_TOKEN="${1:-fake}" WRITE_AWAY_API="$API" mise run release:publish; } +expect() { + got="$(curl -fsS "$API/state")" + [ "$got" = "$1" ] || { echo "$2: got '$got', expected '$1'" >&2; exit 1; } + echo "ok: $2" +} +refused() { echo "publish succeeded: $1" >&2; exit 1; } + +if publish wrong; then refused "bad token"; fi +expect "absent" "a bad token creates nothing" +curl -fsS "$API/fail/2" +if publish; then refused "failed upload"; fi +expect "draft Write_Away-$VER-macos-arm64.zip" "a failed upload stays a draft" +publish +expect "published $ASSETS" "a rerun starts the draft over and publishes it" +if publish; then refused "already published"; fi +expect "published $ASSETS" "a published release is left alone" |
