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#
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#
pub struct TaskErrorA task that failed: what to report, and the exit status the tasks program ends with.
Field message#
pub message: StringThe one line Task.main writes to standard error, after error: .
Field status#
pub status: i32The exit status, such as the status of a program that Task.exec ran. Never 0.
Implementation Debug for TaskError#
impl Debug for TaskErrorOperation debug#
effect fn debug(self: &Self, formatter: &mut Formatter) -> ()
! WriterError
? &mut WriterTaskSet#
pub interface TaskSetA command line whose variants are tasks.
Operation perform#
effect fn perform(self: Self) -> ()
! TaskError | StepFailure | SpawnError | Cancelled
? &mut HostInput | &mut Allocator | &mut ChildProcess | &mut FileSystemRuns the task this value names, with its options.
Task#
pub struct TaskThe functions of a tasks program.
Implementation Task#
impl TaskAssociated function main#
pub effect fn main<T: CommandLine + TaskSet>() -> i32
? &mut HostInputReads 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#
pub effect fn abort(message: string) -> never
! TaskError | OutOfMemoryError
? &mut AllocatorFails the task with message and status 1.
Associated function exec#
pub effect fn exec(program: string, arguments: &[string]) -> ()
! TaskError | HostInputError | OutOfMemoryError
? &mut HostInput | &mut Allocator | &mut FileSystemRuns 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#
pub effect fn execBytes(program: &[u8], arguments: &[Bytes]) -> ()
! TaskError | HostInputError | OutOfMemoryError
? &mut HostInput | &mut Allocator | &mut FileSystemTask.exec, for a program and arguments that need not be UTF-8.
Associated function silk#
pub effect fn silk(arguments: &[string]) -> ()
! TaskError | HostInputError | OutOfMemoryError
? &mut HostInput | &mut Allocator | &mut FileSystemRuns 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#
pub effect fn capture(program: string, arguments: &[string]) -> Bytes
! TaskError | ProcessError | HostInputError | OutOfMemoryError | WriterError
? &mut HostInput | &mut Allocator | &mut ChildProcess | &mut FileSystemRuns 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#
pub effect fn root() -> Path
! TaskError | HostInputError | OutOfMemoryError
? &mut HostInput | &mut AllocatorThe 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#
pub effect fn invocationDirectory() -> Path
! TaskError | HostInputError | OutOfMemoryError
? &mut HostInput | &mut AllocatorThe 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#
pub fn fileAction(code: i32) -> &'static [u8]What a FileOperation code was doing, for a report such as could not read a file: ....
fileReason#
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.