Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DotNetRemovePackage alias for dotnet remove package command #4228

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.Package.Remove;

namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Package.Remove
{
internal sealed class DotNetPackageRemoverFixture : DotNetFixture<DotNetPackageRemoveSettings>
{
public string PackageName { get; set; }

public string Project { get; set; }

protected override void RunTool()
{
var tool = new DotNetPackageRemover(FileSystem, Environment, ProcessRunner, Tools);
tool.Remove(PackageName, Project);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
// See the LICENSE file in the project root for more information.

using Cake.Common.Tests.Fixtures.Tools.DotNet.Package.Add;
using Cake.Common.Tests.Fixtures.Tools.DotNet.SDKCheck;
using Cake.Common.Tests.Fixtures.Tools.DotNet.Workload.Restore;
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.Package.Add;
using Cake.Testing;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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.Package.Remove;
using Cake.Common.Tools.DotNet;
using Cake.Testing;
using Xunit;

namespace Cake.Common.Tests.Unit.Tools.DotNet.Package.Remove
{
public sealed class DotNetPackageRemoverTests
{
public sealed class TheRemoveMethod
{
[Fact]
public void Should_Throw_If_Process_Was_Not_Started()
{
// Given
var fixture = new DotNetPackageRemoverFixture();
fixture.PackageName = "Microsoft.AspNetCore.StaticFiles";
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 DotNetPackageRemoverFixture();
fixture.PackageName = "Microsoft.AspNetCore.StaticFiles";
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_PackageName_Is_Null()
{
// Given
var fixture = new DotNetPackageRemoverFixture();
fixture.PackageName = null;
fixture.GivenDefaultToolDoNotExist();

// When
var result = Record.Exception(() => fixture.Run());

// Then
AssertEx.IsArgumentNullException(result, "packageName");
}

[Fact]
public void Should_Add_Project_Argument()
{
// Given
var fixture = new DotNetPackageRemoverFixture();
fixture.PackageName = "Microsoft.AspNetCore.StaticFiles";
fixture.Project = "ToDo.csproj";

// When
var result = fixture.Run();

// Then
Assert.Equal("remove \"ToDo.csproj\" package Microsoft.AspNetCore.StaticFiles", result.Args);
}
}
}
}
56 changes: 50 additions & 6 deletions src/Cake.Common/Tools/DotNet/DotNetAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Cake.Common.Tools.DotNet.NuGet.Source;
using Cake.Common.Tools.DotNet.Pack;
using Cake.Common.Tools.DotNet.Package.Add;
using Cake.Common.Tools.DotNet.Package.Remove;
using Cake.Common.Tools.DotNet.Publish;
using Cake.Common.Tools.DotNet.Restore;
using Cake.Common.Tools.DotNet.Run;
Expand Down Expand Up @@ -2298,7 +2299,7 @@ public static void DotNetWorkloadRestore(this ICakeContext context, string proje
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("AddPackage")]
[CakeAliasCategory("Package")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Add")]
public static void DotNetAddPackage(this ICakeContext context, string packageName)
{
Expand All @@ -2310,14 +2311,14 @@ public static void DotNetAddPackage(this ICakeContext context, string packageNam
/// </summary>
/// <param name="context">The context.</param>
/// <param name="packageName">The package reference to add.</param>
/// <param name="project">The project or solution file to install workloads for.</param>
/// <param name="project">The target project file path.</param>
/// <example>
/// <code>
/// DotNetAddPackage("Cake.FileHelper", "ToDo.csproj");
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("AddPackage")]
[CakeAliasCategory("Package")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Add")]
public static void DotNetAddPackage(this ICakeContext context, string packageName, string project)
{
Expand All @@ -2342,7 +2343,7 @@ public static void DotNetAddPackage(this ICakeContext context, string packageNam
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("AddPackage")]
[CakeAliasCategory("Package")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Add")]
public static void DotNetAddPackage(this ICakeContext context, string packageName, DotNetPackageAddSettings settings)
{
Expand All @@ -2354,7 +2355,7 @@ public static void DotNetAddPackage(this ICakeContext context, string packageNam
/// </summary>
/// <param name="context">The context.</param>
/// <param name="packageName">The package reference to add.</param>
/// <param name="project">The project or solution file to install workloads for.</param>
/// <param name="project">The target project file path.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
Expand All @@ -2368,7 +2369,7 @@ public static void DotNetAddPackage(this ICakeContext context, string packageNam
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("AddPackage")]
[CakeAliasCategory("Package")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Add")]
public static void DotNetAddPackage(this ICakeContext context, string packageName, string project, DotNetPackageAddSettings settings)
{
Expand All @@ -2385,5 +2386,48 @@ public static void DotNetAddPackage(this ICakeContext context, string packageNam
var adder = new DotNetPackageAdder(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
adder.Add(packageName, project, settings);
}

/// <summary>
/// Removes package reference from a project file.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="packageName">The package reference to remove.</param>
/// <example>
/// <code>
/// DotNetRemovePackage("Cake.FileHelper");
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Package")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Remove")]
public static void DotNetRemovePackage(this ICakeContext context, string packageName)
{
context.DotNetRemovePackage(packageName, null);
}

/// <summary>
/// Removes package reference from a project file.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="packageName">The package reference to remove.</param>
/// <param name="project">The target project file path.</param>
/// <example>
/// <code>
/// DotNetRemovePackage("Cake.FileHelper", "ToDo.csproj");
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Package")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Remove")]
public static void DotNetRemovePackage(this ICakeContext context, string packageName, string project)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

var adder = new DotNetPackageRemover(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
adder.Remove(packageName, project);
}
}
}
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.Package.Remove
{
/// <summary>
/// Contains settings used by <see cref="DotNetPackageRemover" />.
/// </summary>
public sealed class DotNetPackageRemoveSettings : DotNetSettings
{
}
}
Original file line number Diff line number Diff line change
@@ -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;
using Cake.Core;
using Cake.Core.IO;
using Cake.Core.Tooling;

namespace Cake.Common.Tools.DotNet.Package.Remove
{
/// <summary>
/// .NET package remover.
/// </summary>
public sealed class DotNetPackageRemover : DotNetTool<DotNetPackageRemoveSettings>
{
private readonly ICakeEnvironment _environment;

/// <summary>
/// Initializes a new instance of the <see cref="DotNetPackageRemover" /> 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 DotNetPackageRemover(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools) : base(fileSystem, environment, processRunner, tools)
{
_environment = environment;
}

/// <summary>
/// Removes package reference from a project file.
/// </summary>
/// <param name="packageName">The package reference to remove.</param>
/// <param name="project">The target project file path. If not specified, the command searches the current directory for one.</param>
public void Remove(string packageName, string project)
{
if (packageName == null)
{
throw new ArgumentNullException(nameof(packageName));
}

var settings = new DotNetPackageRemoveSettings();
RunCommand(settings, GetArguments(packageName, project, settings));
}

private ProcessArgumentBuilder GetArguments(string packageName, string project, DotNetPackageRemoveSettings settings)
{
var builder = CreateArgumentBuilder(settings);

builder.Append("remove");

// Project path
if (project != null)
{
builder.AppendQuoted(project);
}

// Package Name
builder.AppendSwitch("package", packageName);

return builder;
}
}
}
27 changes: 27 additions & 0 deletions tests/integration/Cake.Common/Tools/DotNet/DotNetAliases.cake
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,32 @@ Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetAddPackage")
Assert.Equal(package, value);
});

Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetRemovePackage")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.Setup")
.Does(() =>
{
// Given
var path = Paths.Temp.Combine("./Cake.Common/Tools/DotNet");
var project = path.CombineWithFilePath("hwapp/hwapp.csproj");
var package = "grok.net";
var value = XmlPeek(
project.FullPath,
$"/Project/ItemGroup/PackageReference[@Include='{package}']/@Include"
);
Assert.Equal(package, value);

// When
DotNetRemovePackage(package, project.FullPath);

value = XmlPeek(
project.FullPath,
$"/Project/ItemGroup/PackageReference[@Include='{package}']/@Include"
);

// Then
Assert.Null(value);
});

Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetBuildServerShutdown")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetRestore")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetBuild")
Expand All @@ -351,6 +377,7 @@ Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetBuildServerShutdown")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetWorkloadUpdate")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetWorkloadRestore")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetAddPackage")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetRemovePackage")
.Does(() =>
{
// When
Expand Down
Loading