Skip to content

Commit

Permalink
Merge pull request #17512 from dsplaisted/workload-suggestions
Browse files Browse the repository at this point in the history
Generate suggestions for workloads to install
  • Loading branch information
dsplaisted authored May 17, 2021
2 parents a04de1d + 1a9a8cc commit 1beaa14
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/Tasks/Common/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,8 @@ The following are names of parameters or literal values and should not be transl
<comment>{StrBegin="NETSDK1146: "}</comment>
</data>
<data name="WorkloadNotInstalled" xml:space="preserve">
<value>NETSDK1147: The following workload packs were not installed: {0}</value>
<value>NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</value>
<comment>{StrBegin="NETSDK1147: "}</comment>
</data>
<data name="WindowsSDKVersionConflicts" xml:space="preserve">
Expand Down Expand Up @@ -806,5 +807,4 @@ The following are names of parameters or literal values and should not be transl
<value>NETSDK1173: The provided type library '{0}' is in an invalid format.</value>
<comment>{StrBegin="NETSDK1173: "}</comment>
</data>

</root>
6 changes: 4 additions & 2 deletions src/Tasks/Common/Resources/xlf/Strings.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,10 @@ The following are names of parameters or literal values and should not be transl
<note>{StrBegin="NETSDK1148: "}</note>
</trans-unit>
<trans-unit id="WorkloadNotInstalled">
<source>NETSDK1147: The following workload packs were not installed: {0}</source>
<target state="translated">NETSDK1147: Nebyly nainstalovány následující sady úloh: {0}.</target>
<source>NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</source>
<target state="new">NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</target>
<note>{StrBegin="NETSDK1147: "}</note>
</trans-unit>
</body>
Expand Down
6 changes: 4 additions & 2 deletions src/Tasks/Common/Resources/xlf/Strings.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,10 @@ The following are names of parameters or literal values and should not be transl
<note>{StrBegin="NETSDK1148: "}</note>
</trans-unit>
<trans-unit id="WorkloadNotInstalled">
<source>NETSDK1147: The following workload packs were not installed: {0}</source>
<target state="translated">NETSDK1147: Die folgenden Workloadpakete wurden nicht installiert: {0}</target>
<source>NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</source>
<target state="new">NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</target>
<note>{StrBegin="NETSDK1147: "}</note>
</trans-unit>
</body>
Expand Down
6 changes: 4 additions & 2 deletions src/Tasks/Common/Resources/xlf/Strings.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,10 @@ The following are names of parameters or literal values and should not be transl
<note>{StrBegin="NETSDK1148: "}</note>
</trans-unit>
<trans-unit id="WorkloadNotInstalled">
<source>NETSDK1147: The following workload packs were not installed: {0}</source>
<target state="new">NETSDK1147: The following workload packs were not installed: {0}</target>
<source>NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</source>
<target state="new">NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</target>
<note>{StrBegin="NETSDK1147: "}</note>
</trans-unit>
</body>
Expand Down
6 changes: 4 additions & 2 deletions src/Tasks/Common/Resources/xlf/Strings.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,10 @@ The following are names of parameters or literal values and should not be transl
<note>{StrBegin="NETSDK1148: "}</note>
</trans-unit>
<trans-unit id="WorkloadNotInstalled">
<source>NETSDK1147: The following workload packs were not installed: {0}</source>
<target state="translated">NETSDK1147: les packs de charges de travail suivants n'ont pas été installés : {0}</target>
<source>NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</source>
<target state="new">NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</target>
<note>{StrBegin="NETSDK1147: "}</note>
</trans-unit>
</body>
Expand Down
6 changes: 4 additions & 2 deletions src/Tasks/Common/Resources/xlf/Strings.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,10 @@ The following are names of parameters or literal values and should not be transl
<note>{StrBegin="NETSDK1148: "}</note>
</trans-unit>
<trans-unit id="WorkloadNotInstalled">
<source>NETSDK1147: The following workload packs were not installed: {0}</source>
<target state="translated">NETSDK1147: i pacchetti del carico di lavoro seguenti non sono stati installati: {0}</target>
<source>NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</source>
<target state="new">NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</target>
<note>{StrBegin="NETSDK1147: "}</note>
</trans-unit>
</body>
Expand Down
6 changes: 4 additions & 2 deletions src/Tasks/Common/Resources/xlf/Strings.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,10 @@ The following are names of parameters or literal values and should not be transl
<note>{StrBegin="NETSDK1148: "}</note>
</trans-unit>
<trans-unit id="WorkloadNotInstalled">
<source>NETSDK1147: The following workload packs were not installed: {0}</source>
<target state="translated">NETSDK1147: 次のワークロード パックはインストールされませんでした: {0}</target>
<source>NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</source>
<target state="new">NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</target>
<note>{StrBegin="NETSDK1147: "}</note>
</trans-unit>
</body>
Expand Down
6 changes: 4 additions & 2 deletions src/Tasks/Common/Resources/xlf/Strings.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,10 @@ The following are names of parameters or literal values and should not be transl
<note>{StrBegin="NETSDK1148: "}</note>
</trans-unit>
<trans-unit id="WorkloadNotInstalled">
<source>NETSDK1147: The following workload packs were not installed: {0}</source>
<target state="translated">NETSDK1147: 다음 워크로드 팩이 설치되지 않았습니다. {0}</target>
<source>NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</source>
<target state="new">NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</target>
<note>{StrBegin="NETSDK1147: "}</note>
</trans-unit>
</body>
Expand Down
6 changes: 4 additions & 2 deletions src/Tasks/Common/Resources/xlf/Strings.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,10 @@ The following are names of parameters or literal values and should not be transl
<note>{StrBegin="NETSDK1148: "}</note>
</trans-unit>
<trans-unit id="WorkloadNotInstalled">
<source>NETSDK1147: The following workload packs were not installed: {0}</source>
<target state="translated">NETSDK1147: Następujące pakiety obciążeń nie zostały zainstalowane: {0}</target>
<source>NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</source>
<target state="new">NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</target>
<note>{StrBegin="NETSDK1147: "}</note>
</trans-unit>
</body>
Expand Down
6 changes: 4 additions & 2 deletions src/Tasks/Common/Resources/xlf/Strings.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,10 @@ The following are names of parameters or literal values and should not be transl
<note>{StrBegin="NETSDK1148: "}</note>
</trans-unit>
<trans-unit id="WorkloadNotInstalled">
<source>NETSDK1147: The following workload packs were not installed: {0}</source>
<target state="translated">NETSDK1147: os seguintes pacotes de carga de trabalho não foram instalados: {0}</target>
<source>NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</source>
<target state="new">NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</target>
<note>{StrBegin="NETSDK1147: "}</note>
</trans-unit>
</body>
Expand Down
6 changes: 4 additions & 2 deletions src/Tasks/Common/Resources/xlf/Strings.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,10 @@ The following are names of parameters or literal values and should not be transl
<note>{StrBegin="NETSDK1148: "}</note>
</trans-unit>
<trans-unit id="WorkloadNotInstalled">
<source>NETSDK1147: The following workload packs were not installed: {0}</source>
<target state="translated">NETSDK1147: следующие пакеты рабочих нагрузок не были установлены: {0}</target>
<source>NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</source>
<target state="new">NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</target>
<note>{StrBegin="NETSDK1147: "}</note>
</trans-unit>
</body>
Expand Down
6 changes: 4 additions & 2 deletions src/Tasks/Common/Resources/xlf/Strings.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,10 @@ The following are names of parameters or literal values and should not be transl
<note>{StrBegin="NETSDK1148: "}</note>
</trans-unit>
<trans-unit id="WorkloadNotInstalled">
<source>NETSDK1147: The following workload packs were not installed: {0}</source>
<target state="translated">NETSDK1147: Şu iş yükü paketleri yüklenmedi: {0}</target>
<source>NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</source>
<target state="new">NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</target>
<note>{StrBegin="NETSDK1147: "}</note>
</trans-unit>
</body>
Expand Down
6 changes: 4 additions & 2 deletions src/Tasks/Common/Resources/xlf/Strings.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,10 @@ The following are names of parameters or literal values and should not be transl
<note>{StrBegin="NETSDK1148: "}</note>
</trans-unit>
<trans-unit id="WorkloadNotInstalled">
<source>NETSDK1147: The following workload packs were not installed: {0}</source>
<target state="translated">NETSDK1147: 未安装以下工作负载包: {0}</target>
<source>NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</source>
<target state="new">NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</target>
<note>{StrBegin="NETSDK1147: "}</note>
</trans-unit>
</body>
Expand Down
6 changes: 4 additions & 2 deletions src/Tasks/Common/Resources/xlf/Strings.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,10 @@ The following are names of parameters or literal values and should not be transl
<note>{StrBegin="NETSDK1148: "}</note>
</trans-unit>
<trans-unit id="WorkloadNotInstalled">
<source>NETSDK1147: The following workload packs were not installed: {0}</source>
<target state="translated">NETSDK1147: 未安裝下列工作負載套件: {0}</target>
<source>NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</source>
<target state="new">NETSDK1147: To build this project, the following workloads must be installed: {0}
To install these workloads, run the following command: {1}</target>
<note>{StrBegin="NETSDK1147: "}</note>
</trans-unit>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Description>The MSBuild targets and properties for building .NET Core projects.</Description>
<OutputType>Library</OutputType>
<TargetFrameworks>$(SdkTargetFramework);net472</TargetFrameworks>
<Nullable>annotations</Nullable>
</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -72,10 +73,13 @@
<ItemGroup>
<Compile Include="**\*.cs" />
<Compile Include="..\Common\**\*.cs" LinkBase="Common" />
<Compile Include="..\..\Resolvers\Microsoft.NET.Sdk.WorkloadManifestReader\**\*.cs" LinkBase="WorkloadManifestReader" />
<Compile Include="..\..\Resolvers\Microsoft.DotNet.MSBuildSdkResolver\FXVersion.cs" LinkBase="WorkloadManifestReader"/>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="..\Common\Resources\Strings.resx" LinkBase="Resources" GenerateSource="True" Namespace="Microsoft.NET.Build.Tasks" />
<EmbeddedResource Include="..\..\Resolvers\Microsoft.NET.Sdk.WorkloadManifestReader\Strings.resx" LinkBase="Resources\WorkloadManifestReoder" GenerateSource="True" Namespace="Microsoft.NET.Sdk.Localization"/>
</ItemGroup>

