@@ -31,82 +31,77 @@ public class ConfigurationKeysGenerator : IIncrementalGenerator
3131 /// <inheritdoc />
3232 public void Initialize ( IncrementalGeneratorInitializationContext context )
3333 {
34- // Get the supported-configurations.json file
35- var jsonFile = context . AdditionalTextsProvider
36- . Where ( static file => Path . GetFileName ( file . Path ) . Equals ( SupportedConfigurationsFileName , StringComparison . OrdinalIgnoreCase ) )
37- . WithTrackingName ( TrackingNames . ConfigurationKeysGenAdditionalText ) ;
38-
39- // Get the supported-configurations-docs.yaml file (optional)
40- var yamlFile = context . AdditionalTextsProvider
41- . Where ( static file => Path . GetFileName ( file . Path ) . Equals ( SupportedConfigurationsDocsFileName , StringComparison . OrdinalIgnoreCase ) )
42- . WithTrackingName ( TrackingNames . ConfigurationKeysGenYamlAdditionalText ) ;
43-
44- // Get the configuration_keys_mapping.json file (optional)
45- var mappingFile = context . AdditionalTextsProvider
46- . Where ( static file => Path . GetFileName ( file . Path ) . Equals ( ConfigurationKeysMappingFileName , StringComparison . OrdinalIgnoreCase ) )
47- . WithTrackingName ( TrackingNames . ConfigurationKeysGenMappingAdditionalText ) ;
48-
49- // Combine all files
50- var combinedFiles = jsonFile . Collect ( ) . Combine ( yamlFile . Collect ( ) ) . Combine ( mappingFile . Collect ( ) ) ;
51-
52- var configContent = combinedFiles . Select ( static ( files , ct ) =>
34+ // Get all configuration files
35+ var configFiles = context . AdditionalTextsProvider
36+ . Where ( static file =>
37+ {
38+ var fileName = Path . GetFileName ( file . Path ) ;
39+ return fileName . Equals ( SupportedConfigurationsFileName , StringComparison . OrdinalIgnoreCase ) ||
40+ fileName . Equals ( SupportedConfigurationsDocsFileName , StringComparison . OrdinalIgnoreCase ) ||
41+ fileName . Equals ( ConfigurationKeysMappingFileName , StringComparison . OrdinalIgnoreCase ) ;
42+ } )
43+ . WithTrackingName ( TrackingNames . ConfigurationKeysGenAdditionalText ) ;
44+ var parsedConfig = configFiles . Collect ( ) . Select ( static ( allFiles , ct ) =>
45+ {
46+ // Find each file type
47+ AdditionalText ? jsonFile = null ;
48+ AdditionalText ? yamlFile = null ;
49+ AdditionalText ? mappingFile = null ;
50+
51+ foreach ( var file in allFiles )
5352 {
54- var ( ( jsonFiles , yamlFiles ) , mappingFiles ) = files ;
55-
56- if ( jsonFiles . Length == 0 )
53+ var fileName = Path . GetFileName ( file . Path ) ;
54+ if ( fileName . Equals ( SupportedConfigurationsFileName , StringComparison . OrdinalIgnoreCase ) )
5755 {
58- return new Result < ( string Json , string ? Yaml , string ? Mapping ) > (
59- ( string . Empty , null , null ) ,
60- new EquatableArray < DiagnosticInfo > (
61- [
62- CreateDiagnosticInfo ( "DDSG0005" , "Configuration file not found" , $ "The file '{ SupportedConfigurationsFileName } ' was not found. Make sure the supported-configurations.json file exists and is included as an AdditionalFile.", DiagnosticSeverity . Error )
63- ] ) ) ;
56+ jsonFile = file ;
6457 }
65-
66- var jsonResult = ExtractConfigurationContent ( jsonFiles [ 0 ] , ct ) ;
67- if ( jsonResult . Errors . Count > 0 )
58+ else if ( fileName . Equals ( SupportedConfigurationsDocsFileName , StringComparison . OrdinalIgnoreCase ) )
6859 {
69- return new Result < ( string Json , string ? Yaml , string ? Mapping ) > ( ( string . Empty , null , null ) , jsonResult . Errors ) ;
70- }
71-
72- string ? yamlContent = null ;
73- if ( yamlFiles . Length > 0 )
74- {
75- var yamlResult = ExtractConfigurationContent ( yamlFiles [ 0 ] , ct ) ;
76- if ( yamlResult . Errors . Count == 0 )
77- {
78- yamlContent = yamlResult . Value ;
79- }
80-
81- // YAML is optional, so we don't fail if it has errors
60+ yamlFile = file ;
8261 }
83-
84- string ? mappingContent = null ;
85- if ( mappingFiles . Length > 0 )
62+ else if ( fileName . Equals ( ConfigurationKeysMappingFileName , StringComparison . OrdinalIgnoreCase ) )
8663 {
87- var mappingResult = ExtractConfigurationContent ( mappingFiles [ 0 ] , ct ) ;
88- if ( mappingResult . Errors . Count == 0 )
89- {
90- mappingContent = mappingResult . Value ;
91- }
92-
93- // Mapping is optional, so we don't fail if it has errors
64+ mappingFile = file ;
9465 }
66+ }
9567
96- return new Result < ( string Json , string ? Yaml , string ? Mapping ) > ( ( jsonResult . Value , yamlContent , mappingContent ) , new EquatableArray < DiagnosticInfo > ( ) ) ;
97- } )
98- . WithTrackingName ( TrackingNames . ConfigurationKeysGenContentExtracted ) ;
68+ // JSON file is required
69+ if ( jsonFile is null )
70+ {
71+ return new Result < ConfigurationData > (
72+ null ! ,
73+ new EquatableArray < DiagnosticInfo > (
74+ [
75+ CreateDiagnosticInfo ( "DDSG0005" , "Configuration file not found" , $ "The file '{ SupportedConfigurationsFileName } ' was not found. Make sure the supported-configurations.json file exists and is included as an AdditionalFile.", DiagnosticSeverity . Error )
76+ ] ) ) ;
77+ }
78+
79+ // Read JSON file (required)
80+ var jsonText = jsonFile . GetText ( ct ) ;
81+ if ( jsonText is null )
82+ {
83+ return new Result < ConfigurationData > (
84+ null ! ,
85+ new EquatableArray < DiagnosticInfo > ( [ CreateDiagnosticInfo ( "DDSG0006" , "Configuration file read error" , $ "Unable to read the content of '{ SupportedConfigurationsFileName } '.", DiagnosticSeverity . Error ) ] ) ) ;
86+ }
87+
88+ // Read optional YAML file
89+ string ? yamlContent = null ;
90+ if ( yamlFile is not null )
91+ {
92+ yamlContent = yamlFile . GetText ( ct ) ? . ToString ( ) ;
93+ }
9994
100- var parsedConfig = configContent . Select ( static ( extractResult , ct ) =>
101- {
102- if ( extractResult . Errors . Count > 0 )
103- {
104- return new Result < ConfigurationData > ( null ! , extractResult . Errors ) ;
105- }
95+ // Read optional mapping file
96+ string ? mappingContent = null ;
97+ if ( mappingFile is not null )
98+ {
99+ mappingContent = mappingFile . GetText ( ct ) ? . ToString ( ) ;
100+ }
106101
107- return ParseConfigurationContent ( extractResult . Value . Json , extractResult . Value . Yaml , extractResult . Value . Mapping , ct ) ;
108- } )
109- . WithTrackingName ( TrackingNames . ConfigurationKeysGenParseConfiguration ) ;
102+ return ParseConfigurationContent ( jsonText . ToString ( ) , yamlContent , mappingContent , ct ) ;
103+ } )
104+ . WithTrackingName ( TrackingNames . ConfigurationKeysGenParseConfiguration ) ;
110105
111106 context . RegisterSourceOutput (
112107 parsedConfig ,
@@ -141,34 +136,6 @@ private static void Execute(SourceProductionContext context, Result<Configuratio
141136 }
142137 }
143138
144- private static Result < string > ExtractConfigurationContent ( AdditionalText file , CancellationToken cancellationToken )
145- {
146- try
147- {
148- var sourceText = file . GetText ( cancellationToken ) ;
149- if ( sourceText is null )
150- {
151- return new Result < string > (
152- string . Empty ,
153- new EquatableArray < DiagnosticInfo > (
154- [
155- CreateDiagnosticInfo ( "DDSG0006" , "Configuration file read error" , $ "Unable to read the content of '{ SupportedConfigurationsFileName } '.", DiagnosticSeverity . Error )
156- ] ) ) ;
157- }
158-
159- return new Result < string > ( sourceText . ToString ( ) , new EquatableArray < DiagnosticInfo > ( ) ) ;
160- }
161- catch ( Exception ex )
162- {
163- return new Result < string > (
164- string . Empty ,
165- new EquatableArray < DiagnosticInfo > (
166- [
167- CreateDiagnosticInfo ( "DDSG0006" , "Configuration file read error" , $ "Error reading '{ SupportedConfigurationsFileName } ': { ex . Message } ", DiagnosticSeverity . Error )
168- ] ) ) ;
169- }
170- }
171-
172139 private static Result < ConfigurationData > ParseConfigurationContent ( string jsonContent , string ? yamlContent , string ? mappingContent , CancellationToken cancellationToken )
173140 {
174141 try
@@ -183,7 +150,7 @@ private static Result<ConfigurationData> ParseConfigurationContent(string jsonCo
183150 null ! ,
184151 new EquatableArray < DiagnosticInfo > (
185152 [
186- CreateDiagnosticInfo ( "DDSG0007" , "JSON parse error" , "Failed to find 'supportedConfigurations' section in supported-configurations.json ." , DiagnosticSeverity . Error )
153+ CreateDiagnosticInfo ( "DDSG0007" , "JSON parse error" , $ "Failed to find 'supportedConfigurations' section in { SupportedConfigurationsFileName } .", DiagnosticSeverity . Error )
187154 ] ) ) ;
188155 }
189156
@@ -323,7 +290,7 @@ private static string GenerateProductPartialClass(string product, List<KeyValueP
323290 var sb = new StringBuilder ( ) ;
324291
325292 AppendFileHeader ( sb ) ;
326- sb . AppendLine ( $ "namespace { Namespace } ;") ;
293+ sb . Append ( "namespace " ) . Append ( Namespace ) . AppendLine ( " ;") ;
327294 sb . AppendLine ( ) ;
328295
329296 if ( string . IsNullOrEmpty ( product ) )
@@ -509,27 +476,15 @@ private static string KeyToConstName(string key, string[]? productNames = null,
509476 }
510477
511478 private static string [ ] ProductNameEquivalents ( string ? productName )
512- {
513- if ( string . IsNullOrEmpty ( productName ) )
479+ => productName switch
514480 {
515- return new [ ] { string . Empty } ;
516- }
517-
518- // we need to keep comparison case-sensitive as there are keys like TraceRemoveIntegrationServiceNamesEnabled and we don't want to strip Tracer
519- switch ( productName )
520- {
521- case "AppSec" :
522- return new [ ] { "Appsec" } ;
523- case "Tracer" :
524- return new [ ] { "Trace" } ;
525- case "CiVisibility" :
526- return new [ ] { "Civisibility" } ;
527- case "OpenTelemetry" :
528- return new [ ] { "Otel" } ;
529- default :
530- return new [ ] { productName ! } ;
531- }
532- }
481+ null or "" => [ string . Empty ] ,
482+ "AppSec" => [ "Appsec" ] ,
483+ "Tracer" => [ "Trace" ] ,
484+ "CiVisibility" => [ "Civisibility" ] ,
485+ "OpenTelemetry" => [ "Otel" ] ,
486+ _ => [ productName ]
487+ } ;
533488
534489 private static DiagnosticInfo CreateDiagnosticInfo ( string id , string title , string message , DiagnosticSeverity severity )
535490 {
0 commit comments