Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #377 from twsouthwick/ImportWindowsDesktopTargets
Browse files Browse the repository at this point in the history
Add ImportWindowsDesktopTargets=true for desktop .NET Framework apps
  • Loading branch information
cartermp authored Feb 18, 2021
2 parents 3118d1e + eaed0db commit 93294d4
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/MSBuild.Abstractions/MSBuildConversionWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public ImmutableDictionary<string, ImmutableDictionary<string, string>> Determin
private void RemoveTargetsNotLoadableByNETSDKMSBuild(string path)
{
var projectFile = File.ReadAllText(path);
if (projectFile is { Length:>0 })
if (projectFile is { Length: > 0 })
{
var replacement =
projectFile
Expand Down Expand Up @@ -178,6 +178,11 @@ private bool TryCreateSdkBaselineProject(string projectFilePath, IProject projec
{
MSBuildHelpers.AddUseWinForms(propGroup);
}

if (MSBuildHelpers.HasWPFOrWinForms(propGroup) && tfm.ContainsIgnoreCase(MSBuildFacts.Net5))
{
MSBuildHelpers.AddImportWindowsDesktopTargets(propGroup);
}
}

// Create a new collection because a project with this name has already been loaded into the global collection.
Expand Down
6 changes: 6 additions & 0 deletions src/MSBuild.Abstractions/MSBuildHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ public static ProjectItemElement GetPackagesConfigItem(ProjectItemGroupElement p
/// </summary>
public static void AddUseWPF(ProjectPropertyGroupElement propGroup) => propGroup.AddProperty(DesktopFacts.UseWPFPropertyName, "true");

/// <summary>
/// Adds the ImportWindowsDesktopTargets=true property to ensure builds targeting .NET Framework will succeed.
/// </summary>
/// <param name="propGroup"></param>
public static void AddImportWindowsDesktopTargets(ProjectPropertyGroupElement propGroup) => propGroup.AddProperty(DesktopFacts.ImportWindowsDesktopTargetsName, "true");

/// <summary>
/// Finds the property group with the TFM specified, which is normally the top-level property group.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/MSBuild.Conversion.Facts/DesktopFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static class DesktopFacts
public const string WinSDKAttribute = "Microsoft.NET.Sdk.WindowsDesktop";
public const string UseWPFPropertyName = "UseWPF";
public const string UseWinFormsPropertyName = "UseWindowsForms";
public const string ImportWindowsDesktopTargetsName = "ImportWindowsDesktopTargets";
public const string DesignerSuffix = ".Designer.cs";
public const string XamlFileExtension = ".xaml";
public const string SettingsDesignerFileName = "Settings.Designer.cs";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,13 @@ public static IProjectRootElement AddDesktopProperties(this IProjectRootElement
MSBuildHelpers.AddUseWPF(propGroup);
}

if (!baselineProject.GlobalProperties.Contains(DesktopFacts.ImportWindowsDesktopTargetsName, StringComparer.OrdinalIgnoreCase)
&& !projectRootElement.Sdk.Equals(DesktopFacts.WinSDKAttribute)
&& (MSBuildHelpers.IsWPF(projectRootElement) || MSBuildHelpers.IsWinForms(projectRootElement)))
{
MSBuildHelpers.AddImportWindowsDesktopTargets(propGroup);
}

return projectRootElement;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<OutputType>WinExe</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<UseWindowsForms>true</UseWindowsForms>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<OutputType>WinExe</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<UseWPF>true</UseWPF>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 93294d4

Please sign in to comment.