Skip to content

Commit

Permalink
Merge pull request cake-build#3953 from Marusyk/rmarusyk/3484
Browse files Browse the repository at this point in the history
Add alias for dotnet workload restore command
  • Loading branch information
devlead committed Oct 9, 2022
2 parents f455742 + 718dbf3 commit fee7a87
Show file tree
Hide file tree
Showing 6 changed files with 428 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.Tools.DotNet.Workload.Restore;

namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Workload.Restore
{
internal sealed class DotNetWorkloadRestorerFixture : DotNetFixture<DotNetWorkloadRestoreSettings>
{
public string Project { get; set; }

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

namespace Cake.Common.Tests.Unit.Tools.DotNet.Workload.Restore
{
public sealed class DotNetWorkloadRestoreTests
{
public sealed class TheBuildMethod
{
[Fact]
public void Should_Throw_If_Process_Was_Not_Started()
{
// Given
var fixture = new DotNetWorkloadRestorerFixture();
fixture.Project = "./src/*";
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 DotNetWorkloadRestorerFixture();
fixture.Project = "./src/*";
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 DotNetWorkloadRestorerFixture();
fixture.Project = "./src/*";
fixture.Settings = null;
fixture.GivenDefaultToolDoNotExist();

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

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

[Fact]
public void Should_Throw_If_Project_Is_Null()
{
// Given
var fixture = new DotNetWorkloadRestorerFixture();
fixture.Settings = new DotNetWorkloadRestoreSettings();
fixture.GivenDefaultToolDoNotExist();

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

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

[Fact]
public void Should_Add_Mandatory_Arguments()
{
// Given
var fixture = new DotNetWorkloadRestorerFixture();
fixture.Project = "./src/*";

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

// Then
Assert.Equal("workload restore \"./src/*\"", result.Args);
}

[Fact]
public void Should_Add_Additional_Arguments()
{
// Given
var fixture = new DotNetWorkloadRestorerFixture();
fixture.Project = "./src/*";
fixture.Settings.ConfigFile = "./nuget.config";
fixture.Settings.DisableParallel = true;
fixture.Settings.IgnoreFailedSources = true;
fixture.Settings.IncludePreviews = true;
fixture.Settings.Interactive = true;
fixture.Settings.NoCache = true;
fixture.Settings.SkipManifestUpdate = true;
fixture.Settings.Source.Add("http://www.nuget.org/api/v2/package");
fixture.Settings.Source.Add("http://www.symbolserver.org/");
fixture.Settings.TempDir = "./src/project";
fixture.Settings.Verbosity = DotNetVerbosity.Diagnostic;

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

// Then
var expected = "workload restore \"./src/*\" --configfile \"/Working/nuget.config\" --disable-parallel --ignore-failed-sources --include-previews --interactive --no-cache --skip-manifest-update --source \"http://www.nuget.org/api/v2/package\" --source \"http://www.symbolserver.org/\" --temp-dir \"/Working/src/project\" --verbosity diagnostic";
Assert.Equal(expected, result.Args);
}

[Theory]
[InlineData("./src/*", "workload restore \"./src/*\"")]
[InlineData("./src/cake build/", "workload restore \"./src/cake build/\"")]
[InlineData("./src/cake build/cake cli", "workload restore \"./src/cake build/cake cli\"")]
public void Should_Quote_Project_Path(string text, string expected)
{
// Given
var fixture = new DotNetWorkloadRestorerFixture();
fixture.Project = text;

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

// Then
Assert.Equal(expected, result.Args);
}

[Fact]
public void Should_Add_Host_Arguments()
{
// Given
var fixture = new DotNetWorkloadRestorerFixture();
fixture.Project = "./src/*";
fixture.Settings.DiagnosticOutput = true;

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

// Then
Assert.Equal("--diagnostics workload restore \"./src/*\"", result.Args);
}
}
}
}
55 changes: 55 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using Cake.Common.Tools.DotNet.Workload.Install;
using Cake.Common.Tools.DotNet.Workload.List;
using Cake.Common.Tools.DotNet.Workload.Repair;
using Cake.Common.Tools.DotNet.Workload.Restore;
using Cake.Common.Tools.DotNet.Workload.Search;
using Cake.Common.Tools.DotNet.Workload.Uninstall;
using Cake.Common.Tools.DotNet.Workload.Update;
Expand Down Expand Up @@ -2245,5 +2246,59 @@ public static void DotNetWorkloadUpdate(this ICakeContext context, DotNetWorkloa
var updater = new DotNetWorkloadUpdater(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
updater.Update(settings);
}

/// <summary>
/// Installs workloads needed for a project or a solution.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="project">The project or solution file to install workloads for.</param>
/// <example>
/// <code>
/// DotNetWorkloadRestore("./src/project");
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Workload")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Workload.Restore")]
public static void DotNetWorkloadRestore(this ICakeContext context, string project)
{
context.DotNetWorkloadRestore(project, null);
}

/// <summary>
/// Installs workloads needed for a project or a solution.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="project">The project or solution file to install workloads for.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
/// var settings = new DotNetWorkloadRestoreSettings
/// {
/// IncludePreviews = true,
/// NoCache = true
/// };
///
/// DotNetWorkloadRestore("./src/project", settings);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Workload")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Workload.Restore")]
public static void DotNetWorkloadRestore(this ICakeContext context, string project, DotNetWorkloadRestoreSettings settings)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

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

var restorer = new DotNetWorkloadRestorer(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
restorer.Restore(project, settings);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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.Core.IO;

namespace Cake.Common.Tools.DotNet.Workload.Restore
{
/// <summary>
/// Contains settings used by <see cref="DotNetWorkloadRestorer" />.
/// </summary>
public sealed class DotNetWorkloadRestoreSettings : DotNetSettings
{
/// <summary>
/// Gets or sets the NuGet configuration file (nuget.config) to use.
/// </summary>
public FilePath ConfigFile { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to prevent restoring multiple projects in parallel.
/// </summary>
public bool DisableParallel { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to treat package source failures as warnings.
/// </summary>
public bool IgnoreFailedSources { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to allow prerelease workload manifests.
/// </summary>
public bool IncludePreviews { 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.
/// </summary>
public bool Interactive { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to do not cache packages and http requests.
/// </summary>
public bool NoCache { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to skip updating the workload manifests.
/// The workload manifests define what assets and versions need to be installed for each workload.
/// </summary>
public bool SkipManifestUpdate { get; set; }

/// <summary>
/// Gets or sets the URI of the NuGet package source to use.
/// This setting overrides all of the sources specified in the nuget.config files.
/// </summary>
public ICollection<string> Source { get; set; } = new List<string>();

/// <summary>
/// Gets or sets the temporary directory used to download and extract NuGet packages (must be secure).
/// </summary>
public DirectoryPath TempDir { get; set; }
}
}
Loading

0 comments on commit fee7a87

Please sign in to comment.