From 8a7302fc789d346bfcf263792ef9f65af8ce8f4d Mon Sep 17 00:00:00 2001 From: James Connor Date: Thu, 17 Feb 2022 11:22:26 +0000 Subject: [PATCH] Add dotnet sln and list commands --- src/Cake.Common/Tools/DotNet/DotNetAliases.cs | 277 +++++++++++++++--- .../Tools/DotNet/List/DotNetList.cs | 86 ++++++ .../Tools/DotNet/List/DotNetListSettings.cs | 13 + src/Cake.Common/Tools/DotNet/Sln/DotNetSln.cs | 154 ++++++++++ .../Tools/DotNet/Sln/DotNetSlnSettings.cs | 13 + 5 files changed, 498 insertions(+), 45 deletions(-) create mode 100644 src/Cake.Common/Tools/DotNet/List/DotNetList.cs create mode 100644 src/Cake.Common/Tools/DotNet/List/DotNetListSettings.cs create mode 100644 src/Cake.Common/Tools/DotNet/Sln/DotNetSln.cs create mode 100644 src/Cake.Common/Tools/DotNet/Sln/DotNetSlnSettings.cs diff --git a/src/Cake.Common/Tools/DotNet/DotNetAliases.cs b/src/Cake.Common/Tools/DotNet/DotNetAliases.cs index 311f0b6a67..c8ad2c3487 100644 --- a/src/Cake.Common/Tools/DotNet/DotNetAliases.cs +++ b/src/Cake.Common/Tools/DotNet/DotNetAliases.cs @@ -10,6 +10,7 @@ using Cake.Common.Tools.DotNet.Clean; using Cake.Common.Tools.DotNet.Execute; using Cake.Common.Tools.DotNet.Format; +using Cake.Common.Tools.DotNet.List; using Cake.Common.Tools.DotNet.MSBuild; using Cake.Common.Tools.DotNet.NuGet.Delete; using Cake.Common.Tools.DotNet.NuGet.Push; @@ -19,6 +20,7 @@ using Cake.Common.Tools.DotNet.Restore; using Cake.Common.Tools.DotNet.Run; using Cake.Common.Tools.DotNet.SDKCheck; +using Cake.Common.Tools.DotNet.Sln; using Cake.Common.Tools.DotNet.Test; using Cake.Common.Tools.DotNet.Tool; using Cake.Common.Tools.DotNet.VSTest; @@ -86,7 +88,8 @@ public static void DotNetExecute(this ICakeContext context, FilePath assemblyPat [CakeMethodAlias] [CakeAliasCategory("Execute")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.Execute")] - public static void DotNetExecute(this ICakeContext context, FilePath assemblyPath, ProcessArgumentBuilder arguments) + public static void DotNetExecute(this ICakeContext context, FilePath assemblyPath, + ProcessArgumentBuilder arguments) { context.DotNetExecute(assemblyPath, arguments, null); } @@ -111,7 +114,8 @@ public static void DotNetExecute(this ICakeContext context, FilePath assemblyPat [CakeMethodAlias] [CakeAliasCategory("Execute")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.Execute")] - public static void DotNetExecute(this ICakeContext context, FilePath assemblyPath, ProcessArgumentBuilder arguments, DotNetExecuteSettings settings) + public static void DotNetExecute(this ICakeContext context, FilePath assemblyPath, + ProcessArgumentBuilder arguments, DotNetExecuteSettings settings) { if (context is null) { @@ -128,7 +132,8 @@ public static void DotNetExecute(this ICakeContext context, FilePath assemblyPat settings = new DotNetExecuteSettings(); } - var executor = new DotNetCoreExecutor(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var executor = new DotNetCoreExecutor(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); executor.Execute(assemblyPath, arguments, settings); } @@ -231,7 +236,8 @@ public static void DotNetRestore(this ICakeContext context, string root, DotNetR settings = new DotNetRestoreSettings(); } - var restorer = new DotNetCoreRestorer(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log); + var restorer = new DotNetCoreRestorer(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools, context.Log); restorer.Restore(root, settings); } @@ -286,7 +292,8 @@ public static void DotNetBuild(this ICakeContext context, string project, DotNet settings = new DotNetBuildSettings(); } - var builder = new DotNetCoreBuilder(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var builder = new DotNetCoreBuilder(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); builder.Build(project, settings); } @@ -341,7 +348,8 @@ public static void DotNetPublish(this ICakeContext context, string project, DotN settings = new DotNetPublishSettings(); } - var publisher = new DotNetCorePublisher(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var publisher = new DotNetCorePublisher(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); publisher.Publish(project, settings); } @@ -496,7 +504,8 @@ public static void DotNetTest(this ICakeContext context, string project, DotNetT [CakeMethodAlias] [CakeAliasCategory("Test")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.Test")] - public static void DotNetTest(this ICakeContext context, string project, ProcessArgumentBuilder arguments, DotNetTestSettings settings) + public static void DotNetTest(this ICakeContext context, string project, ProcessArgumentBuilder arguments, + DotNetTestSettings settings) { if (context is null) { @@ -508,7 +517,8 @@ public static void DotNetTest(this ICakeContext context, string project, Process settings = new DotNetTestSettings(); } - var tester = new DotNetCoreTester(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var tester = new DotNetCoreTester(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); tester.Test(project, arguments, settings); } @@ -563,7 +573,8 @@ public static void DotNetClean(this ICakeContext context, string project, DotNet settings = new DotNetCleanSettings(); } - var cleaner = new DotNetCoreCleaner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var cleaner = new DotNetCoreCleaner(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); cleaner.Clean(project, settings); } @@ -641,7 +652,8 @@ public static void DotNetNuGetDelete(this ICakeContext context, string packageNa [CakeMethodAlias] [CakeAliasCategory("NuGet")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Delete")] - public static void DotNetNuGetDelete(this ICakeContext context, string packageName, DotNetNuGetDeleteSettings settings) + public static void DotNetNuGetDelete(this ICakeContext context, string packageName, + DotNetNuGetDeleteSettings settings) { context.DotNetNuGetDelete(packageName, null, settings); } @@ -691,7 +703,8 @@ public static void DotNetNuGetDelete(this ICakeContext context, DotNetNuGetDelet [CakeMethodAlias] [CakeAliasCategory("NuGet")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Delete")] - public static void DotNetNuGetDelete(this ICakeContext context, string packageName, string packageVersion, DotNetNuGetDeleteSettings settings) + public static void DotNetNuGetDelete(this ICakeContext context, string packageName, string packageVersion, + DotNetNuGetDeleteSettings settings) { if (context is null) { @@ -703,7 +716,8 @@ public static void DotNetNuGetDelete(this ICakeContext context, string packageNa settings = new DotNetNuGetDeleteSettings(); } - var nugetDeleter = new DotNetCoreNuGetDeleter(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var nugetDeleter = new DotNetCoreNuGetDeleter(context.FileSystem, context.Environment, + context.ProcessRunner, context.Tools); nugetDeleter.Delete(packageName, packageVersion, settings); } @@ -752,7 +766,8 @@ public static void DotNetNuGetPush(this ICakeContext context, FilePath packageFi [CakeMethodAlias] [CakeAliasCategory("NuGet")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Push")] - public static void DotNetNuGetPush(this ICakeContext context, FilePath packageFilePath, DotNetNuGetPushSettings settings) + public static void DotNetNuGetPush(this ICakeContext context, FilePath packageFilePath, + DotNetNuGetPushSettings settings) { if (context is null) { @@ -764,7 +779,8 @@ public static void DotNetNuGetPush(this ICakeContext context, FilePath packageFi settings = new DotNetNuGetPushSettings(); } - var restorer = new DotNetCoreNuGetPusher(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var restorer = new DotNetCoreNuGetPusher(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); restorer.Push(packageFilePath?.FullPath, settings); } @@ -791,14 +807,16 @@ public static void DotNetNuGetPush(this ICakeContext context, FilePath packageFi [CakeMethodAlias] [CakeAliasCategory("NuGet")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Source")] - public static void DotNetNuGetAddSource(this ICakeContext context, string name, DotNetNuGetSourceSettings settings) + public static void DotNetNuGetAddSource(this ICakeContext context, string name, + DotNetNuGetSourceSettings settings) { if (context is null) { throw new ArgumentNullException(nameof(context)); } - var sourcer = new DotNetCoreNuGetSourcer(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var sourcer = new DotNetCoreNuGetSourcer(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); sourcer.AddSource(name, settings); } @@ -839,14 +857,16 @@ public static void DotNetNuGetDisableSource(this ICakeContext context, string na [CakeMethodAlias] [CakeAliasCategory("NuGet")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Source")] - public static void DotNetNuGetDisableSource(this ICakeContext context, string name, DotNetNuGetSourceSettings settings) + public static void DotNetNuGetDisableSource(this ICakeContext context, string name, + DotNetNuGetSourceSettings settings) { if (context is null) { throw new ArgumentNullException(nameof(context)); } - var sourcer = new DotNetCoreNuGetSourcer(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var sourcer = new DotNetCoreNuGetSourcer(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); sourcer.DisableSource(name, settings ?? new DotNetNuGetSourceSettings()); } @@ -887,14 +907,16 @@ public static void DotNetNuGetEnableSource(this ICakeContext context, string nam [CakeMethodAlias] [CakeAliasCategory("NuGet")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Source")] - public static void DotNetNuGetEnableSource(this ICakeContext context, string name, DotNetNuGetSourceSettings settings) + public static void DotNetNuGetEnableSource(this ICakeContext context, string name, + DotNetNuGetSourceSettings settings) { if (context is null) { throw new ArgumentNullException(nameof(context)); } - var sourcer = new DotNetCoreNuGetSourcer(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var sourcer = new DotNetCoreNuGetSourcer(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); sourcer.EnableSource(name, settings ?? new DotNetNuGetSourceSettings()); } @@ -937,14 +959,16 @@ public static bool DotNetNuGetHasSource(this ICakeContext context, string name) [CakeMethodAlias] [CakeAliasCategory("NuGet")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Source")] - public static bool DotNetNuGetHasSource(this ICakeContext context, string name, DotNetNuGetSourceSettings settings) + public static bool DotNetNuGetHasSource(this ICakeContext context, string name, + DotNetNuGetSourceSettings settings) { if (context is null) { throw new ArgumentNullException(nameof(context)); } - var sourcer = new DotNetCoreNuGetSourcer(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var sourcer = new DotNetCoreNuGetSourcer(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); return sourcer.HasSource(name, settings ?? new DotNetNuGetSourceSettings()); } @@ -985,14 +1009,16 @@ public static void DotNetNuGetRemoveSource(this ICakeContext context, string nam [CakeMethodAlias] [CakeAliasCategory("NuGet")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Source")] - public static void DotNetNuGetRemoveSource(this ICakeContext context, string name, DotNetNuGetSourceSettings settings) + public static void DotNetNuGetRemoveSource(this ICakeContext context, string name, + DotNetNuGetSourceSettings settings) { if (context is null) { throw new ArgumentNullException(nameof(context)); } - var sourcer = new DotNetCoreNuGetSourcer(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var sourcer = new DotNetCoreNuGetSourcer(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); sourcer.RemoveSource(name, settings ?? new DotNetNuGetSourceSettings()); } @@ -1019,14 +1045,16 @@ public static void DotNetNuGetRemoveSource(this ICakeContext context, string nam [CakeMethodAlias] [CakeAliasCategory("NuGet")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Source")] - public static void DotNetNuGetUpdateSource(this ICakeContext context, string name, DotNetNuGetSourceSettings settings) + public static void DotNetNuGetUpdateSource(this ICakeContext context, string name, + DotNetNuGetSourceSettings settings) { if (context is null) { throw new ArgumentNullException(nameof(context)); } - var sourcer = new DotNetCoreNuGetSourcer(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var sourcer = new DotNetCoreNuGetSourcer(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); sourcer.UpdateSource(name, settings); } @@ -1080,7 +1108,8 @@ public static void DotNetPack(this ICakeContext context, string project, DotNetP settings = new DotNetPackSettings(); } - var packer = new DotNetCorePacker(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var packer = new DotNetCorePacker(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); packer.Pack(project, settings); } @@ -1159,7 +1188,8 @@ public static void DotNetRun(this ICakeContext context, string project, ProcessA [CakeMethodAlias] [CakeAliasCategory("Run")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.Run")] - public static void DotNetRun(this ICakeContext context, string project, ProcessArgumentBuilder arguments, DotNetRunSettings settings) + public static void DotNetRun(this ICakeContext context, string project, ProcessArgumentBuilder arguments, + DotNetRunSettings settings) { if (context is null) { @@ -1171,7 +1201,8 @@ public static void DotNetRun(this ICakeContext context, string project, ProcessA settings = new DotNetRunSettings(); } - var runner = new DotNetCoreRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var runner = new DotNetCoreRunner(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); runner.Run(project, arguments, settings); } @@ -1291,7 +1322,8 @@ public static void DotNetMSBuild(this ICakeContext context, DotNetMSBuildSetting [CakeMethodAlias] [CakeAliasCategory("MSBuild")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.MSBuild")] - public static void DotNetMSBuild(this ICakeContext context, string projectOrDirectory, DotNetMSBuildSettings settings) + public static void DotNetMSBuild(this ICakeContext context, string projectOrDirectory, + DotNetMSBuildSettings settings) { if (context is null) { @@ -1303,10 +1335,152 @@ public static void DotNetMSBuild(this ICakeContext context, string projectOrDire settings = new DotNetMSBuildSettings(); } - var builder = new DotNetCoreMSBuildBuilder(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var builder = new DotNetCoreMSBuildBuilder(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); builder.Build(projectOrDirectory, settings); } + /// + /// Adds a project to a solution + /// + /// The context. + /// Solution file to add to. Uses working directory if null. + /// Project file to add. + /// Solution folder name to add the reference to. Created if it doesn't exist. Adds project to solution root if null. + /// The settings. + /// + /// + /// DotNetSlnAdd("MySolution.sln", "MyProject.csproj", null, settings); + /// + /// DotNetSlnAdd("MySolution.sln", "MyProject.csproj", "MySolutionFolder", settings); + /// + /// + /// + /// If a solution file is not specified, dotnet searches the current working directory for a solution file and uses that file. + /// + [CakeMethodAlias] + [CakeAliasCategory("Sln")] + [CakeNamespaceImport("Cake.Common.Tools.DotNet.Sln")] + public static void DotNetSlnAdd(this ICakeContext context, string solution, string project, + string solutionFolder, DotNetSlnSettings settings) + { + if (context is null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (settings is null) + { + settings = new DotNetSlnSettings(); + } + + var executor = new DotNetSln(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + executor.Add(solution, project, solutionFolder, settings); + } + + /// + /// Removes a project to a solution + /// + /// The context. + /// Solution file to remove from. Searches working directory if null. + /// Project file to remove. + /// The settings. + /// + /// + /// DotNetSlnRemove("MySolution.sln", "MyProject.csproj", settings); + /// + /// + /// + /// If a solution file is not specified, dotnet searches the current working directory for a solution file and uses that file. + /// + [CakeMethodAlias] + [CakeAliasCategory("Sln")] + [CakeNamespaceImport("Cake.Common.Tools.DotNet.Sln")] + public static void DotNetSlnRemove(this ICakeContext context, string solution, string project, + DotNetSlnSettings settings) + { + if (context is null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (settings is null) + { + settings = new DotNetSlnSettings(); + } + + var executor = new DotNetSln(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + executor.Remove(solution, project, settings); + } + + /// + /// Lists project references in a solution + /// + /// The context. + /// Solution file to add to. Uses working directory if null. + /// The settings. + /// An array of absolute paths to any references found. These can be .csproj, .fsproj, .dwproj files etc. + /// + /// + /// DotNetSlnList("MySolution.sln", settings); + /// + /// + /// + /// If a solution file is not specified, dotnet searches the current working directory for a solution file and uses that file. + /// + [CakeMethodAlias] + [CakeAliasCategory("Sln")] + [CakeNamespaceImport("Cake.Common.Tools.DotNet.Sln")] + public static string[] DotNetSlnList(this ICakeContext context, string solution, DotNetSlnSettings settings) + { + if (context is null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (settings is null) + { + settings = new DotNetSlnSettings(); + } + + var executor = new DotNetSln(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + return executor.List(solution, settings); + } + + + /// + /// Given a project file, lists project references. + /// + /// The context. + /// The project to inspect for project references. + /// The settings. + /// An array of absolute paths to any project references found. These can be .csproj, .fsproj, .dwproj files etc. + /// + /// + /// DotNetListProjectReferences("MyProject.csproj", settings); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("List")] + [CakeNamespaceImport("Cake.Common.Tools.DotNet.List")] + public static string[] DotNetListProjectReferences(ICakeContext context, string project, DotNetListSettings settings) + { + if (context is null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (settings is null) + { + settings = new DotNetListSettings(); + } + + var executor = new DotNetList(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); + return executor.ListProjectReferences(project, settings); + } + + /// /// Test one or more projects specified by a path or glob pattern using the VS Test host runner. /// @@ -1325,7 +1499,8 @@ public static void DotNetMSBuild(this ICakeContext context, string projectOrDire [CakeMethodAlias] [CakeAliasCategory("Test")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.VSTest")] - public static void DotNetVSTest(this ICakeContext context, GlobPattern testFile) => context.DotNetVSTest(testFile, null); + public static void DotNetVSTest(this ICakeContext context, GlobPattern testFile) => + context.DotNetVSTest(testFile, null); /// /// Test one or more projects specified by a path or glob pattern with settings using the VS Test host runner. @@ -1404,7 +1579,8 @@ public static void DotNetVSTest(this ICakeContext context, GlobPattern testFile, [CakeMethodAlias] [CakeAliasCategory("Test")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.VSTest")] - public static void DotNetVSTest(this ICakeContext context, IEnumerable testFiles, DotNetVSTestSettings settings) + public static void DotNetVSTest(this ICakeContext context, IEnumerable testFiles, + DotNetVSTestSettings settings) { if (context is null) { @@ -1416,7 +1592,8 @@ public static void DotNetVSTest(this ICakeContext context, IEnumerable settings = new DotNetVSTestSettings(); } - var tester = new DotNetCoreVSTester(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var tester = new DotNetCoreVSTester(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); tester.Test(testFiles, settings); } @@ -1519,7 +1696,8 @@ public static void DotNetTool(this ICakeContext context, FilePath projectPath, s [CakeMethodAlias] [CakeAliasCategory("Tool")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.Tool")] - public static void DotNetTool(this ICakeContext context, FilePath projectPath, string command, ProcessArgumentBuilder arguments) + public static void DotNetTool(this ICakeContext context, FilePath projectPath, string command, + ProcessArgumentBuilder arguments) { if (context is null) { @@ -1547,14 +1725,16 @@ public static void DotNetTool(this ICakeContext context, FilePath projectPath, s [CakeMethodAlias] [CakeAliasCategory("Tool")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.Tool")] - public static void DotNetTool(this ICakeContext context, FilePath projectPath, string command, ProcessArgumentBuilder arguments, DotNetToolSettings settings) + 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); + var runner = new DotNetCoreToolRunner(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); runner.Execute(projectPath, command, arguments, settings); } @@ -1594,14 +1774,16 @@ public static void DotNetBuildServerShutdown(this ICakeContext context) [CakeMethodAlias] [CakeAliasCategory("Build Server")] [CakeNamespaceImport("Cake.Common.Tools.DotNet.BuildServer")] - public static void DotNetBuildServerShutdown(this ICakeContext context, DotNetBuildServerShutdownSettings settings) + public static void DotNetBuildServerShutdown(this ICakeContext context, + DotNetBuildServerShutdownSettings settings) { if (context is null) { throw new ArgumentNullException(nameof(context)); } - var buildServer = new DotNetCoreBuildServer(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var buildServer = new DotNetCoreBuildServer(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); buildServer.Shutdown(settings ?? new DotNetBuildServerShutdownSettings()); } @@ -1657,7 +1839,8 @@ public static void DotNetFormat(this ICakeContext context, string root, DotNetFo settings = new DotNetFormatSettings(); } - var formatter = new DotNetFormatter(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var formatter = new DotNetFormatter(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); formatter.Format(root, null, settings); } @@ -1711,7 +1894,8 @@ public static void DotNetFormatWhitespace(this ICakeContext context, string root settings = new DotNetFormatSettings(); } - var formatter = new DotNetFormatter(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var formatter = new DotNetFormatter(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); formatter.Format(root, "whitespace", settings); } @@ -1765,7 +1949,8 @@ public static void DotNetFormatStyle(this ICakeContext context, string root, Dot settings = new DotNetFormatSettings(); } - var formatter = new DotNetFormatter(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var formatter = new DotNetFormatter(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); formatter.Format(root, "style", settings); } @@ -1819,7 +2004,8 @@ public static void DotNetFormatAnalyzers(this ICakeContext context, string root, settings = new DotNetFormatSettings(); } - var formatter = new DotNetFormatter(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var formatter = new DotNetFormatter(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); formatter.Format(root, "analyzers", settings); } @@ -1842,8 +2028,9 @@ public static void DotNetSDKCheck(this ICakeContext context) throw new ArgumentNullException(nameof(context)); } - var checker = new DotNetSDKChecker(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + var checker = new DotNetSDKChecker(context.FileSystem, context.Environment, context.ProcessRunner, + context.Tools); checker.Check(); } } -} +} \ No newline at end of file diff --git a/src/Cake.Common/Tools/DotNet/List/DotNetList.cs b/src/Cake.Common/Tools/DotNet/List/DotNetList.cs new file mode 100644 index 0000000000..e6d5d4d56e --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/List/DotNetList.cs @@ -0,0 +1,86 @@ +// 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; +using System.IO; +using System.Linq; +using Cake.Common.Tools.DotNet.Format; +using Cake.Core; +using Cake.Core.IO; +using Cake.Core.Tooling; + +namespace Cake.Common.Tools.DotNet.List +{ + /// + /// 'dotnet list' commands + /// + public sealed class DotNetList : DotNetTool + { + /// + /// Initializes a new instance of the class. + /// + /// The file system. + /// The environment. + /// The process runner. + /// The tool locator. + public DotNetList( + IFileSystem fileSystem, + ICakeEnvironment environment, + IProcessRunner processRunner, + IToolLocator tools) : base(fileSystem, environment, processRunner, tools) + { + } + + /// + /// Given a project, outputs all project references + /// + /// The target project path. + /// The settings. + public string[] ListProjectReferences(string project, DotNetListSettings settings) + { + if (project == null) + { + throw new ArgumentNullException(nameof(project)); + } + + if (settings == null) + { + throw new ArgumentNullException(nameof(settings)); + } + + var references = Array.Empty(); + Run(settings, GetListProjectReferencesArguments(project, settings), + new ProcessSettings { RedirectStandardOutput = true }, + process => + { + var projDir = new FileInfo(project).DirectoryName; + + references = + process.GetStandardOutput().Skip(2) + .Select(p => + System.IO.Path.GetFullPath(System.IO.Path.Join(projDir, p)).Replace('/', '\\')) + .ToArray(); + }); + + return references; + } + + private ProcessArgumentBuilder GetListProjectReferencesArguments(string project, DotNetListSettings settings) + { + var builder = CreateArgumentBuilder(settings); + + builder.Append("list"); + + // Specific path? + if (project != null) + { + builder.AppendQuoted(project); + } + + builder.Append("reference"); + + return builder; + } + } +} \ No newline at end of file diff --git a/src/Cake.Common/Tools/DotNet/List/DotNetListSettings.cs b/src/Cake.Common/Tools/DotNet/List/DotNetListSettings.cs new file mode 100644 index 0000000000..8803a94a1d --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/List/DotNetListSettings.cs @@ -0,0 +1,13 @@ +// 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. + +namespace Cake.Common.Tools.DotNet.List +{ + /// + /// DotNetListSettings class + /// + public class DotNetListSettings : DotNetSettings + { + } +} diff --git a/src/Cake.Common/Tools/DotNet/Sln/DotNetSln.cs b/src/Cake.Common/Tools/DotNet/Sln/DotNetSln.cs new file mode 100644 index 0000000000..9fb2049cd5 --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/Sln/DotNetSln.cs @@ -0,0 +1,154 @@ +// 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. + +#nullable enable +using System; +using System.IO; +using System.Linq; +using Cake.Core; +using Cake.Core.IO; +using Cake.Core.Tooling; + +namespace Cake.Common.Tools.DotNet.Sln +{ + /// + /// 'dotnet sln' commands + /// + public sealed class DotNetSln : DotNetTool + { + private readonly ICakeEnvironment _environment; + + /// + /// Initializes a new instance of the class. + /// + /// The file system. + /// The environment. + /// The process runner. + /// The tool locator. + public DotNetSln( + IFileSystem fileSystem, + ICakeEnvironment environment, + IProcessRunner processRunner, + IToolLocator tools) : base(fileSystem, environment, processRunner, tools) + { + _environment = environment; + } + + public void Add(string? solution, string project, string? solutionFolder, DotNetSlnSettings settings) + { + if (project == null) + { + throw new ArgumentNullException(nameof(project)); + } + + Run(settings, GetAddArguments(solution, project, solutionFolder, settings)); + } + + private ProcessArgumentBuilder GetAddArguments(string? solution, string project, string? solutionFolder, + DotNetSlnSettings settings) + { + var builder = CreateArgumentBuilder(settings); + + builder.Append("sln"); + + // Specific path? + if (solution != null) + { + builder.AppendQuoted(solution); + } + + builder.Append("add"); + + if (solutionFolder != null) + { + builder.Append("-s"); + builder.AppendQuoted(solutionFolder); + } + else + { + builder.Append("--in-root"); + } + + builder.AppendQuoted(project); + + return builder; + } + + public void Remove(string? solution, string project, DotNetSlnSettings settings) + { + if (project == null) + { + throw new ArgumentNullException(nameof(project)); + } + + Run(settings, GetRemoveArguments(solution, project, settings)); + } + + private ProcessArgumentBuilder GetRemoveArguments(string? solution, string project, DotNetSlnSettings settings) + { + var builder = CreateArgumentBuilder(settings); + + builder.Append("sln"); + + // Specific path? + if (solution != null) + { + builder.AppendQuoted(solution); + } + + builder.Append("remove"); + + builder.AppendQuoted(project); + + return builder; + } + + /// + /// Cleans the project's output using the specified path and settings. + /// + /// The target project path. + /// The settings. + public string[] List(string? solution, DotNetSlnSettings settings) + { + if (settings == null) + { + throw new ArgumentNullException(nameof(settings)); + } + + string[] projects = Array.Empty(); + Run(settings, GetListArguments(solution, settings), new ProcessSettings { RedirectStandardOutput = true }, + process => + { + var slnDir = solution is null + ? _environment.WorkingDirectory + : new FileInfo(solution).DirectoryName; + + projects = + process.GetStandardOutput().Skip(2).Select(p => + System.IO.Path.GetFullPath(System.IO.Path.Join(slnDir.ToString(), p)) + .Replace('/', '\\')) + .ToArray(); + }); + + return projects; + } + + private ProcessArgumentBuilder GetListArguments(string? solution, DotNetSlnSettings settings) + { + var builder = CreateArgumentBuilder(settings); + + builder.Append("sln"); + + // Specific path? + if (solution != null) + { + builder.AppendQuoted(solution); + } + + builder.Append("list"); + + return builder; + } + } +} \ No newline at end of file diff --git a/src/Cake.Common/Tools/DotNet/Sln/DotNetSlnSettings.cs b/src/Cake.Common/Tools/DotNet/Sln/DotNetSlnSettings.cs new file mode 100644 index 0000000000..e91f1974e6 --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/Sln/DotNetSlnSettings.cs @@ -0,0 +1,13 @@ +// 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. + +namespace Cake.Common.Tools.DotNet.Sln +{ + /// + /// DotNetSlnSettings class + /// + public class DotNetSlnSettings : DotNetSettings + { + } +}