// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using Progmasoft.Tazel.Dsl; using Progmasoft.Tazel.Dsl.Build.Prelude; using Progmasoft.Tazel.Dsl.Common; namespace Progmasoft.Tazel.PluginApi.Steps; /// /// A build step: work of a plugin that reads and writes into its output /// directory. It runs again only when its inputs, its outputs or its changed. A C-family or Zig target /// that depends on it runs after it and finds headers in its output directory. /// public sealed class StepSettings : TargetSettings { /// The files the step reads, below the project file. public Files Inputs { get; init; } = Files.None; /// The files the step writes, relative to its output directory. public IReadOnlyList Outputs { get; init; } = []; /// What else changes what the step makes, such as its options; a different key runs it again. public string Key { get; init; } = ""; /// The work of the step. public Func? Run { get; init; } } /// Declares build steps; the methods of a plugin call it from the BUILD.csx files that use them. public static class Step { /// Declares the step in the BUILD.csx that is running. public static Target Declare( string name, StepSettings settings, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0) { ArgumentNullException.ThrowIfNull(settings); if (settings.Run is null) { throw new DslException($"{Declarations.Current.Place(line)}: the step '{name}' needs its Run"); } return Declarations.Declare(Family.Plugin, TargetKind.Step, name, settings, file, line); } } /// Where a step runs and what it can ask of Tazel. public interface IStepContext { /// The label of the step. string Label { get; } /// The root of the module of the step. string ModuleRoot { get; } /// The directory of the project file that declared the step. string PackageDirectory { get; } /// The directory the step writes its outputs to; it exists when the step runs. string OutputDirectory { get; } /// The full paths of the inputs. IReadOnlyList Inputs { get; } /// The program or library each target the step depends on made, by its label, as a full path. IReadOnlyDictionary Dependencies { get; } /// The platform the build is for. Platform Platform { get; } /// Whether the build is a debug or a release build. BuildMode Mode { get; } /// The full path of a program on PATH, or null. string? FindProgram(string name); /// /// Runs directly, without a shell, with the environment of the actions of the build, in /// or the root of the module; the result has its exit status and output. /// Task RunAsync( string program, IReadOnlyList arguments, string? workingDirectory, CancellationToken cancellationToken); /// Runs in the root of the module; see the overload with every parameter. Task RunAsync(string program, IReadOnlyList arguments) => RunAsync(program, arguments, null, CancellationToken.None); /// Runs in ; see the overload with every parameter. Task RunAsync(string program, IReadOnlyList arguments, string? workingDirectory) => RunAsync(program, arguments, workingDirectory, CancellationToken.None); /// Adds a line to what the step prints, shown with the step when the build reports it. void Log(string message); } /// What a program a step ran did. /// Its exit status. /// What it printed, its errors among it. public sealed record StepRun(int ExitCode, string Output);