Skip to content

Commit

Permalink
(cake-build#3542) Add DotNetTool alias (synonym to DotNetCoreTool)
Browse files Browse the repository at this point in the history
  • Loading branch information
augustoproiete committed Oct 11, 2021
1 parent be2de34 commit 88e8bc2
Show file tree
Hide file tree
Showing 11 changed files with 339 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// See the LICENSE file in the project root for more information.

using Cake.Common.Tests.Fixtures.Tools.DotNetCore.Run;
using Cake.Common.Tools.DotNetCore;
using Cake.Common.Tools.DotNet;
using Cake.Testing;
using Xunit;

Expand Down Expand Up @@ -95,7 +95,7 @@ public void Should_Add_Additional_Settings()
fixture.Settings.Configuration = "Release";
fixture.Settings.Runtime = "win7-x86";
fixture.Settings.Sources = new[] { "https://api.nuget.org/v3/index.json" };
fixture.Settings.RollForward = DotNetCoreRollForward.Major;
fixture.Settings.RollForward = DotNetRollForward.Major;

// When
var result = fixture.Run();
Expand All @@ -119,13 +119,13 @@ public void Should_Add_Host_Arguments()
}

[Theory]
[InlineData(DotNetCoreRollForward.Minor, "run --roll-forward Minor")]
[InlineData(DotNetCoreRollForward.LatestPatch, "run --roll-forward LatestPatch")]
[InlineData(DotNetCoreRollForward.Major, "run --roll-forward Major")]
[InlineData(DotNetCoreRollForward.LatestMinor, "run --roll-forward LatestMinor")]
[InlineData(DotNetCoreRollForward.LatestMajor, "run --roll-forward LatestMajor")]
[InlineData(DotNetCoreRollForward.Disable, "run --roll-forward Disable")]
public void Should_Add_RollForward_Arguments(DotNetCoreRollForward rollForward, string expected)
[InlineData(DotNetRollForward.Minor, "run --roll-forward Minor")]
[InlineData(DotNetRollForward.LatestPatch, "run --roll-forward LatestPatch")]
[InlineData(DotNetRollForward.Major, "run --roll-forward Major")]
[InlineData(DotNetRollForward.LatestMinor, "run --roll-forward LatestMinor")]
[InlineData(DotNetRollForward.LatestMajor, "run --roll-forward LatestMajor")]
[InlineData(DotNetRollForward.Disable, "run --roll-forward Disable")]
public void Should_Add_RollForward_Arguments(DotNetRollForward rollForward, string expected)
{
// Given
var fixture = new DotNetCoreRunnerFixture();
Expand Down
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace Cake.Common.Tools.DotNetCore
namespace Cake.Common.Tools.DotNet
{
/// <summary>
/// Contains the roll forward policy to be used.
/// </summary>
public enum DotNetCoreRollForward
public enum DotNetRollForward
{
/// <summary>
/// Roll forward to the lowest higher minor version, if requested minor version is missing.
Expand Down
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 DotNetRollForward? 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 88e8bc2

Please sign in to comment.