Skip to content

Commit b7c30ba

Browse files
Marusykdevlead
authored andcommitted
Add alias for dotnet workload uninstall command
1 parent dc13a2c commit b7c30ba

5 files changed

Lines changed: 227 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Generic;
6+
using Cake.Common.Tools.DotNet.Workload.Uninstall;
7+
8+
namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Workload.Uninstall
9+
{
10+
internal sealed class DotNetWorkloadUninstallerFixture : DotNetFixture<DotNetWorkloadUninstallSettings>
11+
{
12+
public IEnumerable<string> WorkloadIds { get; set; }
13+
14+
protected override void RunTool()
15+
{
16+
var tool = new DotNetWorkloadUninstaller(FileSystem, Environment, ProcessRunner, Tools);
17+
tool.Uninstall(WorkloadIds);
18+
}
19+
}
20+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Cake.Common.Tests.Fixtures.Tools.DotNet.Workload.Uninstall;
6+
using Cake.Testing;
7+
using Xunit;
8+
9+
namespace Cake.Common.Tests.Unit.Tools.DotNet.Workload.Uninstall
10+
{
11+
public sealed class DotNetWorkloadUninstallTests
12+
{
13+
public sealed class TheWorkloadSearchMethod
14+
{
15+
[Fact]
16+
public void Should_Throw_If_Process_Was_Not_Started()
17+
{
18+
// Given
19+
var fixture = new DotNetWorkloadUninstallerFixture();
20+
fixture.WorkloadIds = new string[] { "maui" };
21+
fixture.GivenProcessCannotStart();
22+
23+
// When
24+
var result = Record.Exception(() => fixture.Run());
25+
26+
// Then
27+
AssertEx.IsCakeException(result, ".NET CLI: Process was not started.");
28+
}
29+
30+
[Fact]
31+
public void Should_Throw_If_Process_Has_A_Non_Zero_Exit_Code()
32+
{
33+
// Given
34+
var fixture = new DotNetWorkloadUninstallerFixture();
35+
fixture.WorkloadIds = new string[] { "maui" };
36+
fixture.GivenProcessExitsWithCode(1);
37+
38+
// When
39+
var result = Record.Exception(() => fixture.Run());
40+
41+
// Then
42+
AssertEx.IsCakeException(result, ".NET CLI: Process returned an error (exit code 1).");
43+
}
44+
45+
[Fact]
46+
public void Should_Add_WorkloadIds_Argument()
47+
{
48+
// Given
49+
var fixture = new DotNetWorkloadUninstallerFixture();
50+
fixture.WorkloadIds = new string[] { "maui-android", "maui-ios" };
51+
52+
// When
53+
var result = fixture.Run();
54+
55+
// Then
56+
Assert.Equal("workload uninstall maui-android maui-ios", result.Args);
57+
}
58+
59+
[Fact]
60+
public void Should_Throw_If_WorkloadIds_Is_Empty()
61+
{
62+
// Given
63+
var fixture = new DotNetWorkloadUninstallerFixture();
64+
fixture.WorkloadIds = new string[] { };
65+
66+
// When
67+
var result = Record.Exception(() => fixture.Run());
68+
69+
// Then
70+
AssertEx.IsArgumentNullException(result, "workloadIds");
71+
}
72+
73+
[Fact]
74+
public void Should_Throw_If_WorkloadIds_Is_Null()
75+
{
76+
// Given
77+
var fixture = new DotNetWorkloadUninstallerFixture();
78+
fixture.WorkloadIds = null;
79+
80+
// When
81+
var result = Record.Exception(() => fixture.Run());
82+
83+
// Then
84+
AssertEx.IsArgumentNullException(result, "workloadIds");
85+
}
86+
}
87+
}
88+
}

src/Cake.Common/Tools/DotNet/DotNetAliases.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using Cake.Common.Tools.DotNet.Tool;
2424
using Cake.Common.Tools.DotNet.VSTest;
2525
using Cake.Common.Tools.DotNet.Workload.Search;
26+
using Cake.Common.Tools.DotNet.Workload.Uninstall;
2627
using Cake.Common.Tools.DotNetCore.Build;
2728
using Cake.Common.Tools.DotNetCore.BuildServer;
2829
using Cake.Common.Tools.DotNetCore.Clean;
@@ -1934,5 +1935,47 @@ public static IEnumerable<DotNetWorkload> DotNetWorkloadSearch(this ICakeContext
19341935
var searcher = new DotNetWorkloadSearcher(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
19351936
return searcher.Search(searchString, settings);
19361937
}
1938+
1939+
/// <summary>
1940+
/// Uninstalls a specified workload.
1941+
/// </summary>
1942+
/// <param name="context">The context.</param>
1943+
/// <param name="workloadId">The workload ID to uninstall.</param>
1944+
/// <example>
1945+
/// <code>
1946+
/// DotNetWorkloadUninstall("maui");
1947+
/// </code>
1948+
/// </example>
1949+
[CakeMethodAlias]
1950+
[CakeAliasCategory("Workload")]
1951+
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Workload.Uninstall")]
1952+
public static void DotNetWorkloadUninstall(this ICakeContext context, string workloadId)
1953+
{
1954+
context.DotNetWorkloadUninstall(new string[] { workloadId });
1955+
}
1956+
1957+
/// <summary>
1958+
/// Uninstalls one or more workloads.
1959+
/// </summary>
1960+
/// <param name="context">The context.</param>
1961+
/// <param name="workloadIds">The workload ID or multiple IDs to uninstall.</param>
1962+
/// <example>
1963+
/// <code>
1964+
/// DotNetWorkloadUninstall(new string[] { "maui", "maui-desktop", "maui-mobile" });
1965+
/// </code>
1966+
/// </example>
1967+
[CakeMethodAlias]
1968+
[CakeAliasCategory("Workload")]
1969+
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Workload.Uninstall")]
1970+
public static void DotNetWorkloadUninstall(this ICakeContext context, IEnumerable<string> workloadIds)
1971+
{
1972+
if (context is null)
1973+
{
1974+
throw new ArgumentNullException(nameof(context));
1975+
}
1976+
1977+
var uninstaller = new DotNetWorkloadUninstaller(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
1978+
uninstaller.Uninstall(workloadIds);
1979+
}
19371980
}
19381981
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace Cake.Common.Tools.DotNet.Workload.Uninstall
6+
{
7+
/// <summary>
8+
/// Contains settings used by <see cref="DotNetWorkloadUninstaller" />.
9+
/// </summary>
10+
public sealed class DotNetWorkloadUninstallSettings : DotNetSettings
11+
{
12+
}
13+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using Cake.Core;
9+
using Cake.Core.IO;
10+
using Cake.Core.Tooling;
11+
12+
namespace Cake.Common.Tools.DotNet.Workload.Uninstall
13+
{
14+
/// <summary>
15+
/// .NET workloads uninstaller.
16+
/// </summary>
17+
public sealed class DotNetWorkloadUninstaller : DotNetTool<DotNetWorkloadUninstallSettings>
18+
{
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="DotNetWorkloadUninstaller" /> class.
21+
/// </summary>
22+
/// <param name="fileSystem">The file system.</param>
23+
/// <param name="environment">The environment.</param>
24+
/// <param name="processRunner">The process runner.</param>
25+
/// <param name="tools">The tool locator.</param>
26+
public DotNetWorkloadUninstaller(
27+
IFileSystem fileSystem,
28+
ICakeEnvironment environment,
29+
IProcessRunner processRunner,
30+
IToolLocator tools) : base(fileSystem, environment, processRunner, tools)
31+
{
32+
}
33+
34+
/// <summary>
35+
/// Uninstalls one or more workloads.
36+
/// </summary>
37+
/// <param name="workloadIds">The workload ID or multiple IDs to uninstall.</param>
38+
public void Uninstall(IEnumerable<string> workloadIds)
39+
{
40+
if (workloadIds == null || !workloadIds.Any())
41+
{
42+
throw new ArgumentNullException(nameof(workloadIds));
43+
}
44+
45+
var settings = new DotNetWorkloadUninstallSettings();
46+
RunCommand(settings, GetArguments(workloadIds, settings));
47+
}
48+
49+
private ProcessArgumentBuilder GetArguments(IEnumerable<string> workloadIds, DotNetWorkloadUninstallSettings settings)
50+
{
51+
var builder = CreateArgumentBuilder(settings);
52+
53+
builder.Append("workload uninstall");
54+
55+
if (workloadIds != null && workloadIds.Any())
56+
{
57+
builder.Append(string.Join(" ", workloadIds));
58+
}
59+
60+
return builder;
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)