Skip to content

Commit 27a28a5

Browse files
authored
Add DotNetAddPackage alias for dotnet add package command (#4195)
* Add DotNetAddPackage alias for dotnet add package command * Add an integration test for DotNetAddPackage alias * Add an integration test for DotNetAddPackage alias * Change the package in integration test for DotNetAddPackage alias
1 parent 4ae30f3 commit 27a28a5

File tree

6 files changed

+433
-1
lines changed

6 files changed

+433
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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.Tools.DotNet.Package.Add;
6+
7+
namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Package.Add
8+
{
9+
internal sealed class DotNetPackageAdderFixture : DotNetFixture<DotNetPackageAddSettings>
10+
{
11+
public string PackageName { get; set; }
12+
13+
public string Project { get; set; }
14+
15+
protected override void RunTool()
16+
{
17+
var tool = new DotNetPackageAdder(FileSystem, Environment, ProcessRunner, Tools);
18+
tool.Add(PackageName, Project, Settings);
19+
}
20+
}
21+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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.Package.Add;
6+
using Cake.Common.Tests.Fixtures.Tools.DotNet.SDKCheck;
7+
using Cake.Common.Tests.Fixtures.Tools.DotNet.Workload.Restore;
8+
using Cake.Common.Tools.DotNet;
9+
using Cake.Common.Tools.DotNet.Package.Add;
10+
using Cake.Testing;
11+
using Xunit;
12+
13+
namespace Cake.Common.Tests.Unit.Tools.DotNet.Package.Add
14+
{
15+
public sealed class DotNetPackageAdderTests
16+
{
17+
public sealed class TheAddMethod
18+
{
19+
[Fact]
20+
public void Should_Throw_If_Process_Was_Not_Started()
21+
{
22+
// Given
23+
var fixture = new DotNetPackageAdderFixture();
24+
fixture.PackageName = "Microsoft.AspNetCore.StaticFiles";
25+
fixture.GivenProcessCannotStart();
26+
27+
// When
28+
var result = Record.Exception(() => fixture.Run());
29+
30+
// Then
31+
AssertEx.IsCakeException(result, ".NET CLI: Process was not started.");
32+
}
33+
34+
[Fact]
35+
public void Should_Throw_If_Process_Has_A_Non_Zero_Exit_Code()
36+
{
37+
// Given
38+
var fixture = new DotNetPackageAdderFixture();
39+
fixture.PackageName = "Microsoft.AspNetCore.StaticFiles";
40+
fixture.GivenProcessExitsWithCode(1);
41+
42+
// When
43+
var result = Record.Exception(() => fixture.Run());
44+
45+
// Then
46+
AssertEx.IsCakeException(result, ".NET CLI: Process returned an error (exit code 1).");
47+
}
48+
49+
[Fact]
50+
public void Should_Throw_If_Settings_Are_Null()
51+
{
52+
// Given
53+
var fixture = new DotNetPackageAdderFixture();
54+
fixture.PackageName = "Microsoft.AspNetCore.StaticFiles";
55+
fixture.Settings = null;
56+
fixture.GivenDefaultToolDoNotExist();
57+
58+
// When
59+
var result = Record.Exception(() => fixture.Run());
60+
61+
// Then
62+
AssertEx.IsArgumentNullException(result, "settings");
63+
}
64+
65+
[Fact]
66+
public void Should_Throw_If_PackageName_Is_Null()
67+
{
68+
// Given
69+
var fixture = new DotNetPackageAdderFixture();
70+
fixture.PackageName = null;
71+
fixture.GivenDefaultToolDoNotExist();
72+
73+
// When
74+
var result = Record.Exception(() => fixture.Run());
75+
76+
// Then
77+
AssertEx.IsArgumentNullException(result, "packageName");
78+
}
79+
80+
[Fact]
81+
public void Should_Add_Project_Argument()
82+
{
83+
// Given
84+
var fixture = new DotNetPackageAdderFixture();
85+
fixture.PackageName = "Microsoft.AspNetCore.StaticFiles";
86+
fixture.Project = "ToDo.csproj";
87+
88+
// When
89+
var result = fixture.Run();
90+
91+
// Then
92+
Assert.Equal("add \"ToDo.csproj\" package Microsoft.AspNetCore.StaticFiles", result.Args);
93+
}
94+
95+
[Fact]
96+
public void Should_Add_Additional_Arguments()
97+
{
98+
// Given
99+
var fixture = new DotNetPackageAdderFixture();
100+
fixture.PackageName = "Microsoft.AspNetCore.StaticFiles";
101+
fixture.Settings.Framework = "net7.0";
102+
fixture.Settings.Interactive = true;
103+
fixture.Settings.NoRestore = true;
104+
fixture.Settings.PackageDirectory = "./src/project";
105+
fixture.Settings.Prerelease = true;
106+
fixture.Settings.Source = "http://www.nuget.org/api/v2/package";
107+
fixture.Settings.Version = "1.0.0";
108+
fixture.Settings.Verbosity = DotNetVerbosity.Diagnostic;
109+
110+
// When
111+
var result = fixture.Run();
112+
113+
// Then
114+
var expected = "add package Microsoft.AspNetCore.StaticFiles --framework net7.0 --interactive --no-restore --package-directory \"/Working/src/project\" --prerelease --source \"http://www.nuget.org/api/v2/package\" --version \"1.0.0\" --verbosity diagnostic";
115+
Assert.Equal(expected, result.Args);
116+
}
117+
}
118+
}
119+
}

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

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Cake.Common.Tools.DotNet.NuGet.Push;
1616
using Cake.Common.Tools.DotNet.NuGet.Source;
1717
using Cake.Common.Tools.DotNet.Pack;
18+
using Cake.Common.Tools.DotNet.Package.Add;
1819
using Cake.Common.Tools.DotNet.Publish;
1920
using Cake.Common.Tools.DotNet.Restore;
2021
using Cake.Common.Tools.DotNet.Run;
@@ -2285,5 +2286,104 @@ public static void DotNetWorkloadRestore(this ICakeContext context, string proje
22852286
var restorer = new DotNetWorkloadRestorer(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
22862287
restorer.Restore(project, settings);
22872288
}
2289+
2290+
/// <summary>
2291+
/// Adds or updates a package reference in a project file.
2292+
/// </summary>
2293+
/// <param name="context">The context.</param>
2294+
/// <param name="packageName">The package reference to add.</param>
2295+
/// <example>
2296+
/// <code>
2297+
/// DotNetAddPackage("Cake.FileHelper");
2298+
/// </code>
2299+
/// </example>
2300+
[CakeMethodAlias]
2301+
[CakeAliasCategory("AddPackage")]
2302+
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Add")]
2303+
public static void DotNetAddPackage(this ICakeContext context, string packageName)
2304+
{
2305+
context.DotNetAddPackage(packageName, null, null);
2306+
}
2307+
2308+
/// <summary>
2309+
/// Adds or updates a package reference in a project file.
2310+
/// </summary>
2311+
/// <param name="context">The context.</param>
2312+
/// <param name="packageName">The package reference to add.</param>
2313+
/// <param name="project">The project or solution file to install workloads for.</param>
2314+
/// <example>
2315+
/// <code>
2316+
/// DotNetAddPackage("Cake.FileHelper", "ToDo.csproj");
2317+
/// </code>
2318+
/// </example>
2319+
[CakeMethodAlias]
2320+
[CakeAliasCategory("AddPackage")]
2321+
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Add")]
2322+
public static void DotNetAddPackage(this ICakeContext context, string packageName, string project)
2323+
{
2324+
context.DotNetAddPackage(packageName, project, null);
2325+
}
2326+
2327+
/// <summary>
2328+
/// Adds or updates a package reference in a project file.
2329+
/// </summary>
2330+
/// <param name="context">The context.</param>
2331+
/// <param name="packageName">The package reference to add.</param>
2332+
/// <param name="settings">The settings.</param>
2333+
/// <example>
2334+
/// <code>
2335+
/// var settings = new DotNetPackageAddSettings
2336+
/// {
2337+
/// NoRestore = true,
2338+
/// Version = "6.1.3"
2339+
/// };
2340+
///
2341+
/// DotNetAddPackage("Cake.FileHelper", settings);
2342+
/// </code>
2343+
/// </example>
2344+
[CakeMethodAlias]
2345+
[CakeAliasCategory("AddPackage")]
2346+
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Add")]
2347+
public static void DotNetAddPackage(this ICakeContext context, string packageName, DotNetPackageAddSettings settings)
2348+
{
2349+
context.DotNetAddPackage(packageName, null, settings);
2350+
}
2351+
2352+
/// <summary>
2353+
/// Adds or updates a package reference in a project file.
2354+
/// </summary>
2355+
/// <param name="context">The context.</param>
2356+
/// <param name="packageName">The package reference to add.</param>
2357+
/// <param name="project">The project or solution file to install workloads for.</param>
2358+
/// <param name="settings">The settings.</param>
2359+
/// <example>
2360+
/// <code>
2361+
/// var settings = new DotNetPackageAddSettings
2362+
/// {
2363+
/// NoRestore = true,
2364+
/// Version = "6.1.3"
2365+
/// };
2366+
///
2367+
/// DotNetAddPackage("Cake.FileHelper", "ToDo.csproj", settings);
2368+
/// </code>
2369+
/// </example>
2370+
[CakeMethodAlias]
2371+
[CakeAliasCategory("AddPackage")]
2372+
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Package.Add")]
2373+
public static void DotNetAddPackage(this ICakeContext context, string packageName, string project, DotNetPackageAddSettings settings)
2374+
{
2375+
if (context is null)
2376+
{
2377+
throw new ArgumentNullException(nameof(context));
2378+
}
2379+
2380+
if (settings is null)
2381+
{
2382+
settings = new DotNetPackageAddSettings();
2383+
}
2384+
2385+
var adder = new DotNetPackageAdder(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
2386+
adder.Add(packageName, project, settings);
2387+
}
22882388
}
22892389
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.Package.Add
8+
{
9+
/// <summary>
10+
/// Contains settings used by <see cref="DotNetPackageAdder" />.
11+
/// </summary>
12+
public sealed class DotNetPackageAddSettings : DotNetSettings
13+
{
14+
/// <summary>
15+
/// Gets or sets a specific framework to compile.
16+
/// </summary>
17+
public string Framework { get; set; }
18+
19+
/// <summary>
20+
/// Gets or sets a value indicating whether to allow the command to stop and wait for user input or action.
21+
/// For example, to complete authentication.
22+
/// </summary>
23+
public bool Interactive { get; set; }
24+
25+
/// <summary>
26+
/// Gets or sets a value indicating whether to not do implicit NuGet package restore.
27+
/// This makes run faster, but requires restore to be done before run is executed.
28+
/// </summary>
29+
public bool NoRestore { get; set; }
30+
31+
/// <summary>
32+
/// Gets or sets the directory path where to restore the packages.
33+
/// The default package restore location is %userprofile%\.nuget\packages on Windows and ~/.nuget/packages on macOS and Linux.
34+
/// </summary>
35+
public DirectoryPath PackageDirectory { get; set; }
36+
37+
/// <summary>
38+
/// Gets or sets a value indicating whether to allow installation of prerelease packages.
39+
/// Available since .NET Core 5 SDK.
40+
/// </summary>
41+
public bool Prerelease { get; set; }
42+
43+
/// <summary>
44+
/// Gets or sets the URI of the NuGet package source to use during the restore operation.
45+
/// </summary>
46+
public string Source { get; set; }
47+
48+
/// <summary>
49+
/// Gets or sets the version of the package to install.
50+
/// If none specified, the latest will be used.
51+
/// </summary>
52+
public string Version { get; set; }
53+
}
54+
}

0 commit comments

Comments
 (0)