<ItemGroup>
Expand Down
41 changes: 36 additions & 5 deletions src/Tasks/Microsoft.NET.Build.Tasks/ShowMissingWorkloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,55 @@
using System.Text;
using System.Threading.Tasks;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.NET.Sdk.WorkloadManifestReader;

namespace Microsoft.NET.Build.Tasks
{
public class ShowMissingWorkloads : TaskBase
{
public ITaskItem[] MissingWorkloadPacks { get; set; }

public string NetCoreRoot { get; set; }

public string NETCoreSdkVersion { get; set; }

public bool GenerateErrorsForMissingWorkloads { get; set; }

[Output]
public ITaskItem[] SuggestedWorkloads { get; set; }

protected override void ExecuteCore()
{
if (MissingWorkloadPacks.Any())
{
// TODO: Once IWorkloadResolver.GetWorkloadSuggestionForMissingPacks is implemented, switch to using that to report recommended workloads to install,
// instead of reporting which workload packs are missing
var errorMessage = string.Format(CultureInfo.CurrentCulture,
Strings.WorkloadNotInstalled, string.Join(" ", MissingWorkloadPacks.Select(p => p.ItemSpec)));
var workloadManifestProvider = new SdkDirectoryWorkloadManifestProvider(NetCoreRoot, NETCoreSdkVersion);
var workloadResolver = WorkloadResolver.Create(workloadManifestProvider, NetCoreRoot, NETCoreSdkVersion);

var suggestedWorkloads = workloadResolver.GetWorkloadSuggestionForMissingPacks(MissingWorkloadPacks.Select(item => item.ItemSpec).ToList());

if (GenerateErrorsForMissingWorkloads)
{
var suggestedInstallCommand = "dotnet workload install " + string.Join(" ", suggestedWorkloads.Select(w => w.Id));

Log.LogError(errorMessage);
var errorMessage = string.Format(CultureInfo.CurrentCulture,
Strings.WorkloadNotInstalled, string.Join(" ", suggestedWorkloads.Select(w => w.Id)), suggestedInstallCommand);

Log.LogError(errorMessage);
}

SuggestedWorkloads = suggestedWorkloads.Select(suggestedWorkload =>
{
var taskItem = new TaskItem(suggestedWorkload.Id);
taskItem.SetMetadata("VisualStudioComponentId", ToSafeId(suggestedWorkload.Id));
return taskItem;
}).ToArray();
}
}

internal static string ToSafeId(string id)
{
return id.Replace("-", ".").Replace(" ", ".").Replace("_", ".");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@ Copyright (c) .NET Foundation. All rights reserved.

<UsingTask TaskName="ShowMissingWorkloads" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />

<Target Name="GetSuggestedWorkloads" Returns="@(SuggestedWorkload)">
<ShowMissingWorkloads MissingWorkloadPacks="@(MissingWorkloadPack)"
NetCoreRoot="$(NetCoreRoot)"
NETCoreSdkVersion="$(NETCoreSdkVersion)"
GenerateErrorsForMissingWorkloads="false">
<Output TaskParameter="SuggestedWorkloads" ItemName="SuggestedWorkload" />
</ShowMissingWorkloads>
</Target>

<Target Name="_CheckForMissingWorkload"
BeforeTargets="_CheckForInvalidConfigurationAndPlatform;_CheckForUnsupportedTargetPlatformIdentifier"
Condition="'@(MissingWorkloadPack)' != ''">

<ShowMissingWorkloads MissingWorkloadPacks="@(MissingWorkloadPack)"/>

<ShowMissingWorkloads MissingWorkloadPacks="@(MissingWorkloadPack)"
NetCoreRoot="$(NetCoreRoot)"
NETCoreSdkVersion="$(NETCoreSdkVersion)"
GenerateErrorsForMissingWorkloads="true"/>
</Target>
</Project>
65 changes: 61 additions & 4 deletions src/Tests/Microsoft.NET.Build.Tests/WorkloadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,84 @@ public void It_should_fail_without_workload()
}

[Fact]
public void It_should_fail_without_workload_when_multitargeted()
public void It_should_create_suggested_workload_items()
{
var testProject = new TestProject()
{
Name = "WorkloadTest",
TargetFrameworks = "net5.0;net5.0-missingworkloadtestplatform"
TargetFrameworks = "net5.0-missingworkloadtestplatform"
};

var testAsset = _testAssetsManager
.CreateTestProject(testProject);

new BuildCommand(testAsset)
var getValuesCommand = new GetValuesCommand(testAsset, "SuggestedWorkload", GetValuesCommand.ValueType.Item);
getValuesCommand.DependsOnTargets = "GetSuggestedWorkloads";
getValuesCommand.MetadataNames.Add("VisualStudioComponentId");
getValuesCommand.ShouldRestore = false;

getValuesCommand.Execute()
.Should()
.Pass();

getValuesCommand.GetValuesWithMetadata().Select(valueAndMetadata => (valueAndMetadata.value, valueAndMetadata.metadata["VisualStudioComponentId"]))
.Should()
.BeEquivalentTo(("microsoft-net-sdk-missingtestworkload", "microsoft.net.sdk.missingtestworkload"));
}

[Fact]
public void It_should_fail_to_restore_without_workload_when_multitargeted()
{
var testProject = new TestProject()
{
Name = "WorkloadTest",
TargetFrameworks = "net5.0-android;net5.0-ios"
};

var testAsset = _testAssetsManager
.CreateTestProject(testProject);

new RestoreCommand(testAsset)
.Execute()
.Should()
.Fail()
.And
.HaveStdOutContaining("NETSDK1147");

// Until https://github.com/NuGet/Home/issues/10872 is fixed, only one of the errors will be reported when restoring
// Once that is fixed we should add the following checks:
// .And
// .HaveStdOutContaining("ios")
// .And
// .HaveStdOutContaining("android");
}

[Fact]
public void It_should_fail_to_build_without_workload_when_multitargeted()
{
var testProject = new TestProject()
{
Name = "WorkloadTest",
TargetFrameworks = "net5.0-android;net5.0-ios"
};

var testAsset = _testAssetsManager
.CreateTestProject(testProject);

new BuildCommand(testAsset)
.ExecuteWithoutRestore()
.Should()
.Fail()
.And
.HaveStdOutContaining("NETSDK1147")
.And
.HaveStdOutContaining("ios")
.And
.HaveStdOutContaining("android");
}

[Fact]
public void It_should_fail_when_multitargeted_to_unknown_platforms()
public void It_should_fail_to_build_when_multitargeted_to_unknown_platforms()
{
var testProject = new TestProject()
{
Expand Down

0 comments on commit 1beaa14

Please sign in to comment.