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

Convert winmdobj project type to CsWinRT component projects #447

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/MSBuild.Abstractions/MSBuildConversionWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ private bool IsSupportedOutputType(ProjectOutputType type) =>
ProjectOutputType.Library => true,
ProjectOutputType.WinExe => true,
ProjectOutputType.AppContainerExe => true,
ProjectOutputType.WinMdObj => true,
_ => false
};

Expand Down Expand Up @@ -283,6 +284,10 @@ private ProjectOutputType GetProjectOutputType(IProjectRootElement root, Project
{
return ProjectOutputType.AppContainerExe;
}
else if (ProjectPropertyHelpers.IsWinMdObjProjectType(outputTypeNode))
{
return ProjectOutputType.WinMdObj;
}
else
{
return ProjectOutputType.Other;
Expand Down
1 change: 1 addition & 0 deletions src/MSBuild.Abstractions/ProjectOutputType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum ProjectOutputType
Exe,
WinExe,
AppContainerExe,
WinMdObj,
Other,
None
}
Expand Down
7 changes: 7 additions & 0 deletions src/MSBuild.Abstractions/ProjectPropertyHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ public static bool IsWinExeOutputType(ProjectPropertyElement prop) =>
prop.ElementName.Equals(MSBuildFacts.OutputTypeNodeName, StringComparison.OrdinalIgnoreCase)
&& prop.Value.Equals(MSBuildFacts.WinExeOutputType, StringComparison.OrdinalIgnoreCase);

/// <summary>
/// Checks if an OutputType node is Exe.
/// </summary>
public static bool IsWinMdObjProjectType(ProjectPropertyElement prop) =>
prop.ElementName.Equals(MSBuildFacts.OutputTypeNodeName, StringComparison.OrdinalIgnoreCase)
&& prop.Value.Equals(MSBuildFacts.WinMdObjOutputType, StringComparison.OrdinalIgnoreCase);

public static bool IsVisualBasicProject(ProjectPropertyElement prop) =>
IsProjectTypeGuidsNode(prop) && prop.Value.Split(';').Any(guidString => Guid.Parse(guidString) == MSBuildFacts.LanguageProjectTypeVisualBasic);

Expand Down
4 changes: 4 additions & 0 deletions src/MSBuild.Conversion.Facts/MSBuildFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ public static class MSBuildFacts
Guid.Parse("{F2A71F9B-5D33-465A-A702-920D77279786}") // F#
);

public static readonly (string Name, string Version) CsWinRTPackageReference = (Name: "Microsoft.Windows.CsWinRT", Version: "1.6.4");

public const string DefaultSDKAttribute = "Microsoft.NET.Sdk";
public const string LowestFrameworkVersionWithSystemValueTuple = "net47";
public const string SharedProjectsImportLabel = "Shared";
Expand Down Expand Up @@ -290,6 +292,7 @@ public static class MSBuildFacts
public const string ExeOutputType = "Exe";
public const string WinExeOutputType = "WinExe";
public const string AppContainerExeOutputType = "AppContainerExe";
public const string WinMdObjOutputType = "winmdobj";
public const string NuGetPackageImportStampNodeName = "NuGetPackageImportStamp";
public const string ReferencePathNodeName = "ReferencePath";
public const string LegacyTargetFrameworkPropertyNodeName = "TargetFrameworkIdentifier";
Expand All @@ -313,5 +316,6 @@ public static class MSBuildFacts
public const string TargetPlatformIdentifierNodeName = "TargetPlatformIdentifier";
public const string UapValue = "UAP";
public const string TargetPlatformVersionNodeName = "TargetPlatformVersion";
public const string CsWinRTComponentName = "CsWinRTComponent";
}
}
1 change: 1 addition & 0 deletions src/MSBuild.Conversion.Project/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void Convert(string outputPath)

// Now we can convert the project over
.ChangeImportsAndAddSdkAttribute(_sdkBaselineProject, _forceRemoveCustomImports)
.AddCsWinRTReferenceAndComponentProperty(_sdkBaselineProject)
.UpdateOutputTypeProperty(_sdkBaselineProject)
.RemoveDefaultedProperties(_sdkBaselineProject, _differs)
.RemoveUnnecessaryPropertiesNotInSDKByDefault(_sdkBaselineProject.ProjectStyle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ public static IProjectRootElement ChangeImportsAndAddSdkAttribute(this IProjectR
return projectRootElement;
}

public static IProjectRootElement AddCsWinRTReferenceAndComponentProperty(this IProjectRootElement projectRootElement, BaselineProject baselineProject)
{
if (baselineProject.OutputType == ProjectOutputType.WinMdObj)
{
var topLevelPropGroup = MSBuildHelpers.GetOrCreateTopLevelPropertyGroup(baselineProject, projectRootElement);
topLevelPropGroup.AddProperty(MSBuildFacts.CsWinRTComponentName, "true");

var packageReferenceItemGroup = projectRootElement.ItemGroups.Where(ig => ig.Items.Any(i => i.ItemType == MSBuildFacts.MSBuildPackageReferenceName))
.FirstOrDefault() ?? projectRootElement.AddItemGroup();
AddPackageReferenceElement(packageReferenceItemGroup, MSBuildFacts.CsWinRTPackageReference.Name, MSBuildFacts.CsWinRTPackageReference.Version);
}

return projectRootElement;
}

public static IProjectRootElement UpdateOutputTypeProperty(this IProjectRootElement projectRootElement, BaselineProject baselineProject)
{
var outputTypeNode = projectRootElement.GetOutputTypeNode();
Expand All @@ -76,6 +91,7 @@ public static IProjectRootElement UpdateOutputTypeProperty(this IProjectRootElem
ProjectOutputType.Library => MSBuildFacts.LibraryOutputType,
ProjectOutputType.WinExe => MSBuildFacts.WinExeOutputType,
ProjectOutputType.AppContainerExe => MSBuildFacts.WinExeOutputType,
ProjectOutputType.WinMdObj => MSBuildFacts.LibraryOutputType,
_ => throw new InvalidOperationException("Unsupported output type: " + baselineProject.OutputType)
};
}
Expand Down