Skip to content

Commit

Permalink
Merge pull request #19786 from Forgind/update-validateexecutablerefer…
Browse files Browse the repository at this point in the history
…ences

Update ValidateExecutableReferences with new schema
  • Loading branch information
marcpopMSFT authored Sep 27, 2021
2 parents 7e3d2fe + f913abd commit f5e3961
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
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

0 comments on commit f5e3961

Please sign in to comment.