From 44b794c34714466a31b15c4af011347ad0b5e5f0 Mon Sep 17 00:00:00 2001 From: Mattias Karlsson Date: Thu, 8 Oct 2026 14:57:26 +0200 Subject: [PATCH] (GH-5112) Add DotNetNuGetLocals aliases - fixes #5112 --- ...erTests.Should_Parse_List_All.verified.txt | 6 + ...ts.Should_Parse_Single_Folder.verified.txt | 6 + ...esult_For_Null_Or_Empty_Input.verified.txt | 14 ++ .../Locals/DotNetNuGetLocalserFixture.cs | 28 ++++ .../Locals/DotNetNuGetLocalsParserTests.cs | 59 +++++++ .../NuGet/Locals/DotNetNuGetLocalserTests.cs | 158 ++++++++++++++++++ .../Tools/DotNet/DotNetAliases.NuGet.cs | 99 +++++++++++ .../NuGet/Locals/DotNetNuGetLocalsFolder.cs | 36 ++++ .../Locals/DotNetNuGetLocalsListResult.cs | 20 +++ .../NuGet/Locals/DotNetNuGetLocalsParser.cs | 69 ++++++++ .../NuGet/Locals/DotNetNuGetLocalsSettings.cs | 16 ++ .../NuGet/Locals/DotNetNuGetLocalser.cs | 101 +++++++++++ .../Tools/DotNet/DotNetAliases.cake | 23 +++ 13 files changed, 635 insertions(+) create mode 100644 src/Cake.Common.Tests/Expectations/DotNetNuGetLocalsParserTests.Should_Parse_List_All.verified.txt create mode 100644 src/Cake.Common.Tests/Expectations/DotNetNuGetLocalsParserTests.Should_Parse_Single_Folder.verified.txt create mode 100644 src/Cake.Common.Tests/Expectations/DotNetNuGetLocalsParserTests.Should_Return_Empty_Result_For_Null_Or_Empty_Input.verified.txt create mode 100644 src/Cake.Common.Tests/Fixtures/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalserFixture.cs create mode 100644 src/Cake.Common.Tests/Unit/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsParserTests.cs create mode 100644 src/Cake.Common.Tests/Unit/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalserTests.cs create mode 100644 src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsFolder.cs create mode 100644 src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsListResult.cs create mode 100644 src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsParser.cs create mode 100644 src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsSettings.cs create mode 100644 src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalser.cs diff --git a/src/Cake.Common.Tests/Expectations/DotNetNuGetLocalsParserTests.Should_Parse_List_All.verified.txt b/src/Cake.Common.Tests/Expectations/DotNetNuGetLocalsParserTests.Should_Parse_List_All.verified.txt new file mode 100644 index 0000000000..caba572bb1 --- /dev/null +++ b/src/Cake.Common.Tests/Expectations/DotNetNuGetLocalsParserTests.Should_Parse_List_All.verified.txt @@ -0,0 +1,6 @@ +{ + HttpCache: C:/Users/username/AppData/Local/NuGet/v3-cache, + GlobalPackages: C:/Users/username/.nuget/packages, + Temp: C:/Users/username/AppData/Local/Temp/NuGetScratch, + PluginsCache: C:/Users/username/AppData/Local/NuGet/plugins-cache +} \ No newline at end of file diff --git a/src/Cake.Common.Tests/Expectations/DotNetNuGetLocalsParserTests.Should_Parse_Single_Folder.verified.txt b/src/Cake.Common.Tests/Expectations/DotNetNuGetLocalsParserTests.Should_Parse_Single_Folder.verified.txt new file mode 100644 index 0000000000..d0695667bb --- /dev/null +++ b/src/Cake.Common.Tests/Expectations/DotNetNuGetLocalsParserTests.Should_Parse_Single_Folder.verified.txt @@ -0,0 +1,6 @@ +{ + HttpCache: C:/Users/username/AppData/Local/NuGet/v3-cache, + GlobalPackages: null, + Temp: null, + PluginsCache: null +} \ No newline at end of file diff --git a/src/Cake.Common.Tests/Expectations/DotNetNuGetLocalsParserTests.Should_Return_Empty_Result_For_Null_Or_Empty_Input.verified.txt b/src/Cake.Common.Tests/Expectations/DotNetNuGetLocalsParserTests.Should_Return_Empty_Result_For_Null_Or_Empty_Input.verified.txt new file mode 100644 index 0000000000..db65d3cdb7 --- /dev/null +++ b/src/Cake.Common.Tests/Expectations/DotNetNuGetLocalsParserTests.Should_Return_Empty_Result_For_Null_Or_Empty_Input.verified.txt @@ -0,0 +1,14 @@ +{ + Null: { + HttpCache: null, + GlobalPackages: null, + Temp: null, + PluginsCache: null + }, + Empty: { + HttpCache: null, + GlobalPackages: null, + Temp: null, + PluginsCache: null + } +} \ No newline at end of file diff --git a/src/Cake.Common.Tests/Fixtures/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalserFixture.cs b/src/Cake.Common.Tests/Fixtures/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalserFixture.cs new file mode 100644 index 0000000000..d92b6f3034 --- /dev/null +++ b/src/Cake.Common.Tests/Fixtures/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalserFixture.cs @@ -0,0 +1,28 @@ +// 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.DotNet.NuGet.Locals; + +namespace Cake.Common.Tests.Fixtures.Tools.DotNet.NuGet.Locals; + +internal sealed class DotNetNuGetLocalserFixture : DotNetFixture +{ + public DotNetNuGetLocalsFolder Folder { get; set; } = DotNetNuGetLocalsFolder.All; + + public bool Clear { get; set; } + + public DotNetNuGetLocalsListResult Output { get; private set; } + + protected override void RunTool() + { + var tool = new DotNetNuGetLocalser(FileSystem, Environment, ProcessRunner, Tools); + if (Clear) + { + tool.Clear(Folder, Settings); + return; + } + + Output = tool.List(Folder, Settings); + } +} diff --git a/src/Cake.Common.Tests/Unit/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsParserTests.cs b/src/Cake.Common.Tests/Unit/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsParserTests.cs new file mode 100644 index 0000000000..3f22a6e205 --- /dev/null +++ b/src/Cake.Common.Tests/Unit/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsParserTests.cs @@ -0,0 +1,59 @@ +// 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.DotNet.NuGet.Locals; + +namespace Cake.Common.Tests.Unit.Tools.DotNet.NuGet.Locals; + +public sealed class DotNetNuGetLocalsParserTests +{ + [Fact] + public async Task Should_Parse_List_All() + { + // Given + var lines = new[] + { + "http-cache: C:\\Users\\username\\AppData\\Local\\NuGet\\v3-cache", + "global-packages: C:\\Users\\username\\.nuget\\packages\\", + "temp: C:\\Users\\username\\AppData\\Local\\Temp\\NuGetScratch", + "plugins-cache: C:\\Users\\username\\AppData\\Local\\NuGet\\plugins-cache" + }; + + // When + var result = DotNetNuGetLocalsParser.Parse(lines); + + // Then + await Verify(result); + } + + [Fact] + public async Task Should_Parse_Single_Folder() + { + // Given + var lines = new[] + { + "http-cache: C:\\Users\\username\\AppData\\Local\\NuGet\\v3-cache" + }; + + // When + var result = DotNetNuGetLocalsParser.Parse(lines); + + // Then + await Verify(result); + } + + [Fact] + public async Task Should_Return_Empty_Result_For_Null_Or_Empty_Input() + { + // Given + IEnumerable empty = []; + + // When + var nullResult = DotNetNuGetLocalsParser.Parse(null); + var emptyResult = DotNetNuGetLocalsParser.Parse(empty); + + // Then + await Verify(new { Null = nullResult, Empty = emptyResult }); + } +} diff --git a/src/Cake.Common.Tests/Unit/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalserTests.cs b/src/Cake.Common.Tests/Unit/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalserTests.cs new file mode 100644 index 0000000000..68b22ecae9 --- /dev/null +++ b/src/Cake.Common.Tests/Unit/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalserTests.cs @@ -0,0 +1,158 @@ +// 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.Tests.Fixtures.Tools.DotNet.NuGet.Locals; +using Cake.Common.Tools.DotNet.NuGet.Locals; +using Cake.Testing; + +namespace Cake.Common.Tests.Unit.Tools.DotNet.NuGet.Locals; + +public sealed class DotNetNuGetLocalserTests +{ + public sealed class TheListMethod + { + [Fact] + public void Should_Throw_If_Process_Was_Not_Started() + { + // Given + var fixture = new DotNetNuGetLocalserFixture(); + fixture.GivenProcessCannotStart(); + + // When + var result = Record.Exception(() => fixture.Run()); + + // Then + AssertEx.IsCakeException(result, ".NET CLI: Process was not started."); + } + + [Fact] + public void Should_Throw_If_Process_Has_A_Non_Zero_Exit_Code() + { + // Given + var fixture = new DotNetNuGetLocalserFixture(); + fixture.GivenProcessExitsWithCode(1); + + // When + var result = Record.Exception(() => fixture.Run()); + + // Then + AssertEx.IsCakeException(result, ".NET CLI: Process returned an error (exit code 1)."); + } + + [Fact] + public void Should_Throw_If_Settings_Are_Null() + { + // Given + var fixture = new DotNetNuGetLocalserFixture(); + fixture.Settings = null; + fixture.GivenDefaultToolDoNotExist(); + + // When + var result = Record.Exception(() => fixture.Run()); + + // Then + AssertEx.IsArgumentNullException(result, "settings"); + } + + [Fact] + public void Should_Throw_If_Folder_Is_Invalid() + { + // Given + var fixture = new DotNetNuGetLocalserFixture(); + fixture.Folder = (DotNetNuGetLocalsFolder)42; + + // When + var result = Record.Exception(() => fixture.Run()); + + // Then + AssertEx.IsArgumentOutOfRangeException(result, "folder"); + } + + [Theory] + [InlineData(DotNetNuGetLocalsFolder.All, "nuget locals all --list")] + [InlineData(DotNetNuGetLocalsFolder.HttpCache, "nuget locals http-cache --list")] + [InlineData(DotNetNuGetLocalsFolder.GlobalPackages, "nuget locals global-packages --list")] + [InlineData(DotNetNuGetLocalsFolder.Temp, "nuget locals temp --list")] + [InlineData(DotNetNuGetLocalsFolder.PluginsCache, "nuget locals plugins-cache --list")] + public void Should_Add_Folder_And_List_Arguments(DotNetNuGetLocalsFolder folder, string expected) + { + // Given + var fixture = new DotNetNuGetLocalserFixture { Folder = folder }; + + // When + var result = fixture.Run(); + + // Then + Assert.Equal(expected, result.Args); + } + + [Fact] + public void Should_Add_Force_English_Output() + { + // Given + var fixture = new DotNetNuGetLocalserFixture(); + fixture.Settings.ForceEnglishOutput = true; + + // When + var result = fixture.Run(); + + // Then + Assert.Equal("nuget locals all --list --force-english-output", result.Args); + } + } + + public sealed class TheClearMethod + { + [Fact] + public void Should_Throw_If_Settings_Are_Null() + { + // Given + var fixture = new DotNetNuGetLocalserFixture { Clear = true }; + fixture.Settings = null; + fixture.GivenDefaultToolDoNotExist(); + + // When + var result = Record.Exception(() => fixture.Run()); + + // Then + AssertEx.IsArgumentNullException(result, "settings"); + } + + [Theory] + [InlineData(DotNetNuGetLocalsFolder.All, "nuget locals all --clear")] + [InlineData(DotNetNuGetLocalsFolder.HttpCache, "nuget locals http-cache --clear")] + [InlineData(DotNetNuGetLocalsFolder.GlobalPackages, "nuget locals global-packages --clear")] + [InlineData(DotNetNuGetLocalsFolder.Temp, "nuget locals temp --clear")] + [InlineData(DotNetNuGetLocalsFolder.PluginsCache, "nuget locals plugins-cache --clear")] + public void Should_Add_Folder_And_Clear_Arguments(DotNetNuGetLocalsFolder folder, string expected) + { + // Given + var fixture = new DotNetNuGetLocalserFixture + { + Folder = folder, + Clear = true + }; + + // When + var result = fixture.Run(); + + // Then + Assert.Equal(expected, result.Args); + } + + [Fact] + public void Should_Add_Force_English_Output() + { + // Given + var fixture = new DotNetNuGetLocalserFixture { Clear = true }; + fixture.Settings.ForceEnglishOutput = true; + + // When + var result = fixture.Run(); + + // Then + Assert.Equal("nuget locals all --clear --force-english-output", result.Args); + } + } +} diff --git a/src/Cake.Common/Tools/DotNet/DotNetAliases.NuGet.cs b/src/Cake.Common/Tools/DotNet/DotNetAliases.NuGet.cs index f673a31759..72b17f2cb1 100644 --- a/src/Cake.Common/Tools/DotNet/DotNetAliases.NuGet.cs +++ b/src/Cake.Common/Tools/DotNet/DotNetAliases.NuGet.cs @@ -4,6 +4,7 @@ using System; using Cake.Common.Tools.DotNet.NuGet.Delete; +using Cake.Common.Tools.DotNet.NuGet.Locals; using Cake.Common.Tools.DotNet.NuGet.Push; using Cake.Common.Tools.DotNet.NuGet.Source; using Cake.Common.Tools.DotNet.NuGet.Why; @@ -576,4 +577,102 @@ public static DotNetNuGetWhyResult DotNetNuGetWhy(this ICakeContext context, str var whyer = new DotNetNuGetWhyer(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); return whyer.Why(project, package, settings); } + + /// + /// Lists local NuGet cache folders. + /// + /// The context. + /// The local folder to list. + /// The parsed local folder paths. + /// + /// + /// var result = DotNetNuGetLocalsList(DotNetNuGetLocalsFolder.All); + /// Information(result.GlobalPackages); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("NuGet")] + [CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Locals")] + public static DotNetNuGetLocalsListResult DotNetNuGetLocalsList(this ICakeContext context, DotNetNuGetLocalsFolder folder) + { + return context.DotNetNuGetLocalsList(folder, null); + } + + /// + /// Lists local NuGet cache folders. + /// + /// The context. + /// The local folder to list. + /// The settings. + /// The parsed local folder paths. + /// + /// + /// var settings = new DotNetNuGetLocalsSettings + /// { + /// ForceEnglishOutput = true + /// }; + /// + /// var result = DotNetNuGetLocalsList(DotNetNuGetLocalsFolder.HttpCache, settings); + /// Information(result.HttpCache); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("NuGet")] + [CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Locals")] + public static DotNetNuGetLocalsListResult DotNetNuGetLocalsList(this ICakeContext context, DotNetNuGetLocalsFolder folder, DotNetNuGetLocalsSettings settings) + { + ArgumentNullException.ThrowIfNull(context); + + settings ??= new DotNetNuGetLocalsSettings(); + + var localser = new DotNetNuGetLocalser(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + return localser.List(folder, settings); + } + + /// + /// Clears local NuGet cache folders. + /// + /// The context. + /// The local folder to clear. + /// + /// + /// DotNetNuGetLocalsClear(DotNetNuGetLocalsFolder.Temp); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("NuGet")] + [CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Locals")] + public static void DotNetNuGetLocalsClear(this ICakeContext context, DotNetNuGetLocalsFolder folder) + { + context.DotNetNuGetLocalsClear(folder, null); + } + + /// + /// Clears local NuGet cache folders. + /// + /// The context. + /// The local folder to clear. + /// The settings. + /// + /// + /// var settings = new DotNetNuGetLocalsSettings + /// { + /// ForceEnglishOutput = true + /// }; + /// + /// DotNetNuGetLocalsClear(DotNetNuGetLocalsFolder.Temp, settings); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("NuGet")] + [CakeNamespaceImport("Cake.Common.Tools.DotNet.NuGet.Locals")] + public static void DotNetNuGetLocalsClear(this ICakeContext context, DotNetNuGetLocalsFolder folder, DotNetNuGetLocalsSettings settings) + { + ArgumentNullException.ThrowIfNull(context); + + settings ??= new DotNetNuGetLocalsSettings(); + + var localser = new DotNetNuGetLocalser(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + localser.Clear(folder, settings); + } } diff --git a/src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsFolder.cs b/src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsFolder.cs new file mode 100644 index 0000000000..f5a813aaaf --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsFolder.cs @@ -0,0 +1,36 @@ +// 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.NuGet.Locals; + +/// +/// The NuGet local folder used by dotnet nuget locals. +/// +public enum DotNetNuGetLocalsFolder +{ + /// + /// All local NuGet folders. + /// + All, + + /// + /// The HTTP request cache. + /// + HttpCache, + + /// + /// The global packages folder. + /// + GlobalPackages, + + /// + /// The temporary cache. + /// + Temp, + + /// + /// The plugins cache. + /// + PluginsCache +} diff --git a/src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsListResult.cs b/src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsListResult.cs new file mode 100644 index 0000000000..629f47e654 --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsListResult.cs @@ -0,0 +1,20 @@ +// 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.Core.IO; + +namespace Cake.Common.Tools.DotNet.NuGet.Locals; + +/// +/// The parsed result of dotnet nuget locals --list. +/// +/// The HTTP cache directory, if listed. +/// The global packages directory, if listed. +/// The temporary cache directory, if listed. +/// The plugins cache directory, if listed. +public sealed record DotNetNuGetLocalsListResult( + DirectoryPath HttpCache, + DirectoryPath GlobalPackages, + DirectoryPath Temp, + DirectoryPath PluginsCache); diff --git a/src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsParser.cs b/src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsParser.cs new file mode 100644 index 0000000000..71e139d420 --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsParser.cs @@ -0,0 +1,69 @@ +// 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 System.Text.RegularExpressions; +using Cake.Core.IO; + +namespace Cake.Common.Tools.DotNet.NuGet.Locals; + +/// +/// Parses English dotnet nuget locals --list output. +/// +public static partial class DotNetNuGetLocalsParser +{ + [GeneratedRegex(@"^(?http-cache|global-packages|temp|plugins-cache):\s*(?.+)$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)] + private static partial Regex LocalsLine(); + + /// + /// Parses redirected standard output from dotnet nuget locals --list. + /// + /// The output lines. + /// The parsed local folder paths. + public static DotNetNuGetLocalsListResult Parse(IEnumerable lines) + { + DirectoryPath httpCache = null; + DirectoryPath globalPackages = null; + DirectoryPath temp = null; + DirectoryPath pluginsCache = null; + + if (lines == null) + { + return new DotNetNuGetLocalsListResult(httpCache, globalPackages, temp, pluginsCache); + } + + foreach (var raw in lines) + { + if (string.IsNullOrWhiteSpace(raw)) + { + continue; + } + + var match = LocalsLine().Match(raw.Trim()); + if (!match.Success) + { + continue; + } + + var path = new DirectoryPath(match.Groups["path"].Value.Trim()); + switch (match.Groups["key"].Value.ToLowerInvariant()) + { + case "http-cache": + httpCache = path; + break; + case "global-packages": + globalPackages = path; + break; + case "temp": + temp = path; + break; + case "plugins-cache": + pluginsCache = path; + break; + } + } + + return new DotNetNuGetLocalsListResult(httpCache, globalPackages, temp, pluginsCache); + } +} diff --git a/src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsSettings.cs b/src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsSettings.cs new file mode 100644 index 0000000000..c5b4a945ae --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalsSettings.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. + +namespace Cake.Common.Tools.DotNet.NuGet.Locals; + +/// +/// Contains settings used by . +/// +public sealed class DotNetNuGetLocalsSettings : DotNetSettings +{ + /// + /// Gets or sets a value indicating whether to force command-line output in English. + /// + public bool ForceEnglishOutput { get; set; } +} diff --git a/src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalser.cs b/src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalser.cs new file mode 100644 index 0000000000..948df7a9db --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/NuGet/Locals/DotNetNuGetLocalser.cs @@ -0,0 +1,101 @@ +// 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.Collections.Generic; +using Cake.Core; +using Cake.Core.IO; +using Cake.Core.Tooling; + +namespace Cake.Common.Tools.DotNet.NuGet.Locals; + +/// +/// .NET NuGet locals runner. Lists or clears local NuGet cache folders. +/// +public sealed class DotNetNuGetLocalser : DotNetTool +{ + /// + /// Initializes a new instance of the class. + /// + /// The file system. + /// The environment. + /// The process runner. + /// The tool locator. + public DotNetNuGetLocalser( + IFileSystem fileSystem, + ICakeEnvironment environment, + IProcessRunner processRunner, + IToolLocator tools) : base(fileSystem, environment, processRunner, tools) + { + } + + /// + /// Lists local NuGet cache folders. + /// + /// The local folder to list. + /// The settings. + /// The parsed local folder paths. + public DotNetNuGetLocalsListResult List(DotNetNuGetLocalsFolder folder, DotNetNuGetLocalsSettings settings) + { + ArgumentNullException.ThrowIfNull(settings); + + var processSettings = new ProcessSettings + { + RedirectStandardOutput = true, + EnvironmentVariables = new Dictionary(settings.EnvironmentVariables) + { + { "DOTNET_CLI_UI_LANGUAGE", "en" }, + { "NO_COLOR", "1" } + } + }; + + IEnumerable result = null; + RunCommand(settings, GetArguments(folder, list: true, settings), processSettings, + process => result = process.GetStandardOutput()); + + return DotNetNuGetLocalsParser.Parse(result); + } + + /// + /// Clears local NuGet cache folders. + /// + /// The local folder to clear. + /// The settings. + public void Clear(DotNetNuGetLocalsFolder folder, DotNetNuGetLocalsSettings settings) + { + ArgumentNullException.ThrowIfNull(settings); + + RunCommand(settings, GetArguments(folder, list: false, settings)); + } + + private ProcessArgumentBuilder GetArguments(DotNetNuGetLocalsFolder folder, bool list, DotNetNuGetLocalsSettings settings) + { + var builder = CreateArgumentBuilder(settings); + + builder.Append("nuget"); + builder.Append("locals"); + builder.Append(GetFolderArgument(folder)); + builder.Append(list ? "--list" : "--clear"); + + if (settings.ForceEnglishOutput) + { + builder.Append("--force-english-output"); + } + + return builder; + } + + private static string GetFolderArgument(DotNetNuGetLocalsFolder folder) + { + return folder switch + { + DotNetNuGetLocalsFolder.All => "all", + DotNetNuGetLocalsFolder.HttpCache => "http-cache", + DotNetNuGetLocalsFolder.GlobalPackages => "global-packages", + DotNetNuGetLocalsFolder.Temp => "temp", + DotNetNuGetLocalsFolder.PluginsCache => "plugins-cache", + _ => throw new ArgumentOutOfRangeException(nameof(folder), folder, "Invalid value") + }; + } +} diff --git a/tests/integration/Cake.Common/Tools/DotNet/DotNetAliases.cake b/tests/integration/Cake.Common/Tools/DotNet/DotNetAliases.cake index cf02d759be..8625e7a2a5 100644 --- a/tests/integration/Cake.Common/Tools/DotNet/DotNetAliases.cake +++ b/tests/integration/Cake.Common/Tools/DotNet/DotNetAliases.cake @@ -448,6 +448,28 @@ Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetNuGetWhy") } }); +Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetNuGetLocalsList") + .IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.Setup") + .Does(() => +{ + // When + var result = DotNetNuGetLocalsList(DotNetNuGetLocalsFolder.All); + + // Then + Assert.False(string.IsNullOrWhiteSpace(result.HttpCache?.FullPath), nameof(result.HttpCache)); + Assert.False(string.IsNullOrWhiteSpace(result.GlobalPackages?.FullPath), nameof(result.GlobalPackages)); + Assert.False(string.IsNullOrWhiteSpace(result.Temp?.FullPath), nameof(result.Temp)); + Assert.False(string.IsNullOrWhiteSpace(result.PluginsCache?.FullPath), nameof(result.PluginsCache)); + Assert.True(DirectoryExists(result.GlobalPackages), result.GlobalPackages.FullPath); +}); + +Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetNuGetLocalsClear") + .Does(() => +{ + // When + DotNetNuGetLocalsClear(DotNetNuGetLocalsFolder.Temp); +}); + Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetProjectConvert") .IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.Setup") .Does(() => @@ -888,6 +910,7 @@ Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetBuildServerShutdown") .IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetSlnList") .IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetSlnMigrate") .IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetNuGetWhy") + .IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetNuGetLocalsList") .IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetProjectConvert") .IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetProjectConvert.DryRun") .IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetSlnRemove")