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 conventions to enforce that a project re…
…ferences/does not reference a package (by name) An example use-case is something like the coverlet collector (https://github.com/coverlet-coverage/coverlet) which should be added ONLY to (ALL) test projects
- Loading branch information
eddie.stanley
committed
Jan 6, 2024
1 parent
0dce893
commit 53ec186
Showing
7 changed files
with
149 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
27 changes: 27 additions & 0 deletions
27
...entional/Conventions/Assemblies/MustNotReferencePackageAssemblyConventionSpecification.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,27 @@ | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
|
||
namespace Conventional.Conventions.Assemblies | ||
{ | ||
public class MustNotReferencePackageAssemblyConventionSpecification : PackageReferenceAssemblyConventionSpecification | ||
{ | ||
private readonly string _needlePackage; | ||
|
||
public MustNotReferencePackageAssemblyConventionSpecification(string needlePackage) | ||
{ | ||
_needlePackage = needlePackage; | ||
} | ||
|
||
protected override ConventionResult IsSatisfiedBy(string assemblyName, XDocument projectDocument) | ||
{ | ||
if (GetPackageReferences(projectDocument).Contains(_needlePackage)) | ||
{ | ||
return ConventionResult.NotSatisfied(assemblyName, string.Format(FailureMessage, assemblyName)); | ||
} | ||
|
||
return ConventionResult.Satisfied(assemblyName); | ||
} | ||
|
||
protected override string FailureMessage => "{0} should not reference package " + _needlePackage; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...onventional/Conventions/Assemblies/MustReferencePackageAssemblyConventionSpecification.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,27 @@ | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
|
||
namespace Conventional.Conventions.Assemblies | ||
{ | ||
public class MustReferencePackageAssemblyConventionSpecification : PackageReferenceAssemblyConventionSpecification | ||
{ | ||
private readonly string _needlePackage; | ||
|
||
public MustReferencePackageAssemblyConventionSpecification(string needlePackage) | ||
{ | ||
_needlePackage = needlePackage; | ||
} | ||
|
||
protected override ConventionResult IsSatisfiedBy(string assemblyName, XDocument projectDocument) | ||
{ | ||
if (GetPackageReferences(projectDocument).Contains(_needlePackage)) | ||
{ | ||
return ConventionResult.Satisfied(assemblyName); | ||
} | ||
|
||
return ConventionResult.NotSatisfied(assemblyName, string.Format(FailureMessage, assemblyName)); | ||
} | ||
|
||
protected override string FailureMessage => "{0} should reference package " + _needlePackage; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...re/Conventional/Conventions/Assemblies/PackageReferenceAssemblyConventionSpecification.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,25 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
using System.Xml.XPath; | ||
|
||
namespace Conventional.Conventions.Assemblies | ||
{ | ||
public abstract class PackageReferenceAssemblyConventionSpecification : AssemblyConventionSpecification | ||
{ | ||
protected IEnumerable<string> GetPackageReferences(XDocument projectDocument) | ||
{ | ||
// The Project element (and descendants) are namespaced in legacy csproj files, so our XPath ignores the | ||
// namespace by considering the local element name only. Once we no-longer need to support legacy csproj | ||
// files, the XPath can be simplified to /Project/ItemGroup/PackageReference | ||
return projectDocument.XPathSelectElements("/*[local-name() = 'Project']/*[local-name() = 'ItemGroup']/*[local-name() = 'PackageReference']") | ||
.Select(referenceElement => referenceElement.Attribute("Include")?.Value) | ||
.Where(value => value != null); | ||
} | ||
|
||
protected override ConventionResult IsSatisfiedByLegacyCsprojFormat(string assemblyName, XDocument projectDocument) | ||
{ | ||
return IsSatisfiedBy(assemblyName, projectDocument); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Core/TestSolution/TestSolution.TestProject/SdkClassLibrary1/SdkClassLibrary1.csproj
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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
</Project> |
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