silk.cli.task.

Commands implemented by a project's tasks.silk program.

Put project automation in src/tasks.silk. A command is a union variant described with silk.cli. silk build, silk run, silk dev, silk test and silk plan run the variants named Build, Run, Dev, Test and Plan; for one the union does not have, Task.main hands the command back and the toolchain runs its built-in. Dependencies' tasks never run. silk init writes a tasks.silk whose standard variants call silk.build.tasks.

Task.main parses typed options and invokes TaskSet.perform. Task.exec and Task.silk share the developer's terminal and environment. SILK_PROJECT_ROOT is the package directory, SILK_INVOCATION_DIRECTORY is where the command was typed, and SILK_COMPILER is the compiler that built the tasks program.

Examples#

silk
import silk.cli.command { Command, CommandLine, Opt }
import silk.cli.task { Task, TaskError, TaskSet }
import silk.io.writer { WriterError }
import silk.mem.allocator { Allocator, OutOfMemoryError }
import silk.system.child_process { ChildProcess, ProcessError }
import silk.system.filesystem { FileError, FileSystem }
import silk.system.host_input { HostInput, HostInputError }
import silk.text.string { String }

union Tasks {
  Codegen,
  Release { pub version: String },
}

impl CommandLine for Tasks {
  static fn command() -> Command<Tasks> {
    return Command<Tasks>.make("tasks", "This project's tasks")
      |> Command.variant("Codegen", "Regenerate the tables")
      |> Command.variant("Release", "Test and pack a release")
      |> Command.arg("version", Opt.about("The version") |> Opt.required)
  }
}

impl TaskSet for Tasks {
  effect fn perform(self: Tasks) -> ()
    ! TaskError | ProcessError | FileError | HostInputError | OutOfMemoryError | WriterError
    ? &mut HostInput | &mut Allocator | &mut ChildProcess | &mut FileSystem {
    match move self {
      Tasks.Codegen => run Task.exec("scripts/generate", &["--pinned"])
      Tasks.Release { version: _ } => {
        run Task.silk(&["test"])
        run Task.silk(&["pack"])
      }
    }
  }
}

pub effect fn main() -> i32 ? &mut HostInput {
  return run Task.main<Tasks>()
}

Import as Task with import silk.cli.task { Task }.

Public declarations: 5.

TaskError#

silk
pub struct TaskError

A task that failed: what to report, and the exit status the tasks program ends with.

Field message#

silk
pub message: String

The one line Task.main writes to standard error, after error: .

Field status#

silk
pub status: i32

The exit status, such as the status of a program that Task.exec ran. Never 0.

Implementation Debug for TaskError#

silk
impl Debug for TaskError

Operation debug#

silk
effect fn debug(self: &Self, formatter: &mut Formatter) -> ()
    ! WriterError
    ? &mut Writer

TaskSet#

silk
pub interface TaskSet

A command line whose variants are tasks.

Operation perform#

silk
effect fn perform(self: Self) -> ()
    ! TaskError | StepFailure | SpawnError | Cancelled
    ? &mut HostInput | &mut Allocator | &mut ChildProcess | &mut FileSystem

Runs the task this value names, with its options.

Task#

silk
pub struct Task

The functions of a tasks program.

Implementation Task#

silk
impl Task

Associated function main#

silk
pub effect fn main<T: CommandLine + TaskSet>() -> i32
    ? &mut HostInput

Reads the command line as T, runs the task it names, and answers the exit status: 0 when the task succeeded, 0 after --help, 2 after a command line error, and a failed task's TaskError.status after writing error: <message> to standard error. An unexpected failure, such as a file that cannot be read, is reported the same way with status 1.

Help names the command silk task, as it is typed. --help without a task's name lists the tasks, then writes the help of each one with its options and arguments. --help=json writes that help as one JSON object, with every task under commands.

build, run, dev, test and plan are the toolchain's own commands. When silk starts the tasks program for one of them and T has no variant of that name, nothing runs here: the status is 0 and silk runs its built-in command.

Associated function abort#

silk
pub effect fn abort(message: string) -> never
    ! TaskError | OutOfMemoryError
    ? &mut Allocator

Fails the task with message and status 1.

Associated function exec#

silk
pub effect fn exec(program: string, arguments: &[string]) -> ()
    ! TaskError | HostInputError | OutOfMemoryError
    ? &mut HostInput | &mut Allocator | &mut FileSystem

Runs program with arguments and waits for it. The program shares the task's standard input, output and error, environment and working directory. A name without a / is looked up on PATH; a path is relative to the working directory, the package directory.

Errors#

TaskError when the program cannot be started, or ends with a nonzero status, which becomes the error's status.

Associated function execBytes#

silk
pub effect fn execBytes(program: &[u8], arguments: &[Bytes]) -> ()
    ! TaskError | HostInputError | OutOfMemoryError
    ? &mut HostInput | &mut Allocator | &mut FileSystem

Task.exec, for a program and arguments that need not be UTF-8.

Associated function silk#

silk
pub effect fn silk(arguments: &[string]) -> ()
    ! TaskError | HostInputError | OutOfMemoryError
    ? &mut HostInput | &mut Allocator | &mut FileSystem

Runs the compiler that built the tasks, SILK_COMPILER, with arguments, as Task.exec does, such as run Task.silk(&["test"]).

Errors#

TaskError when SILK_COMPILER is not set, or as Task.exec.

Associated function capture#

silk
pub effect fn capture(program: string, arguments: &[string]) -> Bytes
    ! TaskError | ProcessError | HostInputError | OutOfMemoryError | WriterError
    ? &mut HostInput | &mut Allocator | &mut ChildProcess | &mut FileSystem

Runs program with arguments to the end and answers its standard output. Its standard error is passed on to the task's. The program starts in the package directory with no environment but PATH, and a name without a / is looked up on PATH.

Errors#

TaskError when the program cannot be found, or ends with a nonzero status or a signal.

Associated function root#

silk
pub effect fn root() -> Path
    ! TaskError | HostInputError | OutOfMemoryError
    ? &mut HostInput | &mut Allocator

The package directory, SILK_PROJECT_ROOT, which is also the working directory a task starts in.

Errors#

TaskError when the variable is not set, as outside silk task.

Associated function invocationDirectory#

silk
pub effect fn invocationDirectory() -> Path
    ! TaskError | HostInputError | OutOfMemoryError
    ? &mut HostInput | &mut Allocator

The directory silk task was typed in, SILK_INVOCATION_DIRECTORY, for a task that takes paths relative to it.

Errors#

TaskError when the variable is not set, as outside silk task.

fileAction#

silk
pub fn fileAction(code: i32) -> &'static [u8]

What a FileOperation code was doing, for a report such as could not read a file: ....

fileReason#

silk
pub fn fileReason(code: i32) -> &'static [u8]

Why a file operation failed, from a FileReason code.

Category: Command line.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page