Skip to content

Commit 1d40c7c

Browse files
Marusykdevlead
authored andcommitted
Add DotNetSlnAdd alias for dotnet sln add command
1 parent e08b39b commit 1d40c7c

File tree

6 files changed

+475
-0
lines changed

6 files changed

+475
-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.Add;
7+
using Cake.Core.IO;
8+
9+
namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Sln.Add
10+
{
11+
internal sealed class DotNetSlnAdderFixture : DotNetFixture<DotNetSlnAddSettings>
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 DotNetSlnAdder(FileSystem, Environment, ProcessRunner, Tools);
20+
tool.Add(Solution, ProjectPath, Settings);
21+
}
22+
}
23+
}
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
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 Cake.Common.Tests.Fixtures.Tools.DotNet.Sln.Add;
7+
using Cake.Common.Tools.DotNet;
8+
using Cake.Core.IO;
9+
using Cake.Testing;
10+
using Xunit;
11+
12+
namespace Cake.Common.Tests.Unit.Tools.DotNet.Sln.Add
13+
{
14+
public sealed class DotNetSlnAdderTests
15+
{
16+
public sealed class TheAddMethod
17+
{
18+
[Fact]
19+
public void Should_Throw_If_Process_Was_Not_Started()
20+
{
21+
// Given
22+
var fixture = new DotNetSlnAdderFixture();
23+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
24+
fixture.GivenProcessCannotStart();
25+
26+
// When
27+
var result = Record.Exception(() => fixture.Run());
28+
29+
// Then
30+
AssertEx.IsCakeException(result, ".NET CLI: Process was not started.");
31+
}
32+
33+
[Fact]
34+
public void Should_Throw_If_Process_Has_A_Non_Zero_Exit_Code()
35+
{
36+
// Given
37+
var fixture = new DotNetSlnAdderFixture();
38+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
39+
fixture.GivenProcessExitsWithCode(1);
40+
41+
// When
42+
var result = Record.Exception(() => fixture.Run());
43+
44+
// Then
45+
AssertEx.IsCakeException(result, ".NET CLI: Process returned an error (exit code 1).");
46+
}
47+
48+
[Fact]
49+
public void Should_Throw_If_ProjectPath_Is_Null()
50+
{
51+
// Given
52+
var fixture = new DotNetSlnAdderFixture();
53+
fixture.ProjectPath = null;
54+
55+
// When
56+
var result = Record.Exception(() => fixture.Run());
57+
58+
// Then
59+
AssertEx.IsArgumentNullException(result, "projectPath");
60+
}
61+
62+
[Fact]
63+
public void Should_Throw_If_ProjectPath_Is_Empty()
64+
{
65+
// Given
66+
var fixture = new DotNetSlnAdderFixture();
67+
fixture.ProjectPath = [];
68+
69+
// When
70+
var result = Record.Exception(() => fixture.Run());
71+
72+
// Then
73+
AssertEx.IsArgumentNullException(result, "projectPath");
74+
}
75+
76+
[Fact]
77+
public void Should_Throw_If_Settings_Are_Null()
78+
{
79+
// Given
80+
var fixture = new DotNetSlnAdderFixture();
81+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
82+
fixture.Settings = null;
83+
84+
// When
85+
var result = Record.Exception(() => fixture.Run());
86+
87+
// Then
88+
AssertEx.IsArgumentNullException(result, "settings");
89+
}
90+
91+
[Fact]
92+
public void Should_Throw_If_InRoot_And_SolutionFolder_Are_Used_Together()
93+
{
94+
// Given
95+
var fixture = new DotNetSlnAdderFixture();
96+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
97+
fixture.Settings.InRoot = true;
98+
fixture.Settings.SolutionFolder = "mylibs";
99+
100+
// When
101+
var result = Record.Exception(() => fixture.Run());
102+
103+
// Then
104+
Assert.IsType<ArgumentException>(result);
105+
Assert.Equal("InRoot and SolutionFolder cannot be used together.", result.Message);
106+
}
107+
108+
[Fact]
109+
public void Should_Add_Solution_Argument()
110+
{
111+
// Given
112+
var fixture = new DotNetSlnAdderFixture();
113+
fixture.Solution = (FilePath)"test.sln";
114+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
115+
116+
// When
117+
var result = fixture.Run();
118+
119+
// Then
120+
Assert.NotNull(result);
121+
Assert.Equal("sln \"/Working/test.sln\" add \"/Working/lib1.csproj\"", result.Args);
122+
}
123+
124+
[Fact]
125+
public void Should_Not_Add_Solution_Argument()
126+
{
127+
// Given
128+
var fixture = new DotNetSlnAdderFixture();
129+
fixture.Solution = null;
130+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
131+
132+
// When
133+
var result = fixture.Run();
134+
135+
// Then
136+
Assert.NotNull(result);
137+
Assert.Equal("sln add \"/Working/lib1.csproj\"", result.Args);
138+
}
139+
140+
[Fact]
141+
public void Should_Add_ProjectPath_Argument()
142+
{
143+
// Given
144+
var fixture = new DotNetSlnAdderFixture();
145+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
146+
147+
// When
148+
var result = fixture.Run();
149+
150+
// Then
151+
Assert.NotNull(result);
152+
Assert.Equal("sln add \"/Working/lib1.csproj\"", result.Args);
153+
}
154+
155+
[Fact]
156+
public void Should_Add_All_ProjectPath()
157+
{
158+
// Given
159+
var fixture = new DotNetSlnAdderFixture();
160+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj", "./lib2.csproj", "./lib3.csproj" };
161+
162+
// When
163+
var result = fixture.Run();
164+
165+
// Then
166+
Assert.NotNull(result);
167+
Assert.Equal("sln add \"/Working/lib1.csproj\" \"/Working/lib2.csproj\" \"/Working/lib3.csproj\"", result.Args);
168+
}
169+
170+
[Fact]
171+
public void Should_Add_InRoot_Argument()
172+
{
173+
// Given
174+
var fixture = new DotNetSlnAdderFixture();
175+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
176+
fixture.Settings.InRoot = true;
177+
178+
// When
179+
var result = fixture.Run();
180+
181+
// Then
182+
Assert.NotNull(result);
183+
Assert.Equal("sln add --in-root \"/Working/lib1.csproj\"", result.Args);
184+
}
185+
186+
[Fact]
187+
public void Should_Add_SolutionFolder_Argument()
188+
{
189+
// Given
190+
var fixture = new DotNetSlnAdderFixture();
191+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
192+
fixture.Settings.SolutionFolder = "mylibs";
193+
194+
// When
195+
var result = fixture.Run();
196+
197+
// Then
198+
Assert.NotNull(result);
199+
Assert.Equal("sln add --solution-folder \"/Working/mylibs\" \"/Working/lib1.csproj\"", result.Args);
200+
}
201+
202+
[Fact]
203+
public void Should_Add_Additional_Arguments()
204+
{
205+
// Given
206+
var fixture = new DotNetSlnAdderFixture();
207+
fixture.Solution = (FilePath)"test.sln";
208+
fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" };
209+
fixture.Settings.Verbosity = DotNetVerbosity.Detailed;
210+
211+
// When
212+
var result = fixture.Run();
213+
214+
// Then
215+
Assert.NotNull(result);
216+
Assert.Equal("sln \"/Working/test.sln\" add \"/Working/lib1.csproj\" --verbosity detailed", result.Args);
217+
}
218+
}
219+
}
220+
}

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using Cake.Common.Tools.DotNet.Restore;
2727
using Cake.Common.Tools.DotNet.Run;
2828
using Cake.Common.Tools.DotNet.SDKCheck;
29+
using Cake.Common.Tools.DotNet.Sln.Add;
2930
using Cake.Common.Tools.DotNet.Sln.List;
3031
using Cake.Common.Tools.DotNet.Test;
3132
using Cake.Common.Tools.DotNet.Tool;
@@ -2972,5 +2973,102 @@ public static IEnumerable<string> DotNetSlnList(this ICakeContext context, FileP
29722973
var lister = new DotNetSlnLister(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
29732974
return lister.List(solution, settings);
29742975
}
2976+
2977+
/// <summary>
2978+
/// Adds one or more projects to the solution file.
2979+
/// </summary>
2980+
/// <param name="context">The context.</param>
2981+
/// <param name="projectPath">The path to the project or projects to add to the solution. Glob patterns are supported on Unix/Linux-based systems.</param>
2982+
/// <example>
2983+
/// <code>
2984+
/// DotNetSlnAdd(GetFiles("./*.csproj"));
2985+
/// </code>
2986+
/// </example>
2987+
[CakeMethodAlias]
2988+
[CakeAliasCategory("Sln")]
2989+
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Sln.Add")]
2990+
public static void DotNetSlnAdd(this ICakeContext context, IEnumerable<FilePath> projectPath)
2991+
{
2992+
context.DotNetSlnAdd(null, projectPath);
2993+
}
2994+
2995+
/// <summary>
2996+
/// Adds one or more projects to the solution file.
2997+
/// </summary>
2998+
/// <param name="context">The context.</param>
2999+
/// <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>
3000+
/// <param name="projectPath">The path to the project or projects to add to the solution. Glob patterns are supported on Unix/Linux-based systems.</param>
3001+
/// <example>
3002+
/// <code>
3003+
/// DotNetSlnAdd("app.sln", GetFiles("./*.csproj"));
3004+
/// </code>
3005+
/// </example>
3006+
[CakeMethodAlias]
3007+
[CakeAliasCategory("Sln")]
3008+
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Sln.Add")]
3009+
public static void DotNetSlnAdd(this ICakeContext context, FilePath solution, IEnumerable<FilePath> projectPath)
3010+
{
3011+
context.DotNetSlnAdd(solution, projectPath, null);
3012+
}
3013+
3014+
/// <summary>
3015+
/// Adds one or more projects to the solution file.
3016+
/// </summary>
3017+
/// <param name="context">The context.</param>
3018+
/// <param name="projectPath">The path to the project or projects to add to the solution. Glob patterns are supported on Unix/Linux-based systems.</param>
3019+
/// <param name="settings">The settings.</param>
3020+
/// <example>
3021+
/// <code>
3022+
/// var settings = new DotNetSlnAddSettings
3023+
/// {
3024+
/// SolutionFolder = "libs/math"
3025+
/// };
3026+
///
3027+
/// DotNetSlnAdd(GetFiles("./*.csproj"), settings);
3028+
/// </code>
3029+
/// </example>
3030+
[CakeMethodAlias]
3031+
[CakeAliasCategory("Sln")]
3032+
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Sln.Add")]
3033+
public static void DotNetSlnAdd(this ICakeContext context, IEnumerable<FilePath> projectPath, DotNetSlnAddSettings settings)
3034+
{
3035+
context.DotNetSlnAdd(null, projectPath, settings);
3036+
}
3037+
3038+
/// <summary>
3039+
/// Adds one or more projects to the solution file.
3040+
/// </summary>
3041+
/// <param name="context">The context.</param>
3042+
/// <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>
3043+
/// <param name="projectPath">The path to the project or projects to add to the solution. Glob patterns are supported on Unix/Linux-based systems.</param>
3044+
/// <param name="settings">The settings.</param>
3045+
/// <example>
3046+
/// <code>
3047+
/// var settings = new DotNetSlnAddSettings
3048+
/// {
3049+
/// SolutionFolder = "libs/math"
3050+
/// };
3051+
///
3052+
/// DotNetSlnAdd("app.sln", GetFiles("./*.csproj"), settings);
3053+
/// </code>
3054+
/// </example>
3055+
[CakeMethodAlias]
3056+
[CakeAliasCategory("Sln")]
3057+
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Sln.Add")]
3058+
public static void DotNetSlnAdd(this ICakeContext context, FilePath solution, IEnumerable<FilePath> projectPath, DotNetSlnAddSettings settings)
3059+
{
3060+
if (context is null)
3061+
{
3062+
throw new ArgumentNullException(nameof(context));
3063+
}
3064+
3065+
if (settings is null)
3066+
{
3067+
settings = new DotNetSlnAddSettings();
3068+
}
3069+
3070+
var adder = new DotNetSlnAdder(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
3071+
adder.Add(solution, projectPath, settings);
3072+
}
29753073
}
29763074
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.Core.IO;
6+
7+
namespace Cake.Common.Tools.DotNet.Sln.Add
8+
{
9+
/// <summary>
10+
/// Contains settings used by <see cref="DotNetSlnAdder" />.
11+
/// </summary>
12+
public sealed class DotNetSlnAddSettings : DotNetSettings
13+
{
14+
/// <summary>
15+
/// Gets or sets a value indicating whether to place the projects in the root of the solution, rather than creating a solution folder.
16+
/// Can't be used with <see cref="SolutionFolder" />.
17+
/// </summary>
18+
public bool InRoot { get; set; }
19+
20+
/// <summary>
21+
/// Gets or sets the destination solution folder path to add the projects to. Can't be used with <see cref="InRoot" />.
22+
/// </summary>
23+
public DirectoryPath SolutionFolder { get; set; }
24+
}
25+
}

0 commit comments

Comments
 (0)