Skip to content

Commit

Permalink
(#3542) Add DotNetTool alias (synonym to DotNetCoreTool)
Browse files Browse the repository at this point in the history
  • Loading branch information
augustoproiete authored and devlead committed Oct 13, 2021
1 parent 55d7d94 commit c408b74
Show file tree
Hide file tree
Showing 10 changed files with 373 additions and 144 deletions.
143 changes: 142 additions & 1 deletion src/Cake.Common/Tools/DotNet/DotNetAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using System.Collections.Generic;
using Cake.Common.IO;
using Cake.Common.Tools.DotNet.MSBuild;
using Cake.Common.Tools.DotNet.Tool;
using Cake.Common.Tools.DotNetCore.MSBuild;
using Cake.Common.Tools.DotNetCore.Tool;
using Cake.Core;
using Cake.Core.Annotations;
using Cake.Core.IO;
Expand Down Expand Up @@ -130,5 +132,144 @@ public static void DotNetMSBuild(this ICakeContext context, string projectOrDire
var builder = new DotNetCoreMSBuildBuilder(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
builder.Build(projectOrDirectory, settings);
}
}

/// <summary>
/// Execute an .NET Core Extensibility Tool.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="command">The command to execute.</param>
/// <example>
/// <code>
/// DotNetTool("cake");
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Tool")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Tool")]
public static void DotNetTool(this ICakeContext context, string command)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

var arguments = new ProcessArgumentBuilder();
var settings = new DotNetToolSettings();

context.DotNetTool(null, command, arguments, settings);
}

/// <summary>
/// Execute an .NET Core Extensibility Tool.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="command">The command to execute.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
/// var settings = new DotNetToolSettings
/// {
/// DiagnosticOutput = true
/// };
///
/// DotNetTool("cake", settings);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Tool")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Tool")]
public static void DotNetTool(this ICakeContext context, string command, DotNetToolSettings settings)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

var arguments = new ProcessArgumentBuilder();

context.DotNetTool(null, command, arguments, settings);
}

/// <summary>
/// Execute an .NET Core Extensibility Tool.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="projectPath">The project path.</param>
/// <param name="command">The command to execute.</param>
/// <example>
/// <code>
/// DotNetTool("./src/project", "xunit", "-xml report.xml");
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Tool")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Tool")]
public static void DotNetTool(this ICakeContext context, FilePath projectPath, string command)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

var arguments = new ProcessArgumentBuilder();
var settings = new DotNetToolSettings();

context.DotNetTool(projectPath, command, arguments, settings);
}

/// <summary>
/// Execute an .NET Core Extensibility Tool.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="projectPath">The project path.</param>
/// <param name="command">The command to execute.</param>
/// <param name="arguments">The arguments.</param>
/// <example>
/// <code>
/// DotNetTool("./src/project", "xunit", "-xml report.xml");
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Tool")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Tool")]
public static void DotNetTool(this ICakeContext context, FilePath projectPath, string command, ProcessArgumentBuilder arguments)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

var settings = new DotNetToolSettings();

context.DotNetTool(projectPath, command, arguments, settings);
}

/// <summary>
/// Execute an .NET Core Extensibility Tool.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="projectPath">The project path.</param>
/// <param name="command">The command to execute.</param>
/// <param name="arguments">The arguments.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
/// DotNetTool("./src/project", "xunit", "-xml report.xml");
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Tool")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Tool")]
public static void DotNetTool(this ICakeContext context, FilePath projectPath, string command, ProcessArgumentBuilder arguments, DotNetToolSettings settings)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

var runner = new DotNetCoreToolRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);

runner.Execute(projectPath, command, arguments, settings);
}
}
}
44 changes: 44 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetRollForward.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Common.Tools.DotNetCore;

namespace Cake.Common.Tools.DotNet
{
/// <summary>
/// Contains the roll forward policy to be used.
/// </summary>
public sealed class DotNetRollForward
{
/// <summary>
/// Roll forward to the lowest higher minor version, if requested minor version is missing.
/// </summary>
public const DotNetCoreRollForward Minor = DotNetCoreRollForward.Minor;

/// <summary>
/// Roll forward to the highest patch version. This disables minor version roll forward.
/// </summary>
public const DotNetCoreRollForward LatestPatch = DotNetCoreRollForward.LatestPatch;

/// <summary>
/// Roll forward to lowest higher major version, and lowest minor version, if requested major version is missing.
/// </summary>
public const DotNetCoreRollForward Major = DotNetCoreRollForward.Major;

/// <summary>
/// Roll forward to highest minor version, even if requested minor version is present.
/// </summary>
public const DotNetCoreRollForward LatestMinor = DotNetCoreRollForward.LatestMinor;

/// <summary>
/// Roll forward to highest major and highest minor version, even if requested major is present.
/// </summary>
public const DotNetCoreRollForward LatestMajor = DotNetCoreRollForward.LatestMajor;

/// <summary>
/// Don't roll forward. Only bind to specified version.
/// </summary>
public const DotNetCoreRollForward Disable = DotNetCoreRollForward.Disable;
}
}
30 changes: 30 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Common.Tools.DotNetCore;
using Cake.Core.Tooling;

