forked from andrewabest/Conventional
-
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.
[andrewabestGH-87] Added new convention to enforce that a given proje…
…ct doesn't include project references to (any) other projects
- Loading branch information
eddie.stanley
committed
Jan 6, 2024
1 parent
4a96fd6
commit 740a4d3
Showing
3 changed files
with
68 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
39 changes: 39 additions & 0 deletions
39
...entional/Conventions/Assemblies/MustNotIncludeProjectReferencesConventionSpecification.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,39 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
using System.Xml.XPath; | ||
|
||
namespace Conventional.Conventions.Assemblies | ||
{ | ||
public class MustNotIncludeProjectReferencesConventionSpecification : AssemblyConventionSpecification | ||
{ | ||
protected override ConventionResult IsSatisfiedBy(string assemblyName, XDocument projectDocument) | ||
{ | ||
var projectReferences = GetProjectReferences(projectDocument).ToArray(); | ||
|
||
if (projectReferences.Any()) | ||
{ | ||
return ConventionResult.NotSatisfied(assemblyName, string.Format(FailureMessage, assemblyName, projectReferences.First())); | ||
} | ||
|
||
return ConventionResult.Satisfied(assemblyName); | ||
} | ||
|
||
protected override ConventionResult IsSatisfiedByLegacyCsprojFormat(string assemblyName, XDocument projectDocument) | ||
{ | ||
return IsSatisfiedBy(assemblyName, projectDocument); | ||
} | ||
|
||
private IEnumerable<string> GetProjectReferences(XDocument projectDocument) | ||
{ | ||
// Note: The Project element (and descendants) are namespaced in legacy csproj files, so our XPath ignores the | ||
// Note: namespace by considering the local element name only. Once we no-longer need to support legacy csproj | ||
// Note: files, the XPath can be simplified to /Project/ItemGroup/ProjectReference | ||
return projectDocument.XPathSelectElements("/*[local-name() = 'Project']/*[local-name() = 'ItemGroup']/*[local-name() = 'ProjectReference']") | ||
.Select(referenceElement => referenceElement.Attribute("Include")?.Value) | ||
.Where(value => value != null); | ||
} | ||
|
||
protected override string FailureMessage => "{0} includes reference to project {1}"; | ||
} | ||
} |