Skip to content

Commit bc7266f

Browse files
authored
Support winrt consumption using CsWinRT reference (#1243)
* Support winrt consumption using CsWinRT reference * add changes from try-convert repo * Move changes from public interface to extension method * Address feedback
1 parent eb94ca7 commit bc7266f

13 files changed

Lines changed: 106 additions & 5 deletions

File tree

src/extensions/try-convert/MSBuild.Abstractions/MSBuildConversionWorkspace.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ private bool IsSupportedOutputType(ProjectOutputType type) =>
249249
ProjectOutputType.Library => true,
250250
ProjectOutputType.WinExe => true,
251251
ProjectOutputType.AppContainerExe => true,
252+
ProjectOutputType.WinMdObj => true,
252253
_ => false
253254
};
254255

@@ -295,6 +296,10 @@ private ProjectOutputType GetProjectOutputType(IProjectRootElement root, Project
295296
{
296297
return ProjectOutputType.AppContainerExe;
297298
}
299+
else if (ProjectPropertyHelpers.IsWinMdObjProjectType(outputTypeNode))
300+
{
301+
return ProjectOutputType.WinMdObj;
302+
}
298303
else
299304
{
300305
return ProjectOutputType.Other;

src/extensions/try-convert/MSBuild.Abstractions/ProjectOutputType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public enum ProjectOutputType
1616
Exe,
1717
WinExe,
1818
AppContainerExe,
19+
WinMdObj,
1920
Other,
2021
None
2122
}

src/extensions/try-convert/MSBuild.Abstractions/ProjectPropertyHelpers.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ public static bool IsWinExeOutputType(ProjectPropertyElement prop) =>
188188
prop.ElementName.Equals(MSBuildFacts.OutputTypeNodeName, StringComparison.OrdinalIgnoreCase)
189189
&& prop.Value.Equals(MSBuildFacts.WinExeOutputType, StringComparison.OrdinalIgnoreCase);
190190

191+
/// <summary>
192+
/// Checks if an OutputType node is WinMdObj.
193+
/// </summary>
194+
public static bool IsWinMdObjProjectType(ProjectPropertyElement prop) =>
195+
prop.ElementName.Equals(MSBuildFacts.OutputTypeNodeName, StringComparison.OrdinalIgnoreCase)
196+
&& prop.Value.Equals(MSBuildFacts.WinMdObjOutputType, StringComparison.OrdinalIgnoreCase);
197+
191198
public static bool IsVisualBasicProject(ProjectPropertyElement prop) =>
192199
IsProjectTypeGuidsNode(prop) && prop.Value.Split(';').Any(guidString => Guid.Parse(guidString) == MSBuildFacts.LanguageProjectTypeVisualBasic);
193200

src/extensions/try-convert/MSBuild.Conversion.Facts/MSBuildFacts.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ public static class MSBuildFacts
288288
public const string ExeOutputType = "Exe";
289289
public const string WinExeOutputType = "WinExe";
290290
public const string AppContainerExeOutputType = "AppContainerExe";
291+
public const string WinMdObjOutputType = "winmdobj";
291292
public const string NuGetPackageImportStampNodeName = "NuGetPackageImportStamp";
292293
public const string ReferencePathNodeName = "ReferencePath";
293294
public const string LegacyTargetFrameworkPropertyNodeName = "TargetFrameworkIdentifier";
@@ -311,5 +312,7 @@ public static class MSBuildFacts
311312
public const string TargetPlatformIdentifierNodeName = "TargetPlatformIdentifier";
312313
public const string UapValue = "UAP";
313314
public const string TargetPlatformVersionNodeName = "TargetPlatformVersion";
315+
public const string CsWinRTComponentName = "CsWinRTComponent";
316+
public static readonly (string Name, string Version) CsWinRTPackageReference = (Name: "Microsoft.Windows.CsWinRT", Version: "1.6.4");
314317
}
315318
}

src/extensions/try-convert/MSBuild.Conversion.Project/Converter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public void Convert(string outputPath)
4646

4747
// Now we can convert the project over
4848
.ChangeImportsAndAddSdkAttribute(_sdkBaselineProject, _forceRemoveCustomImports)
49+
.AddCsWinRTReferenceAndComponentProperty(_sdkBaselineProject)
4950
.UpdateOutputTypeProperty(_sdkBaselineProject)
5051
.RemoveDefaultedProperties(_sdkBaselineProject, _differs)
5152
.RemoveUnnecessaryPropertiesNotInSDKByDefault(_sdkBaselineProject.ProjectStyle)

src/extensions/try-convert/MSBuild.Conversion.Project/ProjectRootElementExtensionsForConversion.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ public static IProjectRootElement ChangeImportsAndAddSdkAttribute(this IProjectR
6868
return projectRootElement;
6969
}
7070

