core · daukle/daukle · 0.25.0 · 3 examples

2. Plugins, and how daukle gets them

daukle has no languages in it. Every ability you use is a plugin, and this page is about where plugins come from, how they are pinned, and how to write one.

The example on this page is daukle-local-plugin, in this repository.

The three ways to name a plugin

[plugins]
# a published coordinate, resolved through a resolver
npm = { resolver = "github", coordinate = "daukle/npm@^1.0.0" }

# a url and the digest of the bytes it must serve
npm = { url = "https://.../plugin.lua", sha256 = "..." }

# a path inside your project, for a plugin you are writing
banner = "./plugins/banner.lua"

A coordinate still ends in a digest. The resolver asks the forge which release matches the range, and what comes back is fetched and checked. There is no unpinned form of any acquisition in daukle and no flag that relaxes one.

That is worth stating plainly because it is the property everything else rests on: a build that succeeded once succeeds the same way forever, or it fails loudly. The one exception is a deliberate resolve, and page 4 covers it.

Depending on part of a plugin

A plugin may ship MODULES, and a project names the ones it loads. Since daukle 0.9.0 a [plugins] entry takes modules:

[plugins]
# only the lint module, and only its grant
tools = { resolver = "github", coordinate = "example/tools@^1", modules = ["lint"] }
[plugins]
# the plugin's main chunk AND the lint module
tools = { resolver = "github", coordinate = "example/tools@^1", modules = ["main", "lint"] }

No plugin in this organization ships a module today; the shape is core's, and the names above are illustrative.

Each module declares its own daukle.plugin{}, so each carries its own uses and env, and depending on one never grants what another needs: the line above that names only lint gets the module's grant and not the main chunk's exec. "main" is the plugin's plugin.lua; any other name is modules/<name>.lua inside the plugin's published archive. With no modules key, only plugin.lua loads, exactly as before. The artifact is fetched and checked once, however many of its modules the line names, and config print shows a row per module as <label>/<module>, which daukle plugin update also accepts. A requires override on the line reaches whichever named module declares that alias, and one that no named module declares is refused by name.

Since daukle 0.9.1, a module the plugin does not carry is refused naming the line and the modules it does carry. A name is matched exactly, case included, and two names on one line that differ only in case are refused. A plugin that carries a modules/main.lua is refused too, because "main" always means plugin.lua and that file could never load, and a [plugins] key may not contain /.

Writing one

A plugin is a Lua file. This is a complete one:

-- A whole plugin. It declares one toolchain, and that toolchain generates one
-- file from the manifest.
--
-- daukle.plugin MUST be the first call in the chunk: the pre-pass that reads
-- "uses" runs before anything else, and a daukle.require above this line is
-- refused rather than silently allowed.
daukle.plugin{ api = 1, uses = {} }

daukle.toolchain{
  name = "banner",
  -- generate is a PURE function of the manifest. It may not call exec or tool,
  -- because "daukle check" runs it and check must never start your compiler.
  -- It returns a table of relative path to file text, and daukle does every
  -- write, which is why the sandbox has no write verb at all.
  generate = function(context)
    -- In generate the block is context.config; in a TASK the same block is
    -- context.toolchain.config. Real plugins keep a one line config_of helper
    -- so the two cannot be confused.
    local config = context.config
    local greeting = config.greeting or "hello"
    local lines = {
      "/* Generated by the banner toolchain. Do not edit. */",
      '#define BANNER "' .. greeting .. ' from ' .. context.project .. '"',
      '#define BANNER_VERSION "' .. context.version .. '"',
    }
    return { ["banner.h"] = table.concat(lines, "\n") .. "\n" }
  end,
}

And the manifest that uses it:

schema = 1
project = "daukle/example-local-plugin"
version = "3.1.4"

[modules]

# A plugin can be a LOCAL PATH while you are writing it. The same plugin is
# published by giving it a coordinate instead; nothing in the plugin changes.
[plugins]
banner = "./plugins/banner.lua"

