Summary
Executes a generic tool/process based on arguments, tool executable names and redirects standard output.
Syntax
[CakeMethodAlias]
[CakeAliasCategory("Command")]
public static int Command(this ICakeContext context, ICollection<string> toolExecutableNames, out string standardOutput, ProcessArgumentBuilder arguments = null, int expectedExitCode = 0, Func<CommandSettings, CommandSettings> settingsCustomization = null)
Examples
using System.Text.Json.Serialization;
using System.Text.Json;
#tool dotnet:?package=DPI&version=2022.8.21.54
// Example with ProcessArgumentBuilder
var exitCode = Command(
new []{ "dpi", "dpi.exe"},
out var standardOutput,
new ProcessArgumentBuilder()
.Append("nuget")
.AppendQuoted(Context.Environment.WorkingDirectory.FullPath)
.AppendSwitch("--output", " ", "JSON")
.Append("analyze")
);
var packageReferences = JsonSerializer.Deserialize<DPIPackageReference[]>(
standardOutput
);
// Example with implicit ProcessArgumentBuilder
var implicitExitCode = Command(
new []{ "dpi", "dpi.exe"},
out var implicitStandardOutput,
$"nuget --output JSON analyze"
);
var implicitPackageReferences = JsonSerializer.Deserialize<DPIPackageReference[]>(
implicitStandardOutput
);
// Example settings customization
var settingsCustomizationExitCode = Command(
new []{ "dpi", "dpi.exe"},
out var settingsCustomizationStandardOutput,
$"nuget --output JSON analyze",
settingsCustomization: settings => settings
.WithToolName("DPI")
.WithArgumentCustomization(args => args.AppendSwitchQuoted("--buildversion", " ", "1.0.0"))
);
var settingsCustomizationPackageReferences = JsonSerializer.Deserialize<DPIPackageReference[]>(
settingsCustomizationStandardOutput
);
// Record used in example above
public record DPIPackageReference(
[property: JsonPropertyName("source")]
string Source,
[property: JsonPropertyName("sourceType")]
string SourceType,
[property: JsonPropertyName("packageId")]
string PackageId,
[property: JsonPropertyName("version")]
string Version
);
Attributes
Parameters
Name |
Type |
Description |
context |
ICakeContext |
The context. |
toolExecutableNames |
ICollection<string> |
The tool executable names. |
standardOutput |
string |
The standard output. |
arguments |
ProcessArgumentBuilder |
The optional arguments. |
expectedExitCode |
int |
The expected exit code (default 0). |
settingsCustomization |
Func<CommandSettings, CommandSettings> |
The optional settings customization. |
Return Value
Type |
Description |
int |
The exit code. |