CommandAliases.

Command(ICakeContext, CommandSettings, string, ProcessArgumentBuilder) Method

Summary

Executes a generic tool/process based on arguments, settings and redirects standard output.
Namespace
Cake.Common.Tools.Command
Containing Type
CommandAliases

Syntax

[CakeMethodAlias]
[CakeAliasCategory("Command")]
public static int Command(this ICakeContext context, CommandSettings settings, out string standardOutput, ProcessArgumentBuilder arguments = null)

Examples

 using System.Text.Json.Serialization;
 using System.Text.Json;
 #tool dotnet:?package=DPI&version=2022.8.21.54
 // Reusable tools settings i.e. created in setup.
 var settings = new CommandSettings {
         ToolName = "DPI",
         ToolExecutableNames =  new []{ "dpi", "dpi.exe" },
      };

 // Example with ProcessArgumentBuilder
 var exitCode = Command(
     settings,
     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(
      settings,
      out var implicitStandardOutput,
      $"nuget --output JSON analyze"
 );

 var implicitPackageReferences =  JsonSerializer.Deserialize<DPIPackageReference[]>(
     implicitStandardOutput
 );

 // 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

Type Description
CakeMethodAliasAttribute An attribute used to mark script method aliases.
CakeAliasCategoryAttribute An attribute used for documentation of alias methods/properties.

Parameters

Name Type Description
context ICakeContext The context.
settings CommandSettings The settings.
standardOutput string The standard output.
arguments ProcessArgumentBuilder The optional arguments.

Return Value

Type Description
int The exit code.