Skip to content

Commit

Permalink
Merge pull request #3860 from Marusyk/rmarusyk/3486
Browse files Browse the repository at this point in the history
Add alias for dotnet workload uninstall command
  • Loading branch information
devlead authored Aug 23, 2022
2 parents dc13a2c + b7c30ba commit 255feb4
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<DotNetWorkloadUninstallSettings>
{
public IEnumerable<string> WorkloadIds { get; set; }

protected override void RunTool()
{
var tool = new DotNetWorkloadUninstaller(FileSystem, Environment, ProcessRunner, Tools);
tool.Uninstall(WorkloadIds);
}
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
}
43 changes: 43 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1934,5 +1935,47 @@ public static IEnumerable<DotNetWorkload> DotNetWorkloadSearch(this ICakeContext
var searcher = new DotNetWorkloadSearcher(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
return searcher.Search(searchString, settings);
}

/// <summary>
/// Uninstalls a specified workload.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="workloadId">The workload ID to uninstall.</param>
/// <example>
/// <code>
/// DotNetWorkloadUninstall("maui");
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Workload")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Workload.Uninstall")]
public static void DotNetWorkloadUninstall(this ICakeContext context, string workloadId)
{
context.DotNetWorkloadUninstall(new string[] { workloadId });
}

/// <summary>
/// Uninstalls one or more workloads.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="workloadIds">The workload ID or multiple IDs to uninstall.</param>
/// <example>
/// <code>
/// DotNetWorkloadUninstall(new string[] { "maui", "maui-desktop", "maui-mobile" });
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Workload")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Workload.Uninstall")]
public static void DotNetWorkloadUninstall(this ICakeContext context, IEnumerable<string> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Contains settings used by <see cref="DotNetWorkloadUninstaller" />.
/// </summary>
public sealed class DotNetWorkloadUninstallSettings : DotNetSettings
{
}
}
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// .NET workloads uninstaller.
/// </summary>
public sealed class DotNetWorkloadUninstaller : DotNetTool<DotNetWorkloadUninstallSettings>
{
/// <summary>
/// Initializes a new instance of the <see cref="DotNetWorkloadUninstaller" /> class.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="environment">The environment.</param>
/// <param name="processRunner">The process runner.</param>
/// <param name="tools">The tool locator.</param>
public DotNetWorkloadUninstaller(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools) : base(fileSystem, environment, processRunner, tools)
{
}

/// <summary>
/// Uninstalls one or more workloads.
/// </summary>
/// <param name="workloadIds">The workload ID or multiple IDs to uninstall.</param>
public void Uninstall(IEnumerable<string> workloadIds)
{
if (workloadIds == null || !workloadIds.Any())
{
throw new ArgumentNullException(nameof(workloadIds));
}

var settings = new DotNetWorkloadUninstallSettings();
RunCommand(settings, GetArguments(workloadIds, settings));
}

private ProcessArgumentBuilder GetArguments(IEnumerable<string> workloadIds, DotNetWorkloadUninstallSettings settings)
{
var builder = CreateArgumentBuilder(settings);

builder.Append("workload uninstall");

if (workloadIds != null && workloadIds.Any())
{
builder.Append(string.Join(" ", workloadIds));
}

return builder;
}
}
}

0 comments on commit 255feb4

Please sign in to comment.