71+
public static IProjectRootElement AddCsWinRTReferenceAndComponentProperty(this IProjectRootElement projectRootElement, BaselineProject baselineProject)
72+
{
73+
if (baselineProject.OutputType == ProjectOutputType.WinMdObj)
74+
{
75+
var topLevelPropGroup = MSBuildHelpers.GetOrCreateTopLevelPropertyGroup(baselineProject, projectRootElement);
76+
topLevelPropGroup.AddProperty(MSBuildFacts.CsWinRTComponentName, "true");
77+
78+
var packageReferenceItemGroup = projectRootElement.ItemGroups.Where(ig => ig.Items.Any(i => i.ItemType == MSBuildFacts.MSBuildPackageReferenceName))
79+
.FirstOrDefault() ?? projectRootElement.AddItemGroup();
80+
AddPackageReferenceElement(packageReferenceItemGroup, MSBuildFacts.CsWinRTPackageReference.Name, MSBuildFacts.CsWinRTPackageReference.Version);
81+
}
82+
83+
return projectRootElement;
84+
}
85+
7186
public static IProjectRootElement UpdateOutputTypeProperty(this IProjectRootElement projectRootElement, BaselineProject baselineProject)
7287
{
7388
var outputTypeNode = projectRootElement.GetOutputTypeNode();
@@ -79,6 +94,7 @@ public static IProjectRootElement UpdateOutputTypeProperty(this IProjectRootElem
7994
ProjectOutputType.Library => MSBuildFacts.LibraryOutputType,
8095
ProjectOutputType.WinExe => MSBuildFacts.WinExeOutputType,
8196
ProjectOutputType.AppContainerExe => MSBuildFacts.WinExeOutputType,
97+
ProjectOutputType.WinMdObj => MSBuildFacts.LibraryOutputType,
8298
_ => throw new InvalidOperationException("Unsupported output type: " + baselineProject.OutputType)
8399
};
84100
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
9+
namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Windows.UWPtoWinAppSDKUpgrade.Utils
10+
{
11+
public static class IProjectExtensions
12+
{
13+
public static IEnumerable<string> AllProjectReferences(this IProject project) => project.GetRoslynProject().AllProjectReferences.Select(projRef => projRef.ProjectId.ToString());
14+
}
15+
}

src/extensions/windows/Microsoft.DotNet.UpgradeAssistant.Extensions.Windows/UWPtoWinAppSDKUpgrade/UWPToWinUIHelpers.cs renamed to src/extensions/windows/Microsoft.DotNet.UpgradeAssistant.Extensions.Windows/UWPtoWinAppSDKUpgrade/Utils/SyntaxNodeExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
using Microsoft.CodeAnalysis.CSharp.Syntax;
1010
using Microsoft.CodeAnalysis.Diagnostics;
1111

12-
namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Windows.UWPtoWinAppSDKUpgrade
12+
namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Windows.UWPtoWinAppSDKUpgrade.Utils
1313
{
14-
internal static class UWPToWinUIHelpers
14+
internal static class SyntaxNodeExtensions
1515
{
1616
public static IEnumerable<string> GetAllImportedNamespaces(this SyntaxNode node)
1717
{

src/extensions/windows/Microsoft.DotNet.UpgradeAssistant.Extensions.Windows/UWPtoWinAppSDKUpgrade/WinUIAppWIndowAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
using Microsoft.CodeAnalysis.CSharp;
1111
using Microsoft.CodeAnalysis.CSharp.Syntax;
1212
using Microsoft.CodeAnalysis.Diagnostics;
13-
using Microsoft.DotNet.UpgradeAssistant.Extensions.Windows.UWPtoWinAppSDKUpgrade;
1413
using Microsoft.DotNet.UpgradeAssistant.Extensions.Windows.UWPtoWinAppSDKUpgrade.Abstractions;
14+
using Microsoft.DotNet.UpgradeAssistant.Extensions.Windows.UWPtoWinAppSDKUpgrade.Utils;
1515

1616
namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Windows
1717
{

src/extensions/windows/Microsoft.DotNet.UpgradeAssistant.Extensions.Windows/UWPtoWinAppSDKUpgrade/WinUIInteropAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using Microsoft.CodeAnalysis.CSharp;
1111
using Microsoft.CodeAnalysis.CSharp.Syntax;
1212
using Microsoft.CodeAnalysis.Diagnostics;
13-
using Microsoft.DotNet.UpgradeAssistant.Extensions.Windows.UWPtoWinAppSDKUpgrade;
13+
using Microsoft.DotNet.UpgradeAssistant.Extensions.Windows.UWPtoWinAppSDKUpgrade.Utils;
1414

1515
namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Windows
1616
{

0 commit comments

Comments
 (0)