Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support gathering additional arbitrary properties from referenced pro…
…jects (#5994) This allows additional properties to be gathered from project references via the project reference protocol. This is in order to support generating an error when a self-contained Executable references a non-SelfContained Executable, or vice versa, as described in dotnet/sdk#15117. The referenced project can declare what additional properties should be gathered with AdditionalTargetFrameworkInfoProperty items: <ItemGroup> <AdditionalTargetFrameworkInfoProperty Include="SelfContained"/> <AdditionalTargetFrameworkInfoProperty Include="_IsExecutable"/> </ItemGroup> These items will then be included in the AdditionalPropertiesFromProject metadata on the _MSBuildProjectReferenceExistent items. This value will have PropertyName=PropertyValue combinations joined by semicolons, and those sets of properties for the different TargetFramework values will be joined by double semicolons. For example, a project multitargeted to two TargetFrameworks may have the following for the AdditionalPropertiesFromProject metadata: SelfContained=true;_IsExecutable=true;;SelfContained=false;_IsExecutable=true Finding the right set of properties from the referenced project will required looking up the index of the NearestTargetFramework in the TargetFrameworks, and using that index to select the set of properties in the AdditionalPropertiesFromProject metadata. For example: string nearestTargetFramework = project.GetMetadata("NearestTargetFramework"); int targetFrameworkIndex = project.GetMetadata("TargetFrameworks").Split(';').ToList().IndexOf(nearestTargetFramework); string projectAdditionalPropertiesMetadata = project.GetMetadata("AdditionalPropertiesFromProject").Split(new[] { ";;" }, StringSplitOptions.None)[targetFrameworkIndex]; Dictionary<string, string> projectAdditionalProperties = new(StringComparer.OrdinalIgnoreCase); foreach (var propAndValue in projectAdditionalPropertiesMetadata.Split(';')) { var split = propAndValue.Split('='); projectAdditionalProperties[split[0]] = split[1]; } This is implemented in dotnet/sdk#15134.
- Loading branch information
1 parent
74cbf25
commit 83cd7d4
Showing
9 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.Build.Framework; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Xml.Linq; | ||
|
||
namespace Microsoft.Build.Tasks | ||
{ | ||
/// <summary> | ||
/// Combines items that represent properties and values into an XML representation. | ||
/// </summary> | ||
public class CombineTargetFrameworkInfoProperties : TaskExtension | ||
{ | ||
/// <summary> | ||
/// The root element name to use for the generated XML string | ||
/// </summary> | ||
public string RootElementName { get; set; } | ||
|
||
/// <summary> | ||
/// Items to include in the XML. The ItemSpec should be the property name, and it should have Value metadata for its value. | ||
/// </summary> | ||
public ITaskItem[] PropertiesAndValues { get; set; } | ||
|
||
/// <summary> | ||
/// The generated XML representation of the properties and values. | ||
/// </summary> | ||
[Output] | ||
public string Result { get; set; } | ||
|
||
public override bool Execute() | ||
{ | ||
if (PropertiesAndValues != null) | ||
{ | ||
XElement root = new XElement(RootElementName); | ||
|
||
foreach (var item in PropertiesAndValues) | ||
{ | ||
root.Add(new XElement(item.ItemSpec, item.GetMetadata("Value"))); | ||
} | ||
|
||
Result = root.ToString(); | ||
} | ||
return !Log.HasLoggedErrors; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.Build.Framework; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Xml.Linq; | ||
|
||
namespace Microsoft.Build.Tasks | ||
{ | ||
/// <summary> | ||
/// Combines multiple XML elements | ||
/// </summary> | ||
public class CombineXmlElements : TaskExtension | ||
{ | ||
/// <summary> | ||
/// The root element name to use for the generated XML string | ||
/// </summary> | ||
public string RootElementName { get; set; } | ||
|
||
/// <summary> | ||
/// The XML elements to include as children of the root element | ||
/// </summary> | ||
public ITaskItem [] XmlElements { get; set; } | ||
|
||
/// <summary> | ||
/// The generated XML | ||
/// </summary> | ||
[Output] | ||
public string Result { get; set; } | ||
|
||
public override bool Execute() | ||
{ | ||
if (XmlElements != null) | ||
{ | ||
XElement root = new XElement(RootElementName); | ||
|
||
foreach (var item in XmlElements) | ||
{ | ||
root.Add(XElement.Parse(item.ItemSpec)); | ||
} | ||
|
||
Result = root.ToString(); | ||
} | ||
|
||
return !Log.HasLoggedErrors; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters