@@ -33,7 +33,7 @@ public static CompilationResult GenerateAssembly(Compilation compilation)
3333 } ;
3434 }
3535
36- private static readonly ConcurrentDictionary < string , Assembly > Assemblies = new ( ) ;
36+ private static readonly ConcurrentDictionary < string , Tuple < List < SyntaxTree > , Assembly > > Assemblies = new ( ) ;
3737
3838 private static readonly string [ ] DependencyAssemblies = new [ ]
3939 {
@@ -55,21 +55,33 @@ public static CompilationResult GenerateAssembly(Compilation compilation)
5555 public static Assembly GetAssembly ( string name )
5656 {
5757 Assemblies . TryGetValue ( name , out var assembly ) ;
58- return assembly ;
58+ return assembly . Item2 ;
5959 }
6060
61- public static Assembly Generate ( string name , string pattern , Generator generatorPrototype = null )
62- {
63- if ( Assemblies . ContainsKey ( name ) ) { return Assemblies [ name ] ; }
61+ public static Assembly Generate ( string name , string pattern , Generator generatorPrototype = null )
62+ {
63+ ( _ , var assembly ) = GenerateVerbose ( name , pattern , generatorPrototype ) ;
64+ return assembly ;
65+ }
66+
67+ public static ( List < SyntaxTree > , Assembly ) GenerateVerbose ( string name , string pattern , Generator generatorPrototype = null )
68+ {
69+ if ( Assemblies . TryGetValue ( name , out var assembly ) ) { return ( assembly . Item1 , assembly . Item2 ) ; }
6470
6571 var files = Glob . ExpandNames ( pattern ) . OrderByDescending ( f => f ) ;
6672
67- return GenerateFiles ( name , files , generatorPrototype ) ;
73+ return GenerateFilesVerbose ( name , files , generatorPrototype ) ;
6874 }
6975
70- public static Assembly GenerateFiles ( string name , IEnumerable < string > files , Generator generatorPrototype = null )
71- {
72- if ( Assemblies . ContainsKey ( name ) ) { return Assemblies [ name ] ; }
76+ public static Assembly GenerateFiles ( string name , IEnumerable < string > files , Generator generatorPrototype = null )
77+ {
78+ ( _ , var assembly ) = GenerateFilesVerbose ( name , files , generatorPrototype ) ;
79+ return assembly ;
80+ }
81+
82+ public static ( List < SyntaxTree > , Assembly ) GenerateFilesVerbose ( string name , IEnumerable < string > files , Generator generatorPrototype = null )
83+ {
84+ if ( Assemblies . TryGetValue ( name , out var assembly ) ) { return ( assembly . Item1 , assembly . Item2 ) ; }
7385
7486 generatorPrototype ??= new Generator
7587 {
@@ -123,28 +135,40 @@ public static Assembly GenerateFiles(string name, IEnumerable<string> files, Gen
123135
124136 gen . Generate ( files ) ;
125137
126- return CompileFiles ( name , output . Files ) ;
138+ return CompileFilesVerbose ( name , output . Files ) ;
127139 }
128140
129- public static Assembly CompileFiles ( string name , IEnumerable < string > files )
130- {
131- return Compile ( name , files . Select ( f => File . ReadAllText ( f ) ) . ToArray ( ) ) ;
141+ public static Assembly CompileFiles ( string name , IEnumerable < string > files )
142+ {
143+ ( _ , var assembly ) = CompileFilesVerbose ( name , files ) ;
144+ return assembly ;
145+ }
146+
147+ public static ( List < SyntaxTree > , Assembly ) CompileFilesVerbose ( string name , IEnumerable < string > files )
148+ {
149+ return CompileVerbose ( name , files . Select ( f => File . ReadAllText ( f ) ) . ToArray ( ) ) ;
132150 }
133151
134152 private static readonly LanguageVersion MaxLanguageVersion = Enum
135153 . GetValues ( typeof ( LanguageVersion ) )
136154 . Cast < LanguageVersion > ( )
137155 . Max ( ) ;
138156
139- public static Assembly Compile ( string name , params string [ ] contents )
157+ public static Assembly Compile ( string name , params string [ ] contents )
140158 {
159+ ( _ , var assembly ) = CompileVerbose ( name , contents ) ;
160+ return assembly ;
161+ }
162+
163+ public static ( List < SyntaxTree > , Assembly ) CompileVerbose ( string name , params string [ ] contents )
164+ {
141165 var trustedAssembliesPaths = ( ( string ) AppContext . GetData ( "TRUSTED_PLATFORM_ASSEMBLIES" ) ) . Split ( Path . PathSeparator ) ;
142166 var references = trustedAssembliesPaths
143167 . Where ( p => DependencyAssemblies . Contains ( Path . GetFileNameWithoutExtension ( p ) ) )
144168 . Select ( p => MetadataReference . CreateFromFile ( p ) )
145169 . ToList ( ) ;
146170 var options = new CSharpParseOptions ( kind : SourceCodeKind . Regular , languageVersion : MaxLanguageVersion ) ;
147- var syntaxTrees = contents . Select ( c => CSharpSyntaxTree . ParseText ( c , options ) ) ;
171+ var syntaxTrees = contents . Select ( c => CSharpSyntaxTree . ParseText ( c , options ) ) . ToList ( ) ;
148172 var compilation = CSharpCompilation . Create ( name , syntaxTrees )
149173 . AddReferences ( references )
150174 . WithOptions ( new CSharpCompilationOptions ( OutputKind . DynamicallyLinkedLibrary ) ) ;
@@ -155,9 +179,9 @@ public static Assembly Compile(string name, params string[] contents)
155179 Assert . True ( result . Result . Success ) ;
156180 Assert . NotNull ( result . Assembly ) ;
157181
158- Assemblies [ name ] = result . Assembly ;
182+ Assemblies [ name ] = Tuple . Create ( syntaxTrees , result . Assembly ) ;
159183
160- return result . Assembly ;
184+ return ( syntaxTrees , result . Assembly ) ;
161185 }
162186 }
163187}
0 commit comments