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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Notable changes to `libtmux`.
supplied script.
- Docs: The API export identifies the numbered rock named by the selected
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.
- 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
93 changes: 72 additions & 21 deletions scripts/docs/export.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local shell = require("support.shell")

local M = {}

---@type table<string, string[]>
M.EXPECTED_MODULES = {
["libtmux.query"] = {
"NULL",
Expand All @@ -30,22 +31,6 @@ M.EXPECTED_MODULES = {
["libtmux.runtime.luv"] = { "run" },
["libtmux.runtime.nvim"] = { "start" },
}
M.EXPECTED_TYPES = {
"libtmux.Buffer",
"libtmux.Client",
"libtmux.Configurable",
"libtmux.Entity",
"libtmux.Pane",
"libtmux.Reference",
"libtmux.Request",
"libtmux.Runtime",
"libtmux.Selection",
"libtmux.Server",
"libtmux.Session",
"libtmux.Snapshot",
"libtmux.Window",
"libtmux.WindowLink",
}
-- Public modules whose annotations the export adds to a copy of the source, so the runtime
-- modules stay free of export-only tags.
M.ANNOTATED = {
Expand Down Expand Up @@ -140,7 +125,7 @@ function M.public_declarations(raw)
local declarations = {}
for _, entry in ipairs(raw) do
local name = entry.name or ""
if name:sub(1, 8) == "libtmux." then
if name == "libtmux" or name:sub(1, 8) == "libtmux." then
local item = M.normalize(entry)
local fields = {}
for _, field in ipairs(item.fields or {}) do
Expand Down Expand Up @@ -230,14 +215,76 @@ function M.content_files(source, entries)
return rows
end

--- Public types declared in this checkout, plus the API shared by every release.
---@param source string
---@return table<string, boolean>
function M.required_types(source)
local required = {
["libtmux.Entity"] = true,
["libtmux.Reference"] = true,
["libtmux.Request"] = true,
["libtmux.Runtime"] = true,
["libtmux.Selection"] = true,
["libtmux.Server"] = true,
["libtmux.Snapshot"] = true,
}
local directory = source .. "/lua"
for _, relative in ipairs(fs.walk(directory)) do
if relative:match("%.lua$") then
local text = "\n" .. files.read(directory .. "/" .. relative)
for kind, name in text:gmatch("\n%-%-%-@(%a+)[ \t]+(libtmux%.[%w_%.]+)") do
if kind == "class" or kind == "alias" then
required[name] = true
end
end
end
end
return required
end

local function required_modules(source)
local required = {}
for name, fields in pairs(M.EXPECTED_MODULES) do
required[name] = fields
end
-- Root coverage comes from the selected initializer.
required.libtmux = nil
local path = source .. "/lua/libtmux/init.lua"
if not fs.is_file(path) then
return required
end

local text = "\n" .. files.read(path)
local class = text:find("\n%-%-%-@class[ \t]+libtmux%f[^%w_.]")
local returned = text:match("\nreturn[ \t]+([%a_][%w_]*)%f[^%w_]")
local fields = {}
if returned then
local member = returned .. "%.([%a][%w_]*)"
for line in text:gmatch("\n([^\n]*)") do
local name = line:match("^function[ \t]+" .. member .. "[ \t]*%(")
or line:match("^" .. member .. "[ \t]*=")
if name then
fields[#fields + 1] = name
end
end
end
-- Older initializers return an unannotated table containing only version metadata.
if class or #fields > 0 then
required.libtmux = fields
end
return required
end

---@param payload table
function M.validate(payload)
---@param source? string
function M.validate(payload, source)
source = source or repo.root
local declarations = {}
for _, entry in ipairs(payload.declarations) do
declarations[entry.name] = entry
end
local missing = {}
for _, name in ipairs(M.EXPECTED_TYPES) do
for name in pairs(M.required_types(source)) do
if not declarations[name] then
missing[#missing + 1] = name
end
Expand All @@ -246,7 +293,11 @@ function M.validate(payload)
table.sort(missing)
fail("missing public types: " .. table.concat(missing, ", "))
end
for module, expected in pairs(M.EXPECTED_MODULES) do
local modules = required_modules(source)
if declarations.libtmux and not modules.libtmux then
fail("unexpected public module: libtmux")
end
for module, expected in pairs(modules) do
local entry = declarations[module]
if not entry then
fail("missing public module: " .. module)
Expand Down Expand Up @@ -389,7 +440,7 @@ function M.build_payload(source, luals)
}
payload.examples, payload.api_examples =
M.example_bundle(source, api_examples.public_symbols(declarations))
M.validate(payload)
M.validate(payload, source)
return payload
end

Expand Down
Loading