# Tasks

> A project's own commands, plinto <name>: a bash script, a Lua script or a Java file in its tasks/ folder.

A task is a command of your project's own. Put a file in its `tasks/` folder, and `plinto <name>` runs it, with every argument after the name.

```sh
$ plinto fmt --check
src/greet.go
$ echo $?
1
```

## Files {#files}

| File | Runs with |
|---|---|
| `tasks/fmt.sh` | bash, whatever its `#!` line says (Git's bash on Windows) |
| `tasks/reset.lua` | Lua 5.4, which plinto carries: nothing to install |
| `tasks/DbReset.java` | `java`, as a source file: the project's JDK, else the one plinto runs on |

- **A name** has lowercase letters, digits and `-`, as `db-reset`. A Java task's file is its name in UpperCamelCase: `DbReset.java` is `plinto db-reset`.
- **Only files directly in `tasks/`** are tasks. Put helpers in a subfolder, such as `tasks/lib/`. `plinto help` warns about a file that looks like a task but isn't one, such as `tasks/Lint.sh` or `tasks/fmt.py`.
- **The first comment** is the task's summary, which `plinto help` lists under the project's tasks.
- **A name is taken once.** A task named like one of plinto's commands makes that command fail, and runs neither. Two files of one name, such as `fmt.sh` and `fmt.lua`, make `plinto fmt` fail with both named. Rename one.

## How a task runs {#running}

- **In the project's folder,** wherever you typed the command, with your terminal's input and output. Ctrl-C stops it.
- **Its exit code is plinto's.** plinto prints nothing of its own, so `plinto fmt --check` works in a git hook.
- **Everything after the name is the task's,** `--help` and `--` too. plinto's own options come before the name: `plinto -p src/Greeter fmt`, or `plinto -a fmt` for every project of the workspace that has the task, those it depends on first, stopping at the first that fails.
- **Nothing is built first.** A task that needs the build runs `"$PLINTO_CLI" build` itself.
- **With the group's tools.** The project's `build-dependencies` come first on the task's `PATH`, as the version group pins them: with `Go = 1.27`, `gofmt` is the group's Go, not whichever one your machine has, and it moves when the group does. A JDK among them is `JAVA_HOME`. The first time, plinto downloads them, saying so on standard error; after that, a task starts as fast as without them, offline too.
- **Only when you name it.** No build, release or editor runs a task, and `tasks/` is in no release.

| Variable | What it holds |
|---|---|
| `PLINTO_PROJECT`, `PLINTO_VERSION` | the project's name and version |
| `PLINTO_PROJECT_DIR`, `PLINTO_BUILD_DIR` | the project's folder and its `build/` |
| `PLINTO_WORKSPACE_DIR` | the workspace's folder, when there is one |
| `PLINTO_TASK` | the task's name |
| `PLINTO_CLI` | the plinto that started it, which runs on plinto's own Java whatever `JAVA_HOME` says |
| `PLINTO_TOOLS` | a folder per tool, named after it: `$PLINTO_TOOLS/Go`, and the packages it brings |
| `PATH` | the build dependencies' `bin/` folders, then yours |
| `JAVA_HOME` | when a build dependency is a JDK |

Everything else your shell has is there too: your credentials, `SSH_AUTH_SOCK`. A tool only a task uses, such as a linter, goes in `build-dependencies` too. A project without a version group, outside a workspace, runs its tasks with your `PATH`'s tools, and plinto warns when it declares some.

## Lua tasks {#lua}

Lua is the portable kind: a real language, the same on macOS, Linux and Windows, with no bash. The `plinto` table adds what Lua lacks.

```text file=tasks/reset.lua
-- Deletes generated code and the local database, then seeds again; --keep-db keeps the database
local keep_db = plinto.args[1] == "--keep-db"

plinto.delete("build/generated")
for _, file in ipairs(plinto.glob("src/**/*_gen.go")) do
  plinto.delete(file)
end

local db = plinto.project.build_dir .. "/greeter.db"
if not keep_db then
  plinto.delete(db)
end

plinto.env.GREETER_DB = db             -- what the commands below get
local code = plinto.cli{ "run", "--", "--seed" }
if code ~= 0 then
  plinto.fail("seeding failed with exit code " .. code)
end
```

| Lua | What it does |
|---|---|
| `plinto.args` | the arguments, a list; Lua's `arg` and `...` have them too |
| `plinto.project` | `name`, `version`, `dir`, `build_dir`; also `plinto.workspace_dir` and `plinto.task` |
| `plinto.tools` | each tool's `version` and `dir`: `plinto.tools.Go.version` |
| `plinto.env` | the environment: setting a key changes what the commands the task runs get |
| `plinto.exec{cmd, …}` | runs a command with the terminal, with no shell in between, and returns its exit code |
| `plinto.output{cmd, …}` | the same, returning its output; a command that fails is an error |
| `plinto.cli{…}` | runs this plinto: `plinto.cli{"build"}` |
| `plinto.delete(path)` | a file, or a folder with everything in it; one that isn't there is fine. Only within the project's folder |
| `plinto.copy(from, to)`, `plinto.move(from, to)` | files or folders |
| `plinto.mkdir(path)` | with its parents |
| `plinto.exists(path)`, `plinto.list(folder)` | whether it's there; the names in a folder |
| `plinto.glob(pattern)` | the project's files that match: `*` within a folder, `**` across folders |
| `plinto.read(path)`, `plinto.write(path, text)` | a whole file as text |
| `plinto.fail(message)` | prints `error: message` and exits with 1 |

Paths are relative to the project's folder. An error names the task's file and line, as `tasks/reset.lua:4: plinto.delete: ../Other is outside Greeter's folder`, and exits with 1. `os.exit(n)` exits with `n`. `require("lib.common")` loads `tasks/lib/common.lua`.

## Java tasks {#java}

```text file=tasks/DbReset.java
// Drops and creates the local database again; --yes doesn't ask first
void main(String[] args) throws Exception {
    var db = "greeter_" + System.getenv("USER");
    if (!List.of(args).contains("--yes") && !IO.readln("Drop " + db + "? [y/N] ").equals("y")) {
        return;
    }
    run("dropdb", "--if-exists", db);
    run("createdb", db);
}

void run(String... command) throws Exception {
    int code = new ProcessBuilder(command).inheritIO().start().waitFor();
    if (code != 0) System.exit(code);
}
```

`plinto db-reset` runs `java tasks/DbReset.java` with the project's JDK when its `build-dependencies` have one, such as `Java = 25`, else with the Java plinto runs on. A file without a class needs Java 25; on an older one, declare a class with a `static void main`. Helpers go in a package folder, such as `tasks/lib/Db.java` with `package lib;`. A task gets the JDK, not the project's classes or libraries.

- [Command line](cli.md): plinto's own commands
- [Run a script during the build](../guides/build-scripts.md): scripts that run as part of every build
- [Your own scripts](../languages/custom.md): a whole build of your own