[toolchains.banner]
greeting = "good morning"

daukle sync writes build/daukle/banner/banner.h:

/* Generated by the banner toolchain. Do not edit. */
#define BANNER "good morning from example/your-own-plugin"
#define BANNER_VERSION "3.1.4"

What uses is for

uses = {} above means the plugin asks for no host capability at all. A plugin that needs to run a program declares uses = { "exec", "tool" }, one that fetches declares "fetch", and so on.

It is a statement a reader of your manifest can act on. A plugin that did not declare exec cannot start a process, and the refusal comes from daukle rather than from the plugin's good intentions. When you review a dependency's plugin, uses is the line to read first.

exec and provision are also withheld by WHEN, whatever uses says. One plugin.lua may declare a source beside a toolchain or a publisher, so a GitHub plugin can both fetch a manifest and publish a release. But a source's load runs on every command, often for a dependency you did not write, so it may not start a process or provision a tool: the call is refused naming the phase, daukle.exec is not available while a source loads. Call them from a task or a publish callback. A chunk's top level and a toolchain's generate are refused the same way. A resolver may not declare either at all, because it is acquired before any pin exists. A chunk that declares a source beside exec or provision needs api = 2, which daukle 0.8.0 is the first to provide.

And anywhere else, too: since daukle 0.8.1 the two verbs run ONLY inside a task's run or a publisher's publish, and since the migrator kind also a migrator's run and build. The named refusals above come first because they tell you which phase you are in; beneath them, any call from outside those callbacks is refused with daukle.exec is available only inside a task, publish or migrator callback. A plugin may not set a __gc finalizer either, since one runs at whatever moment Lua collects garbage, outside every callback: setmetatable refuses a metatable carrying __gc, and since daukle 0.9.1 so does the one in the project's own daukle.lua. __close is unaffected, because a to-be-closed variable runs at scope exit, inside the callback that declared it.

Running what your build produced: daukle.built

daukle.tool(name) finds a program on the search path and root:tool(member) finds one in a provisioned toolchain. A program your own task just built is neither, so a plugin that wants to run it declares "built" beside "exec":

daukle.plugin{ api = 1, uses = { "tool", "exec", "built" } }

-- inside a task's run(context), after the build step has linked bin/app
local app = daukle.built(context.host.os == "windows" and "bin/app.exe" or "bin/app")
daukle.exec(app, { "--port", "8080" })

The path is relative to the running task's derived directory (build/daukle/<toolchain>/) and answers the same tool handle daukle.tool does. It is held to the rules daukle.write is held to: written with /, never absolute or drive-lettered, no .. anywhere, no empty or . segment and no control character. It must name a regular file that is already there, so a directory or a missing file is refused by name, and a link the build left behind is followed and must still land inside the derived directory. No extension is guessed: on Windows name the .exe yourself. A .bat or .cmd is refused exactly as daukle.tool refuses one, because starting it takes cmd.exe.

daukle.built is available exactly where daukle.exec is, inside a task's run, a publisher's publish or a migrator's run and build, and refused with the same phase-naming messages everywhere else. A daukle that predates it refuses uses = { "built" } by name, so it needs no api bump.

Looking at a directory: daukle.list

daukle.list(path) lists a directory under the plugin's base directory, held to the containment rule daukle.read is held to. Declare "list" in uses:

local entries, why = daukle.list("scripts")
if entries == nil and why ~= nil then error(why) end   -- it exists and cannot be listed
for _, entry in ipairs(entries or {}) do
  -- entry.name, and entry.kind: "file", "directory" or "link"
end
path namesdaukle.list returns
a directoryits entries sorted by name, without . and ..
nothingnil
a filenil and "<path> is not a directory"
a directory that cannot be openednil and the reason
something outside the base directoryit raises, as daukle.read does

A link is reported as "link" and never followed, a Windows junction included, so a walk does not descend into what a link points at. It returns rather than raises for the three cases a plugin has to tell apart, because the sandbox has no pcall. A daukle that predates it refuses uses = { "list" } by name, so it needs no api bump.

