diff --git a/src/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk/src/AddRuntimeOSPropertyTask.cs b/src/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk/src/AddRuntimeOSPropertyTask.cs new file mode 100644 index 00000000000..c1fd9efbd4c --- /dev/null +++ b/src/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk/src/AddRuntimeOSPropertyTask.cs @@ -0,0 +1,46 @@ +// 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 Microsoft.Build.Construction; +using Microsoft.Build.Framework; + +namespace Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk +{ + public class GenerateRuntimeOSPropsFile : BuildTask + { + public const string RuntimeOSProperty = "RuntimeOS"; + + [Required] + public string RuntimePropsFilePath {get ; set; } + + public override bool Execute() + { + ProjectRootElement project = ProjectRootElement.Create(); + CreateRuntimeIdentifier(project); + project.Save(RuntimePropsFilePath); + return !Log.HasLoggedErrors; + } + + public void CreateRuntimeIdentifier(ProjectRootElement project) + { + var rid = PlatformAbstractions.RuntimeEnvironment.GetRuntimeIdentifier(); + string[] ridParts = rid.Split('-'); + + if (ridParts.Length < 1) + { + throw new System.InvalidOperationException($"Unknown rid format {rid}."); + } + + string osNameAndVersion = ridParts[0]; + + var propertyGroup = project.CreatePropertyGroupElement(); + project.AppendChild(propertyGroup); + + var runtimeProperty = propertyGroup.AddProperty(RuntimeOSProperty, $"{osNameAndVersion}"); + runtimeProperty.Condition = $"'$({RuntimeOSProperty})' == ''"; + + Log.LogMessage($"Running on OS with RID {rid}, so defaulting RuntimeOS to '{osNameAndVersion}'"); + } + } +} diff --git a/src/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk/src/build/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk.targets b/src/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk/src/build/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk.targets index ff961d705d3..60226868739 100644 --- a/src/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk/src/build/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk.targets +++ b/src/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk/src/build/Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk.targets @@ -3,6 +3,7 @@ + <_OriginalTargetFrameworks>$(TargetFrameworks) @@ -116,4 +117,9 @@ @(PackageTargetFrameworksFinalList) + + + + +