Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ValidateExecutableReferences with new schema #19786

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 @@ -5,8 +5,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.Build.Framework;

Expand All @@ -20,6 +18,8 @@ public class ValidateExecutableReferences : TaskBase

public ITaskItem[] ReferencedProjects { get; set; } = Array.Empty<ITaskItem>();

public bool UseAttributeForTargetFrameworkInfoPropertyNames { get; set; }

protected override void ExecuteCore()
{
if (!IsExecutable)
Expand All @@ -28,7 +28,7 @@ protected override void ExecuteCore()
return;
}

foreach (var project in ReferencedProjects)
foreach (ITaskItem project in ReferencedProjects)
{
string nearestTargetFramework = project.GetMetadata("NearestTargetFramework");

Expand All @@ -40,16 +40,18 @@ protected override void ExecuteCore()
}

var additionalPropertiesXml = XElement.Parse(project.GetMetadata("AdditionalPropertiesFromProject"));
var targetFrameworkElement = additionalPropertiesXml.Element(nearestTargetFramework);
XElement targetFrameworkElement = UseAttributeForTargetFrameworkInfoPropertyNames ?
additionalPropertiesXml.Elements().Where(el => el.HasAttributes && el.FirstAttribute.Value.Equals(nearestTargetFramework)).Single() :
additionalPropertiesXml.Element(nearestTargetFramework);
Dictionary<string, string> projectAdditionalProperties = new(StringComparer.OrdinalIgnoreCase);
foreach (var propertyElement in targetFrameworkElement.Elements())
foreach (XElement propertyElement in targetFrameworkElement.Elements())
{
projectAdditionalProperties[propertyElement.Name.LocalName] = propertyElement.Value;
}

var shouldBeValidatedAsExecutableReference = MSBuildUtilities.ConvertStringToBool(projectAdditionalProperties["ShouldBeValidatedAsExecutableReference"], true);
var referencedProjectIsExecutable = MSBuildUtilities.ConvertStringToBool(projectAdditionalProperties["_IsExecutable"]);
var referencedProjectIsSelfContained = MSBuildUtilities.ConvertStringToBool(projectAdditionalProperties["SelfContained"]);
bool shouldBeValidatedAsExecutableReference = MSBuildUtilities.ConvertStringToBool(projectAdditionalProperties["ShouldBeValidatedAsExecutableReference"], true);
bool referencedProjectIsExecutable = MSBuildUtilities.ConvertStringToBool(projectAdditionalProperties["_IsExecutable"]);
bool referencedProjectIsSelfContained = MSBuildUtilities.ConvertStringToBool(projectAdditionalProperties["SelfContained"]);

if (referencedProjectIsExecutable && shouldBeValidatedAsExecutableReference)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,10 @@ Copyright (c) .NET Foundation. All rights reserved.

<UsingTask TaskName="ValidateExecutableReferences" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />

<PropertyGroup>
<_UseAttributeForTargetFrameworkInfoPropertyNames Condition="$([MSBuild]::VersionGreaterThanOrEquals($(MSBuildVersion), '17.0'))">true</_UseAttributeForTargetFrameworkInfoPropertyNames>
</PropertyGroup>

<Target Name="ValidateExecutableReferences"
AfterTargets="_GetProjectReferenceTargetFrameworkProperties"
Condition="'$(ValidateExecutableReferencesMatchSelfContained)' != 'false'">
Expand All @@ -1089,6 +1093,7 @@ Copyright (c) .NET Foundation. All rights reserved.
SelfContained="$(SelfContained)"
IsExecutable="$(_IsExecutable)"
ReferencedProjects="@(_MSBuildProjectReferenceExistent)"
UseAttributeForTargetFrameworkInfoPropertyNames="$(_UseAttributeForTargetFrameworkInfoPropertyNames)"
/>

</Target>
Expand Down