namespace Cake.Common.Tools.DotNet
{
/// <summary>
/// Contains common settings used by <see cref="DotNetTool{TSettings}" />.
/// </summary>
public abstract class DotNetSettings : ToolSettings
{
/// <summary>
/// Gets or sets the verbosity of logging to use.
/// </summary>
public DotNetCoreVerbosity? Verbosity { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to not enable diagnostic output.
/// </summary>
public bool DiagnosticOutput { get; set; }

/// <summary>
/// Gets or sets the dotnet roll forward policy.
/// </summary>
public DotNetCoreRollForward? RollForward { get; set; }
}
}
115 changes: 115 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using Cake.Core;
using Cake.Core.IO;
using Cake.Core.Tooling;

namespace Cake.Common.Tools.DotNet
{
/// <summary>
/// Base class for all .NET Core related tools.
/// </summary>
/// <typeparam name="TSettings">The settings type.</typeparam>
public abstract class DotNetTool<TSettings> : Tool<TSettings>
where TSettings : DotNetSettings
{
/// <summary>
/// Initializes a new instance of the <see cref="DotNetTool{TSettings}" /> class.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="environment">The environment.</param>
/// <param name="processRunner">The process runner.</param>
/// <param name="tools">The tool locator.</param>
protected DotNetTool(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools)
: base(fileSystem, environment, processRunner, tools)
{
}

/// <summary>
/// Gets the name of the tool.
/// </summary>
/// <returns>The name of the tool.</returns>
protected override string GetToolName()
{
return ".NET Core CLI";
}

/// <summary>
/// Gets the possible names of the tool executable.
/// </summary>
/// <returns>The tool executable name.</returns>
protected override IEnumerable<string> GetToolExecutableNames()
{
return new[] { "dotnet", "dotnet.exe" };
}

/// <summary>
/// Runs the dotnet cli command using the specified settings and arguments.
/// </summary>
/// <param name="settings">The settings.</param>
/// <param name="arguments">The arguments.</param>
protected void RunCommand(TSettings settings, ProcessArgumentBuilder arguments)
{
// add arguments common to all commands last
AppendCommonArguments(arguments, settings);

Run(settings, arguments, null, null);
}

/// <summary>
/// Runs the dotnet cli command using the specified settings and arguments.
/// </summary>
/// <param name="settings">The settings.</param>
/// <param name="arguments">The arguments.</param>
/// <param name="processSettings">The processSettings.</param>
protected void RunCommand(TSettings settings, ProcessArgumentBuilder arguments, ProcessSettings processSettings)
{
// add arguments common to all commands last
AppendCommonArguments(arguments, settings);

Run(settings, arguments, processSettings, null);
}

/// <summary>
/// Creates a <see cref="ProcessArgumentBuilder"/> and adds common commandline arguments.
/// </summary>
/// <param name="settings">The settings.</param>
/// <returns>Instance of <see cref="ProcessArgumentBuilder"/>.</returns>
protected ProcessArgumentBuilder CreateArgumentBuilder(TSettings settings)
{
var builder = new ProcessArgumentBuilder();

if (settings.DiagnosticOutput)
{
builder.Append("--diagnostics");
}

return builder;
}

/// <summary>
/// Adds common commandline arguments.
/// </summary>
/// <param name="builder">Process argument builder to update.</param>
/// <param name="settings">The settings.</param>
/// <returns>Returns <see cref="ProcessArgumentBuilder"/> updated with common commandline arguments.</returns>
private ProcessArgumentBuilder AppendCommonArguments(ProcessArgumentBuilder builder, TSettings settings)
{
// Verbosity
if (settings.Verbosity.HasValue)
{
builder.Append("--verbosity");
builder.Append(settings.Verbosity.ToString().ToLower());
}

return builder;
}
}
}
16 changes: 16 additions & 0 deletions src/Cake.Common/Tools/DotNet/Tool/DotNetToolSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Common.Tools.DotNetCore;
using Cake.Common.Tools.DotNetCore.Tool;

namespace Cake.Common.Tools.DotNet.Tool
{
/// <summary>
/// Contains settings used by <see cref="DotNetCoreToolRunner" />.
/// </summary>
public class DotNetToolSettings : DotNetCoreSettings
{
}
}
Loading

0 comments on commit c408b74

Please sign in to comment.