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 @@ -14,6 +14,9 @@ Notable changes to `libtmux`.
checkout’s `release.json`, even when older rockspecs are present.
- Docs: The API reference for `v0.1.0alpha1` can be built from its own Lua
types. Each release must export the classes and aliases its source declares.
- Docs: Guides and Lua examples appear once when you rebuild the API reference
or change its output directory. The exporter preserves an existing guide or
example directory that belongs to another project.
- 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 @@ -30,6 +30,10 @@ 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.

Use `--output DIR` to choose another directory. Rebuilding replaces the
export's `api.json`, `guides/` and `examples/`. If you added a guide or
edited an exported example, the command names the path and leaves it in place.

Check the READMEs:

```console
Expand Down
146 changes: 140 additions & 6 deletions scripts/docs/export.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ local fs = require("support.fs")
local json = require("support.json")
local repo = require("support.repo")
local shell = require("support.shell")
local uv = require("support.uv")

local M = {}
local write_directory = ".libtmux-docs-write"

---@type table<string, string[]>
M.EXPECTED_MODULES = {
Expand Down Expand Up @@ -179,10 +181,46 @@ function M.rock_metadata(source)
return { name = "libtmux", version = version, source_tag = tag }
end

local function previous_export(directory)
local manifest = directory .. "/api.json"
if fs.is_symlink(manifest) or not fs.is_file(manifest) then
return nil
end
local ok, model = pcall(json.read, manifest)
if
ok
and type(model) == "table"
and model.schema == 1
and model.port == "lua"
and type(model.source) == "table"
and model.source.repository == "libtmux/libtmux-lua"
and type(model.exporter) == "table"
and type(model.guides) == "table"
and type(model.examples) == "table"
and (
model.exporter.name == "scripts/docs/docs.lua"
or model.exporter.name == "scripts/export-docs"
)
then
return model
end
return nil
end

--- The guides or examples under `source`, as { path, content } rows sorted by path.
---@param source string
---@param entries { [1]: string, [2]: string }[] pairs of a directory (or file) and a file suffix
function M.content_files(source, entries)
local function skip(directory)
if directory == source .. "/docs/_build" then
return true
end
local parent, name = directory:match("^(.*)/([^/]+)$")
if name == write_directory then
return true
end
return (name == "guides" or name == "examples") and previous_export(parent) ~= nil
end
local paths, seen = {}, {}
local function add(path)
if not seen[path] then
Expand All @@ -197,7 +235,7 @@ function M.content_files(source, entries)
add(base)
end
elseif fs.is_directory(source .. "/" .. base) then
for _, relative in ipairs(fs.walk(source .. "/" .. base)) do
for _, relative in ipairs(fs.walk(source .. "/" .. base, skip)) do
if relative:sub(-#suffix) == suffix then
add(base .. "/" .. relative)
end
Expand Down Expand Up @@ -446,14 +484,113 @@ end

local function write_bundle(output, name, rows)
local directory = output .. "/" .. name
fs.remove_tree(directory)
for _, entry in ipairs(rows) do
local target = directory .. "/" .. entry.path
fs.mkdir_p(target:match("^(.*)/[^/]+$"))
files.write(target, entry.content)
end
end

local function occupied(path)
fail(path .. " already exists. Choose an unused export directory.")
end

local function check_bundle(directory, rows)
if fs.is_symlink(directory) then
occupied(directory)
end
if not fs.stat(directory) then
return
end
if not rows or not fs.is_directory(directory) then
occupied(directory)
end
local owned = { [directory] = true }
for _, row in ipairs(rows) do
if type(row) ~= "table" or type(row.path) ~= "string" or type(row.content) ~= "string" then
occupied(directory)
end
local path = directory .. "/" .. row.path
owned[path] = row.content
local parent = path:match("^(.*)/")
while parent and not owned[parent] do
owned[parent] = true
parent = parent:match("^(.*)/")
end
end
local paths = fs.walk(directory, function(path)
if owned[path] ~= true then
occupied(path)
end
return false
end)
for _, name in ipairs(paths) do
local path = directory .. "/" .. name
if
type(owned[path]) ~= "string"
or fs.is_symlink(path)
or not fs.is_file(path)
or files.read(path) ~= owned[path]
then
occupied(path)
end
end
end

--- Stage all three outputs on the destination filesystem before replacing a previous export.
function M.write_export(output, payload)
if output == "" then
fail("Choose a directory for the export.")
end
output = output:gsub("([^/])/+$", "%1")
if fs.is_symlink(output) then
occupied(output)
end
fs.mkdir_p(output)
local stage = output .. "/" .. write_directory
local made, reason = uv.fs_mkdir(stage, 448)
if not made then
fail("Cannot create export directory " .. stage .. ": " .. tostring(reason))
end
local saved, installed = {}, {}
local ok, message = pcall(function()
local previous = previous_export(output)
local manifest = output .. "/api.json"
if not previous and (fs.stat(manifest) or fs.is_symlink(manifest)) then
occupied(manifest)
end
for _, name in ipairs({ "guides", "examples" }) do
check_bundle(output .. "/" .. name, previous and previous[name])
write_bundle(stage, name, payload[name])
end
json.write(stage .. "/api.json", payload, 2, true)
fs.mkdir_p(stage .. "/previous")
for _, name in ipairs({ "guides", "examples", "api.json" }) do
local target = output .. "/" .. name
if fs.stat(target) then
assert(os.rename(target, stage .. "/previous/" .. name))
saved[#saved + 1] = name
end
if fs.stat(stage .. "/" .. name) then
assert(os.rename(stage .. "/" .. name, target))
installed[#installed + 1] = name
end
end
end)
if not ok then
for _, name in ipairs(installed) do
assert(os.rename(output .. "/" .. name, stage .. "/" .. name))
end
for _, name in ipairs(saved) do
assert(os.rename(stage .. "/previous/" .. name, output .. "/" .. name))
end
end
fs.remove_tree(stage)
if not ok then
error(message, 0)
end
end

--- Exports the source and, unless `check` is set, writes api.json and the guide and example
--- bundles into `output`. Returns a one-line summary.
---@param options { source: string, luals: string, output: string, check: boolean }
Expand All @@ -467,10 +604,7 @@ function M.run(options)
end
local payload = M.build_payload(options.source, options.luals)
if not options.check then
fs.mkdir_p(options.output)
json.write(options.output .. "/api.json", payload, 2, true)
write_bundle(options.output, "guides", payload.guides)
write_bundle(options.output, "examples", payload.examples)
M.write_export(options.output, payload)
end
local fields = 0
for _, item in ipairs(payload.declarations) do
Expand Down
77 changes: 77 additions & 0 deletions scripts/docs/tests/test_export.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,83 @@ function M.test_export_annotations_are_isolated_from_runtime_modules()
test.assertEquals(#export.ANNOTATED, 3)
end

function M.test_export_skips_generated_bundles_inside_its_sources()
local root = fs.mkdtemp("libtmux-docs-inputs-")
for _, path in ipairs({
"docs/_build/guides",
"docs/drafts/guides",
"docs/previous/guides",
"examples/preview/examples",
}) do
fs.mkdir_p(root .. "/" .. path)
end
local model = {
schema = 1,
port = "lua",
source = { repository = "libtmux/libtmux-lua" },
exporter = { name = "scripts/docs/docs.lua" },
guides = {},
examples = {},
}
json.write(root .. "/examples/preview/api.json", model)
model.exporter.name = "scripts/export-docs"
json.write(root .. "/docs/previous/api.json", model)
files.write(root .. "/docs/previous/guides/copied.md", "previous export\n")
files.write(root .. "/docs/_build/guides/README.md", "previous export\n")
files.write(root .. "/docs/previous/notes.md", "author notes beside the export\n")
files.write(root .. "/docs/drafts/api.json", '{"schema":1}')
files.write(root .. "/docs/drafts/guides/guide.md", "new guide\n")
files.write(root .. "/examples/preview/examples/demo.lua", "print('previous export')\n")
files.write(root .. "/examples/preview.lua", "print('demo')\n")
local rows = export.content_files(root, { { "docs", ".md" }, { "examples", ".lua" } })
fs.remove_tree(root)
local paths = {}
for _, row in ipairs(rows) do
paths[#paths + 1] = row.path
end
test.assertEquals(
paths,
{ "docs/drafts/guides/guide.md", "docs/previous/notes.md", "examples/preview.lua" }
)
end

function M.test_export_preserves_a_guide_in_another_projects_output_directory()
local output = fs.mkdtemp("libtmux-docs-destination-")
fs.mkdir_p(output .. "/guides")
files.write(output .. "/guides/welcome.md", "Welcome\n")
test.assertErrorMsgContains(output .. "/guides", export.write_export, output, {})
test.assertEquals(files.read(output .. "/guides/welcome.md"), "Welcome\n")
fs.remove_tree(output)
end

function M.test_rebuilding_preserves_edits_in_a_previous_export()
local output = fs.mkdtemp("libtmux-docs-rebuild-")
local model = {
schema = 1,
port = "lua",
source = { repository = "libtmux/libtmux-lua" },
exporter = { name = "scripts/docs/docs.lua" },
guides = { { path = "docs/guide.md", content = "Old guide\n" } },
examples = { { path = "examples/demo.lua", content = "print(1)\n" } },
}
export.write_export(output, model)
local demo = output .. "/examples/examples/demo.lua"
local guide = output .. "/guides/docs/guide.md"
model.guides[1].content = "New guide\n"
model.invalid = math.huge
test.assertErrorMsgContains("non-finite", export.write_export, output, model)
test.assertEquals(files.read(guide), "Old guide\n")
model.invalid = nil
files.write(demo, "print(2)\n")
test.assertErrorMsgContains(demo, export.write_export, output, model)
test.assertEquals(files.read(demo), "print(2)\n")
test.assertEquals(files.read(guide), "Old guide\n")
files.write(demo, "print(1)\n")
export.write_export(output, model)
test.assertEquals(files.read(guide), "New guide\n")
fs.remove_tree(output)
end

function M.test_the_export_is_deterministic_json()
local value = json.decode('{"b":[2,{"z":1,"a":2}],"a":"x"}')
test.assertEquals(json.encode(value, nil, true), '{"a":"x","b":[2,{"a":2,"z":1}]}')
Expand Down
9 changes: 7 additions & 2 deletions scripts/support/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ end

--- Every file under a directory, as paths relative to it, sorted. Symlinks are not followed.
---@param root string
---@param skip_directory? fun(path: string): boolean
---@return string[]
function M.walk(root)
function M.walk(root, skip_directory)
local found = {}
local function visit(relative)
for _, name in ipairs(M.list(relative == "" and root or root .. "/" .. relative)) do
local directory = relative == "" and root or root .. "/" .. relative
if skip_directory and skip_directory(directory) then
return
end
for _, name in ipairs(M.list(directory)) do
local child = relative == "" and name or relative .. "/" .. name
local stat = uv.fs_lstat(root .. "/" .. child)
if stat and stat.type == "directory" then
Expand Down