From b7c30ba5dadff622cb06ccf66f5c1edd2fc04985 Mon Sep 17 00:00:00 2001 From: Roman Marusyk Date: Mon, 4 Apr 2022 23:30:30 +0300 Subject: [PATCH] Add alias for dotnet workload uninstall command --- .../DotNetWorkloadUninstallerFixture.cs | 20 +++++ .../Uninstall/DotNetWorkloadUninstallTests.cs | 88 +++++++++++++++++++ src/Cake.Common/Tools/DotNet/DotNetAliases.cs | 43 +++++++++ .../DotNetWorkloadUninstallSettings.cs | 13 +++ .../Uninstall/DotNetWorkloadUninstaller.cs | 63 +++++++++++++ 5 files changed, 227 insertions(+) create mode 100644 src/Cake.Common.Tests/Fixtures/Tools/DotNet/Workload/Uninstall/DotNetWorkloadUninstallerFixture.cs create mode 100644 src/Cake.Common.Tests/Unit/Tools/DotNet/Workload/Uninstall/DotNetWorkloadUninstallTests.cs create mode 100644 src/Cake.Common/Tools/DotNet/Workload/Uninstall/DotNetWorkloadUninstallSettings.cs create mode 100644 src/Cake.Common/Tools/DotNet/Workload/Uninstall/DotNetWorkloadUninstaller.cs diff --git a/src/Cake.Common.Tests/Fixtures/Tools/DotNet/Workload/Uninstall/DotNetWorkloadUninstallerFixture.cs b/src/Cake.Common.Tests/Fixtures/Tools/DotNet/Workload/Uninstall/DotNetWorkloadUninstallerFixture.cs new file mode 100644 index 0000000000..745b70853b --- /dev/null +++ b/src/Cake.Common.Tests/Fixtures/Tools/DotNet/Workload/Uninstall/DotNetWorkloadUninstallerFixture.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 System.Collections.Generic; +using Cake.Common.Tools.DotNet.Workload.Uninstall; + +namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Workload.Uninstall +{ + internal sealed class DotNetWorkloadUninstallerFixture : DotNetFixture + { + public IEnumerable WorkloadIds { get; set; } + + protected override void RunTool() + { + var tool = new DotNetWorkloadUninstaller(FileSystem, Environment, ProcessRunner, Tools); + tool.Uninstall(WorkloadIds); + } + } +} diff --git a/src/Cake.Common.Tests/Unit/Tools/DotNet/Workload/Uninstall/DotNetWorkloadUninstallTests.cs b/src/Cake.Common.Tests/Unit/Tools/DotNet/Workload/Uninstall/DotNetWorkloadUninstallTests.cs new file mode 100644 index 0000000000..a1f31ae00d --- /dev/null +++ b/src/Cake.Common.Tests/Unit/Tools/DotNet/Workload/Uninstall/DotNetWorkloadUninstallTests.cs @@ -0,0 +1,88 @@ +// 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.Workload.Uninstall; +using Cake.Testing; +using Xunit; + +namespace Cake.Common.Tests.Unit.Tools.DotNet.Workload.Uninstall +{ + public sealed class DotNetWorkloadUninstallTests + { + public sealed class TheWorkloadSearchMethod + { + [Fact] + public void Should_Throw_If_Process_Was_Not_Started() + { + // Given + var fixture = new DotNetWorkloadUninstallerFixture(); + fixture.WorkloadIds = new string[] { "maui" }; + 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 DotNetWorkloadUninstallerFixture(); + fixture.WorkloadIds = new string[] { "maui" }; + 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_Add_WorkloadIds_Argument() + { + // Given + var fixture = new DotNetWorkloadUninstallerFixture(); + fixture.WorkloadIds = new string[] { "maui-android", "maui-ios" }; + + // When + var result = fixture.Run(); + + // Then + Assert.Equal("workload uninstall maui-android maui-ios", result.Args); + } + + [Fact] + public void Should_Throw_If_WorkloadIds_Is_Empty() + { + // Given + var fixture = new DotNetWorkloadUninstallerFixture(); + fixture.WorkloadIds = new string[] { }; + + // When + var result = Record.Exception(() => fixture.Run()); + + // Then + AssertEx.IsArgumentNullException(result, "workloadIds"); + } + + [Fact] + public void Should_Throw_If_WorkloadIds_Is_Null() + { + // Given + var fixture = new DotNetWorkloadUninstallerFixture(); + fixture.WorkloadIds = null; + + // When + var result = Record.Exception(() => fixture.Run()); + + // Then + AssertEx.IsArgumentNullException(result, "workloadIds"); + } + } + } +} diff --git a/src/Cake.Common/Tools/DotNet/DotNetAliases.cs b/src/Cake.Common/Tools/DotNet/DotNetAliases.cs index 0a84cb7bca..a5540defb2 100644 --- a/src/Cake.Common/Tools/DotNet/DotNetAliases.cs +++ b/src/Cake.Common/Tools/DotNet/DotNetAliases.cs @@ -23,6 +23,7 @@ using Cake.Common.Tools.DotNet.Tool; using Cake.Common.Tools.DotNet.VSTest; using Cake.Common.Tools.DotNet.Workload.Search; +using Cake.Common.Tools.DotNet.Workload.Uninstall; using Cake.Common.Tools.DotNetCore.Build; using Cake.Common.Tools.DotNetCore.BuildServer; using Cake.Common.Tools.DotNetCore.Clean; @@ -1934,5 +1935,47 @@ public static IEnumerable DotNetWorkloadSearch(this ICakeContext var searcher = new DotNetWorkloadSearcher(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); return searcher.Search(searchString, settings); } + + /// + /// Uninstalls a specified workload. + /// + /// The context. + /// The workload ID to uninstall. + /// + /// + /// DotNetWorkloadUninstall("maui"); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Workload")] + [CakeNamespaceImport("Cake.Common.Tools.DotNet.Workload.Uninstall")] + public static void DotNetWorkloadUninstall(this ICakeContext context, string workloadId) + { + context.DotNetWorkloadUninstall(new string[] { workloadId }); + } + + /// + /// Uninstalls one or more workloads. + /// + /// The context. + /// The workload ID or multiple IDs to uninstall. + /// + /// + /// DotNetWorkloadUninstall(new string[] { "maui", "maui-desktop", "maui-mobile" }); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Workload")] + [CakeNamespaceImport("Cake.Common.Tools.DotNet.Workload.Uninstall")] + public static void DotNetWorkloadUninstall(this ICakeContext context, IEnumerable workloadIds) + { + if (context is null) + { + throw new ArgumentNullException(nameof(context)); + } + + var uninstaller = new DotNetWorkloadUninstaller(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools); + uninstaller.Uninstall(workloadIds); + } } } diff --git a/src/Cake.Common/Tools/DotNet/Workload/Uninstall/DotNetWorkloadUninstallSettings.cs b/src/Cake.Common/Tools/DotNet/Workload/Uninstall/DotNetWorkloadUninstallSettings.cs new file mode 100644 index 0000000000..f30a97d7a0 --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/Workload/Uninstall/DotNetWorkloadUninstallSettings.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.Workload.Uninstall +{ + /// + /// Contains settings used by . + /// + public sealed class DotNetWorkloadUninstallSettings : DotNetSettings + { + } +} diff --git a/src/Cake.Common/Tools/DotNet/Workload/Uninstall/DotNetWorkloadUninstaller.cs b/src/Cake.Common/Tools/DotNet/Workload/Uninstall/DotNetWorkloadUninstaller.cs new file mode 100644 index 0000000000..5fe7c0d573 --- /dev/null +++ b/src/Cake.Common/Tools/DotNet/Workload/Uninstall/DotNetWorkloadUninstaller.cs @@ -0,0 +1,63 @@ +// 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 System.Linq; +using Cake.Core; +using Cake.Core.IO; +using Cake.Core.Tooling; + +namespace Cake.Common.Tools.DotNet.Workload.Uninstall +{ + /// + /// .NET workloads uninstaller. + /// + public sealed class DotNetWorkloadUninstaller : DotNetTool + { + /// + /// Initializes a new instance of the class. + /// + /// The file system. + /// The environment. + /// The process runner. + /// The tool locator. + public DotNetWorkloadUninstaller( + IFileSystem fileSystem, + ICakeEnvironment environment, + IProcessRunner processRunner, + IToolLocator tools) : base(fileSystem, environment, processRunner, tools) + { + } + + /// + /// Uninstalls one or more workloads. + /// + /// The workload ID or multiple IDs to uninstall. + public void Uninstall(IEnumerable workloadIds) + { + if (workloadIds == null || !workloadIds.Any()) + { + throw new ArgumentNullException(nameof(workloadIds)); + } + + var settings = new DotNetWorkloadUninstallSettings(); + RunCommand(settings, GetArguments(workloadIds, settings)); + } + + private ProcessArgumentBuilder GetArguments(IEnumerable workloadIds, DotNetWorkloadUninstallSettings settings) + { + var builder = CreateArgumentBuilder(settings); + + builder.Append("workload uninstall"); + + if (workloadIds != null && workloadIds.Any()) + { + builder.Append(string.Join(" ", workloadIds)); + } + + return builder; + } + } +}