forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation that executable references match SelfContained
Fixes dotnet#15117
- Loading branch information
1 parent
4ec3a71
commit 166aa0f
Showing
4 changed files
with
170 additions
and
33 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/Tasks/Microsoft.NET.Build.Tasks/ValidateExecutableReferences.cs
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,59 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Build.Framework; | ||
|
||
namespace Microsoft.NET.Build.Tasks | ||
{ | ||
public class ValidateExecutableReferences : TaskBase | ||
{ | ||
public bool SelfContained { get; set; } | ||
|
||
public bool IsExecutable { get; set; } | ||
|
||
public ITaskItem[] ReferencedProjects { get; set; } = Array.Empty<ITaskItem>(); | ||
|
||
protected override void ExecuteCore() | ||
{ | ||
if (!IsExecutable) | ||
{ | ||
// If current project is not executable, then we don't need to check its references | ||
return; | ||
} | ||
|
||
foreach (var project in ReferencedProjects) | ||
{ | ||
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]; | ||
} | ||
|
||
var referencedProjectIsExecutable = MSBuildUtilities.ConvertStringToBool(projectAdditionalProperties["_IsExecutable"]); | ||
var referencedProjectIsSelfContained = MSBuildUtilities.ConvertStringToBool(projectAdditionalProperties["SelfContained"]); | ||
|
||
if (referencedProjectIsExecutable) | ||
{ | ||
if (SelfContained && !referencedProjectIsSelfContained) | ||
{ | ||
Log.LogError(Strings.SelfContainedExeCannotReferenceNonSelfContained, project.ItemSpec); | ||
} | ||
else if (!SelfContained && referencedProjectIsSelfContained) | ||
{ | ||
Log.LogError(Strings.NonSelfContainedExeCannottReferenceSelfContained, project.ItemSpec); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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