1
1
using System . Text ;
2
2
using dotnetCampus . Logger . Assets . Templates ;
3
+ using dotnetCampus . Logger . Utils . CodeAnalysis ;
3
4
using Microsoft . CodeAnalysis ;
4
5
using Microsoft . CodeAnalysis . CSharp ;
5
6
using Microsoft . CodeAnalysis . CSharp . Syntax ;
7
+ using Microsoft . CodeAnalysis . Diagnostics ;
6
8
using Microsoft . CodeAnalysis . Text ;
7
9
using static dotnetCampus . Logger . Utils . CodeAnalysis . ProgramMainExtensions ;
8
10
@@ -16,7 +18,7 @@ public class ProgramMainLogGenerator : IIncrementalGenerator
16
18
{
17
19
public void Initialize ( IncrementalGeneratorInitializationContext context )
18
20
{
19
- var provider = context . SyntaxProvider . CreateSyntaxProvider ( ( node , ct ) =>
21
+ var syntaxProvider = context . SyntaxProvider . CreateSyntaxProvider ( ( node , ct ) =>
20
22
{
21
23
if ( node is not MethodDeclarationSyntax mds )
22
24
{
@@ -51,11 +53,28 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
51
53
return programTypeSymbol ;
52
54
} ) ;
53
55
54
- context . RegisterSourceOutput ( provider , Execute ) ;
56
+ context . RegisterSourceOutput ( syntaxProvider . Combine ( context . AnalyzerConfigOptionsProvider ) , Execute ) ;
55
57
}
56
58
57
- private void Execute ( SourceProductionContext context , INamedTypeSymbol programTypeSymbol )
59
+ private void Execute ( SourceProductionContext context , ( INamedTypeSymbol programTypeSymbol , AnalyzerConfigOptionsProvider analyzerConfigOptions ) tuple )
58
60
{
61
+ var ( programTypeSymbol , provider ) = tuple ;
62
+
63
+ if ( provider . GlobalOptions
64
+ . TryGetValue < bool > ( "_DLGenerateSource" , out var isGenerateSource )
65
+ is var result
66
+ && ! result )
67
+ {
68
+ // 此项目未设置必要的属性(通常这是不应该出现的,因为 buildTransitive 传递的编译目标会自动生成这些属性)。
69
+ return ;
70
+ }
71
+
72
+ if ( ! isGenerateSource )
73
+ {
74
+ // 属性设置为不生成源代码。
75
+ return ;
76
+ }
77
+
59
78
// 生成 Program.Logger.g.cs
60
79
var partialLoggerFile = GeneratorInfo . GetEmbeddedTemplateFile < Program > ( ) ;
61
80
var generatedLoggerText = ConvertPartialProgramLogger ( partialLoggerFile . Content , programTypeSymbol ) ;
0 commit comments