ReferencesRuler 1.1.6

There is a newer version of this package available.
See the version list below for details.

Requires NuGet 2.12 or higher.

dotnet add package ReferencesRuler --version 1.1.6                
NuGet\Install-Package ReferencesRuler -Version 1.1.6                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ReferencesRuler" Version="1.1.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ReferencesRuler --version 1.1.6                
#r "nuget: ReferencesRuler, 1.1.6"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ReferencesRuler as a Cake Addin
#addin nuget:?package=ReferencesRuler&version=1.1.6

// Install ReferencesRuler as a Cake Tool
#tool nuget:?package=ReferencesRuler&version=1.1.6                

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:

  1. ProjectReferences including PrivateAssets="All" check.
  2. PackageReferences including PrivateAssets="All" and Version check.
  3. ProjectReferences existence check: checks if a project reference is still a valid project in the solution.
  4. Project GUID in the Solution file. It should always be "9A19103F-16F7-4668-BE54-9A1E7A4F7556" and not "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC".
  5. 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

  1. Get the nuget package ReferencesRuler
  2. [Create a unit test](#Enforcing rules)
  3. Enjoy your stable architecture 😃

Rules

The rules are declarative and can be used for either project or package references. There are:

  1. Forbidden - main rule
  2. Allowed - exception from the main rule
  3. 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")

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(@"*B*", @"*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

  1. Before starting please create an issue first. That way we can discuss the feature before implementing it.
  2. Create a pull request.
  3. After creating a release, the new version will be available on the nuget.org package page.
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has 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.

Version Downloads Last updated
1.1.10 3,535 8/11/2021
1.1.9 59,327 2/25/2021
1.1.8 3,304 9/8/2020
1.1.7 5,229 12/31/2019
1.1.6 1,124 12/28/2019

Bringing the project to the open source community.