Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,6 @@ Copyright (c) .NET Foundation. All rights reserved.
<!-- Import workload targets -->
<Import Project="Microsoft.NET.Sdk.ImportWorkloads.targets" Condition="'$(MSBuildEnableWorkloadResolver)' == 'true'" />

<Import Project="$(MSBuildThisFileDirectory)Microsoft.NET.Windows.targets" Condition="'$(TargetPlatformIdentifier)' == 'Windows'" />

<!-- Import WindowsDesktop targets if necessary -->
<PropertyGroup Condition=" '$(ImportWindowsDesktopTargets)' == ''
and ('$(UseWpf)' == 'true' Or '$(UseWindowsForms)' == 'true') ">
<ImportWindowsDesktopTargets Condition=" '$(TargetFrameworkIdentifier)' == '.NETCoreApp'
and $([MSBuild]::VersionGreaterThanOrEquals($(_TargetFrameworkVersionWithoutV), '3.0'))
and '$(TargetPlatformIdentifier)' == 'Windows' ">true</ImportWindowsDesktopTargets>
<ImportWindowsDesktopTargets Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">true</ImportWindowsDesktopTargets>
</PropertyGroup>
<PropertyGroup Condition=" '$(ImportWindowsDesktopTargets)' == 'true'">
<AfterMicrosoftNETSdkTargets>$(AfterMicrosoftNETSdkTargets);$(MSBuildThisFileDirectory)../../Microsoft.NET.Sdk.WindowsDesktop/targets/Microsoft.NET.Sdk.WindowsDesktop.targets</AfterMicrosoftNETSdkTargets>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetPlatformIdentifier)' != ''">
<!-- Set TargetPlatformVersion if it isn't set. This is so that we have a valid TargetPlatformMoniker for Restore.
This avoids an issue where projects with multitargeted unknown platforms would fail to restore because the platforms
weren't included in the TargetPlatformMoniker for restore, obscuring the errors about unknown platforms or missing
workloads. -->
<TargetPlatformVersion Condition="'$(TargetPlatformVersion)' == ''">0.0</TargetPlatformVersion>

<!-- Default SupportedOSPlatformVersion to TargetPlatformVersion -->
<SupportedOSPlatformVersion Condition="'$(SupportedOSPlatformVersion)' == ''">$(TargetPlatformVersion)</SupportedOSPlatformVersion>
</PropertyGroup>

<!-- Checks for EOL frameworks -->
<Import Project="$(MSBuildThisFileDirectory)Microsoft.NET.EolTargetFrameworks.targets" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1063,5 +1063,36 @@ Copyright (c) .NET Foundation. All rights reserved.
<Import Project="$(MSBuildThisFileDirectory)Microsoft.NET.ILLink.targets" />
<Import Project="$(MSBuildThisFileDirectory)Microsoft.NET.Sdk.Analyzers.targets" Condition="'$(Language)' == 'C#' or '$(Language)' == 'VB'" />

<!-- Import WindowsDesktop targets if necessary -->
<PropertyGroup Condition=" '$(ImportWindowsDesktopTargets)' == ''
and ('$(UseWpf)' == 'true' Or '$(UseWindowsForms)' == 'true') ">
<ImportWindowsDesktopTargets Condition=" '$(TargetFrameworkIdentifier)' == '.NETCoreApp'
and $([MSBuild]::VersionGreaterThanOrEquals($(_TargetFrameworkVersionWithoutV), '3.0'))
and '$(TargetPlatformIdentifier)' == 'Windows' ">true</ImportWindowsDesktopTargets>
<ImportWindowsDesktopTargets Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">true</ImportWindowsDesktopTargets>
</PropertyGroup>
<PropertyGroup Condition=" '$(ImportWindowsDesktopTargets)' == 'true'">
<AfterMicrosoftNETSdkTargets>$(AfterMicrosoftNETSdkTargets);$(MSBuildThisFileDirectory)../../Microsoft.NET.Sdk.WindowsDesktop/targets/Microsoft.NET.Sdk.WindowsDesktop.targets</AfterMicrosoftNETSdkTargets>
</PropertyGroup>

<Import Project="$(MSBuildThisFileDirectory)Microsoft.NET.Windows.targets" Condition="'$(TargetPlatformIdentifier)' == 'Windows'" />

<!-- TargetPlatformMoniker and TargetPlatformDisplayName would normally be set in Microsoft.Common.CurrentVersion.targets. However, the
TargetPlatformVersion may have been set later than that in evaluation by platform-specific targets or Directory.Build.targets. So fix up those properties here -->
<PropertyGroup Condition="'$(TargetPlatformMoniker)' == '' and '$(TargetPlatformIdentifier)' != ''">
<!-- Set TargetPlatformVersion if it isn't set. This is so that we have a valid TargetPlatformMoniker for Restore.
This avoids an issue where projects with multitargeted unknown platforms would fail to restore because the platforms
weren't included in the TargetPlatformMoniker for restore, obscuring the errors about unknown platforms or missing
workloads. -->
<TargetPlatformVersion Condition="'$(TargetPlatformVersion)' == ''">0.0</TargetPlatformVersion>
<TargetPlatformMoniker>$(TargetPlatformIdentifier),Version=$(TargetPlatformVersion)</TargetPlatformMoniker>
<TargetPlatformDisplayName>$([Microsoft.Build.Utilities.ToolLocationHelper]::GetPlatformSDKDisplayName($(TargetPlatformIdentifier), $(TargetPlatformVersion)))</TargetPlatformDisplayName>
</PropertyGroup>

