https://github.com/kzu/analyzersettings
https://github.com/kzu/analyzersettings
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/kzu/analyzersettings
- Owner: kzu
- License: mit
- Created: 2019-08-27T02:25:20.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-05-09T21:46:12.000Z (about 6 years ago)
- Last Synced: 2025-06-24T07:27:46.976Z (about 1 year ago)
- Size: 19.5 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AnalyzerSettings from MSBuild
[](https://www.nuget.org/packages/AnalyzerSettings)
[](https://www.nuget.org/packages/AnalyzerSettings)
[](http://build.azdo.io/kzu/oss/28)
[](LICENSE)
[](https://kzu.blob.core.windows.net/nuget/index.json)
Analyzers typically need some sort of configuration to tweak how they work.
In order to avoid inventing your own mecanism for settings, the
[AnalyzerSettings](https://www.nuget.org/packages/AnalyzerSettings) package
provides the basic targets to allow the settings to be driven by MSBuild
properties and items, with full support for incremental builds and both
design-time and compile-time analyzer settings.
Your analyzer targets can simply declare settings to persist as `AnalyzerSetting`
items, and this package will automatically persist and expose those to your analyzers:
```xml
```
Note how you can even leverage `ValueOrDefault` to easily provide sensible defaults for your
analyzer setting. Of course, any of the built-in
[MSBuild property functions](https://docs.microsoft.com/en-us/visualstudio/msbuild/property-functions?view=vs-2019#msbuild-property-functions) can similarly be used.
Consuming the settings is equally straightforward, and requires no dependency with this package
from your analyzer:
```csharp
public class MyCodeGenAnalyzer : DiagnosticAnalyzer
{
public override void Initialize(AnalysisContext context)
{
// Just to show *all* analyzer contexts are supported via their AnalyzerOptions
context.RegisterCompilationAction(AnalyzeCompilation);
context.RegisterSemanticModelAction(AnalyzeSemanticModel);
context.RegisterOperationAction(AnalyzeOperation/*, params OperationKind[] */);
context.RegisterSymbolAction(AnalyzeSymbol /*, params SymbolKind[] */);
context.RegisterSyntaxNodeAction(AnalyzeSyntaxNode/*, params SyntaxKind[] */);
}
// Simply converts the additional file that ends in `AnalyzerSettings.ini` into a Dictionary
private Dictionary GetAnalyzerSettings(AnalyzerOptions options)
{
var settingsFile = options.AdditionalFiles.FirstOrDefault(x => x.Path.EndsWith("AnalyzerSettings.ini", StringComparison.OrdinalIgnoreCase));
if (settingsFile == null)
return new Dictionary();
// Added some extra precautions in parsing each line.
// You could also filter lines for settings starting with your known ones only...
return settingsFile
.GetText()
.Lines.Select(line => line.ToString())
.Where(line => !string.IsNullOrEmpty(line))
.Select(line => line.Split('='))
.Where(pair => pair.Length == 2)
.ToDictionary(pair => pair[0].Trim(), pair => pair[1].Trim());
}
private void AnalyzeCompilation(CompilationAnalysisContext context)
{
var settings = GetAnalyzerSettings(context.Options);
var useSingleFile = settings.TryGetValue("SingleFileCodeGeneration", out var singleFileString) &&
bool.TryParse(singleFileString, out var singleFile) && singleFile;
// ...
}
private void RegisterSemanticModelAction(SemanticModelAnalysisContext context)
{
var settings = GetAnalyzerSettings(context.Options);
// ...
}
private void RegisterOperationAction(OperationAnalysisContext context)
{
var settings = GetAnalyzerSettings(context.Options);
// ...
}
private void RegisterSymbolAction(SymbolAnalysisContext context)
{
var settings = GetAnalyzerSettings(context.Options);
// ...
}
private void RegisterSyntaxNodeAction(SyntaxNodeAnalysisContext context)
{
var settings = GetAnalyzerSettings(context.Options);
// ...
}
}
```
Unless 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.