Skip to content

Commit

Permalink
Merge pull request #4392 from Marusyk/rmarusyk/3817-3
Browse files Browse the repository at this point in the history
Add DotNetSlnRemove alias for dotnet sln remove command
  • Loading branch information
devlead authored Nov 23, 2024
2 parents 8ca6cc3 + c9af6f9 commit 1ea4871
Show file tree
Hide file tree
Showing 6 changed files with 373 additions and 0 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.Sln.Remove;
using Cake.Core.IO;

namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Sln.Remove
{
internal sealed class DotNetSlnRemoverFixture : DotNetFixture<DotNetSlnRemoveSettings>
{
public FilePath Solution { get; set; }

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

protected override void RunTool()
{
var tool = new DotNetSlnRemover(FileSystem, Environment, ProcessRunner, Tools);
tool.Remove(Solution, ProjectPath, Settings);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// 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.Sln.Remove;
using Cake.Common.Tools.DotNet;
using Cake.Core.IO;
using Cake.Testing;
using Xunit;

namespace Cake.Common.Tests.Unit.Tools.DotNet.Sln.Remove
{
public sealed class DotNetSlnRemoverTests
{
public sealed class TheAddMethod
{
[Fact]
public void Should_Throw_If_Process_Was_Not_Started()
{
// Given
var fixture = new DotNetSlnRemoverFixture();
fixture.ProjectPath = new[] { (FilePath)"./lib1.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 DotNetSlnRemoverFixture();
fixture.ProjectPath = new[] { (FilePath)"./lib1.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_ProjectPath_Is_Null()
{
// Given
var fixture = new DotNetSlnRemoverFixture();
fixture.ProjectPath = null;

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

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

[Fact]
public void Should_Throw_If_ProjectPath_Is_Empty()
{
// Given
var fixture = new DotNetSlnRemoverFixture();
fixture.ProjectPath = new FilePath[] { };

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

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

[Fact]
public void Should_Throw_If_Settings_Are_Null()
{
// Given
var fixture = new DotNetSlnRemoverFixture();
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
fixture.Settings = null;

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

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

[Fact]
public void Should_Add_Solution_Argument()
{
// Given
var fixture = new DotNetSlnRemoverFixture();
fixture.Solution = (FilePath)"test.sln";
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };

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

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

[Fact]
public void Should_Not_Add_Solution_Argument()
{
// Given
var fixture = new DotNetSlnRemoverFixture();
fixture.Solution = null;
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };

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

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

[Fact]
public void Should_Add_ProjectPath_Argument()
{
// Given
var fixture = new DotNetSlnRemoverFixture();
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };

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

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

[Fact]
public void Should_Add_All_ProjectPath()
{
// Given
var fixture = new DotNetSlnRemoverFixture();
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj", "./lib2.csproj", "./lib3.csproj" };

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

// Then
Assert.NotNull(result);
Assert.Equal("sln remove \"/Working/lib1.csproj\" \"/Working/lib2.csproj\" \"/Working/lib3.csproj\"", result.Args);
}

[Fact]
public void Should_Add_Additional_Arguments()
{
// Given
var fixture = new DotNetSlnRemoverFixture();
fixture.Solution = (FilePath)"test.sln";
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
fixture.Settings.Verbosity = DotNetVerbosity.Detailed;

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

// Then
Assert.NotNull(result);
Assert.Equal("sln \"/Working/test.sln\" remove \"/Working/lib1.csproj\" --verbosity detailed", result.Args);
}
}
}
}
74 changes: 74 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using Cake.Common.Tools.DotNet.SDKCheck;
using Cake.Common.Tools.DotNet.Sln.Add;
using Cake.Common.Tools.DotNet.Sln.List;
using Cake.Common.Tools.DotNet.Sln.Remove;
using Cake.Common.Tools.DotNet.Test;
using Cake.Common.Tools.DotNet.Tool;
using Cake.Common.Tools.DotNet.VSTest;
Expand Down Expand Up @@ -3070,5 +3071,78 @@ public static void DotNetSlnAdd(this ICakeContext context, FilePath solution, IE
var adder = new DotNetSlnAdder(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
adder.Add(solution, projectPath, settings);
}

/// <summary>
/// Removes a project or multiple projects from the solution file.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="projectPath">The path to the project or projects to remove from the solution.</param>
/// <example>
/// <code>
/// DotNetSlnRemove(GetFiles("./*.csproj"));
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Sln")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Sln.Remove")]
public static void DotNetSlnRemove(this ICakeContext context, IEnumerable<FilePath> projectPath)
{
context.DotNetSlnRemove(null, projectPath);
}

/// <summary>
/// Removes a project or multiple projects from the solution file.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="solution">The solution file to use. If it is unspecified, the command searches the current directory for one and fails if there are multiple solution files.</param>
/// <param name="projectPath">The path to the project or projects to remove from the solution.</param>
/// <example>
/// <code>
/// DotNetSlnRemove("app.sln", GetFiles("./*.csproj"));
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Sln")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Sln.Remove")]
public static void DotNetSlnRemove(this ICakeContext context, FilePath solution, IEnumerable<FilePath> projectPath)
{
context.DotNetSlnRemove(solution, projectPath, null);
}

/// <summary>
/// Removes a project or multiple projects from the solution file.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="solution">The solution file to use. If it is unspecified, the command searches the current directory for one and fails if there are multiple solution files.</param>
/// <param name="projectPath">The path to the project or projects to remove from the solution.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
/// var settings = new DotNetSlnRemoveSettings
/// {
/// Verbosity = DotNetVerbosity.Diagnostic
/// };
///
/// DotNetSlnRemove("app.sln", GetFiles("./*.csproj"), settings);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Sln")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Sln.Remove")]
public static void DotNetSlnRemove(this ICakeContext context, FilePath solution, IEnumerable<FilePath> projectPath, DotNetSlnRemoveSettings settings)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

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

var remover = new DotNetSlnRemover(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
remover.Remove(solution, projectPath, settings);
}
}
}
13 changes: 13 additions & 0 deletions src/Cake.Common/Tools/DotNet/Sln/Remove/DotNetSlnRemoveSettings.cs
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.Sln.Remove
{
/// <summary>
/// Contains settings used by <see cref="DotNetSlnRemover" />.
/// </summary>
public sealed class DotNetSlnRemoveSettings : DotNetSettings
{
}
}
80 changes: 80 additions & 0 deletions src/Cake.Common/Tools/DotNet/Sln/Remove/DotNetSlnRemover.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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.Sln.Remove
{
/// <summary>
/// .NET project remover.
/// </summary>
public sealed class DotNetSlnRemover : DotNetTool<DotNetSlnRemoveSettings>
{
private readonly ICakeEnvironment _environment;

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

/// <summary>
/// Removes a project or multiple projects from the solution file.
/// </summary>
/// <param name="solution">The solution file to use. If it is unspecified, the command searches the current directory for one and fails if there are multiple solution files.</param>
/// <param name="projectPath">The path to the project or projects to remove from the solution.</param>
/// <param name="settings">The settings.</param>
public void Remove(FilePath solution, IEnumerable<FilePath> projectPath, DotNetSlnRemoveSettings settings)
{
if (projectPath == null || !projectPath.Any())
{
throw new ArgumentNullException(nameof(projectPath));
}
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

RunCommand(settings, GetArguments(solution, projectPath, settings));
}

private ProcessArgumentBuilder GetArguments(FilePath solution, IEnumerable<FilePath> projectPath, DotNetSlnRemoveSettings settings)
{
var builder = CreateArgumentBuilder(settings);

builder.Append("sln");

// Solution path
if (solution != null)
{
builder.AppendQuoted(solution.MakeAbsolute(_environment).FullPath);
}

builder.Append("remove");

// Project path
foreach (var project in projectPath)
{
builder.AppendQuoted(project.MakeAbsolute(_environment).FullPath);
}

return builder;
}
}
}
Loading

0 comments on commit 1ea4871

Please sign in to comment.