{"id":25609556,"url":"https://github.com/digitecgalaxus/projectsruler","last_synced_at":"2025-06-14T11:06:28.626Z","repository":{"id":47990473,"uuid":"223990626","full_name":"DigitecGalaxus/ProjectsRuler","owner":"DigitecGalaxus","description":"This is a C# Project for architectural governance. It checks the .csproj and .sln files and can be used to implement some rules upon them.","archived":false,"fork":false,"pushed_at":"2025-04-25T08:15:37.000Z","size":110,"stargazers_count":10,"open_issues_count":10,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-14T09:53:19.706Z","etag":null,"topics":["architecture","architecture-governance","csharp","dotnet","governance","nuget","software-architecture"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DigitecGalaxus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-25T16:15:02.000Z","updated_at":"2024-11-07T20:24:55.000Z","dependencies_parsed_at":"2022-08-12T16:01:10.410Z","dependency_job_id":null,"html_url":"https://github.com/DigitecGalaxus/ProjectsRuler","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/DigitecGalaxus/ProjectsRuler","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DigitecGalaxus%2FProjectsRuler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DigitecGalaxus%2FProjectsRuler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DigitecGalaxus%2FProjectsRuler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DigitecGalaxus%2FProjectsRuler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DigitecGalaxus","download_url":"https://codeload.github.com/DigitecGalaxus/ProjectsRuler/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DigitecGalaxus%2FProjectsRuler/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259804872,"owners_count":22913903,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["architecture","architecture-governance","csharp","dotnet","governance","nuget","software-architecture"],"created_at":"2025-02-21T21:41:09.765Z","updated_at":"2025-06-14T11:06:28.611Z","avatar_url":"https://github.com/DigitecGalaxus.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ProjectsRuler\n\n![Pipeline](https://github.com/DigitecGalaxus/ProjectsRuler/workflows/.NET%20Core/badge.svg)\n![Nuget](https://img.shields.io/nuget/v/ReferencesRuler)\n\n# Introduction\n\nReferencesRuler 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:\n\n1. ProjectReferences including *PrivateAssets=\"All\"* check.\n2. PackageReferences including *PrivateAssets=\"All\"* and *Version* check.\n3. ProjectReferences existence check: checks if a project reference is still a valid project in the solution.\n4. Project GUID in the Solution file. It should always be *\"9A19103F-16F7-4668-BE54-9A1E7A4F7556\"* and not *\"FAE04EC0-301F-11D3-BF4B-00C04F79EFBC\"*.\n5. Project and PackageReferences duplicates.\n\nThe 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.\n\nThe main limitation: **It relies on having the sourcecode while running the tests!**\n\n# Getting Started\n\n1. Get the nuget package [ReferencesRuler](https://www.nuget.org/packages/ReferencesRuler/)\n2. [Create a unit test](#Enforcing-rules)\n3. Enjoy your stable architecture :)\n\n# Rules\n\nThe rules are declarative and can be used for either project or package references. There are:\n\n1. **Forbidden** - main rule\n2. **Allowed** - exception from the main rule\n3. **ExplicitlyForbidden** - override of the exception which cannot be overriden.\n\nThe 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:\n\n* **isPrivateAssetsAllSet** - can be used for either project or package references. It checks if the attribute *PrivateAssets=\"All\"* is set.\n* **version** - makes sense only with package references. If set with project references the ruler can deliver unexpected results.\n\n## Rules examples\n\n* A typical project/package reference rule:\n  `new ReferenceRule(@\"*B*\", @\"*A*\", RuleKind.Forbidden, description: \"B Projects must not reference A, they must use the new Framework Projects\")`\n* *PrivateAssets=\"All\"* rule:\n  `new ReferenceRule(@\"*\", @\"*\", RuleKind.Forbidden, description: \"All references have to have PrivateAssets set to All.\")`\n* Version rule - only checks for the exact version:\n  `new ReferenceRule(@\"*\", @\"SomeNugetPackage\", RuleKind.Forbidden, description: \"Package version 1.2.3 is forbidden.\", version=\"1.2.3\")`\n\n## Fluent builder\n\nRules can also be creating using the fluent builder API:\n\n```C#\nvar rule = ReferenceRule.For(\"Project.A\")\n    .Referencing(\"Project.B\")\n    .IsForbidden()\n    .Because(\"A should not reference B\")\n    .BuildRule();\n```\n\n# Enforcing rules \u003ca name=\"enforcing_rules\"\u003e\u003c/a\u003e\n\nIn 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.\n\n```C#\n[Test]\npublic void ItIsNotAllowedToReferenceProjectAFromProjectB()\n{\n    AssertReferenceRules(\n        // The rules\n        new ReferenceRule(\n            patternFrom: @\"*B*\",\n            patternTo: @\"*A*\",\n            RuleKind.Forbidden,\n            description: \"B Projects must not reference A, they must use the new Framework Projects\")\n    );\n}\n\nprivate void AssertReferenceRules(params ReferenceRule[] rules)\n{\n    var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);\n    var solutionDir = Path.Combine(dir, @\"..\\..\\..\\\");\n\n    var complaints = ProjectsRuler.GetProjectReferencesComplaints(solutionDir, rules);\n    // or var complaints = ProjectsRuler.GetPackageReferencesComplaints(solutionDir, rules);\n\n    Assert.IsEmpty(complaints);\n}\n```\n\n# Project references exitence check\n\nThere is a dedicated checker for that. It uses the same csproj parser as all the other tools in the ruler: **CsprojReferencesExtractor**.\n\n```C#\n[Test]\npublic void CheckForBrokenReferences()\n{\n    var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);\n    var solutionDir = Path.Combine(dir, @\"..\\..\\..\\\");\n    var solutionFilePath = Path.Combine(solutionDir, \"MySolution.sln\");\n    var checker = new ReferencesExistenceChecker(\n        new SolutionParser(),\n        new CsprojReferencesExtractor());\n\n    var messages = checker.CheckProjectReferencesExistenceInSolution(solutionFilePath, \"*.csproj\").ToList;\n\n    if (messages.Any())\n    {\n        Assert.Fail($\"Check for broken references failed. See messages:\\n{string.Join(\"\\n\", messages)}\");\n    }\n}\n```\n\n# Solution file validity check\n\nThere 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.\n\n```C#\n[Test]\npublic void CheckIfSolutionHasAllValidProjectGuids()\n{\n    var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);\n    var solutionDir = Path.Combine(dir, @\"..\\..\\..\\\");\n\n    var projectGuidChecker = new SolutionProjectGuidChecker(new SolutionParser());\n    var complaints = new ProjectFilesRunner(solutionDir, \"*.sln\")\n        .CollectComplaintsForFiles(\n            filePath =\u003e new[]\n            {\n                projectGuidChecker.CheckSolutionProjectGuids(\n                    filePath,\n                    \"9A19103F-16F7-4668-BE54-9A1E7A4F7556\",\n                    \".csproj\")\n            });\n\n    Assert.IsEmpty(complaints);\n}\n```\n\n# Advanced extensibility\n\nIn 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:\n\n```C#\nvar dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);\nvar solutionDir = Path.Combine(dir, @\"..\\..\\..\\\");\n\nvar runner = new ReferencesRulerRunner(\n    extractor: new CsprojReferencesExtractor(),\n    referencesRuler: new ReferencesRuler(\n        patternParser: new WildcardPatternParser(),\n        rules: rules),\n    filesRunner: new ProjectFilesRunner(\n        solutionPath: solutionDir,\n        filesExtension: \"*.csproj\"));\n\nvar complaints = runner.GetPackageReferencesComplaints();\n```\n\nRemark: 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. ¯\\\\_(ツ)_/¯\n\n# Build and Test\n\nPlease keep this project .NET Standard 2.0 so as many people as possible can use this tool.\n\n# Contribute\n\n1. Before starting please [create an issue first](https://github.com/DigitecGalaxus/ProjectsRuler/issues). That way we can discuss the feature before implementing it.\n2. Create a pull request.\n3. After creating a release, the new version will be available on the [nuget.org package page](https://www.nuget.org/packages/ReferencesRuler/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigitecgalaxus%2Fprojectsruler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdigitecgalaxus%2Fprojectsruler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigitecgalaxus%2Fprojectsruler/lists"}