From c408b74672822713ac474a14f94b2947da49cef8 Mon Sep 17 00:00:00 2001 From: "C. Augusto Proiete" Date: Wed, 6 Oct 2021 20:54:24 -0300 Subject: [PATCH] (#3542) Add DotNetTool alias (synonym to DotNetCoreTool) --- src/Cake.Common/Tools/DotNet/DotNetAliases.cs | 143 +++++++++++++++++- .../Tools/DotNet/DotNetRollForward.cs | 44 ++++++ .../Tools/DotNet/DotNetSettings.cs | 30 ++++ src/Cake.Common/Tools/DotNet/DotNetTool.cs | 115 ++++++++++++++ .../Tools/DotNet/Tool/DotNetToolSettings.cs | 16 ++ .../Tools/DotNetCore/DotNetCoreAliases.cs | 58 ++----- .../Tools/DotNetCore/DotNetCoreSettings.cs | 18 +-- .../Tools/DotNetCore/DotNetCoreTool.cs | 84 +--------- .../DotNetCore/Tool/DotNetCoreToolRunner.cs | 5 +- .../DotNetCore/Tool/DotNetCoreToolSettings.cs | 4 +- 10 files changed, 373 insertions(+), 144 deletions(-) create mode 100644 src/Cake.Common/Tools/DotNet/DotNetRollForward.cs create mode 100644 src/Cake.Common/Tools/DotNet/DotNetSettings.cs create mode 100644 src/Cake.Common/Tools/DotNet/DotNetTool.cs create mode 100644 src/Cake.Common/Tools/DotNet/Tool/DotNetToolSettings.cs diff --git a/src/Cake.Common/Tools/DotNet/DotNetAliases.cs b/src/Cake.Common/Tools/DotNet/DotNetAliases.cs index 8383f779f2..ba5da0f966 100644 --- a/src/Cake.Common/Tools/DotNet/DotNetAliases.cs +++ b/src/Cake.Common/Tools/DotNet/DotNetAliases.cs @@ -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; @@ -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); } - } + + /// + /// Execute an .NET Core Extensibility Tool. + /// + /// The context. + /// The command to execute. + /// + /// + /// DotNetTool("cake"); + /// + /// + [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); + } + + /// + /// Execute an .NET Core Extensibility Tool. + /// + /// The context. + /// The command to execute. + /// The settings. + /// + /// + /// var settings = new DotNetToolSettings + /// { + /// DiagnosticOutput = true + /// }; + /// + /// DotNetTool("cake", settings); + /// + /// + [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); + } + + /// + /// Execute an .NET Core Extensibility Tool. + /// + /// The context. + /// The project path. + /// The command to execute. + /// + /// + /// DotNetTool("./src/project", "xunit", "-xml report.xml"); + /// + /// + [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); + } + + /// + /// Execute an .NET Core Extensibility Tool. + /// + /// The context. + /// The project path. + /// The command to execute. + /// The arguments. + /// + /// + /// DotNetTool("./src/project", "xunit", "-xml report.xml"); + /// + /// + [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); + } + + /// + /// Execute an .NET Core Extensibility Tool. + /// + /// The context. + /// The project path. + /// The command to execute. + /// The arguments. + /// The settings. + /// + /// + /// DotNetTool("./src/project", "xunit", "-xml report.xml"); + /// + /// + [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); + } + } } diff --git a/src/Cake.Common/Tools/DotNet/DotNetRollForward.cs b/src/Cake.Common/Tools/DotNet/DotNetRollForward.cs new file mode 100644 index 0000000000..528966b633 --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/DotNetRollForward.cs @@ -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 +{ + /// + /// Contains the roll forward policy to be used. + /// + public sealed class DotNetRollForward + { + /// + /// Roll forward to the lowest higher minor version, if requested minor version is missing. + /// + public const DotNetCoreRollForward Minor = DotNetCoreRollForward.Minor; + + /// + /// Roll forward to the highest patch version. This disables minor version roll forward. + /// + public const DotNetCoreRollForward LatestPatch = DotNetCoreRollForward.LatestPatch; + + /// + /// Roll forward to lowest higher major version, and lowest minor version, if requested major version is missing. + /// + public const DotNetCoreRollForward Major = DotNetCoreRollForward.Major; + + /// + /// Roll forward to highest minor version, even if requested minor version is present. + /// + public const DotNetCoreRollForward LatestMinor = DotNetCoreRollForward.LatestMinor; + + /// + /// Roll forward to highest major and highest minor version, even if requested major is present. + /// + public const DotNetCoreRollForward LatestMajor = DotNetCoreRollForward.LatestMajor; + + /// + /// Don't roll forward. Only bind to specified version. + /// + public const DotNetCoreRollForward Disable = DotNetCoreRollForward.Disable; + } +} diff --git a/src/Cake.Common/Tools/DotNet/DotNetSettings.cs b/src/Cake.Common/Tools/DotNet/DotNetSettings.cs new file mode 100644 index 0000000000..e7ffab853b --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/DotNetSettings.cs @@ -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 +{ + /// + /// Contains common settings used by . + /// + public abstract class DotNetSettings : ToolSettings + { + /// + /// Gets or sets the verbosity of logging to use. + /// + public DotNetCoreVerbosity? Verbosity { get; set; } + + /// + /// Gets or sets a value indicating whether to not enable diagnostic output. + /// + public bool DiagnosticOutput { get; set; } + + /// + /// Gets or sets the dotnet roll forward policy. + /// + public DotNetCoreRollForward? RollForward { get; set; } + } +} diff --git a/src/Cake.Common/Tools/DotNet/DotNetTool.cs b/src/Cake.Common/Tools/DotNet/DotNetTool.cs new file mode 100644 index 0000000000..7bd386d471 --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/DotNetTool.cs @@ -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 +{ + /// + /// Base class for all .NET Core related tools. + /// + /// The settings type. + public abstract class DotNetTool : Tool + where TSettings : DotNetSettings + { + /// + /// Initializes a new instance of the class. + /// + /// The file system. + /// The environment. + /// The process runner. + /// The tool locator. + protected DotNetTool( + IFileSystem fileSystem, + ICakeEnvironment environment, + IProcessRunner processRunner, + IToolLocator tools) + : base(fileSystem, environment, processRunner, tools) + { + } + + /// + /// Gets the name of the tool. + /// + /// The name of the tool. + protected override string GetToolName() + { + return ".NET Core CLI"; + } + + /// + /// Gets the possible names of the tool executable. + /// + /// The tool executable name. + protected override IEnumerable GetToolExecutableNames() + { + return new[] { "dotnet", "dotnet.exe" }; + } + + /// + /// Runs the dotnet cli command using the specified settings and arguments. + /// + /// The settings. + /// The arguments. + protected void RunCommand(TSettings settings, ProcessArgumentBuilder arguments) + { + // add arguments common to all commands last + AppendCommonArguments(arguments, settings); + + Run(settings, arguments, null, null); + } + + /// + /// Runs the dotnet cli command using the specified settings and arguments. + /// + /// The settings. + /// The arguments. + /// The processSettings. + protected void RunCommand(TSettings settings, ProcessArgumentBuilder arguments, ProcessSettings processSettings) + { + // add arguments common to all commands last + AppendCommonArguments(arguments, settings); + + Run(settings, arguments, processSettings, null); + } + + /// + /// Creates a and adds common commandline arguments. + /// + /// The settings. + /// Instance of . + protected ProcessArgumentBuilder CreateArgumentBuilder(TSettings settings) + { + var builder = new ProcessArgumentBuilder(); + + if (settings.DiagnosticOutput) + { + builder.Append("--diagnostics"); + } + + return builder; + } + + /// + /// Adds common commandline arguments. + /// + /// Process argument builder to update. + /// The settings. + /// Returns updated with common commandline arguments. + private ProcessArgumentBuilder AppendCommonArguments(ProcessArgumentBuilder builder, TSettings settings) + { + // Verbosity + if (settings.Verbosity.HasValue) + { + builder.Append("--verbosity"); + builder.Append(settings.Verbosity.ToString().ToLower()); + } + + return builder; + } + } +} diff --git a/src/Cake.Common/Tools/DotNet/Tool/DotNetToolSettings.cs b/src/Cake.Common/Tools/DotNet/Tool/DotNetToolSettings.cs new file mode 100644 index 0000000000..d3aa9ad9f7 --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/Tool/DotNetToolSettings.cs @@ -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 +{ + /// + /// Contains settings used by . + /// + public class DotNetToolSettings : DotNetCoreSettings + { + } +} diff --git a/src/Cake.Common/Tools/DotNetCore/DotNetCoreAliases.cs b/src/Cake.Common/Tools/DotNetCore/DotNetCoreAliases.cs index e712a14a31..000214d375 100644 --- a/src/Cake.Common/Tools/DotNetCore/DotNetCoreAliases.cs +++ b/src/Cake.Common/Tools/DotNetCore/DotNetCoreAliases.cs @@ -7,6 +7,7 @@ using Cake.Common.IO; using Cake.Common.Tools.DotNet; using Cake.Common.Tools.DotNet.MSBuild; +using Cake.Common.Tools.DotNet.Tool; using Cake.Common.Tools.DotNetCore.Build; using Cake.Common.Tools.DotNetCore.BuildServer; using Cake.Common.Tools.DotNetCore.Clean; @@ -1391,6 +1392,7 @@ public static void DotNetCoreVSTest(this ICakeContext context, IEnumerable + /// [deprecated] DotNetCoreTool is obsolete and will be removed in a future release. Use instead. /// Execute an .NET Core Extensibility Tool. /// /// The context. @@ -1403,20 +1405,14 @@ public static void DotNetCoreVSTest(this ICakeContext context, IEnumerable + /// [deprecated] DotNetCoreTool is obsolete and will be removed in a future release. Use instead. /// Execute an .NET Core Extensibility Tool. /// /// The context. @@ -1435,19 +1431,14 @@ public static void DotNetCoreTool(this ICakeContext context, string command) [CakeMethodAlias] [CakeAliasCategory("Tool")] [CakeNamespaceImport("Cake.Common.Tools.DotNetCore.Tool")] + [Obsolete("DotNetCoreTool is obsolete and will be removed in a future release. Use DotNetTool instead.")] public static void DotNetCoreTool(this ICakeContext context, string command, DotNetCoreToolSettings settings) { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } - - var arguments = new ProcessArgumentBuilder(); - - context.DotNetCoreTool(null, command, arguments, settings); + context.DotNetTool(command, settings); } /// + /// [deprecated] DotNetCoreTool is obsolete and will be removed in a future release. Use instead. /// Execute an .NET Core Extensibility Tool. /// /// The context. @@ -1461,20 +1452,14 @@ public static void DotNetCoreTool(this ICakeContext context, string command, Dot [CakeMethodAlias] [CakeAliasCategory("Tool")] [CakeNamespaceImport("Cake.Common.Tools.DotNetCore.Tool")] + [Obsolete("DotNetCoreTool is obsolete and will be removed in a future release. Use DotNetTool instead.")] public static void DotNetCoreTool(this ICakeContext context, FilePath projectPath, string command) { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } - - var arguments = new ProcessArgumentBuilder(); - var settings = new DotNetCoreToolSettings(); - - context.DotNetCoreTool(projectPath, command, arguments, settings); + context.DotNetTool(projectPath, command); } /// + /// [deprecated] DotNetCoreTool is obsolete and will be removed in a future release. Use instead. /// Execute an .NET Core Extensibility Tool. /// /// The context. @@ -1489,19 +1474,14 @@ public static void DotNetCoreTool(this ICakeContext context, FilePath projectPat [CakeMethodAlias] [CakeAliasCategory("Tool")] [CakeNamespaceImport("Cake.Common.Tools.DotNetCore.Tool")] + [Obsolete("DotNetCoreTool is obsolete and will be removed in a future release. Use DotNetTool instead.")] public static void DotNetCoreTool(this ICakeContext context, FilePath projectPath, string command, ProcessArgumentBuilder arguments) { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } - - var settings = new DotNetCoreToolSettings(); - - context.DotNetCoreTool(projectPath, command, arguments, settings); + context.DotNetTool(projectPath, command, arguments); } /// + /// [deprecated] DotNetCoreTool is obsolete and will be removed in a future release. Use instead. /// Execute an .NET Core Extensibility Tool. /// /// The context. @@ -1517,16 +1497,10 @@ public static void DotNetCoreTool(this ICakeContext context, FilePath projectPat [CakeMethodAlias] [CakeAliasCategory("Tool")] [CakeNamespaceImport("Cake.Common.Tools.DotNetCore.Tool")] + [Obsolete("DotNetCoreTool is obsolete and will be removed in a future release. Use DotNetTool instead.")] public static void DotNetCoreTool(this ICakeContext context, FilePath projectPath, string command, ProcessArgumentBuilder arguments, DotNetCoreToolSettings settings) { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } - - var runner = new DotNetCoreToolRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); - - runner.Execute(projectPath, command, arguments, settings); + context.DotNetTool(projectPath, command, arguments, settings); } /// diff --git a/src/Cake.Common/Tools/DotNetCore/DotNetCoreSettings.cs b/src/Cake.Common/Tools/DotNetCore/DotNetCoreSettings.cs index 6fdce8ce6a..e1c9cc61bc 100644 --- a/src/Cake.Common/Tools/DotNetCore/DotNetCoreSettings.cs +++ b/src/Cake.Common/Tools/DotNetCore/DotNetCoreSettings.cs @@ -2,28 +2,14 @@ // 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.Core.Tooling; +using Cake.Common.Tools.DotNet; namespace Cake.Common.Tools.DotNetCore { /// /// Contains common settings used by . /// - public abstract class DotNetCoreSettings : ToolSettings + public abstract class DotNetCoreSettings : DotNetSettings { - /// - /// Gets or sets the verbosity of logging to use. - /// - public DotNetCoreVerbosity? Verbosity { get; set; } - - /// - /// Gets or sets a value indicating whether to not enable diagnostic output. - /// - public bool DiagnosticOutput { get; set; } - - /// - /// Gets or sets the dotnet roll forward policy. - /// - public DotNetCoreRollForward? RollForward { get; set; } } } diff --git a/src/Cake.Common/Tools/DotNetCore/DotNetCoreTool.cs b/src/Cake.Common/Tools/DotNetCore/DotNetCoreTool.cs index 185a5dd3b2..c8c370ef3c 100644 --- a/src/Cake.Common/Tools/DotNetCore/DotNetCoreTool.cs +++ b/src/Cake.Common/Tools/DotNetCore/DotNetCoreTool.cs @@ -2,7 +2,7 @@ // 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.Common.Tools.DotNet; using Cake.Core; using Cake.Core.IO; using Cake.Core.Tooling; @@ -13,7 +13,7 @@ namespace Cake.Common.Tools.DotNetCore /// Base class for all .NET Core related tools. /// /// The settings type. - public abstract class DotNetCoreTool : Tool + public abstract class DotNetCoreTool : DotNetTool where TSettings : DotNetCoreSettings { /// @@ -31,85 +31,5 @@ public abstract class DotNetCoreTool : Tool : base(fileSystem, environment, processRunner, tools) { } - - /// - /// Gets the name of the tool. - /// - /// The name of the tool. - protected override string GetToolName() - { - return ".NET Core CLI"; - } - - /// - /// Gets the possible names of the tool executable. - /// - /// The tool executable name. - protected override IEnumerable GetToolExecutableNames() - { - return new[] { "dotnet", "dotnet.exe" }; - } - - /// - /// Runs the dotnet cli command using the specified settings and arguments. - /// - /// The settings. - /// The arguments. - protected void RunCommand(TSettings settings, ProcessArgumentBuilder arguments) - { - // add arguments common to all commands last - AppendCommonArguments(arguments, settings); - - Run(settings, arguments, null, null); - } - - /// - /// Runs the dotnet cli command using the specified settings and arguments. - /// - /// The settings. - /// The arguments. - /// The processSettings. - protected void RunCommand(TSettings settings, ProcessArgumentBuilder arguments, ProcessSettings processSettings) - { - // add arguments common to all commands last - AppendCommonArguments(arguments, settings); - - Run(settings, arguments, processSettings, null); - } - - /// - /// Creates a and adds common commandline arguments. - /// - /// The settings. - /// Instance of . - protected ProcessArgumentBuilder CreateArgumentBuilder(TSettings settings) - { - var builder = new ProcessArgumentBuilder(); - - if (settings.DiagnosticOutput) - { - builder.Append("--diagnostics"); - } - - return builder; - } - - /// - /// Adds common commandline arguments. - /// - /// Process argument builder to update. - /// The settings. - /// Returns updated with common commandline arguments. - private ProcessArgumentBuilder AppendCommonArguments(ProcessArgumentBuilder builder, TSettings settings) - { - // Verbosity - if (settings.Verbosity.HasValue) - { - builder.Append("--verbosity"); - builder.Append(settings.Verbosity.ToString().ToLower()); - } - - return builder; - } } } diff --git a/src/Cake.Common/Tools/DotNetCore/Tool/DotNetCoreToolRunner.cs b/src/Cake.Common/Tools/DotNetCore/Tool/DotNetCoreToolRunner.cs index ff053daa3c..84ee744d53 100644 --- a/src/Cake.Common/Tools/DotNetCore/Tool/DotNetCoreToolRunner.cs +++ b/src/Cake.Common/Tools/DotNetCore/Tool/DotNetCoreToolRunner.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using Cake.Common.Tools.DotNet.Tool; using Cake.Core; using Cake.Core.IO; using Cake.Core.Tooling; @@ -39,7 +40,7 @@ public sealed class DotNetCoreToolRunner : DotNetCoreTool /// The command to execute. /// The arguments. /// The settings. - public void Execute(FilePath projectPath, string command, ProcessArgumentBuilder arguments, DotNetCoreToolSettings settings) + public void Execute(FilePath projectPath, string command, ProcessArgumentBuilder arguments, DotNetToolSettings settings) { if (string.IsNullOrWhiteSpace(command)) { @@ -59,7 +60,7 @@ public void Execute(FilePath projectPath, string command, ProcessArgumentBuilder RunCommand(settings, GetArguments(command, arguments, settings), processSettings); } - private ProcessArgumentBuilder GetArguments(string command, ProcessArgumentBuilder arguments, DotNetCoreToolSettings settings) + private ProcessArgumentBuilder GetArguments(string command, ProcessArgumentBuilder arguments, DotNetToolSettings settings) { var builder = CreateArgumentBuilder(settings); diff --git a/src/Cake.Common/Tools/DotNetCore/Tool/DotNetCoreToolSettings.cs b/src/Cake.Common/Tools/DotNetCore/Tool/DotNetCoreToolSettings.cs index 2698601c92..fc1324a18c 100644 --- a/src/Cake.Common/Tools/DotNetCore/Tool/DotNetCoreToolSettings.cs +++ b/src/Cake.Common/Tools/DotNetCore/Tool/DotNetCoreToolSettings.cs @@ -2,12 +2,14 @@ // 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.DotNet.Tool; + namespace Cake.Common.Tools.DotNetCore.Tool { /// /// Contains settings used by . /// - public sealed class DotNetCoreToolSettings : DotNetCoreSettings + public sealed class DotNetCoreToolSettings : DotNetToolSettings { } }