Skip to content

Commit

Permalink
Add alias for dotnet workload search command
Browse files Browse the repository at this point in the history
  • Loading branch information
Marusyk authored and devlead committed Apr 3, 2022
1 parent ade1893 commit 4cb0a7a
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 2 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.Search;

namespace Cake.Common.Tests.Fixtures.Tools.DotNet.Workload.Search
{
internal sealed class DotNetWorkloadSearcherFixture : DotNetFixture<DotNetWorkloadSearchSettings>
{
public string SearchString { get; set; }

protected override void RunTool()
{
var tool = new DotNetWorkloadSearcher(FileSystem, Environment, ProcessRunner, Tools);
tool.Search(SearchString);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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.Search;
using Cake.Testing;
using Xunit;

namespace Cake.Common.Tests.Unit.Tools.DotNet.Workload.Search
{
public sealed class DotNetWorkloadSearchTests
{
public sealed class TheWorkloadSearchMethod
{
[Fact]
public void Should_Throw_If_Process_Was_Not_Started()
{
// Given
var fixture = new DotNetWorkloadSearcherFixture();
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 DotNetWorkloadSearcherFixture();
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_Add_SearchString_Argument()
{
// Given
var fixture = new DotNetWorkloadSearcherFixture();
fixture.SearchString = "maui";

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

// Then
Assert.Equal("workload search maui", result.Args);
}
}
}
}
42 changes: 42 additions & 0 deletions src/Cake.Common/Tools/DotNet/DotNetAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Cake.Common.Tools.DotNet.Test;
using Cake.Common.Tools.DotNet.Tool;
using Cake.Common.Tools.DotNet.VSTest;
using Cake.Common.Tools.DotNet.Workload.Search;
using Cake.Common.Tools.DotNetCore.Build;
using Cake.Common.Tools.DotNetCore.BuildServer;
using Cake.Common.Tools.DotNetCore.Clean;
Expand Down Expand Up @@ -1845,5 +1846,46 @@ public static void DotNetSDKCheck(this ICakeContext context)
var checker = new DotNetSDKChecker(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
checker.Check();
}

/// <summary>
/// Lists available workloads.
/// </summary>
/// <param name="context">The context.</param>
/// <example>
/// <code>
/// DotNetWorkloadSearch();
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Workload")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Workload.Search")]
public static void DotNetWorkloadSearch(this ICakeContext context)
{
context.DotNetWorkloadSearch(null);
}

/// <summary>
/// Lists available workloads by specifying all or part of the workload ID.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="searchString">The workload ID to search for, or part of it.</param>
/// <example>
/// <code>
/// DotNetWorkloadSearch("maui");
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("Workload")]
[CakeNamespaceImport("Cake.Common.Tools.DotNet.Workload.Search")]
public static void DotNetWorkloadSearch(this ICakeContext context, string searchString)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

var searcher = new DotNetWorkloadSearcher(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
searcher.Search(searchString);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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.Workload.Search
{
/// <summary>
/// Contains settings used by <see cref="DotNetWorkloadSearcher" />.
/// </summary>
public sealed class DotNetWorkloadSearchSettings : DotNetSettings
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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.Core;
using Cake.Core.IO;
using Cake.Core.Tooling;

namespace Cake.Common.Tools.DotNet.Workload.Search
{
/// <summary>
/// .NET workloads searcher.
/// </summary>
public sealed class DotNetWorkloadSearcher : DotNetTool<DotNetWorkloadSearchSettings>
{
/// <summary>
/// Initializes a new instance of the <see cref="DotNetWorkloadSearcher" /> 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 DotNetWorkloadSearcher(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools) : base(fileSystem, environment, processRunner, tools)
{
}

/// <summary>
/// Lists the latest available version of the .NET SDK and .NET Runtime, for each feature band.
/// </summary>
/// <param name="searchString">The workload ID to search for, or part of it.</param>
public void Search(string searchString)
{
var settings = new DotNetWorkloadSearchSettings();
RunCommand(settings, GetArguments(searchString, settings));
}

private ProcessArgumentBuilder GetArguments(string searchString, DotNetWorkloadSearchSettings settings)
{
var builder = CreateArgumentBuilder(settings);

builder.Append("workload search");

if (!string.IsNullOrEmpty(searchString))
{
builder.Append(searchString);
}

return builder;
}
}
}
16 changes: 14 additions & 2 deletions tests/integration/Cake.Common/Tools/DotNet/DotNetAliases.cake
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetFormat")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.Setup")
.Does(() =>
{
// Given
// Given
var path = Paths.Temp.Combine("./Cake.Common/Tools/DotNet");
var project = path.CombineWithFilePath("hwapp/hwapp.csproj");

Expand All @@ -259,7 +259,18 @@ Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetSDKCheck")
.Does(() =>
{
// When
DotNetSDKCheck();
DotNetSDKCheck();
});

Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetWorkloadSearch")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.Setup")
.Does(() =>
{
// Given
var searchString = "maui";

// When
DotNetWorkloadSearch(searchString);
});

Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetBuildServerShutdown")
Expand All @@ -279,6 +290,7 @@ Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetBuildServerShutdown")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetTest.Fail")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetFormat")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetSDKCheck")
.IsDependentOn("Cake.Common.Tools.DotNet.DotNetAliases.DotNetWorkloadSearch")
.Does(() =>
{
// When
Expand Down

0 comments on commit 4cb0a7a

Please sign in to comment.