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 DotNetRemoveReference alias for dotnet remove reference command #4354

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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,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
Loading