Search code examples
cakebuild

How to print tool command line in Cake


I have some complex tool execution with many params:

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);

I need to print full command line of executed command.

It is possible only with Diagnostic log mode but I don't want to use Diagnostic output.


Solution

  • Solution

    I wrote simple extensions:

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

    Usage

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

    Output

    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