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 5, 2024
1 parent
bc95afc
commit e160a4c
Showing
3 changed files
with
61 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
36 changes: 36 additions & 0 deletions
36
...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,36 @@ | ||
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) | ||
{ | ||
return projectDocument.XPathSelectElements("/Project/ItemGroup/ProjectReference") | ||
.Select(referenceElement => referenceElement.Attribute("Include")?.Value) | ||
.Where(value => value != null); | ||
} | ||
|
||
protected override string FailureMessage => "{0} includes reference to project {1}"; | ||
} | ||
} |