From 044dfcdcc36d4a2e4542b610678a355ee4f5ce12 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 5 Dec 2026 12:12:25 -0600 Subject: [PATCH 1/2] fix(api-model[lua]) Match example setup to the source why: Lua checkout examples use APIs absent from the numbered rock. The Watch example also requires a newer tmux than the general floor. what: - Build the cited SCM rock for source-bound examples. - Resolve numbered recipes from the selected Git tag and release file. - Check exported example bytes and retain legacy launcher commands. --- packages/api-model/src/doc/roles.ts | 1 + .../api-model/src/languages/lua-examples.ts | 72 ++++++-- packages/api-model/src/languages/lua.ts | 8 +- packages/api-model/test/lua-examples.test.ts | 163 ++++++++++++++---- scripts/gen-api-model.mjs | 45 ++++- .../ports/lua/examples/capture-pane-output.md | 6 +- .../ports/lua/guides/attaching-to-tmux.md | 11 +- site/src/lib/versions.ts | 21 ++- site/test/versions.test.ts | 28 +++ 9 files changed, 293 insertions(+), 62 deletions(-) diff --git a/packages/api-model/src/doc/roles.ts b/packages/api-model/src/doc/roles.ts index 9211c2601..5dc3636b4 100644 --- a/packages/api-model/src/doc/roles.ts +++ b/packages/api-model/src/doc/roles.ts @@ -298,6 +298,7 @@ const SYNTAX: Record> = { // Swift doc comment ``Server`` is a symbol link, not code. swift: new Set(['docc']), ruby: new Set(['yard']), + lua: new Set(['markdown-link']), cxx: new Set([]), kotlin: new Set(['kdoc', 'jsdoc']), scala: new Set(['scaladoc', 'jsdoc']), diff --git a/packages/api-model/src/languages/lua-examples.ts b/packages/api-model/src/languages/lua-examples.ts index 8abbde228..d2c362614 100644 --- a/packages/api-model/src/languages/lua-examples.ts +++ b/packages/api-model/src/languages/lua-examples.ts @@ -4,7 +4,13 @@ export interface LuaExampleManifest { schema: number /** `launcher` is a shell script that starts the tmux daemon; programs that start their own omit it. */ setup: { lua: string; luv: string; launcher?: string } - examples: { id: string; symbols: string[]; file: string; description: string; stdout: string }[] + examples: { id: string; symbols: string[]; file: string; description: string; stdout: string; min_tmux?: string }[] +} + +export interface LuaExampleOptions { + releaseRock?: string + localCheckout?: boolean + matchesExample?: (path: string, content: string) => boolean } /** Preserve complete source files exported by Lua's native documentation tool. */ @@ -12,7 +18,7 @@ export function attachCompleteLuaExamples( symbols: ApiSymbol[], manifest: LuaExampleManifest | undefined, files: { path: string; content: string }[] | undefined, - source: { repository: string; revision: string }, + source: { repository: string; revision: string } & LuaExampleOptions, ): void { if (manifest === undefined) return const require = (condition: unknown, message: string): void => { @@ -32,10 +38,33 @@ export function attachCompleteLuaExamples( const code = matches[0].content require(typeof code === 'string' && code.trim() && code.endsWith('\n') && !code.includes('\r'), `source file must retain complete LF bytes: ${path}`) + require(!source.matchesExample || source.matchesExample(path, code), + `${path} differs from the selected checkout; regenerate the native Lua docs artifact with scripts/docs/docs.lua export`) return code } const launcher = launcherPath === undefined ? undefined : { path: launcherPath, code: read(launcherPath) } + const rock = source.releaseRock + require(rock === undefined || /^[0-9][0-9a-z.-]*-[1-9]\d*$/.test(rock), 'invalid numbered rock argument') const url = (path: string) => `https://github.com/${source.repository}/blob/${source.revision}/${path}` + const installLuv = `$ luarocks --tree ./rocks install luv ${manifest.setup.luv} &&\n` + let install: string + let setup: string + if (source.localCheckout) { + install = installLuv + ' luarocks --tree ./rocks make rockspecs/libtmux-scm-1.rockspec\n' + setup = 'From the root of the local Lua checkout that produced this preview, build the SCM rock.' + } else if (rock) { + install = installLuv + ` luarocks --tree ./rocks install libtmux ${rock} &&\n` + + ' eval "$(luarocks --tree ./rocks path)"\n' + setup = 'Install the numbered rock in a new directory.' + } else { + install = installLuv + ' rocks="$PWD/rocks" &&\n' + + ' (cd ../libtmux-lua && luarocks --tree "$rocks" make rockspecs/libtmux-scm-1.rockspec)\n' + const sourceTree = `https://github.com/${source.repository}/tree/${source.revision}` + setup = `Put an empty example directory next to a copy of the [Lua source revision](${sourceTree}) named \`libtmux-lua\`. Build the SCM rock from the example directory.` + } + const launcherGuide = source.localCheckout ? '`examples/api/README.md` for launcher prerequisites' + : `the [launcher prerequisites](${url('examples/api/README.md')})` + const launcherSetup = launcher ? [`See ${launcherGuide}.`] : [] const seen = new Set() const ids = new Set() for (const example of manifest.examples) { @@ -45,42 +74,53 @@ export function attachCompleteLuaExamples( require(typeof example.description === 'string' && example.description.trim(), 'task description is missing') require(typeof example.stdout === 'string' && example.stdout.trim() && example.stdout.endsWith('\n'), 'expected output is missing') + require(example.min_tmux === undefined || (typeof example.min_tmux === 'string' && /^\d+\.\d+$/.test(example.min_tmux)), + 'invalid tmux floor') require(Array.isArray(example.symbols) && example.symbols.length, 'API targets are missing') const code = read(example.file) - const name = `${example.id}.lua` + const name = source.localCheckout ? example.file : `${example.id}.lua` + const shell = source.localCheckout ? launcherPath : 'run.sh' const blocks: NonNullable = [ { lang: 'console', - intro: `${example.description} Use Lua 5.1 or newer with LuaRocks, tmux 3.3a or newer, and a C compiler and CMake for luv on Linux. In a new directory, install the library and its standalone runtime dependency from LuaRocks:`, - code: '$ luarocks --tree ./rocks install luv &&\n' + - ' luarocks --tree ./rocks install libtmux &&\n' + - ' eval "$(luarocks --tree ./rocks path)"\n', + intro: [ + example.description, + `Use Lua 5.1 to 5.5 or LuaJIT with LuaRocks, tmux ${example.min_tmux ?? '3.3a'} or newer, and a C compiler and CMake for luv on Linux.`, + ...launcherSetup, + setup, + ].join(' '), + code: install, }, ...(launcher === undefined ? [] : [{ lang: 'sh', - intro: 'Save this complete launcher as `run.sh`. It starts a private tmux daemon and cleans up after success or failure. A failed daemon shutdown reports and retains its socket directory.', - sourceUrl: url(launcher.path), + intro: source.localCheckout ? `Use \`${launcher.path}\` from the same checkout.` + : 'Save the complete launcher as `run.sh`. It starts and stops a private tmux daemon. A failed shutdown reports and retains its socket directory.', + sourceUrl: source.localCheckout ? undefined : url(launcher.path), code: launcher.code, }]), { lang: 'lua', - intro: launcher === undefined - ? `Save the complete program as \`${name}\`. It starts its own private tmux server, uses it and stops it, so no existing session is needed.` - : `Save the complete program as \`${name}\`. Closing the library connection leaves the daemon running; the launcher owns its lifetime.`, - sourceUrl: url(example.file), + intro: source.localCheckout ? `Use \`${name}\` from the same checkout.` + : launcher === undefined + ? `Save the complete program as \`${name}\`. It starts its own private tmux server and stops it before exiting, including after a failure.` + : `Save the complete program as \`${name}\` beside \`run.sh\`. Closing the connection leaves the daemon running. The launcher owns its lifetime.`, + sourceUrl: source.localCheckout ? undefined : url(example.file), code, }, launcher === undefined ? { lang: 'console', - intro: 'Run the saved program. It runs `/usr/bin/tmux`; set `TMUX_BIN` to another absolute path to use a different binary. Assertions and operation errors produce a failing exit status:', + intro: (source.localCheckout ? 'Run the program from that checkout.' : 'Run the saved program.') + + ' Set `TMUX_BIN` to use an absolute path other than `/usr/bin/tmux`.', code: `$ eval "$(luarocks --tree ./rocks path)" &&\n lua ${name}\n`, } : { lang: 'console', - intro: 'Run the saved files. Assertions and operation errors produce a failing exit status:', - code: `$ eval "$(luarocks --tree ./rocks path)" &&\n sh run.sh ${name}\n`, + intro: (source.localCheckout ? 'Run the files from that checkout.' : 'Run the saved files together.') + + ' The launcher uses tmux on `PATH` unless `TMUX_BIN` selects another binary.', + code: `$ eval "$(luarocks --tree ./rocks path)" &&\n sh ${shell} "${name}"\n`, }, + { lang: 'text', intro: 'Expected output.', code: example.stdout }, ] for (const id of example.symbols) { require(!seen.has(id), `duplicate API target: ${id}`) diff --git a/packages/api-model/src/languages/lua.ts b/packages/api-model/src/languages/lua.ts index 2625bf4a7..0f31e7049 100644 --- a/packages/api-model/src/languages/lua.ts +++ b/packages/api-model/src/languages/lua.ts @@ -1,5 +1,5 @@ import type { ApiModel, ApiSymbol, DocBlock, Param, Signature } from '../model.ts' -import { attachCompleteLuaExamples, type LuaExampleManifest } from './lua-examples.ts' +import { attachCompleteLuaExamples, type LuaExampleManifest, type LuaExampleOptions } from './lua-examples.ts' interface LuaNode { name?: string @@ -145,7 +145,7 @@ function originOf( return undefined } -export function extractLua(input: unknown, expectedRevision?: string): ApiModel { +export function extractLua(input: unknown, expectedRevision?: string, exampleOptions: LuaExampleOptions = {}): ApiModel { const artifact = assertArtifact(input, expectedRevision) const symbols: ApiSymbol[] = [] const byName = new Map(artifact.declarations.map((d) => [d.name, d])) @@ -198,7 +198,7 @@ export function extractLua(input: unknown, expectedRevision?: string): ApiModel }) } } - attachCompleteLuaExamples(symbols, artifact.api_examples, artifact.examples, artifact.source) + attachCompleteLuaExamples(symbols, artifact.api_examples, artifact.examples, { ...artifact.source, ...exampleOptions }) return { port: 'lua', repo: artifact.source.repository, @@ -207,7 +207,7 @@ export function extractLua(input: unknown, expectedRevision?: string): ApiModel sources: [{ product: 'core', package: artifact.package.name, - version: artifact.package.version, + version: exampleOptions.releaseRock ?? artifact.package.version, repo: artifact.source.repository, revision: artifact.source.revision, extractedRevision: artifact.source.revision, diff --git a/packages/api-model/test/lua-examples.test.ts b/packages/api-model/test/lua-examples.test.ts index fff0974a3..cf908117a 100644 --- a/packages/api-model/test/lua-examples.test.ts +++ b/packages/api-model/test/lua-examples.test.ts @@ -3,19 +3,25 @@ import type { ApiSymbol } from '../src/model.ts' import { attachCompleteLuaExamples, type LuaExampleManifest } from '../src/languages/lua-examples.ts' const source = { repository: 'libtmux/libtmux-lua', revision: 'a'.repeat(40) } +const taggedSource = { ...source, releaseRock: '0.1.1-1' } const program = 'local text = ""\nprint(text)\n' const launcher = '#!/bin/sh\nset -eu\nlua "$1"\n' +const launcherPath = 'examples/api/run.sh' const files = [ { path: 'examples/api/connect.lua', content: program }, - { path: 'examples/api/run.sh', content: launcher }, { path: 'examples/ordinary.lua', content: 'print("not a complete example")\n' }, ] +const releaseFiles = [...files, { path: launcherPath, content: launcher }] const manifest = (): LuaExampleManifest => ({ schema: 1, - setup: { lua: '5.5.1', luv: '1.52.1-0', launcher: 'examples/api/run.sh' }, + setup: { lua: '5.5.1', luv: '1.52.1-0' }, examples: [{ id: 'connect', symbols: ['libtmux.Runtime:connect'], file: 'examples/api/connect.lua', description: 'Connect to a tmux daemon.', stdout: 'connected\n' }], }) +const releaseManifest = (path = launcherPath) => { + const value = manifest() + return { ...value, setup: { ...value.setup, launcher: path } } +} const target = (): ApiSymbol => ({ id: 'libtmux.Runtime:connect', name: 'connect', kind: 'method', modifiers: [], signatures: [], source: { file: 'lua/libtmux/_internal/runtime.lua', revision: source.revision }, @@ -23,41 +29,120 @@ const target = (): ApiSymbol => ({ }) describe('complete Lua API examples from native artifacts', () => { - it('retains native docs, exact full files, registry setup, and both source links', () => { + it('keeps the source build and direct command for programs that own their server', () => { const symbol = target() attachCompleteLuaExamples([symbol], manifest(), files, source) expect(symbol.doc?.summary).toBe('Existing native documentation.') const examples = symbol.doc!.examples! - expect(examples).toHaveLength(4) + expect(examples.map((example) => example.lang)).toEqual(['console', 'lua', 'console', 'text']) + expect(examples[1].code).toBe(program) + expect(examples[1].sourceUrl).toBe(`https://github.com/${source.repository}/blob/${source.revision}/examples/api/connect.lua`) + expect(examples[0].code).toBe('$ luarocks --tree ./rocks install luv 1.52.1-0 &&\n' + + ' rocks="$PWD/rocks" &&\n' + + ' (cd ../libtmux-lua && luarocks --tree "$rocks" make rockspecs/libtmux-scm-1.rockspec)\n') + expect(examples[0].intro).toContain(`https://github.com/${source.repository}/tree/${source.revision}`) + expect(examples[0].intro).toContain('Lua 5.1 to 5.5 or LuaJIT') + expect(examples[0].intro).toContain('tmux 3.3a or newer') + expect(examples[0].intro).not.toContain('launcher prerequisites') + expect(examples[1].intro).toContain('`connect.lua`') + expect(examples[1].intro).toContain('starts its own private tmux server') + expect(examples[1].intro).not.toMatch(/launcher/i) + expect(examples[2].intro).toContain('Run the saved program.') + expect(examples[2].intro).toContain('`TMUX_BIN`') + expect(examples[2].code).toBe('$ eval "$(luarocks --tree ./rocks path)" &&\n lua connect.lua\n') + expect(examples.some((example) => example.lang === 'sh' || example.code.includes('run.sh'))).toBe(false) + expect(examples.some((example) => example.code.includes('not a complete example'))).toBe(false) + }) + + it('retains the legacy launcher bytes, numbered rock and quoted run command', () => { + const symbol = target() + attachCompleteLuaExamples([symbol], releaseManifest(), releaseFiles, taggedSource) + const examples = symbol.doc!.examples! + expect(examples.map((example) => example.lang)).toEqual(['console', 'sh', 'lua', 'console', 'text']) + expect(examples[0].code).toBe('$ luarocks --tree ./rocks install luv 1.52.1-0 &&\n' + + ' luarocks --tree ./rocks install libtmux 0.1.1-1 &&\n' + + ' eval "$(luarocks --tree ./rocks path)"\n') + expect(examples[0].intro).toContain(`https://github.com/${source.repository}/blob/${source.revision}/examples/api/README.md`) expect(examples[1].code).toBe(launcher) + expect(examples[1].sourceUrl).toBe(`https://github.com/${source.repository}/blob/${source.revision}/${launcherPath}`) expect(examples[2].code).toBe(program) - expect(examples[1].sourceUrl).toBe(`https://github.com/${source.repository}/blob/${source.revision}/examples/api/run.sh`) - expect(examples[2].sourceUrl).toBe(`https://github.com/${source.repository}/blob/${source.revision}/examples/api/connect.lua`) - expect(examples[0].code).toBe('$ luarocks --tree ./rocks install luv &&\n' + - ' luarocks --tree ./rocks install libtmux &&\n' + - ' eval "$(luarocks --tree ./rocks path)"\n') - expect(examples[0].intro).toContain('Lua 5.1 or newer') - expect(examples[0].intro).not.toMatch(/git|clone/i) - expect(examples[2].intro).toContain('`connect.lua`') - expect(examples[3].code).toBe('$ eval "$(luarocks --tree ./rocks path)" &&\n sh run.sh connect.lua\n') - expect(examples.some((example) => example.code.includes('not a complete example'))).toBe(false) + expect(examples[2].intro).toContain('The launcher owns its lifetime') + expect(examples[3].intro).toContain('The launcher uses tmux on `PATH` unless `TMUX_BIN` selects another binary.') + expect(examples[3].code).toBe('$ eval "$(luarocks --tree ./rocks path)" &&\n sh run.sh "connect.lua"\n') }) - it('shows plain run commands for a manifest without a launcher', () => { - const value = manifest() - delete value.setup.launcher + it('installs the numbered rock for a tag with a standalone example', () => { const symbol = target() - attachCompleteLuaExamples([symbol], value, files.filter((file) => file.path !== 'examples/api/run.sh'), source) + attachCompleteLuaExamples([symbol], manifest(), files, taggedSource) const examples = symbol.doc!.examples! - expect(examples.map((example) => example.lang)).toEqual(['console', 'lua', 'console']) - expect(examples[0].code).toContain('luarocks --tree ./rocks install libtmux') - expect(examples[1].code).toBe(program) - expect(examples[1].sourceUrl).toBe(`https://github.com/${source.repository}/blob/${source.revision}/examples/api/connect.lua`) - expect(examples[1].intro).toContain('`connect.lua`') - expect(examples[1].intro).toContain('its own private tmux server') - expect(examples[2].code).toBe('$ eval "$(luarocks --tree ./rocks path)" &&\n lua connect.lua\n') - expect(examples[2].intro).toContain('TMUX_BIN') - expect(JSON.stringify(examples)).not.toMatch(/launcher|run\.sh/) + expect(examples[0].code).toContain('luarocks --tree ./rocks install libtmux 0.1.1-1') + expect(examples[2].code).toContain('lua connect.lua') + }) + + it('builds the captured source for a checkout with a launcher', () => { + const symbol = target() + attachCompleteLuaExamples([symbol], releaseManifest(), releaseFiles, source) + const examples = symbol.doc!.examples! + expect(examples[0].code).toContain('make rockspecs/libtmux-scm-1.rockspec') + expect(examples[3].code).toContain('sh run.sh "connect.lua"') + }) + + it('uses the checkout that supplied a local example', () => { + const symbol = target() + attachCompleteLuaExamples([symbol], manifest(), files, { ...source, localCheckout: true }) + const examples = symbol.doc!.examples! + expect(examples[0].intro).toContain('the local Lua checkout that produced this preview') + expect(examples[0].intro).not.toContain('https://github.com/') + expect(examples[0].code).toBe('$ luarocks --tree ./rocks install luv 1.52.1-0 &&\n' + + ' luarocks --tree ./rocks make rockspecs/libtmux-scm-1.rockspec\n') + expect(examples[1]).toMatchObject({ lang: 'lua', code: program }) + expect(examples[1].intro).toContain('`examples/api/connect.lua`') + expect(examples[1].sourceUrl).toBeUndefined() + expect(examples[2].intro).toContain('Run the program from that checkout.') + expect(examples[2].code).toBe('$ eval "$(luarocks --tree ./rocks path)" &&\n lua examples/api/connect.lua\n') + }) + + it('uses local launcher files when a checkout at a tag has working changes', () => { + const symbol = target() + attachCompleteLuaExamples([symbol], releaseManifest(), releaseFiles, { ...taggedSource, localCheckout: true }) + const examples = symbol.doc!.examples! + expect(examples[0].intro).toContain('`examples/api/README.md`') + expect(examples[0].intro).not.toContain('https://github.com/') + expect(examples[0].code).not.toContain('install libtmux 0.1.1-1') + expect(examples[1].sourceUrl).toBeUndefined() + expect(examples[2].sourceUrl).toBeUndefined() + expect(examples[3].code).toContain('sh examples/api/run.sh "examples/api/connect.lua"') + }) + + it.each([ + ['the clean source lacks the program', manifest(), files, false, 'examples/api/connect.lua', undefined], + ['the working program has changed', manifest(), files, true, 'examples/api/connect.lua', program + '-- edited\n'], + ['the launcher has changed', releaseManifest(), releaseFiles, true, launcherPath, launcher + '# edited\n'], + ] as const)('refuses a stale export when %s', (_label, selected, bundled, localCheckout, path, actual) => { + const checkout = new Map(bundled.map((row) => [row.path, row.content])) + if (actual === undefined) checkout.delete(path) + else checkout.set(path, actual) + expect(() => attachCompleteLuaExamples([target()], selected, bundled, { + ...source, + localCheckout, + matchesExample: (file: string, content: string) => checkout.get(file) === content, + })).toThrow(`${path} differs from the selected checkout; regenerate the native Lua docs artifact`) + }) + + it('accepts a numbered prerelease rock', () => { + const symbol = target() + attachCompleteLuaExamples([symbol], releaseManifest(), releaseFiles, { ...source, releaseRock: '0.1.0alpha4-1' }) + expect(symbol.doc?.examples?.[0].code).toContain('luarocks --tree ./rocks install libtmux 0.1.0alpha4-1') + }) + + it('renders the example-specific tmux floor and recorded output', () => { + const symbol = target() + const base = manifest() + const value = { ...base, examples: [{ ...base.examples[0], min_tmux: '3.7' }] } + attachCompleteLuaExamples([symbol], value, files, source) + const examples = symbol.doc!.examples! + expect(examples[0].intro).toContain('tmux 3.7 or newer') + expect(examples.at(-1)).toEqual({ lang: 'text', intro: 'Expected output.', code: 'connected\n' }) }) it('preserves historical native artifacts without the opt-in manifest', () => { @@ -68,7 +153,7 @@ describe('complete Lua API examples from native artifacts', () => { }) it('rejects missing or ambiguous full-file payloads and changed line endings', () => { - for (const invalid of [files.slice(0, 1), [...files, files[0]], + for (const invalid of [files.slice(1), [...files, files[0]], files.map((file) => ({ ...file, content: file.content.replaceAll('\n', '\r\n') }))]) { expect(() => attachCompleteLuaExamples([target()], manifest(), invalid, source)).toThrow() } @@ -83,10 +168,10 @@ describe('complete Lua API examples from native artifacts', () => { it('rejects unsafe paths and absent runtime or output contracts', () => { for (const mutate of [ - (value: LuaExampleManifest) => { value.setup.launcher = '../run.sh' }, (value: LuaExampleManifest) => { value.examples[0].file = '../../connect.lua' }, (value: LuaExampleManifest) => { value.setup.lua = 'latest' }, (value: LuaExampleManifest) => { value.setup.luv = 'latest' }, + (value: LuaExampleManifest) => { value.examples[0].min_tmux = 'current' }, (value: LuaExampleManifest) => { value.examples[0].stdout = '' }, (value: LuaExampleManifest) => { value.examples[0].description = '' }, ]) { @@ -100,11 +185,19 @@ describe('complete Lua API examples from native artifacts', () => { .toThrow(/Lua repository/) }) - it('still checks a launcher the manifest names', () => { - const value = manifest() - expect(() => attachCompleteLuaExamples([target()], value, files.filter((file) => file.path !== 'examples/api/run.sh'), source)) - .toThrow(/resolve once: examples\/api\/run\.sh/) - value.setup.launcher = 'examples/api/other.sh' - expect(() => attachCompleteLuaExamples([target()], value, files, source)).toThrow(/unexpected launcher path/) + it('requires exactly one complete file at the supported launcher path', () => { + for (const payload of [files, [...releaseFiles, { path: launcherPath, content: launcher }]]) { + expect(() => attachCompleteLuaExamples([target()], releaseManifest(), payload, source)) + .toThrow(/resolve once: examples\/api\/run\.sh/) + } + const badLineEndings = [...files, { path: launcherPath, content: launcher.replaceAll('\n', '\r\n') }] + expect(() => attachCompleteLuaExamples([target()], releaseManifest(), badLineEndings, source)) + .toThrow(/complete LF bytes: examples\/api\/run\.sh/) + for (const path of ['../run.sh', 'examples/api/other.sh', '']) { + expect(() => attachCompleteLuaExamples([target()], releaseManifest(path), releaseFiles, source)) + .toThrow(/unexpected launcher path/) + } + expect(() => attachCompleteLuaExamples([target()], releaseManifest(), releaseFiles, { ...source, releaseRock: 'scm-1' })) + .toThrow(/numbered rock argument/) }) }) diff --git a/scripts/gen-api-model.mjs b/scripts/gen-api-model.mjs index 42b329c01..f020e40d2 100644 --- a/scripts/gen-api-model.mjs +++ b/scripts/gen-api-model.mjs @@ -12,7 +12,9 @@ import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, import { homedir, tmpdir } from 'node:os' import { dirname, isAbsolute, join, relative, resolve } from 'node:path' import { fileURLToPath } from 'node:url' +import { isDeepStrictEqual } from 'node:util' import { PORTS as PORT_DEFS } from '../site/src/lib/ports.ts' +import { buildTarget, releaseRockForBuild } from '../site/src/lib/versions.ts' import { extractDoxygen } from '../packages/api-model/src/languages/doxygen.ts' import { extractLua } from '../packages/api-model/src/languages/lua.ts' import { extractPerlArtifact } from '../packages/api-model/src/languages/perl.ts' @@ -695,7 +697,48 @@ for (const [port, cfg] of Object.entries(PORTS)) { } const artifact = JSON.parse(readFileSync(artifactPath, 'utf8')) const nativeExtractors = { raku: extractRakuArtifact, perl: extractPerlArtifact, ruby: extractRuby, lua: extractLua, powershell: extractPowerShell, julia: extractJulia, elixir: extractElixir, ocaml: extractOcamlArtifact, haskell: extractHaskellArtifact, clojure: extractClojure, lisp: extractCommonLisp, erlang: extractErlang } - model = nativeExtractors[port](artifact, head) + if (port === 'lua' && artifact.api_examples !== undefined) { + const status = git(checkout, 'status', '--porcelain', '--untracked-files=normal') + if (status === undefined) throw new Error(`gen-api-model: cannot inspect the Lua checkout at ${checkout}`) + const localCheckout = status !== '' || revision !== head + const build = process.env.LIBTMUX_DOCS_PORT === port ? buildTarget(process.env) : undefined + const releaseManifest = build && (build.kind === 'tag' || build.kind === 'alias') + ? git(checkout, 'show', `${head}:release.json`) + : undefined + // The public tag carries the package version even in older exporter artifacts. + const declaredRock = releaseManifest === undefined ? artifact.package?.version : JSON.parse(releaseManifest).version + const releaseRock = build && releaseRockForBuild(build, declaredRock) + if (releaseRock) { + const ref = `refs/tags/${releaseRock.tag}` + const commit = git(checkout, 'rev-parse', '--verify', `${ref}^{commit}`) + if (commit !== head) { + throw new Error(`gen-api-model: Lua checkout ${head} does not match ${ref} (${commit ?? 'missing tag'})`) + } + } + const readExample = (path) => { + try { + return localCheckout + ? readFileSync(join(checkout, path)) + : execFileSync('git', ['-C', checkout, 'show', `${head}:${path}`], { stdio: ['ignore', 'pipe', 'ignore'] }) + } catch { + return undefined + } + } + const manifestPath = 'examples/api/manifest.json' + let manifest + try { + manifest = JSON.parse(readExample(manifestPath)?.toString('utf8') ?? '') + } catch { + manifest = undefined + } + if (!isDeepStrictEqual(artifact.api_examples, manifest)) { + throw new Error(`${manifestPath} differs from the selected checkout. Regenerate the native Lua docs artifact with scripts/docs/docs.lua export.`) + } + const matchesExample = (path, content) => readExample(path)?.equals(Buffer.from(content)) ?? false + model = extractLua(artifact, head, { releaseRock: releaseRock?.version, localCheckout, matchesExample }) + } else { + model = nativeExtractors[port](artifact, head) + } if (port === 'raku') { // The exporter runs in the library checkout, so its bytes and compiler are part of what the model claims. const producer = model.nativeProvenance.exporter diff --git a/site/src/content/docs/ports/lua/examples/capture-pane-output.md b/site/src/content/docs/ports/lua/examples/capture-pane-output.md index ce5897326..8a83304e5 100644 --- a/site/src/content/docs/ports/lua/examples/capture-pane-output.md +++ b/site/src/content/docs/ports/lua/examples/capture-pane-output.md @@ -16,8 +16,10 @@ You need tmux 3.3a or newer and a Unix environment; no existing session is required. Save the program as `capture.lua` and run it after the [setup](#setup-and-run). -The library connects to a running server and does not start one, so the program -starts its own with the `tmux` command and stops it at the end. +This program uses the published `libtmux` 0.1.1-1 rock and starts its private +server with luv. The Lua checkout has a capture example that starts and stops +the server with the SCM rock. +[Checkout setup and examples](https://github.com/libtmux/libtmux-lua/blob/d8266f22bb7ddace64b597fc35899ab72c71cf9f/examples/api/README.md). ## Read what's on screen diff --git a/site/src/content/docs/ports/lua/guides/attaching-to-tmux.md b/site/src/content/docs/ports/lua/guides/attaching-to-tmux.md index 5d8991b90..a0abb95ff 100644 --- a/site/src/content/docs/ports/lua/guides/attaching-to-tmux.md +++ b/site/src/content/docs/ports/lua/guides/attaching-to-tmux.md @@ -27,9 +27,9 @@ existing server, and `print_work` connects a `Server` to it and finds `work`. Without the variable, the program starts a private server of its own in a new temporary directory, creates `work` with [`Server:new_session`](../../reference/libtmux-server-new_session/), and -connects a second `Server` to that socket by path. The library connects to a -running server and does not start one, so the program has `tmux` start the -daemon. It stops the private server with +connects a second `Server` to that socket by path. This program uses the +published `libtmux` 0.1.1-1 rock and starts the daemon with luv. It stops the +private server with [`Server:command`](../../reference/libtmux-server-command/) and removes the directory after success or failure. If the server does not stop, the program reports the error and keeps the directory. @@ -154,6 +154,11 @@ The program prints `work`. To use an existing server of your own, set `LIBTMUX_SOCKET_PATH` to its socket before running the program; it prints `work` and leaves that server running. +The Lua checkout provides `Runtime:start_server` and `OwnedServer:stop`. +Install the SCM rock and run the checkout's `connect.lua` to print `connected` +after reading the demonstration session through a second handle. +[Checkout setup and program](https://github.com/libtmux/libtmux-lua/blob/d8266f22bb7ddace64b597fc35899ab72c71cf9f/examples/api/README.md). + ## Find or create a session diff --git a/site/src/lib/versions.ts b/site/src/lib/versions.ts index d46c430f6..795fcf294 100644 --- a/site/src/lib/versions.ts +++ b/site/src/lib/versions.ts @@ -341,7 +341,26 @@ export function buildTarget(env: Record) { const version = env.LIBTMUX_DOCS_VERSION ?? 'latest' const kind = (env.LIBTMUX_DOCS_VERSION_KIND ?? 'trunk') as VersionKind const isDefault = env.LIBTMUX_DOCS_IS_DEFAULT === 'true' - return { version, kind, isDefault } + const resolvesTo = env.LIBTMUX_DOCS_RESOLVES_TO + return { version, kind, isDefault, ...(resolvesTo ? { resolvesTo } : {}) } +} + +/** The numbered Lua rock that matches a tag or an alias to a tag. */ +export function releaseRockForBuild( + build: { version: string; kind: VersionKind; resolvesTo?: string }, + declared: string | undefined, +): { tag: string; version: string } | undefined { + const slug = build.kind === 'tag' ? build.version : build.kind === 'alias' ? build.resolvesTo : undefined + if (!slug) return undefined + const tag = releaseTag(slug, { tagGrammar: 'luarocks' }) + if (!tag && build.kind === 'alias') return undefined + if (!tag || typeof declared !== 'string') throw new Error('Lua release build requires a numbered rock version') + const expected = tag.startsWith('v') ? tag : `v${tag}` + const rock = packageVersion(declared, 'luarocks') + if (rock.tag !== expected || rock.revision < 1 || declared.startsWith('v')) { + throw new Error(`Lua rock version ${declared} does not match release tag ${slug}`) + } + return { tag: expected, version: declared } } /** diff --git a/site/test/versions.test.ts b/site/test/versions.test.ts index e47d9277f..c6e22f2c6 100644 --- a/site/test/versions.test.ts +++ b/site/test/versions.test.ts @@ -5,12 +5,14 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { fileURLToPath } from 'node:url' import { + buildTarget, comparePackageVersions, compareTags, newestPublishedTag, packageVersionIsPrerelease, parseTag, registryVersion, + releaseRockForBuild, releaseTag, selectBuildVersions, sortVersions, @@ -469,3 +471,29 @@ describe('registryVersion', () => { expect(registryVersion(next, undefined, ['0.1.0'], rust)).toBe('latest') }) }) + +describe('releaseRockForBuild', () => { + it.each(['0.1.1', '0.1.0alpha4', '0.1.0beta2', '0.1.0rc1'])('keeps the matching rock revision for tag %s', (version) => { + const build = buildTarget({ LIBTMUX_DOCS_VERSION_KIND: 'tag', LIBTMUX_DOCS_VERSION: `v${version}` }) + expect(releaseRockForBuild(build, `${version}-1`)).toEqual({ tag: `v${version}`, version: `${version}-1` }) + }) + + it('uses the alias target when latest points to a tag', () => { + const build = buildTarget({ + LIBTMUX_DOCS_VERSION_KIND: 'alias', LIBTMUX_DOCS_VERSION: 'latest', LIBTMUX_DOCS_RESOLVES_TO: 'v0.1.1', + }) + expect(releaseRockForBuild(build, '0.1.1-1')).toEqual({ tag: 'v0.1.1', version: '0.1.1-1' }) + expect(releaseRockForBuild({ ...build, resolvesTo: 'latest' }, '0.1.1-1')).toBeUndefined() + }) + + it.each(['trunk', 'branch', 'pr'] as const)('builds from source for %s even when the manifest declares a rock', (kind) => { + expect(releaseRockForBuild({ kind, version: 'v0.1.1' }, '0.1.1-1')).toBeUndefined() + }) + + it('rejects stale package metadata or an unnumbered rock on a release page', () => { + const build = { kind: 'tag' as const, version: 'v0.1.1' } + for (const rock of ['0.1.0-1', '0.1.1', 'scm-1']) { + expect(() => releaseRockForBuild(build, rock)).toThrow(/does not match release tag v0\.1\.1/) + } + }) +}) From 1f378a3f518d7757cbd27f102c67da376d8194ca Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 5 Dec 2026 13:13:34 -0600 Subject: [PATCH 2/2] docs(lua): Link checkout calls to the program that uses them why: The shared guide also appears beside older Lua releases. Their reference lacks the methods used to start and stop a private server. what: - Link both calls to the lines in the cited checkout and describe the session that the program reads. --- .../content/docs/ports/lua/guides/attaching-to-tmux.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/site/src/content/docs/ports/lua/guides/attaching-to-tmux.md b/site/src/content/docs/ports/lua/guides/attaching-to-tmux.md index a0abb95ff..3a481adbd 100644 --- a/site/src/content/docs/ports/lua/guides/attaching-to-tmux.md +++ b/site/src/content/docs/ports/lua/guides/attaching-to-tmux.md @@ -154,9 +154,11 @@ The program prints `work`. To use an existing server of your own, set `LIBTMUX_SOCKET_PATH` to its socket before running the program; it prints `work` and leaves that server running. -The Lua checkout provides `Runtime:start_server` and `OwnedServer:stop`. -Install the SCM rock and run the checkout's `connect.lua` to print `connected` -after reading the demonstration session through a second handle. +The checkout's `connect.lua` starts a private tmux server, reads its session +through a second handle, prints `connected`, and stops the server. Install the +SCM rock to run it. See [`Runtime:start_server`](https://github.com/libtmux/libtmux-lua/blob/d8266f22bb7ddace64b597fc35899ab72c71cf9f/examples/api/connect.lua#L14) +and [`OwnedServer:stop`](https://github.com/libtmux/libtmux-lua/blob/d8266f22bb7ddace64b597fc35899ab72c71cf9f/examples/api/connect.lua#L27) +in the complete program. [Checkout setup and program](https://github.com/libtmux/libtmux-lua/blob/d8266f22bb7ddace64b597fc35899ab72c71cf9f/examples/api/README.md).