Plugins / Writing a plugin
Writing a plugin
From xkit-build/plugins, docs/writing-plugins.md at 161ceab (opens in a new tab)
A plugin is a build script with functions that turn a call in project.build into a module. It is made of three things xkit gives every script:
build_commandruns a program as a module: with declared inputs and outputs, only when they changed, withrunandtestforxkit runandxkit test.tooldownloads the program once per version, checked against its SHA-256 andproject.lock.- The build language: functions with typed parameters and defaults, conditions, lists, strings as types for fixed choices.
Start in your project
A plugin can start as a file next to project.build and move to a repository once it works:
my-project/
├── project.build
├── mkdocs.yml
├── tools/
│ └── docs.build
└── docs/
└── index.md
// tools/docs.build: static sites with mkdocs of the system
build_docs = fn(name: str, directory: str = ".", dependencies: list<module> = list()) -> module {
build_command(
name,
list("mkdocs", "build", "--site-dir", "{output}/site"),
inputs = list("docs", "mkdocs.yml"),
outputs = list("{output}/site/index.html"),
directory = directory,
run = list("mkdocs", "serve"),
dependencies = dependencies,
)
}
// project.build
use "./tools/docs.build"
build_docs("site")
xkit builds the site once and then only when a file in docs/ or mkdocs.yml changed, xkit run serves it. This plugin needs mkdocs installed. The next step is to bring the program along with tool, like the Go plugin does.
A plugin, step by step
go/plugin.build (opens in a new tab) is the reference. Its parts:
// Go programs with the toolchain of go.dev: build, run and test, for this machine or other targets
The first comment line describes the plugin in xkit plugins and in the choice of xkit new.
go_version = "1.27.4"
// the names go.dev gives this machine, the go command runs on it
_go_host_system = if host_operating_system() == "macos" { "darwin" } else { host_operating_system() }
_go_host_architecture = if host_architecture() == "aarch64" { "arm64" } else { "amd64" }
// the names Go gives the target the script is evaluated for: linux-aarch64 is GOOS=linux GOARCH=arm64
_go_system = if operating_system() == "macos" { "darwin" } else { operating_system() }
_go_architecture = if architecture() == "aarch64" { "arm64" } else if architecture() == "x86_64" { "amd64" } else if architecture() == "x86" { "386" } else { "arm" }
Public values are defaults that scripts may read or override, go_version here. Names starting with _ stay inside the plugin: helpers and the names the vendor gives this machine and the target.
A script whose modules have targets is evaluated once for every target, and its plugins with it. operating_system() and architecture() describe the target of that evaluation, the host_ functions the machine that runs the tools, see language.md.
go_tool = fn(version: str) -> str {
archive = "go" + version + "." + _go_host_system + "-" + _go_host_architecture + if host_operating_system() == "windows" { ".zip" } else { ".tar.gz" }
url = "https://dl.google.com/go/" + archive
home = tool("go", version, url, sha256_url = url + ".sha256")
home + "/bin/" + host_executable("go")
}
tool downloads the archive for this machine once, checks it against the SHA-256 the vendor publishes, and returns its directory. It picks the archive with the host_ functions: the compiler runs on this machine, also when it builds for another target. The version is a parameter, so a project can build with another one without a new release of the plugin.
build_go = fn(
name: str,
package: str = "./src",
directory: str = ".",
version: str = go_version,
toolchain: "auto" | "local" = "auto",
targets: list<target> = list(),
goos: str = env("GOOS", _go_system),
goarch: str = env("GOARCH", _go_architecture),
flags: list<str> = list(),
test_flags: list<str> = list(),
inputs: list<str> = list(),
environment: list<str> = list(),
dependencies: list<module> = list(),
) -> module {
The function a script calls. Everything but the name has a default, so build_go("server") is a whole build. toolchain takes only the two strings it lists, a typo is an error that names the nearest one. targets takes the names of xkit's targets, like list("linux-aarch64", "windows-x86_64").
go = go_tool(version)
program = "{output}/" + name + if goos == "windows" { ".exe" } else { "" }
build_command(
name,
list(go, "build", "-trimpath", "-o", program) + flags + list(package),
inputs = list("**/*.go", "go.mod", "go.sum") + inputs,
outputs = list(program),
environment = list(
"GOTOOLCHAIN=" + toolchain,
"GOOS=" + goos,
"GOARCH=" + goarch,
"GOMODCACHE=" + cache_directory("go/modules"),
"GOCACHE=" + cache_directory("go/build"),
"GOFLAGS=-modcacherw",
) + environment,
environment_inputs = list("GOOS", "GOARCH", "CGO_ENABLED", "GOEXPERIMENT"),
directory = directory,
run = list(program),
test = list(go, "test") + test_flags + list("./..."),
dependencies = dependencies,
targets = targets,
)
}
inputsnames every file the compiler reads. xkit runs the command again only when one of them changed, so a missing input means a stale program.outputsare below{output}, which isbuild/commands/<name>/in the module directory, for another targetbuild/<target>/commands/<name>/, and emptied before every run.targetsgoes on tobuild_command, which runs the command once for every target, each time withgoosandgoarchof that target.- The caches of the tool go to
cache_directory, not into the project or the home directory:clean --cacheremoves them with the rest of xkit's cache, and they are shared by all projects. environmentcomes after the plugin's own variables, so a script can override them.environment_inputsare variables of the user's shell that change what the compiler writes: their values count as inputs.runis whatxkit runstarts,testwhatxkit testruns.
Guidelines
- One function per kind of module, named
build_<language>, which returnsmodule.namecomes first, everything else has a default. - A
directoryparameter, passed tobuild_command.xkit addwritesdirectory = "<name>"into the call, so a module of the plugin can live next to others. - A
dependenciesparameter of typelist<module>, passed on, so the module builds after the modules it uses and again when they change. - Versions as parameters with a public default, like
version: str = go_version. A new version of the language should not need a new release of the plugin. - A SHA-256 for every download, with
sha256orsha256_url. Without one, the first download is trusted and onlyproject.lockchecks the following ones. - Strings as types for fixed choices, like
toolchain: "auto" | "local", instead ofstr. - Programs named for the system they run on.
executable(name)givesname.exefor a Windows target,host_executable(name)for the programs of this machine, like the compiler of a tool. - A
targetsparameter of typelist<target>, passed tobuild_command, so a script can build the module for other systems. The plugin reads the target withoperating_system()andarchitecture(), and downloads its tools with thehost_functions. - No shell. Commands are a program and its arguments:
sh -candcmd /cdo not exist everywhere. fail("...")for what the plugin does not support, like a system the vendor has no download for, with a message that says what to do instead.- Files the plugin brings are found through
plugin_directory, not through paths relative to the script. - JUnit reports when the test tool can write them:
test = list(tool, "test", "--junit", "{test_reports}/report.xml")letsxkit testcount the tests. Otherwise only the exit code counts.
Templates
Templates are what xkit new <plugin> and xkit add <plugin> copy:
go/
├── plugin.build
└── templates/
└── app/
├── module.build
├── go.mod
└── src/
├── main.go
└── main_test.go
// a Go program with a test
build_go("{{build.name}}")
module.buildholds exactly one call of a function of the plugin, with the placeholders below. Its first comment line describes the template in the choice ofxkit new.newwrites the call intoproject.buildafter theuse,addaddsdirectory = "<name>".- Every other file is copied, with the placeholders replaced in its content and its path.
appis the template without a name:xkit new go hello. Others are named after a colon,xkit new go:cli hello. A plugin with only one template needs noapp.- Templates named
appor ending in-appare programs:addputs them after the libraries in the script.
| Placeholder | Becomes |
|---|---|
{{build.name}} |
the name of the project or module, like my-tool |
{{build.identifier}} |
the name as an identifier, with _ for every character that is no letter or digit: my_tool |
{{build.domain}} |
the user's domain, like example.com |
{{build.package}} |
the reversed domain and the name, like com.example.my_tool, or what --package says |
The user is asked for the domain only when a template uses {{build.domain}} or {{build.package}}. A {{build.…}} that is none of them is an error, most likely a typo. The braces of the language itself stay as they are.
Trying it out
- As a file:
use "./path/to/plugin.build"in a test project loads the plugin as it is on disk, without git. - As the official list: commit the plugin to a clone of this repository, or to a repository laid out like it, and build with
XKIT_PLUGINS=file:///path/to/the/clone. Thenuse <name>,xkit pluginsandxkit new <name>take your plugin. Only committed changes count, and tags make releases. - Errors name the line of the plugin next to the call in the script.
- On every system: a plugin that downloads its compiler has to pick the right archive for macOS, Linux and Windows, on ARM and x86. The scenarios of xkit-build/regressions run on all of them.
Publishing
- In a repository of your own:
plugin.buildat its root isuse "you/name", one in a directoryuse "you/plugins/name". Releases are tagsv1.2.3, for a plugin in a directoryname-v1.2.3. - In this repository: see Adding a plugin.
- Use semantic versions. Scripts pin a release in
project.lockand--refreshstays within its major version, so breaking changes need a new major version.
Limits
What plugins cannot do, today:
The language
- No loops, no indexing into lists, no maps, no string functions besides
+andmatch. A plugin cannot look into a list it is given, like searchingenvironmentforGOOS, so such things need parameters of their own. - A function that calls itself may go at most 64 calls deep.
- Defaults are evaluated once, when the plugin loads:
env(...)in a default reads the environment of the build, which is what it should do.
Modules
- Loading a plugin only defines functions. A
build_commandat the top of a plugin is an error: modules come from the calls in the script. - One call of
build_commandis one module that runs one command. Steps one after another are several commands that depend on each other. - A plugin has no view of the release flag of
xkit --release. Release builds need a parameter, likeflags. - The modules of xkit take from a command what the table lists: C and C++ sources and headers, jars, Java and Kotlin sources. Other kinds of outputs only reach other commands.
Up to date and caching
- xkit knows only what the command declares. It does not see files a command reads without naming them, and it does not sandbox commands.
- A command runs whole: when one input changed, the command runs again with all of them. Incremental work is the tool's own, in its caches under
cache_directory, because{output}is emptied before every run. - Tests count per test only from JUnit XML reports in
{test_reports}, otherwise by their exit code.
Dependencies and downloads
- Downloads go through
tool: one archive per tool and version, zip or tar, with an optional SHA-256. There is no HTTP in the language. - The libraries of a language are left to its own tools, like Go modules, which
godownloads and checks withgo.sum. They are not inproject.lock, and they may need network on the first build.
The command line
- A plugin adds no commands to xkit, and does not change
install,ideaorclean.xkit idealeaves command modules out. xkit runstarts therunof a command as it is,xkit testitstest. Running a program built for another system fails like it would without xkit.- The choice of
xkit newlists only the templates of the official plugins. Other plugins are started withxkit new someone/repository.