Skip to content

Commit 1ea4871

Browse files
authored
Merge pull request #4392 from Marusyk/rmarusyk/3817-3
Add DotNetSlnRemove alias for dotnet sln remove command
2 parents 8ca6cc3 + c9af6f9 commit 1ea4871

File tree

6 files changed

+373
-0
lines changed

6 files changed

+373
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Generic;
6+
using Cake.Common.Tools.DotNet.Sln.Remove;
7+
using Cake.Core.IO;
8+
9+
namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Sln.Remove
10+
{
11+
internal sealed class DotNetSlnRemoverFixture : DotNetFixture<DotNetSlnRemoveSettings>
12+
{
13+
public FilePath Solution { get; set; }
14+
15+
public IEnumerable<FilePath> ProjectPath { get; set; }
16+
17+
protected override void RunTool()
18+
{
19+
var tool = new DotNetSlnRemover(FileSystem, Environment, ProcessRunner, Tools);
20+
tool.Remove(Solution, ProjectPath, Settings);
21+
}
22+
}
23+
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Cake.Common.Tests.Fixtures.Tools.DotNet.Sln.Remove;
6+
using Cake.Common.Tools.DotNet;
7+
using Cake.Core.IO;
8+
using Cake.Testing;
9+
using Xunit;
10+
11+
namespace Cake.Common.Tests.Unit.Tools.DotNet.Sln.Remove
12+
{
13+
public sealed class DotNetSlnRemoverTests
14+
{
15+
public sealed class TheAddMethod
16+
{
17+
[Fact]
18+
public void Should_Throw_If_Process_Was_Not_Started()
19+
{
20+
// Given
21+
var fixture = new DotNetSlnRemoverFixture();
22+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
23+
fixture.GivenProcessCannotStart();
24+
25+
// When
26+
var result = Record.Exception(() => fixture.Run());
27+
28+
// Then
29+
AssertEx.IsCakeException(result, ".NET CLI: Process was not started.");
30+
}
31+
32+
[Fact]
33+
public void Should_Throw_If_Process_Has_A_Non_Zero_Exit_Code()
34+
{
35+
// Given
36+
var fixture = new DotNetSlnRemoverFixture();
37+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
38+
fixture.GivenProcessExitsWithCode(1);
39+
40+
// When
41+
var result = Record.Exception(() => fixture.Run());
42+
43+
// Then
44+
AssertEx.IsCakeException(result, ".NET CLI: Process returned an error (exit code 1).");
45+
}
46+
47+
[Fact]
48+
public void Should_Throw_If_ProjectPath_Is_Null()
49+
{
50+
// Given
51+
var fixture = new DotNetSlnRemoverFixture();
52+
fixture.ProjectPath = null;
53+
54+
// When
55+
var result = Record.Exception(() => fixture.Run());
56+
57+
// Then
58+
AssertEx.IsArgumentNullException(result, "projectPath");
59+
}
60+
61+
[Fact]
62+
public void Should_Throw_If_ProjectPath_Is_Empty()
63+
{
64+
// Given
65+
var fixture = new DotNetSlnRemoverFixture();
66+
fixture.ProjectPath = new FilePath[] { };
67+
68+
// When
69+
var result = Record.Exception(() => fixture.Run());
70+
71+
// Then
72+
AssertEx.IsArgumentNullException(result, "projectPath");
73+
}
74+
75+
[Fact]
76+
public void Should_Throw_If_Settings_Are_Null()
77+
{
78+
// Given
79+
var fixture = new DotNetSlnRemoverFixture();
80+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
81+
fixture.Settings = null;
82+
83+
// When
84+
var result = Record.Exception(() => fixture.Run());
85+
86+
// Then
87+
AssertEx.IsArgumentNullException(result, "settings");
88+
}
89+
90+
[Fact]
91+
public void Should_Add_Solution_Argument()
92+
{
93+
// Given
94+
var fixture = new DotNetSlnRemoverFixture();
95+
fixture.Solution = (FilePath)"test.sln";
96+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
97+
98+
// When
99+
var result = fixture.Run();
100+
101+
// Then
102+
Assert.NotNull(result);
103+
Assert.Equal("sln \"/Working/test.sln\" remove \"/Working/lib1.csproj\"", result.Args);
104+
}
105+
106+
[Fact]
107+
public void Should_Not_Add_Solution_Argument()
108+
{
109+
// Given
110+
var fixture = new DotNetSlnRemoverFixture();
111+
fixture.Solution = null;
112+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
113+
114+
// When
115+
var result = fixture.Run();
116+
117+
// Then
118+
Assert.NotNull(result);
119+
Assert.Equal("sln remove \"/Working/lib1.csproj\"", result.Args);
120+
}
121+
122+
[Fact]
123+
public void Should_Add_ProjectPath_Argument()
124+
{
125+
// Given
126+
var fixture = new DotNetSlnRemoverFixture();
127+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
128+
129+
// When
130+
var result = fixture.Run();
131+
132+
// Then
133+
Assert.NotNull(result);
134+
Assert.Equal("sln remove \"/Working/lib1.csproj\"", result.Args);
135+
}
136+
137+
[Fact]
138+
public void Should_Add_All_ProjectPath()
139+
{
140+
// Given
141+
var fixture = new DotNetSlnRemoverFixture();
142+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj", "./lib2.csproj", "./lib3.csproj" };
143+
144+
// When
145+
var result = fixture.Run();
146+
147+
// Then
148+
Assert.NotNull(result);
149+
Assert.Equal("sln remove \"/Working/lib1.csproj\" \"/Working/lib2.csproj\" \"/Working/lib3.csproj\"", result.Args);
150+
}
151+
152+
[Fact]
153+
public void Should_Add_Additional_Arguments()
154+
{
155+
// Given
156+
var fixture = new DotNetSlnRemoverFixture();
157+
fixture.Solution = (FilePath)"test.sln";
158+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
159+
fixture.Settings.Verbosity = DotNetVerbosity.Detailed;
160+
161+
// When
162+
var result = fixture.Run();
163+
164+
// Then
165+
Assert.NotNull(result);
166+
Assert.Equal("sln \"/Working/test.sln\" remove \"/Working/lib1.csproj\" --verbosity detailed", result.Args);
167+
}
168+
}
169+
}
170+
}

src/Cake.Common/Tools/DotNet/DotNetAliases.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using Cake.Common.Tools.DotNet.SDKCheck;
2929
using Cake.Common.Tools.DotNet.Sln.Add;
3030
using Cake.Common.Tools.DotNet.Sln.List;
31+
using Cake.Common.Tools.DotNet.Sln.Remove;
3132
using Cake.Common.Tools.DotNet.Test;
3233
using Cake.Common.Tools.DotNet.Tool;
3334
using Cake.Common.Tools.DotNet.VSTest;
@@ -3070,5 +3071,78 @@ public static void DotNetSlnAdd(this ICakeContext context, FilePath solution, IE
30703071
var adder = new DotNetSlnAdder(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
30713072
adder.Add(solution, projectPath, settings);
30723073
}
3074+
3075+
/// <summary>
3076+
/// Removes a project or multiple projects from the solution file.
3077+
/// </summary>
3078+
/// <param name="context">The context.</param>
3079+
/// <param name="projectPath">The path to the project or projects to remove from the solution.</param>
3080+
/// <example>
3081+
/// <code>
3082+
/// DotNetSlnRemove(GetFiles("./*.csproj"));
3083+
/// </code>
3084+
/// </example>
3085+
[CakeMethodAlias]
3086+
[CakeAliasCategory("Sln")]
3087+
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Sln.Remove")]
3088+
public static void DotNetSlnRemove(this ICakeContext context, IEnumerable<FilePath> projectPath)
3089+
{
3090+
context.DotNetSlnRemove(null, projectPath);
3091+
}
3092+
3093+
/// <summary>
3094+
/// Removes a project or multiple projects from the solution file.
3095+
/// </summary>
3096+
/// <param name="context">The context.</param>
3097+
/// <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>
3098+
/// <param name="projectPath">The path to the project or projects to remove from the solution.</param>
3099+
/// <example>
3100+
/// <code>
3101+
/// DotNetSlnRemove("app.sln", GetFiles("./*.csproj"));
3102+
/// </code>
3103+
/// </example>
3104+
[CakeMethodAlias]
3105+
[CakeAliasCategory("Sln")]
3106+
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Sln.Remove")]
3107+
public static void DotNetSlnRemove(this ICakeContext context, FilePath solution, IEnumerable<FilePath> projectPath)
3108+
{
3109+
context.DotNetSlnRemove(solution, projectPath, null);
3110+
}
3111+
3112+
/// <summary>
3113+
/// Removes a project or multiple projects from the solution file.
3114+
/// </summary>
3115+
/// <param name="context">The context.</param>
3116+
/// <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>
3117+
/// <param name="projectPath">The path to the project or projects to remove from the solution.</param>
3118+
/// <param name="settings">The settings.</param>
3119+
/// <example>
3120+
/// <code>
3121+
/// var settings = new DotNetSlnRemoveSettings
3122+
/// {
3123+
/// Verbosity = DotNetVerbosity.Diagnostic
3124+
/// };
3125+
///
3126+
/// DotNetSlnRemove("app.sln", GetFiles("./*.csproj"), settings);
3127+
/// </code>
3128+
/// </example>
3129+
[CakeMethodAlias]
3130+
[CakeAliasCategory("Sln")]
3131+
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Sln.Remove")]
3132+
public static void DotNetSlnRemove(this ICakeContext context, FilePath solution, IEnumerable<FilePath> projectPath, DotNetSlnRemoveSettings settings)
3133+
{
3134+
if (context is null)
3135+
{
3136+
throw new ArgumentNullException(nameof(context));
3137+
}
3138+
3139+
if (settings is null)
3140+
{
3141+
settings = new DotNetSlnRemoveSettings();
3142+
}
3143+
3144+
var remover = new DotNetSlnRemover(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
3145+
remover.Remove(solution, projectPath, settings);
3146+
}
30733147
}
30743148
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace Cake.Common.Tools.DotNet.Sln.Remove
6+
{
7+
/// <summary>
8+
/// Contains settings used by <see cref="DotNetSlnRemover" />.
9+
/// </summary>
10+
public sealed class DotNetSlnRemoveSettings : DotNetSettings
11+
{
12+
}
13+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using Cake.Core;
9+
using Cake.Core.IO;
10+
using Cake.Core.Tooling;
11+
12+
namespace Cake.Common.Tools.DotNet.Sln.Remove
13+
{
14+
/// <summary>
15+
/// .NET project remover.
16+
/// </summary>
17+
public sealed class DotNetSlnRemover : DotNetTool<DotNetSlnRemoveSettings>
18+
{
19+
private readonly ICakeEnvironment _environment;
20+
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="DotNetSlnRemover" /> class.
23+
/// </summary>
24+
/// <param name="fileSystem">The file system.</param>
25+
/// <param name="environment">The environment.</param>
26+
/// <param name="processRunner">The process runner.</param>
27+
/// <param name="tools">The tool locator.</param>
28+
public DotNetSlnRemover(
29+
IFileSystem fileSystem,
30+
ICakeEnvironment environment,
31+
IProcessRunner processRunner,
32+
IToolLocator tools) : base(fileSystem, environment, processRunner, tools)
33+
{
34+
_environment = environment;
35+
}
36+
37+
/// <summary>
38+
/// Removes a project or multiple projects from the solution file.
39+
/// </summary>
40+
/// <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>
41+
/// <param name="projectPath">The path to the project or projects to remove from the solution.</param>
42+
/// <param name="settings">The settings.</param>
43+
public void Remove(FilePath solution, IEnumerable<FilePath> projectPath, DotNetSlnRemoveSettings settings)
44+
{
45+
if (projectPath == null || !projectPath.Any())
46+
{
47+
throw new ArgumentNullException(nameof(projectPath));
48+
}
49+
if (settings == null)
50+
{
51+
throw new ArgumentNullException(nameof(settings));
52+
}
53+
54+
RunCommand(settings, GetArguments(solution, projectPath, settings));
55+
}
56+
57+
private ProcessArgumentBuilder GetArguments(FilePath solution, IEnumerable<FilePath> projectPath, DotNetSlnRemoveSettings settings)
58+
{
59+
var builder = CreateArgumentBuilder(settings);
60+
61+
builder.Append("sln");
62+
63+
// Solution path
64+
if (solution != null)
65+
{
66+
builder.AppendQuoted(solution.MakeAbsolute(_environment).FullPath);
67+
}
68+
69+
builder.Append("remove");
70+
71+
// Project path
72+
foreach (var project in projectPath)
73+
{
74+
builder.AppendQuoted(project.MakeAbsolute(_environment).FullPath);
75+
}
76+
77+
return builder;
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)