{"id":17038035,"url":"https://github.com/kzu/analyzersettings","last_synced_at":"2025-06-26T02:33:24.752Z","repository":{"id":141699000,"uuid":"204602181","full_name":"kzu/AnalyzerSettings","owner":"kzu","description":null,"archived":false,"fork":false,"pushed_at":"2020-05-09T21:46:12.000Z","size":20,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-24T07:27:46.976Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"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/kzu.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"devlooped"}},"created_at":"2019-08-27T02:25:20.000Z","updated_at":"2020-06-11T13:34:08.000Z","dependencies_parsed_at":null,"dependency_job_id":"b551cd60-b88f-40da-8ae8-504e567a3c30","html_url":"https://github.com/kzu/AnalyzerSettings","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/kzu/AnalyzerSettings","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kzu%2FAnalyzerSettings","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kzu%2FAnalyzerSettings/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kzu%2FAnalyzerSettings/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kzu%2FAnalyzerSettings/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kzu","download_url":"https://codeload.github.com/kzu/AnalyzerSettings/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kzu%2FAnalyzerSettings/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261986684,"owners_count":23240687,"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":[],"created_at":"2024-10-14T08:55:46.825Z","updated_at":"2025-06-26T02:33:24.732Z","avatar_url":"https://github.com/kzu.png","language":null,"funding_links":["https://github.com/sponsors/devlooped"],"categories":[],"sub_categories":[],"readme":"# AnalyzerSettings from MSBuild\n\n[![Version](https://img.shields.io/nuget/vpre/AnalyzerSettings.svg)](https://www.nuget.org/packages/AnalyzerSettings)\n[![Downloads](https://img.shields.io/nuget/dt/AnalyzerSettings)](https://www.nuget.org/packages/AnalyzerSettings)\n[![Build Status](https://dev.azure.com/kzu/oss/_apis/build/status/AnalyzerSettings?branchName=master)](http://build.azdo.io/kzu/oss/28)\n[![License](https://img.shields.io/github/license/kzu/AnalyzerSettings.svg)](LICENSE)\n[![CI NuGet Feed](https://img.shields.io/badge/nuget-ci_feed-%23004880?logo=NuGet)](https://kzu.blob.core.windows.net/nuget/index.json)\n\n\nAnalyzers typically need some sort of configuration to tweak how they work. \nIn order to avoid inventing your own mecanism for settings, the \n[AnalyzerSettings](https://www.nuget.org/packages/AnalyzerSettings) package \nprovides the basic targets to allow the settings to be driven by MSBuild \nproperties and items, with full support for incremental builds and both \ndesign-time and compile-time analyzer settings.\n\nYour analyzer targets can simply declare settings to persist as `AnalyzerSetting` \nitems, and this package will automatically persist and expose those to your analyzers:\n\n```xml\n\u003c!-- In your analyzer package .targets  --\u003e\n\u003cItemGroup\u003e\n    \u003cAnalyzerSetting Include=\"SingleFileCodeGeneration\" Value=\"$([MSBuild]::ValueOrDefault('$(SingleFileCodeGeneration)', 'true'))\" /\u003e\n\u003c/ItemGroup\u003e\n```\n\nNote how you can even leverage `ValueOrDefault` to easily provide sensible defaults for your \nanalyzer setting. Of course, any of the built-in \n[MSBuild property functions](https://docs.microsoft.com/en-us/visualstudio/msbuild/property-functions?view=vs-2019#msbuild-property-functions) can similarly be used.\n\nConsuming the settings is equally straightforward, and requires no dependency with this package \nfrom your analyzer:\n\n```csharp\npublic class MyCodeGenAnalyzer : DiagnosticAnalyzer\n{\n    public override void Initialize(AnalysisContext context)\n    {\n        // Just to show *all* analyzer contexts are supported via their AnalyzerOptions\n        context.RegisterCompilationAction(AnalyzeCompilation);\n        context.RegisterSemanticModelAction(AnalyzeSemanticModel);\n        context.RegisterOperationAction(AnalyzeOperation/*, params OperationKind[] */);\n        context.RegisterSymbolAction(AnalyzeSymbol /*, params SymbolKind[] */);\n        context.RegisterSyntaxNodeAction(AnalyzeSyntaxNode/*, params SyntaxKind[] */);\n    }\n\n    // Simply converts the additional file that ends in `AnalyzerSettings.ini` into a Dictionary\u003cstring, string\u003e\n    private Dictionary\u003cstring, string\u003e GetAnalyzerSettings(AnalyzerOptions options)\n    {\n        var settingsFile = options.AdditionalFiles.FirstOrDefault(x =\u003e x.Path.EndsWith(\"AnalyzerSettings.ini\", StringComparison.OrdinalIgnoreCase));\n        if (settingsFile == null)\n            return new Dictionary\u003cstring, string\u003e();\n\n        // Added some extra precautions in parsing each line.\n        // You could also filter lines for settings starting with your known ones only...\n        return settingsFile\n            .GetText()\n            .Lines.Select(line =\u003e line.ToString())\n            .Where(line =\u003e !string.IsNullOrEmpty(line))\n            .Select(line =\u003e line.Split('='))\n            .Where(pair =\u003e pair.Length == 2)\n            .ToDictionary(pair =\u003e pair[0].Trim(), pair =\u003e pair[1].Trim());\n    }\n\n    private void AnalyzeCompilation(CompilationAnalysisContext context)\n    {\n        var settings = GetAnalyzerSettings(context.Options);\n        var useSingleFile = settings.TryGetValue(\"SingleFileCodeGeneration\", out var singleFileString) \u0026\u0026\n            bool.TryParse(singleFileString, out var singleFile) \u0026\u0026 singleFile;\n        // ... \n    }\n\n    private void RegisterSemanticModelAction(SemanticModelAnalysisContext context)\n    {\n        var settings = GetAnalyzerSettings(context.Options);\n        // ... \n    }\n\n    private void RegisterOperationAction(OperationAnalysisContext context)\n    {\n        var settings = GetAnalyzerSettings(context.Options);\n        // ... \n    }\n    \n    private void RegisterSymbolAction(SymbolAnalysisContext context)\n    {\n        var settings = GetAnalyzerSettings(context.Options);\n        // ... \n    }\n\n    private void RegisterSyntaxNodeAction(SyntaxNodeAnalysisContext context)\n    {\n        var settings = GetAnalyzerSettings(context.Options);\n        // ... \n    }    \n}\n```\n\nUnless disabled by setting the MSBuild property `EnableDefaultOutputPathsAnalyzerSettings` to `false`, all output properties from the [.NET SDK](https://github.com/dotnet/sdk/blob/master/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.DefaultOutputPaths.targets) are included as analyzer settings since they are generally useful.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkzu%2Fanalyzersettings","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkzu%2Fanalyzersettings","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkzu%2Fanalyzersettings/lists"}