-
Notifications
You must be signed in to change notification settings - Fork 857
Expand file tree
/
Copy pathMetadataReportsGenerator.cs
More file actions
106 lines (91 loc) · 4.01 KB
/
MetadataReportsGenerator.cs
File metadata and controls
106 lines (91 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.Gen.Shared;
using Microsoft.Shared.DiagnosticIds;
namespace Microsoft.Gen.MetadataExtractor;
/// <summary>
/// Generates reports for compliance & metrics annotations.
/// </summary>
[Generator]
public sealed class MetadataReportsGenerator : ISourceGenerator
{
private const string GenerateMetadataMSBuildProperty = "build_property.GenerateMetadataReport";
private const string ReportOutputPathMSBuildProperty = "build_property.MetadataReportOutputPath";
private const string RootNamespace = "build_property.rootnamespace";
private const string FallbackFileName = "MetadataReport.json";
private readonly string _fileName;
private string? _directory;
/// <summary>
/// Initializes a new instance of the <see cref="MetadataReportsGenerator"/> class.
/// </summary>
public MetadataReportsGenerator()
: this(filePath: null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="MetadataReportsGenerator"/> class.
/// </summary>
/// <param name="filePath">The report path and name.</param>
public MetadataReportsGenerator(string? filePath)
{
if (filePath is not null)
{
_directory = Path.GetDirectoryName(filePath);
_fileName = Path.GetFileName(filePath);
}
else
{
_fileName = FallbackFileName;
}
}
/// <summary>
/// Initializes the generator.
/// </summary>
/// <param name="context">The generator initialization context.</param>
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(TypeDeclarationSyntaxReceiver.Create);
}
/// <summary>
/// Generates reports for compliance & metrics annotations.
/// </summary>
/// <param name="context">The generator execution context.</param>
public void Execute(GeneratorExecutionContext context)
{
context.CancellationToken.ThrowIfCancellationRequested();
if (context.SyntaxReceiver is not TypeDeclarationSyntaxReceiver ||
((TypeDeclarationSyntaxReceiver)context.SyntaxReceiver).TypeDeclarations.Count == 0 ||
!GeneratorUtilities.ShouldGenerateReport(context, GenerateMetadataMSBuildProperty))
{
// nothing to do yet
return;
}
var options = context.AnalyzerConfigOptions.GlobalOptions;
_directory ??= GeneratorUtilities.TryRetrieveOptionsValue(options, ReportOutputPathMSBuildProperty, out var reportOutputPath)
? reportOutputPath!
: GeneratorUtilities.GetDefaultReportOutputPath(options);
if (string.IsNullOrWhiteSpace(_directory))
{
// Report diagnostic:
var diagnostic = new DiagnosticDescriptor(
DiagnosticIds.AuditReports.AUDREPGEN000,
"MetadataReport generator couldn't resolve output path for the report. It won't be generated.",
"Both <MetadataReportOutputPath> and <OutputPath> MSBuild properties are not set. The report won't be generated.",
nameof(DiagnosticIds.AuditReports),
DiagnosticSeverity.Info,
isEnabledByDefault: true,
helpLinkUri: string.Format(CultureInfo.InvariantCulture, DiagnosticIds.UrlFormat, DiagnosticIds.AuditReports.AUDREPGEN000));
context.ReportDiagnostic(Diagnostic.Create(diagnostic, location: null));
return;
}
MetadataEmitter emitter = new MetadataEmitter(RootNamespace);
#pragma warning disable RS1035 // Do not use APIs banned for analyzers
_ = Directory.CreateDirectory(_directory);
File.WriteAllText(Path.Combine(_directory, _fileName), emitter.Emit(context), Encoding.UTF8);
#pragma warning restore RS1035 // Do not use APIs banned for analyzers
}
}