1+ using System . Collections . Generic ;
12using System . Collections . Immutable ;
23using System . Linq ;
34using System . Text ;
45using Microsoft . CodeAnalysis ;
6+ using Microsoft . CodeAnalysis . CSharp ;
57
68namespace Allure . Build . SourceGenerators ;
79
@@ -16,6 +18,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
1618 pair . Right
1719 . GetOptions ( pair . Left )
1820 . TryGetValue ( "build_metadata.AdditionalFiles.Allure_ProjectSuffix" , out var value )
21+ && SyntaxFacts . IsValidIdentifier ( value )
1922 ? value
2023 : null )
2124 . Where ( static ( suffix ) => ! string . IsNullOrEmpty ( suffix ) )
@@ -24,42 +27,98 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2427
2528 context . RegisterSourceOutput ( sampleProjectSuffixes , static ( spc , suffixes ) =>
2629 {
27- var sb = new StringBuilder ( ) ;
28- sb . AppendLine ( "namespace Allure.Testing;" ) ;
29- sb . AppendLine ( ) ;
30- sb . AppendLine ( "/// <summary>" ) ;
31- sb . AppendLine ( "/// Use the static properties of this class to pass selected samples to" ) ;
32- sb . AppendLine ( "/// <see cref=\" global::Allure.Testing.AllureSampleRunner\" /> methods." ) ;
33- sb . AppendLine ( "/// </summary>" ) ;
34- sb . AppendLine ( "/// <remarks>" ) ;
35- sb . AppendLine ( "/// This class was generated by Allure.Build.SourceGenerator based on the" ) ;
36- sb . AppendLine ( "/// content of this project's <c>AllureSample</c> items" ) ;
37- sb . AppendLine ( "/// (by default, these are the files from the <c>./Samples/</c> directory)." ) ;
38- sb . AppendLine ( "/// Please, make sure the top-level elements in this directory have names" ) ;
39- sb . AppendLine ( "/// that are valid C# identifiers (a .cs extension is allowed at the end of C#" ) ;
40- sb . AppendLine ( "/// source files)." ) ;
41- sb . AppendLine ( "/// </remarks>" ) ;
42- sb . AppendLine ( "internal record class AllureSampleRegistry" ) ;
43- sb . AppendLine ( "{" ) ;
44- sb . AppendLine ( " public string Name { get; init; }" ) ;
45- sb . AppendLine ( ) ;
46- sb . AppendLine ( " AllureSampleRegistry(string name) => this.Name = name;" ) ;
47- sb . AppendLine ( ) ;
48- bool extraNewLine = false ;
49- foreach ( var suffix in suffixes )
50- {
51- if ( extraNewLine )
52- {
53- sb . AppendLine ( ) ;
54- }
30+ var code = GenerateSampleRegistryCode ( suffixes ) ;
5531
56- sb . AppendFormat ( " public static AllureSampleRegistry {0} {{ get; set; }} = new(\" {0}\" );" , suffix ) ;
57- sb . AppendLine ( ) ;
58- extraNewLine = true ;
59- }
60- sb . AppendLine ( "}" ) ;
61-
62- spc . AddSource ( "AllureSampleRegistry.g.cs" , sb . ToString ( ) ) ;
32+ spc . AddSource ( "AllureSampleRegistry.g.cs" , code ) ;
6333 } ) ;
6434 }
35+
36+ static string GenerateSampleRegistryCode ( IEnumerable < string > sampleSuffixes )
37+ {
38+ var sb = new StringBuilder ( ) ;
39+ sb . AppendLine ( "namespace Allure.Testing;" ) ;
40+ sb . AppendLine ( ) ;
41+ AppendRegistryEntryClass ( sb ) ;
42+ sb . AppendLine ( ) ;
43+ AppendRegistryClass ( sb , sampleSuffixes ) ;
44+ return sb . ToString ( ) ;
45+ }
46+
47+ static void AppendRegistryEntryClass ( StringBuilder sb )
48+ {
49+ sb . AppendLine ( "/// <summary>" ) ;
50+ sb . AppendLine ( "/// Contains the data required to run a specific sample project and" ) ;
51+ sb . AppendLine ( "/// access its result files." ) ;
52+ sb . AppendLine ( "/// </summary>" ) ;
53+ sb . AppendLine ( "/// <remarks>" ) ;
54+ sb . AppendLine ( "/// Use the instances exposed by" ) ;
55+ sb . AppendLine ( "/// <see cref=\" global::Allure.Testing.AllureSampleRegistry\" />" ) ;
56+ sb . AppendLine ( "/// instead of creating your own." ) ;
57+ sb . AppendLine ( "/// </remarks>" ) ;
58+ sb . AppendLine ( "internal record class AllureSampleRegistryEntry(" ) ;
59+ sb . AppendLine ( " string Name," ) ;
60+ sb . AppendLine ( " string ProjectPath," ) ;
61+ sb . AppendLine ( " string DefaultResultsPath" ) ;
62+ sb . AppendLine ( ");" ) ;
63+ }
64+
65+ static void AppendRegistryClass ( StringBuilder sb , IEnumerable < string > suffixes )
66+ {
67+ sb . AppendLine ( "/// <summary>" ) ;
68+ sb . AppendLine ( "/// Exposes a set of testing samples available to this project" ) ;
69+ sb . AppendLine ( "/// via a set of static properties." ) ;
70+ sb . AppendLine ( "/// </summary>" ) ;
71+ sb . AppendLine ( "/// <remarks>" ) ;
72+ sb . AppendLine ( "/// Pass a selected sample to" ) ;
73+ sb . AppendLine ( "/// <see cref=\" global::Allure.Testing.AllureSampleRunner\" /> methods" ) ;
74+ sb . AppendLine ( "/// to run it and access the test results." ) ;
75+ sb . AppendLine ( "/// </remarks>" ) ;
76+ sb . AppendLine ( "internal static class AllureSampleRegistry" ) ;
77+ sb . AppendLine ( "{" ) ;
78+ AppendRegistryEntryProperties ( sb , suffixes ) ;
79+ sb . AppendLine ( "}" ) ;
80+ }
81+
82+ static void AppendRegistryEntryProperties ( StringBuilder sb , IEnumerable < string > suffixes )
83+ {
84+ var first = suffixes . FirstOrDefault ( ) ;
85+ if ( first is not null )
86+ {
87+ AppendRegistryEntryProperty ( sb , first ) ;
88+ }
89+
90+ foreach ( var rest in suffixes . Skip ( 1 ) )
91+ {
92+ sb . AppendLine ( ) ;
93+ AppendRegistryEntryProperty ( sb , rest ) ;
94+ }
95+ }
96+
97+ static void AppendRegistryEntryProperty ( StringBuilder sb , string suffix )
98+ {
99+ sb . AppendFormat ( " public static global::Allure.Testing.AllureSampleRegistryEntry {0} {{ get; }}" , suffix ) ;
100+ sb . AppendLine ( ) ;
101+ sb . AppendLine ( " = new(" ) ;
102+ sb . AppendFormat ( " \" {0}\" ," , suffix ) ;
103+ sb . AppendLine ( ) ;
104+ sb . AppendLine ( " global::System.IO.Path.Combine(" ) ;
105+ sb . AppendLine ( " global::Allure.Testing.AllureBuildProperties.Allure_SampleSolutionDir," ) ;
106+ sb . AppendLine ( " string.Format(" ) ;
107+ sb . AppendFormat ( " \" {{0}}.{0}\" ," , suffix ) ;
108+ sb . AppendLine ( ) ;
109+ sb . AppendLine ( " global::Allure.Testing.AllureBuildProperties.Allure_SampleSolutionName" ) ;
110+ sb . AppendLine ( " )," ) ;
111+ sb . AppendLine ( " string.Format(" ) ;
112+ sb . AppendFormat ( " \" {{0}}.{0}.csproj\" ," , suffix ) ;
113+ sb . AppendLine ( ) ;
114+ sb . AppendLine ( " global::Allure.Testing.AllureBuildProperties.Allure_SampleSolutionName" ) ;
115+ sb . AppendLine ( " )" ) ;
116+ sb . AppendLine ( " )," ) ;
117+ sb . AppendLine ( " string.Format(" ) ;
118+ sb . AppendLine ( " global::Allure.Testing.AllureBuildProperties.Allure_SampleResultsDirectoryFormat," ) ;
119+ sb . AppendFormat ( " \" {0}\" " , suffix ) ;
120+ sb . AppendLine ( ) ;
121+ sb . AppendLine ( " )" ) ;
122+ sb . AppendLine ( " );" ) ;
123+ }
65124}
0 commit comments