<!-- Default SupportedOSPlatformVersion to TargetPlatformVersion -->
<PropertyGroup Condition="'$(SupportedOSPlatformVersion)' == ''">
<SupportedOSPlatformVersion>$(TargetPlatformVersion)</SupportedOSPlatformVersion>
</PropertyGroup>

<Import Project="$(AfterMicrosoftNETSdkTargets)" Condition="'$(AfterMicrosoftNETSdkTargets)' != ''"/>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,120 @@ public void It_fails_if_target_platform_identifier_and_version_are_invalid()
.NotHaveStdOutContaining("NETSDK1140");
}

[WindowsOnlyFact]
public void UseWPFCanBeSetInDirectoryBuildTargets()
{
var testDir = _testAssetsManager.CreateTestDirectory();

var newCommand = new DotnetCommand(Log);
newCommand.WorkingDirectory = testDir.Path;

newCommand.Execute("new", "wpf", "--debug:ephemeral-hive").Should().Pass();

var projectPath = Path.Combine(testDir.Path, Path.GetFileName(testDir.Path) + ".csproj");

var project = XDocument.Load(projectPath);
var ns = project.Root.Name.Namespace;

project.Root.Element(ns + "PropertyGroup")
.Element(ns + "UseWPF")
.Remove();

project.Save(projectPath);

string DirectoryBuildTargetsContent = @"
<Project>
<PropertyGroup>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
";

File.WriteAllText(Path.Combine(testDir.Path, "Directory.Build.targets"), DirectoryBuildTargetsContent);

var buildCommand = new BuildCommand(Log, testDir.Path);

buildCommand.Execute()
.Should()
.Pass();
}

[WindowsOnlyFact]
public void TargetPlatformVersionCanBeSetInDirectoryBuildTargets()
{
var testProject = new TestProject()
{
TargetFrameworks = "net5.0-windows"
};

var testAsset = _testAssetsManager.CreateTestProject(testProject);

string targetPlatformVersion = "10.0.18362.0";

string DirectoryBuildTargetsContent = $@"
<Project>
<PropertyGroup>
<TargetPlatformVersion>{targetPlatformVersion}</TargetPlatformVersion>
</PropertyGroup>
</Project>
";

File.WriteAllText(Path.Combine(testAsset.TestRoot, "Directory.Build.targets"), DirectoryBuildTargetsContent);

var buildCommand = new BuildCommand(testAsset);
buildCommand.Execute()
.Should()
.Pass();

GetPropertyValue(testAsset, "SupportedOSPlatformVersion").Should().Be(targetPlatformVersion);
GetPropertyValue(testAsset, "TargetPlatformMinVersion").Should().Be(targetPlatformVersion);
GetPropertyValue(testAsset, "TargetPlatformMoniker").Should().Be($"Windows,Version={targetPlatformVersion}");
}

[WindowsOnlyFact]
public void SupportedOSPlatformVersionCanBeSetInDirectoryBuildTargets()
{
var testProject = new TestProject()
{
TargetFrameworks = "net5.0-windows10.0.19041.0"
};

var testAsset = _testAssetsManager.CreateTestProject(testProject);

string supportedOSPlatformVersion = "10.0.18362.0";

string DirectoryBuildTargetsContent = $@"
<Project>
<PropertyGroup>
<SupportedOSPlatformVersion>{supportedOSPlatformVersion}</SupportedOSPlatformVersion>
</PropertyGroup>
</Project>
";

File.WriteAllText(Path.Combine(testAsset.TestRoot, "Directory.Build.targets"), DirectoryBuildTargetsContent);

var buildCommand = new BuildCommand(testAsset);
buildCommand.Execute()
.Should()
.Pass();

GetPropertyValue(testAsset, "SupportedOSPlatformVersion").Should().Be(supportedOSPlatformVersion);
GetPropertyValue(testAsset, "TargetPlatformMinVersion").Should().Be(supportedOSPlatformVersion);
GetPropertyValue(testAsset, "TargetPlatformVersion").Should().Be("10.0.19041.0");
GetPropertyValue(testAsset, "TargetPlatformMoniker").Should().Be("Windows,Version=10.0.19041.0");
}


private string GetPropertyValue(TestAsset testAsset, string propertyName)
{
var getValueCommand = new GetValuesCommand(testAsset, propertyName);
getValueCommand.Execute()
.Should()
.Pass();

return getValueCommand.GetValues().Single();
}

[WindowsOnlyFact]
public void It_can_use_source_generators_with_wpf()
{
Expand Down