Running another task: daukle.run_task

daukle.run_task(name) runs another task of the project, with everything it depends on, in the middle of the running task, and returns true, or false and the failing task's message. Declare "run_task" in uses:

local ok, why = daukle.run_task("node-pi-extension:test")
if not ok then error(why, 0) end   -- what "a && b" does when b fails

A task runs at most once per invocation, whichever plan reaches it first: a dependency the nested run already ran is skipped by the outer plan, and the other way round. A task that is running right now is refused, itself or any task below it, since running it again from inside it would never end. An unknown name, an edge naming nothing and a cycle are refused as daukle <task> refuses them. It returns rather than raises because the sandbox has no pcall and a plugin implementing a || b needs the failure as a value. It is available inside a task's run and a publisher's publish, where a task run is in progress, and refused everywhere else. A daukle that predates it refuses uses = { "run_task" } by name, so it needs no api bump.

What api is for, and when to raise it

api = 1 says which plugin api your plugin was written against. daukle refuses an api HIGHER than it provides, by name and with its own version in the message, so a plugin needing something newer than the daukle running it says so instead of misbehaving:

plugin "gradle": needs daukle api 2 and this daukle 0.7.0 provides 1, so upgrade daukle

A LOWER api is accepted forever. A plugin declaring api = 1 keeps working on every later daukle, so there is no reason to raise yours to match a newer release. Raise it only when your plugin genuinely needs a capability that an older daukle lacks.

Most new capabilities need no api bump at all, because uses already polices them. A verb that a daukle does not have is refused by name the moment the plugin is declared, which every daukle back to 0.1.0 does. The api number exists for the capability shape that has no signal of its own: a new field inside a context table your callback already receives, which an older daukle simply leaves absent.

Declare the LOWEST api your plugin actually needs. Declaring a higher one refuses daukles that would have run your plugin perfectly well.

Telling an empty list from an empty table

Lua has one table type, so jvmArgs = [] and an empty [a.b] header both arrive as {}. Every table daukle hands you that was an ARRAY answers getmetatable with "daukle.array", empty or not, whether it came from daukle.toml or from a document daukle.parse or daukle.json_parse returned, so a plugin can ask:

local function is_array(value)
  return type(value) == "table" and getmetatable(value) == "daukle.array"
end

The mark arrived with api = 5; on an older daukle every table answers nil, which is why a plugin relying on it declares api = 5. A marked table refuses setmetatable, and copying it into a new table drops the mark. A table a daukle.lua builds is never marked, because Lua cannot say which {} was meant.

What the sandbox gives a plugin

string, table, math, and the handful of base functions. Not io, os, require, load or debug. A plugin cannot read your home directory, and a daukle check on a repository you just cloned cannot start a process.

Two consequences surprise people, so they are worth saying now:

fails the task. That is usually the right answer anyway.

daukle.manifest, for a plugin that declares a task per project entry

A plugin's task list is usually fixed, because javac has a fixed set of things to do. A plugin driving a tool whose task list the PROJECT extends is different: a CMake project has its own targets, a package has its own scripts. Such a plugin reads the manifest when it loads and calls daukle.task once per entry.

daukle.manifest is the manifest's file name, so the plugin never writes one of its own:

daukle.plugin{ api = 1, uses = { "read", "parse" } }

daukle.toolchain{ name = "demo", generate = function() return {} end }

local config = daukle.parse(daukle.read(daukle.manifest), daukle.manifest)
local scripts = config.toolchains and config.toolchains.demo and config.toolchains.demo.scripts
for name in pairs(scripts or {}) do
  daukle.task{ name = "demo:" .. name, run = function() ... end }
end

Write "daukle.toml" instead and you have made that spelling a requirement of every project your plugin serves, because a manifest passed to daukle sync <path> under any other name then fails before anything runs.

Three things to know:

