ReferencesRuler 1.1.8
See the version list below for details.
Requires NuGet 4.3 or higher.
dotnet add package ReferencesRuler --version 1.1.8
NuGet\Install-Package ReferencesRuler -Version 1.1.8
<PackageReference Include="ReferencesRuler" Version="1.1.8" />
paket add ReferencesRuler --version 1.1.8
#r "nuget: ReferencesRuler, 1.1.8"
// Install ReferencesRuler as a Cake Addin #addin nuget:?package=ReferencesRuler&version=1.1.8 // Install ReferencesRuler as a Cake Tool #tool nuget:?package=ReferencesRuler&version=1.1.8
ProjectsRuler
Introduction
ReferencesRuler is a set of tools for the governance in a .NET solution. It is intended to run as an unit test since it parses project and solution files locally. It undestands the .NET Core csproj format and covers the following checks:
- ProjectReferences including PrivateAssets="All" check.
- PackageReferences including PrivateAssets="All" and Version check.
- ProjectReferences existence check: checks if a project reference is still a valid project in the solution.
- Project GUID in the Solution file. It should always be "9A19103F-16F7-4668-BE54-9A1E7A4F7556" and not "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC".
- Project and PackageReferences duplicates.
The ReferencesRuler is highly modular and extensible. You can use its components as you like. For example: you can use either the WildcardPatternParser or RegexPatternParser. You can write your own references extractor and use it (inject) instead of CsprojReferencesExtractor - if you want to support some other language, framework, framework version etc.
The main limitation: It relies on having the sourcecode while running the tests!
Getting Started
- Get the nuget package ReferencesRuler
- Create a unit test
- Enjoy your stable architecture 😃
Rules
The rules are declarative and can be used for either project or package references. There are:
- Forbidden - main rule
- Allowed - exception from the main rule
- ExplicitlyForbidden - override of the exception which cannot be overriden.
The rules are declared in that particular order, because each kind of rule is stronger than the previous one. The obligatory fields in each rule are from, to, kind and description. There are also two optional fields:
- isPrivateAssetsAllSet - can be used for either project or package references. It checks if the attribute PrivateAssets="All" is set.
- version - makes sense only with package references. If set with project references the ruler can deliver unexpected results.
Rules examples
- A typical project/package reference rule:
new ReferenceRule(@"*B*", @"*A*", RuleKind.Forbidden, description: "B Projects must not reference A, they must use the new Framework Projects")
- PrivateAssets="All" rule:
new ReferenceRule(@"*", @"*", RuleKind.Forbidden, description: "All references have to have PrivateAssets set to All.")
- Version rule - only checks for the exact version:
new ReferenceRule(@"*", @"SomeNugetPackage", RuleKind.Forbidden, description: "Package version 1.2.3 is forbidden.", version="1.2.3")
Fluent builder
Rules can also be creating using the fluent builder API:
var rule = ReferenceRule.For("Project.A")
.Referencing("Project.B")
.IsForbidden()
.Because("A should not reference B")
.BuildRule();
Enforcing rules
In order to enforce rules, the ReferencesRuler is used. There are two separate methods for project and for package references. It is highly modular and extensible. You can use parsers and runners that suits your use case the best. Here is the typical .NET use case.
[Test]
public void ItIsNotAllowedToReferenceProjectAFromProjectB()
{
AssertReferenceRules(
// The rules
new ReferenceRule(
patternFrom: @"*B*",
patternTo: @"*A*",
RuleKind.Forbidden,
description: "B Projects must not reference A, they must use the new Framework Projects")
);
}
private void AssertReferenceRules(params ReferenceRule[] rules)
{
var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var solutionDir = Path.Combine(dir, @"..\..\..\");
var complaints = ProjectsRuler.GetProjectReferencesComplaints(solutionDir, rules);
// or var complaints = ProjectsRuler.GetPackageReferencesComplaints(solutionDir, rules);
Assert.IsEmpty(complaints);
}
Project references exitence check
There is a dedicated checker for that. It uses the same csproj parser as all the other tools in the ruler: CsprojReferencesExtractor.
[Test]
public void CheckForBrokenReferences()
{
var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var solutionDir = Path.Combine(dir, @"..\..\..\");
var solutionFilePath = Path.Combine(solutionDir, "MySolution.sln");
var checker = new ReferencesExistenceChecker(
new SolutionParser(),
new CsprojReferencesExtractor());
var messages = checker.CheckProjectReferencesExistenceInSolution(solutionFilePath, "*.csproj").ToList;
if (messages.Any())
{
Assert.Fail($"Check for broken references failed. See messages:\n{string.Join("\n", messages)}");
}
}
Solution file validity check
There is a change in the solution file format since the .net core. This code snippet checks all the solution files in the repository rood directory. It ignores folders. It only checks the projects with the given file extension.
[Test]
public void CheckIfSolutionHasAllValidProjectGuids()
{
var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var solutionDir = Path.Combine(dir, @"..\..\..\");
var projectGuidChecker = new SolutionProjectGuidChecker(new SolutionParser());
var complaints = new ProjectFilesRunner(solutionDir, "*.sln")
.CollectComplaintsForFiles(
filePath => new[]
{
projectGuidChecker.CheckSolutionProjectGuids(
filePath,
"9A19103F-16F7-4668-BE54-9A1E7A4F7556",
".csproj")
});
Assert.IsEmpty(complaints);
}
Advanced extensibility
In case of a scenario that some of the ruler components would need to be replaced, it can be easily injected and the whole setup can be done like this:
var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var solutionDir = Path.Combine(dir, @"..\..\..\");
var runner = new ReferencesRulerRunner(
extractor: new CsprojReferencesExtractor(),
referencesRuler: new ReferencesRuler(
patternParser: new WildcardPatternParser(),
rules: rules),
filesRunner: new ProjectFilesRunner(
solutionPath: solutionDir,
filesExtension: "*.csproj"));
var complaints = runner.GetPackageReferencesComplaints();
Remark: this is identical setup as in the ProjectsRuler static class. Do this only if your setup is different in any way. E.g. you want to use the Regex instead of the Wildcard patterns for the rules. ¯\(ツ)/¯
Build and Test
Please keep this project .NET Standard 2.0 so as many people as possible can use this tool.
Contribute
- Before starting please create an issue first. That way we can discuss the feature before implementing it.
- Create a pull request.
- After creating a release, the new version will be available on the nuget.org package page.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Use newer (correct) logo