Skip to content

Commit

Permalink
Add DotNetAddReference alias for dotnet add reference command
Browse files Browse the repository at this point in the history
  • Loading branch information
Marusyk authored and devlead committed Sep 26, 2024
1 parent 9b171c0 commit dab22bf
Show file tree
Hide file tree
Showing 6 changed files with 414 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.Reference.Add;
using Cake.Core.IO;

namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Reference.Add
{
internal sealed class DotNetReferenceAdderFixture : DotNetFixture<DotNetReferenceAddSettings>
{
public string Project { get; set; }

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

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

namespace Cake.Common.Tests.Unit.Tools.DotNet.Reference.Add
{
public sealed class DotNetReferenceAdderTests
{
public sealed class TheAddMethod
{
[Fact]
public void Should_Throw_If_Process_Was_Not_Started()
{
// Given
var fixture = new DotNetReferenceAdderFixture();
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 DotNetReferenceAdderFixture();
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 DotNetReferenceAdderFixture();
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 DotNetReferenceAdderFixture();
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 DotNetReferenceAdderFixture();
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 DotNetReferenceAdderFixture();
fixture.ProjectReferences = new[] { (FilePath)"./lib1.csproj" };
fixture.Project = null;

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

// Then
Assert.Equal("add reference \"/Working/lib1.csproj\"", result.Args);
}

[Fact]
public void Should_Add_All_Project_References()
{
// Given
var fixture = new DotNetReferenceAdderFixture();
fixture.ProjectReferences = new[] { (FilePath)"./lib1.csproj", "./lib2/*.csproj" };
fixture.Project = null;

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

// Then
Assert.Equal("add reference \"/Working/lib1.csproj\" \"/Working/lib2/*.csproj\"", result.Args);
}

[Fact]
public void Should_Add_Project_Argument()
{
// Given
var fixture = new DotNetReferenceAdderFixture();
fixture.ProjectReferences = new[] { (FilePath)"./lib1.csproj" };
fixture.Project = "ToDo.csproj";

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

// Then
Assert.Equal("add \"ToDo.csproj\" reference \"/Working/lib1.csproj\"", result.Args);
}

[Fact]
public void Should_Add_Additional_Arguments()
{
// Given
var fixture = new DotNetReferenceAdderFixture();
fixture.ProjectReferences = new[] { (FilePath)"./lib1.csproj" };
fixture.Project = "ToDo.csproj";
fixture.Settings.Framework = "net7.0";
fixture.Settings.Interactive = true;
fixture.Settings.Verbosity = DotNetVerbosity.Diagnostic;

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

// Then
var expected = "add \"ToDo.csproj\" reference \"/Working/lib1.csproj\" --framework net7.0 --interactive --verbosity diagnostic";
Assert.Equal(expected, result.Args);
}
}
}
}
98 changes: 98 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Cake.Common.Tools.DotNet.Package.Add;
using Cake.Common.Tools.DotNet.Package.Remove;
using Cake.Common.Tools.DotNet.Publish;
using Cake.Common.Tools.DotNet.Reference.Add;
using Cake.Common.Tools.DotNet.Restore;
using Cake.Common.Tools.DotNet.Run;
using Cake.Common.Tools.DotNet.SDKCheck;
Expand Down Expand Up @@ -2429,5 +2430,102 @@ public static void DotNetRemovePackage(this ICakeContext context, string package
var adder = new DotNetPackageRemover(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
adder.Remove(packageName, project);
}

/// <summary>
/// Adds project-to-project (P2P) references.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="projectReferences">One or more project references to add. Glob patterns are supported on Unix/Linux-based systems.</param>
/// <example>
/// <code>
/// DotNetAddReference(GetFiles("./src/*.csproj"));
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("AddReference")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Reference.Add")]
public static void DotNetAddReference(this ICakeContext context, IEnumerable<FilePath> projectReferences)
{
context.DotNetAddReference(projectReferences, null);
}

/// <summary>
/// Adds project-to-project (P2P) references.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="projectReferences">One or more project references to add. Glob patterns are supported on Unix/Linux-based systems.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
/// var settings = new DotNetReferenceAddSettings
/// {
/// Framework = "net8.0"
/// };
///
/// DotNetAddReference(GetFiles("./src/*.csproj"), settings);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("AddReference")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Reference.Add")]
public static void DotNetAddReference(this ICakeContext context, IEnumerable<FilePath> projectReferences, DotNetReferenceAddSettings settings)
{
context.DotNetAddReference(null, projectReferences, settings);
}

/// <summary>
/// Adds project-to-project (P2P) references.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="project">The target project file path. If not specified, the command searches the current directory for one.</param>
/// <param name="projectReferences">One or more project references to add. Glob patterns are supported on Unix/Linux-based systems.</param>
/// <example>
/// <code>
/// DotNetAddReference("./app/app.csproj", GetFiles("./src/*.csproj"));
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("AddReference")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Reference.Add")]
public static void DotNetAddReference(this ICakeContext context, string project, IEnumerable<FilePath> projectReferences)
{
context.DotNetAddReference(project, projectReferences, null);
}

/// <summary>
/// Adds project-to-project (P2P) references.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="project">The target project file path. If not specified, the command searches the current directory for one.</param>
/// <param name="projectReferences">One or more project references to add. Glob patterns are supported on Unix/Linux-based systems.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
/// var settings = new DotNetReferenceAddSettings
/// {
/// Framework = "net8.0"
/// };
///
/// DotNetAddReference("./app/app.csproj", GetFiles("./src/*.csproj"), settings);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("AddReference")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Reference.Add")]
public static void DotNetAddReference(this ICakeContext context, string project, IEnumerable<FilePath> projectReferences, DotNetReferenceAddSettings settings)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

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

var adder = new DotNetReferenceAdder(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
adder.Add(project, projectReferences, settings);
}
}
}
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.

namespace Cake.Common.Tools.DotNet.Reference.Add
{
/// <summary>
/// Contains settings used by <see cref="DotNetReferenceAdder" />.
/// </summary>
public sealed class DotNetReferenceAddSettings : DotNetSettings
{
/// <summary>
/// Gets or sets a specific framework.
/// </summary>
public string Framework { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to allow the command to stop and wait for user input or action.
/// For example, to complete authentication. Available since .NET Core 3.0 SDK.
/// </summary>
public bool Interactive { get; set; }
}
}
Loading

0 comments on commit dab22bf

Please sign in to comment.