如何在Cake中打印工具命令行

How to print tool command line in Cake

我有一些带有很多参数的复杂工具执行:

var settings = new DotNetCoreBuildSettings 
{
    Configuration = "Release",
    NoIncremental = true,
    ArgumentCustomization = arg => arg
        .Append("--source \"https://api.nuget.org/v3/index.json\"")
        .Append("/p:SourceLinkCreate=true")
};
// Execute dotnet build
DotNetCoreBuild("pathToProject", settings);

我需要打印已执行命令的完整命令行。

只能使用诊断日志模式,但我不想使用诊断输出。

解决方案

我写了简单的扩展:

/// <summary>
/// Temporary sets logging verbosity.
/// </summary>
/// <example>
/// <code>
/// // Temporary sets logging verbosity to Diagnostic.
/// using(context.UseVerbosity(Verbosity.Diagnostic))
/// {
///     context.DotNetCoreBuild(project, settings);
/// }
/// </code>
/// </example>
public static VerbosityChanger UseVerbosity(this ICakeContext context, Verbosity newVerbosity) =>
     new VerbosityChanger(context.Log, newVerbosity);

/// <summary>
/// Temporary sets logging verbosity to Diagnostic.
/// </summary>
/// <example>
/// <code>
/// // Temporary sets logging verbosity to Diagnostic.
/// using(context.UseDiagnosticVerbosity())
/// {
///     context.DotNetCoreBuild(project, settings);
/// }
/// </code>
/// </example>
public static VerbosityChanger UseDiagnosticVerbosity(this ICakeContext context) =>
    context.UseVerbosity(Verbosity.Diagnostic);

/// <summary>
/// Cake log verbosity changer.
/// Restores old verbosity on Dispose.
/// </summary>
public class VerbosityChanger : IDisposable
{
    ICakeLog _log;
    Verbosity _oldVerbosity;

    public VerbosityChanger(ICakeLog log, Verbosity newVerbosity)
    {
        _log = log;
        _oldVerbosity = log.Verbosity;
        _log.Verbosity = newVerbosity;
    }

    public void Dispose() => _log.Verbosity = _oldVerbosity;
}

用法

using(context.UseDiagnosticVerbosity())
{
    context.DotNetCoreBuild(project, settings);
}

输出

Executing: /usr/bin/dotnet build "/home/travis/build/micro-elements/MicroElements.Swashbuckle.FluentValidation/src/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation.csproj" --configuration Release --no-incremental  --source "https://api.nuget.org/v3/index.json"  /p:SourceLinkCreate=true