Skip to content

Commit

Permalink
Merge pull request cake-build#4354 from Marusyk/rmarusyk/4352
Browse files Browse the repository at this point in the history
Add DotNetRemoveReference alias for dotnet remove reference command
  • Loading branch information
devlead authored Oct 11, 2024
2 parents 86ad011 + 23f6983 commit 1bb0807
Show file tree
Hide file tree
Showing 6 changed files with 406 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.Reference.Remove;
using Cake.Core.IO;

namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Reference.Remove
{
internal sealed class DotNetReferenceRemoverFixture : DotNetFixture<DotNetReferenceRemoveSettings>
{
public string Project { get; set; }

public IEnumerable<FilePath> ProjectReferences { get; set; }

protected override void RunTool()
{
var tool = new DotNetReferenceRemover(FileSystem, Environment, ProcessRunner, Tools);
tool.Remove(Project, ProjectReferences, Settings);
}
}
}
Original file line number Diff line number Diff line change
@@ -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 System;
using Cake.Common.Tests.Fixtures.Tools.DotNet.Reference.Remove;
using Cake.Common.Tools.DotNet;
using Cake.Core.IO;
using Cake.Testing;
using Xunit;

namespace Cake.Common.Tests.Unit.Tools.DotNet.Reference.Remove
{
public sealed class DotNetReferenceRemoverTests
{
public sealed class TheRemoveMethod
{
[Fact]
public void Should_Throw_If_Process_Was_Not_Started()
{
// Given
var fixture = new DotNetReferenceRemoverFixture();
fixture.ProjectReferences = new[] { (FilePath)"./test/unit.tests.csproj" };
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 DotNetReferenceRemoverFixture();
fixture.ProjectReferences = new[] { (FilePath)"./test/unit.tests.csproj" };
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 DotNetReferenceRemoverFixture();
fixture.ProjectReferences = new[] { (FilePath)"./test/unit.tests.csproj" };
fixture.Settings = null;
fixture.GivenDefaultToolDoNotExist();

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

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

[Fact]
public void Should_Throw_If_ProjectReferences_Is_Null()
{
// Given
var fixture = new DotNetReferenceRemoverFixture();
fixture.ProjectReferences = null;
fixture.GivenDefaultToolDoNotExist();

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

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

[Fact]
public void Should_Throw_If_ProjectReferences_Is_Empty()
{
// Given
var fixture = new DotNetReferenceRemoverFixture();
fixture.ProjectReferences = Array.Empty<FilePath>();
fixture.GivenDefaultToolDoNotExist();

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

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

[Fact]
public void Should_Not_Add_Project_Argument()
{
// Given
var fixture = new DotNetReferenceRemoverFixture();
fixture.ProjectReferences = new[] { (FilePath)"./lib1.csproj" };
fixture.Project = null;

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

// Then
Assert.Equal("remove reference \"/Working/lib1.csproj\"", result.Args);
}

[Fact]
public void Should_Add_All_Project_References()
{
// Given
var fixture = new DotNetReferenceRemoverFixture();
fixture.ProjectReferences = new[] { (FilePath)"./lib1.csproj", "./lib2/*.csproj" };
fixture.Project = null;

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

// Then
Assert.Equal("remove reference \"/Working/lib1.csproj\" \"/Working/lib2/*.csproj\"", result.Args);
}

[Fact]
public void Should_Add_Project_Argument()
{
// Given
var fixture = new DotNetReferenceRemoverFixture();
fixture.ProjectReferences = new[] { (FilePath)"./lib1.csproj" };
fixture.Project = "ToDo.csproj";

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

// Then
Assert.Equal("remove \"ToDo.csproj\" reference \"/Working/lib1.csproj\"", result.Args);
}

[Fact]
public void Should_Add_Additional_Arguments()
{
// Given
var fixture = new DotNetReferenceRemoverFixture();
fixture.ProjectReferences = new[] { (FilePath)"./lib1.csproj" };
fixture.Project = "ToDo.csproj";
fixture.Settings.Framework = "net7.0";
fixture.Settings.Verbosity = DotNetVerbosity.Diagnostic;

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

// Then
var expected = "remove \"ToDo.csproj\" reference \"/Working/lib1.csproj\" --framework net7.0 --verbosity diagnostic";
Assert.Equal(expected, result.Args);
}
}
}
}
106 changes: 102 additions & 4 deletions src/Cake.Common/Tools/DotNet/DotNetAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Cake.Common.Tools.DotNet.Package.Search;
using Cake.Common.Tools.DotNet.Publish;
using Cake.Common.Tools.DotNet.Reference.Add;
using Cake.Common.Tools.DotNet.Reference.Remove;
using Cake.Common.Tools.DotNet.Restore;
using Cake.Common.Tools.DotNet.Run;
using Cake.Common.Tools.DotNet.SDKCheck;
Expand Down Expand Up @@ -2444,7 +2445,7 @@ public static void DotNetRemovePackage(this ICakeContext context, string package
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("AddReference")]
[CakeAliasCategory("Reference")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Reference.Add")]
public static void DotNetAddReference(this ICakeContext context, IEnumerable<FilePath> projectReferences)
{
Expand All @@ -2468,7 +2469,7 @@ public static void DotNetAddReference(this ICakeContext context, IEnumerable<Fil
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("AddReference")]
[CakeAliasCategory("Reference")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Reference.Add")]
public static void DotNetAddReference(this ICakeContext context, IEnumerable<FilePath> projectReferences, DotNetReferenceAddSettings settings)
{
Expand All @@ -2487,7 +2488,7 @@ public static void DotNetAddReference(this ICakeContext context, IEnumerable<Fil
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("AddReference")]
[CakeAliasCategory("Reference")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Reference.Add")]
public static void DotNetAddReference(this ICakeContext context, string project, IEnumerable<FilePath> projectReferences)
{
Expand All @@ -2512,7 +2513,7 @@ public static void DotNetAddReference(this ICakeContext context, string project,
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("AddReference")]
[CakeAliasCategory("Reference")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Reference.Add")]
public static void DotNetAddReference(this ICakeContext context, string project, IEnumerable<FilePath> projectReferences, DotNetReferenceAddSettings settings)
{
Expand All @@ -2530,6 +2531,103 @@ public static void DotNetAddReference(this ICakeContext context, string project,
adder.Add(project, projectReferences, settings);
}

/// <summary>
/// Removes project-to-project (P2P) references.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="projectReferences">Project-to-project (P2P) references to remove. You can specify one or multiple projects. Glob patterns are supported on Unix/Linux based terminals.</param>
/// <example>
/// <code>
/// DotNetRemoveReference(GetFiles("./src/*.csproj"));
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Reference")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Reference.Remove")]
public static void DotNetRemoveReference(this ICakeContext context, IEnumerable<FilePath> projectReferences)
{
context.DotNetRemoveReference(projectReferences, null);
}

/// <summary>
/// Removes project-to-project (P2P) references.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="projectReferences">Project-to-project (P2P) references to remove. You can specify one or multiple projects. Glob patterns are supported on Unix/Linux based terminals.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
/// var settings = new DotNetReferenceRemoveSettings
/// {
/// Framework = "net8.0"
/// };
///
/// DotNetRemoveReference(GetFiles("./src/*.csproj"), settings);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Reference")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Reference.Remove")]
public static void DotNetRemoveReference(this ICakeContext context, IEnumerable<FilePath> projectReferences, DotNetReferenceRemoveSettings settings)
{
context.DotNetRemoveReference(null, projectReferences, settings);
}

/// <summary>
/// Removes project-to-project (P2P) references.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="project">Target project file. If not specified, the command searches the current directory for one.</param>
/// <param name="projectReferences">Project-to-project (P2P) references to remove. You can specify one or multiple projects. Glob patterns are supported on Unix/Linux based terminals.</param>
/// <example>
/// <code>
/// DotNetRemoveReference("./app/app.csproj", GetFiles("./src/*.csproj"));
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Reference")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Reference.Remove")]
public static void DotNetRemoveReference(this ICakeContext context, string project, IEnumerable<FilePath> projectReferences)
{
context.DotNetRemoveReference(project, projectReferences, null);
}

/// <summary>
/// Removes project-to-project (P2P) references.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="project">Target project file. If not specified, the command searches the current directory for one.</param>
/// <param name="projectReferences">Project-to-project (P2P) references to remove. You can specify one or multiple projects. Glob patterns are supported on Unix/Linux based terminals.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
/// var settings = new DotNetReferenceRemoveSettings
/// {
/// Framework = "net8.0"
/// };
///
/// DotNetRemoveReference("./app/app.csproj", GetFiles("./src/*.csproj"), settings);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Reference")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Reference.Remove")]
public static void DotNetRemoveReference(this ICakeContext context, string project, IEnumerable<FilePath> projectReferences, DotNetReferenceRemoveSettings settings)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

if (settings is null)
{
settings = new DotNetReferenceRemoveSettings();
}

var remover = new DotNetReferenceRemover(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
remover.Remove(project, projectReferences, settings);
}

/// <summary>
/// List packages on available from source using specified settings.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.Reference.Remove
{
/// <summary>
/// Contains settings used by <see cref="DotNetReferenceRemover" />.
/// </summary>
public sealed class DotNetReferenceRemoveSettings : DotNetSettings
{
/// <summary>
/// Gets or sets a specific framework.
/// </summary>
public string Framework { get; set; }
}
}
Loading

0 comments on commit 1bb0807

Please sign in to comment.