Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Notable changes to `libtmux`.
- Docs: the README starts with a calculator in a private tmux pane. Run it
from standalone Lua or Neovim and read the reply over control mode.

- Docs: The API export includes `examples/api/run.sh` for releases that use
it to start tmux. Readers can run those releases’ examples with the
supplied script.
- Runtime: `Runtime:start_server` starts a private tmux server and resolves to
an `OwnedServer` with its `server` handle, its `socket_path` and `stop()`.
The server lives in a new directory under `root` (the system temporary
Expand Down
4 changes: 4 additions & 0 deletions scripts/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Install LuaLS, then export into the default directory:
$ luajit scripts/docs/docs.lua export
```

Use `--source /path/to/checkout` to export another revision. For releases
whose examples start tmux with `examples/api/run.sh`, the export includes
that script alongside the Lua programs.

Check the READMEs:

```console
Expand Down
33 changes: 21 additions & 12 deletions scripts/docs/api_examples.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,35 @@ local function non_blank(text)
return type(text) == "string" and text:find("%S") ~= nil
end

local function check_source_file(path)
require_that(fs.is_file(path) and not fs.is_symlink(path), "example file is missing or linked")
local content = files.read(path)
require_that(
content:find("%S") and content:sub(-1) == "\n" and not content:find("\r", 1, true),
"example files must contain complete UTF-8 text with LF endings"
)
end

--- Reads the manifest of the complete examples under `root` and checks it against the files.
--- `symbols`, when given, is the set a target must belong to.
---@param root string
---@param symbols? table<string, boolean>
---@param options? { allow_launcher: boolean } Older releases used a shared tmux launcher.
---@return table manifest
function M.load_examples(root, symbols)
function M.load_examples(root, symbols, options)
local manifest = json.read(root .. "/examples/api/manifest.json")
check_keys(manifest, { "schema", "setup", "examples" })
require_that(manifest.schema == 1, "unsupported schema")
local setup = manifest.setup
check_keys(setup, { "lua", "luv" })
check_keys(
setup,
{ "lua", "luv" },
options and options.allow_launcher and { "launcher" } or nil
)
if setup.launcher ~= nil then
require_that(setup.launcher == "examples/api/run.sh", "unexpected launcher path")
check_source_file(root .. "/" .. setup.launcher)
end
require_that(
type(setup.lua) == "string" and setup.lua:match("^5%.[1-5]%.%d+$"),
"Lua version must be pinned"
Expand All @@ -87,16 +105,7 @@ function M.load_examples(root, symbols)
example.file == "examples/api/" .. name .. ".lua",
"unexpected example file path"
)
local path = root .. "/" .. example.file
require_that(
fs.is_file(path) and not fs.is_symlink(path),
"example file is missing or linked"
)
local content = files.read(path)
require_that(
content:find("%S") and content:sub(-1) == "\n" and not content:find("\r", 1, true),
"example files must contain complete UTF-8 text with LF endings"
)
check_source_file(root .. "/" .. example.file)
listed[example.file] = true
require_that(non_blank(example.description), "missing task description")
require_that(
Expand Down
25 changes: 19 additions & 6 deletions scripts/docs/export.lua
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,23 @@ local function annotate(source_lua)
end
end

--- Collect example source and the manifest that assigns complete programs to API symbols.
---@param source string
---@param symbols table<string, boolean>
---@return table[] examples
---@return table? manifest
function M.example_bundle(source, symbols)
local entries = { { "examples", ".lua" } }
local manifest
if fs.is_directory(source .. "/examples/api") then
manifest = api_examples.load_examples(source, symbols, { allow_launcher = true })
if manifest.setup.launcher then
entries[#entries + 1] = { manifest.setup.launcher, "" }
end
end
return M.content_files(source, entries), manifest
end

---@param source string The checkout to export.
---@param luals string The language server executable.
---@return table payload
Expand Down Expand Up @@ -381,13 +398,9 @@ function M.build_payload(source, luals)
package = M.rock_metadata(source),
declarations = declarations,
guides = M.content_files(source, { { "README.md", "" }, { "docs", ".md" } }),
examples = M.content_files(source, { { "examples", ".lua" } }),
}
-- Historical sources predate complete examples; an opted-in directory must validate.
if fs.is_directory(source .. "/examples/api") then
payload.api_examples =
api_examples.load_examples(source, api_examples.public_symbols(declarations))
end
payload.examples, payload.api_examples =
M.example_bundle(source, api_examples.public_symbols(declarations))
M.validate(payload)
return payload
end
Expand Down
36 changes: 36 additions & 0 deletions scripts/docs/tests/test_api_examples.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local test = require("luaunit")
local api = require("docs.api_examples")
local export = require("docs.export")
local files = require("support.files")
local fs = require("support.fs")
local json = require("support.json")
Expand Down Expand Up @@ -101,6 +102,41 @@ function M.test_complete_programs_cover_their_existing_targets()
test.assertEquals(seen, TARGETS)
end

local function set_launcher(path)
manifest.setup = { lua = manifest.setup.lua, luv = manifest.setup.luv, launcher = path }
save()
end

function M.test_an_export_includes_only_the_launcher_its_examples_require()
local name = "examples/api/run.sh"
local content = "#!/bin/sh\nexit 23\n"
files.write(root .. "/" .. name, content)
local function exported_launcher()
local rows, loaded = export.example_bundle(root, TARGETS)
test.assertEquals(assert(loaded).setup, manifest.setup)
for _, row in ipairs(rows) do
if row.path == name then
return row.content
end
end
end

test.assertNil(exported_launcher())
set_launcher(name)
test.assertEquals(exported_launcher(), content)
rejects("unexpected or missing manifest fields")
end

function M.test_a_release_launcher_must_be_the_listed_regular_file()
set_launcher("../run.sh")
test.assertErrorMsgContains("launcher path", export.example_bundle, root, TARGETS)
set_launcher("examples/api/run.sh")
test.assertErrorMsgContains("missing or linked", export.example_bundle, root, TARGETS)
local path = root .. "/" .. manifest.setup.launcher
assert(require("support.uv").fs_symlink("connect.lua", path))
test.assertErrorMsgContains("missing or linked", export.example_bundle, root, TARGETS)
end

function M.test_receiver_and_collection_identities_match_native_annotations()
local declarations = {
{
Expand Down
Loading