chunk loads before overlays are applied. Declare what the tasks come from in the primary file.

be spelled publish-github.

where the entries are written.

One toolchain, several directories

A manifest names a toolchain instance with [toolchains.<name>]. Two keys let the name differ from the plugin and point it at a subdirectory:

[toolchains.web]
toolchain = "npm"
dir = "web"

so a manifest that never writes it reads as before.

segment. It is absent for the project root.

The instance's name is what its tasks, its build/daukle/<name>/ directory and a publish from are keyed on. The plugin's tasks are declared once per instance under that name, so the instance web of a toolchain npm runs web:build, and an edge from npm:install to npm:build becomes one from web:install to web:build. A bare edge such as partOf = "build", and an edge naming another toolchain, are left as they are.

context.root is where the toolchain builds. For an instance with no dir it is the project root as seen from the derived directory, ../../... With dir = "web" it is ../../../web. generate and every task receive the same value, so a plugin never joins a directory of its own.

A toolchain may not be named migrate, because build/daukle/migrate belongs to daukle migrate. These keys arrived with api = 3, so a plugin that relies on dir declares it.

Migrating: daukle.migrator and daukle.translator

daukle migrate (see daukle) asks plugins two questions, and a plugin answers with one declaration each. Both are available only to a plugin that a manifest or daukle migrate loaded, and both need api = 3.

daukle.plugin{ api = 3, uses = { "exec" } }

daukle.toolchain{ name = "stub", generate = function() return {} end }

daukle.migrator{
  tool = "faketool",
  toolchain = "stub",
  run = function(context) ... end,
  build = function(context) ... end,
}

daukle.migrator{ tool, toolchain, run, build } is the plugin for the OLD tool tool. toolchain names a toolchain this same plugin declared above the migrator, the one the migrated project will build with. run is required and build is optional. A plugin declares at most one migrator.

Both callbacks receive a context table:

fieldholds
toolthe migrator's tool
instancethe toolchain instance name migrate gave this detection
dirthe directory the detection was found in, absent at the project root
rootwhat context.root will be for that instance, as in the section above
host{ os, arch }, as in a task's context
proofin build only: the proof table run returned

The instance is the toolchain's own name for a detection at the root, and <toolchain>-<dir> for one in a subdirectory, with each / in dir replaced by - and each byte a task name refuses by _ and its two lowercase hex digits, so packages/@acme/web under node is node-packages-_40acme-web. A name another detection already took, or one past 255 bytes, is a blocker.

exec and provision work inside run and build, and daukle.built and daukle.write as well. The working directory is the instance's derived directory, build/daukle/<instance>/, which daukle creates before the call, and daukle.write writes inside it only, as it does in a task. Declare the verbs in uses as for a toolchain.

run asks the old tool for the project's model and returns data only:

return {
  manifest = { toolchains = { [context.instance] = {} } },
  foreign = {},
  blockers = {},
  owned = { "faketool.conf", "out" },
  proof = {
    task = context.instance .. ":build",
    compare = {
      { old = "out/greeting.txt",
        new = "build/daukle/" .. context.instance .. "/greeting.txt",
        as = "bytes" },
    },
  },
}
keymeaning
manifesta fragment of daukle.toml, merged with every other migrator's and translator's. Two fragments setting one key to different values is a blocker naming the key and both sources. It may not set plugins or schema, nor toolchain or dir in any toolchains entry, which core owns; a translation may set toolchain on an instance it creates, never on one a detection created
foreignthe old tool's own plugins the project uses, each { kind, id, config? }, to be replaced by a plugin that claims it in a catalog's replaces. One no catalog plugin claims, or two do, is a blocker
blockerswhat cannot be expressed, each { construct, where, why, surface? }
notesoptional: sentences the migration reports without blocking, such as a choice it froze or a part it did not carry. Each is printed as daukle migrate: migrator <label>: <note> whatever the outcome, and listed in --report as { source, note }; anything but a list of strings is an error naming the plugin
ownedthe relative paths of the old tool's files, moved to build/daukle/migrate/old once the proof passes. daukle.toml, daukle.lua, ., build and anything at or below build/daukle are refused, as is a path that climbs out or holds a backslash
instancesoptional, api = 10: directories below this detection's own dir (any, at the root) that are projects of their own, each migrated next by this same migrator as a detection of its own, named and rooted as any detection in a subdirectory. It is how a migrator answers for a directory detection folded into its own: matches of one plugin nested inside each other collapse to the outermost, because only the outer migrator can tell a part of its own build from a separate project. A path that is absolute, holds .. or a backslash, is ., or is at or below build/daukle is refused, and so is one not below dir; a directory already detected is not run twice
filesoptional, api = 4: a list of { path, text } that daukle writes beside daukle.toml before the proof runs, creating directories. path is relative to the project and must be a file below daukle/<instance>/, where <instance> is the one toolchain the returned manifest names; an absolute path, .., a backslash, a control character or a directory is refused, and so is a text that is not a string. On a failed proof the files stay beside daukle.toml
proof{ task, compare }: the task to run in the new project, and the artifacts to compare, each { old, new, as } with as one of "bytes", "archive" or, since api 9, "archive-json", and both paths relative to the project

A result that is not this shape is an error naming the plugin, because it is a plugin bug and not a fact about the user's project. A missing proof, or an empty compare, is a blocker instead: the migration cannot be shown to build the same artifacts. An empty Lua table arrives as an empty object, and every list key accepts it as an empty list.

as = "archive" compares the file members' names and bytes and ignores their order, directory members and timestamps, so a jar written by a different tool still compares equal for the same content. A symlink member is always a difference. as = "archive-json" does the same and compares a member named *.json by its parsed value, so whitespace and key order do not count, for a plugin that renders a manifest into the archive itself; a member that does not parse is compared by its bytes. as = "bytes" compares the two files.

After the manifest is written, migrate runs the new project's proof.task, and build has already built the old project with context.proof set to the same proof table, so compare.old exists before the comparison. build returns nothing.

daukle.translator{ tool, id, translate } replaces one foreign plugin: id of tool. It is the plugin a catalog's replaces names, and it declares the settings that foreign plugin carried:

daukle.translator{
  tool = "faketool",
  id = "org.example.release",
  translate = function(foreign)
    return { manifest = { publish = { release = { from = foreign.instance } } }, blockers = {} }
  end,
}

translate receives { tool, kind, id, instance, resolving, config? } and returns { manifest, blockers }, checked as above, and since api 8 optionally files, each below daukle/<instance>/ of a toolchain instance the translation's own manifest sets, and written before the proof as a migrator's are. A translator is process free: exec and provision are refused by name, exactly as in a source's load, so it cannot start the old tool. daukle.pin works in a --resolve run, which is how a resolver pins what a migrator handed it.

daukle.manifest is the empty string while migrate loads plugins, since no manifest exists yet. A chunk that tests the name with string.match still works, and one that reads the file named by it finds nothing to read.

Where generated files go

build/daukle/<toolchain>/ is derived: daukle clean deletes it, and it is gitignored. That is where a generated header, a generated build.gradle or a tool's scratch output belongs.

daukle/<toolchain>/ is committed: it is for what a resolve produced and a clone must carry. Page 4 is the one place it appears. Core loads it by itself: every file directly inside a daukle/<dir>/ that an overlay format reads (a .lua file) runs after daukle.toml and daukle.lua, directories and then files in sorted order, so a project writes no include line for one. A generated pin file should do nothing when the toolchain it edits is not declared, so a project that drops that toolchain keeps working. Since api = 6.

Nothing a plugin generates goes at the project root. The rule is simple: if the user is not supposed to edit it, it does not sit where the user works.

What this page does not cover

Publishing a plugin, which is a release with plugin.lua as an asset, and plugin dependencies through daukle.require. Both are real and neither is here yet.

Next