Skip to content

Commit

Permalink
Merge pull request #3952 from Marusyk/rmarusyk/3487
Browse files Browse the repository at this point in the history
Add alias for dotnet workload update command
  • Loading branch information
devlead authored Oct 9, 2022
2 parents 82cdbad + 3a20edb commit f455742
Show file tree
Hide file tree
Showing 6 changed files with 354 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.Update;

namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Workload.Update
{
internal sealed class DotNetWorkloadUpdaterFixture : DotNetFixture<DotNetWorkloadUpdateSettings>
{
protected override void RunTool()
{
var tool = new DotNetWorkloadUpdater(FileSystem, Environment, ProcessRunner, Tools);
tool.Update(Settings);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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.Update;
using Cake.Common.Tools.DotNet;
using Cake.Testing;
using Xunit;

namespace Cake.Common.Tests.Unit.Tools.DotNet.Workload.Update
{
public sealed class DotNetWorkloadUpdateTests
{
public sealed class TheWorkloadUpdateMethod
{
[Fact]
public void Should_Throw_If_Process_Was_Not_Started()
{
// Given
var fixture = new DotNetWorkloadUpdaterFixture();
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 DotNetWorkloadUpdaterFixture();
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 DotNetWorkloadUpdaterFixture();
fixture.Settings = null;
fixture.GivenDefaultToolDoNotExist();

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

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

[Fact]
public void Should_Add_Additional_Arguments()
{
// Given
var fixture = new DotNetWorkloadUpdaterFixture();
fixture.Settings.AdvertisingManifestsOnly = true;
fixture.Settings.ConfigFile = "./nuget.config";
fixture.Settings.DisableParallel = true;
fixture.Settings.FromPreviousSdk = true;
fixture.Settings.IgnoreFailedSources = true;
fixture.Settings.IncludePreviews = true;
fixture.Settings.Interactive = true;
fixture.Settings.NoCache = 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 update --advertising-manifests-only --configfile \"/Working/nuget.config\" --disable-parallel --from-previous-sdk --ignore-failed-sources --include-previews --interactive --no-cache --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);
}
}
}
}
53 changes: 53 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using Cake.Common.Tools.DotNet.Workload.Repair;
using Cake.Common.Tools.DotNet.Workload.Search;
using Cake.Common.Tools.DotNet.Workload.Uninstall;
using Cake.Common.Tools.DotNet.Workload.Update;
using Cake.Common.Tools.DotNetCore.Build;
using Cake.Common.Tools.DotNetCore.BuildServer;
using Cake.Common.Tools.DotNetCore.Clean;
Expand Down Expand Up @@ -2192,5 +2193,57 @@ public static void DotNetWorkloadRepair(this ICakeContext context, DotNetWorkloa
var repairer = new DotNetWorkloadRepairer(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
repairer.Repair(settings);
}

/// <summary>
/// Updates all installed workloads to the newest available version.
/// </summary>
/// <param name="context">The context.</param>
/// <example>
/// <code>
/// DotNetWorkloadUpdate();
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Workload")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Workload.Update")]
public static void DotNetWorkloadUpdate(this ICakeContext context)
{
context.DotNetWorkloadUpdate(null);
}

/// <summary>
/// Updates all installed workloads to the newest available version.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
/// var settings = new DotNetWorkloadUpdateSettings
/// {
/// IncludePreviews = true,
/// NoCache = true
/// };
///
/// DotNetWorkloadUpdate(settings);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Workload")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Workload.Update")]
public static void DotNetWorkloadUpdate(this ICakeContext context, DotNetWorkloadUpdateSettings settings)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

if (settings == null)
{
settings = new DotNetWorkloadUpdateSettings();
}

var updater = new DotNetWorkloadUpdater(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
updater.Update(settings);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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.Update
{
/// <summary>
/// Contains settings used by <see cref="DotNetWorkloadUpdater" />.
/// </summary>
public sealed class DotNetWorkloadUpdateSettings : DotNetSettings
{
/// <summary>
/// Gets or sets a value indicating whether to downloads advertising manifests but doesn't update any workloads.
/// </summary>
public bool AdvertisingManifestsOnly { get; set; }

/// <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 include workloads installed with previous SDK versions in the update.
/// </summary>
public bool FromPreviousSdk { 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 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; }
}
}
122 changes: 122 additions & 0 deletions src/Cake.Common/Tools/DotNet/Workload/Update/DotNetWorkloadUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// 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 System.Linq;
using Cake.Core;
using Cake.Core.IO;
using Cake.Core.Tooling;

namespace Cake.Common.Tools.DotNet.Workload.Update
{
/// <summary>
/// .NET workloads updater.
/// </summary>
public sealed class DotNetWorkloadUpdater : DotNetTool<DotNetWorkloadUpdateSettings>
{
private readonly ICakeEnvironment _environment;

/// <summary>
/// Initializes a new instance of the <see cref="DotNetWorkloadUpdater" /> class.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="environment">The environment.</param>
/// <param name="processRunner">The process runner.</param>
/// <param name="tools">The tool locator.</param>
public DotNetWorkloadUpdater(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools) : base(fileSystem, environment, processRunner, tools)
{
_environment = environment;
}

/// <summary>
/// Updates all installed workloads to the newest available version.
/// </summary>
/// <param name="settings">The settings.</param>
public void Update(DotNetWorkloadUpdateSettings settings)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

RunCommand(settings, GetArguments(settings));
}

private ProcessArgumentBuilder GetArguments(DotNetWorkloadUpdateSettings settings)
{
var builder = CreateArgumentBuilder(settings);

builder.Append("workload update");

// Advertising Manifests Only
if (settings.AdvertisingManifestsOnly)
{
builder.Append("--advertising-manifests-only");
}

// Config File
if (settings.ConfigFile != null)
{
builder.AppendSwitchQuoted("--configfile", settings.ConfigFile.MakeAbsolute(_environment).FullPath);
}

// Disable Parallel
if (settings.DisableParallel)
{
builder.Append("--disable-parallel");
}

// From Previous SDK
if (settings.FromPreviousSdk)
{
builder.Append("--from-previous-sdk");
}

// Ignore Failed Sources
if (settings.IgnoreFailedSources)
{
builder.Append("--ignore-failed-sources");
}

// Include Previews
if (settings.IncludePreviews)
{
builder.Append("--include-previews");
}

// Interactive
if (settings.Interactive)
{
builder.Append("--interactive");
}

// No Cache
if (settings.NoCache)
{
builder.Append("--no-cache");
}

// Source
if (settings.Source != null && settings.Source.Any())
{
foreach (var source in settings.Source)
{
builder.AppendSwitchQuoted("--source", source);
}
}

// Temp Dir
if (settings.TempDir != null)
{
builder.AppendSwitchQuoted("--temp-dir", settings.TempDir.MakeAbsolute(_environment).FullPath);
}

return builder;
}
}
}
9 changes: 9 additions & 0 deletions tests/integration/Cake.Common/Tools/DotNet/DotNetAliases.cake
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,14 @@ Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetWorkloadRepair")
DotNetWorkloadRepair();
});

Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetWorkloadUpdate")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.Setup")
.Does(() =>
{
// When
DotNetWorkloadUpdate();
});

Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetBuildServerShutdown")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetRestore")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetBuild")
Expand All @@ -307,6 +315,7 @@ Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetBuildServerShutdown")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetSDKCheck")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetWorkloadSearch")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetWorkloadRepair")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetWorkloadUpdate")
.Does(() =>
{
// When
Expand Down

0 comments on commit f455742

Please sign in to comment.