11namespace Try . Core
22{
33 using System ;
4- using System . Collections . Concurrent ;
54 using System . Collections . Generic ;
65 using System . Collections . Immutable ;
76 using System . ComponentModel . DataAnnotations ;
@@ -25,16 +24,18 @@ public class CompilationService
2524 public const string DefaultRootNamespace = $ "{ nameof ( Try ) } .{ nameof ( UserComponents ) } ";
2625
2726 private const string WorkingDirectory = "/TryMudBlazor/" ;
28- private const string DefaultImports = @"@using System.ComponentModel.DataAnnotations
29- @using System.Linq
30- @using System.Net.Http
31- @using System.Net.Http.Json
32- @using Microsoft.AspNetCore.Components.Forms
33- @using Microsoft.AspNetCore.Components.Routing
34- @using Microsoft.AspNetCore.Components.Web
35- @using Microsoft.JSInterop
36- @using MudBlazor
37- " ;
27+ private static readonly string [ ] DefaultImports =
28+ [
29+ "@using System.ComponentModel.DataAnnotations" ,
30+ "@using System.Linq" ,
31+ "@using System.Net.Http" ,
32+ "@using System.Net.Http.Json" ,
33+ "@using Microsoft.AspNetCore.Components.Forms" ,
34+ "@using Microsoft.AspNetCore.Components.Routing" ,
35+ "@using Microsoft.AspNetCore.Components.Web" ,
36+ "@using Microsoft.JSInterop" ,
37+ "@using MudBlazor"
38+ ] ;
3839
3940 private const string MudBlazorServices = @"
4041<MudDialogProvider FullWidth=""true"" MaxWidth=""MaxWidth.ExtraSmall"" />
@@ -53,9 +54,8 @@ @using MudBlazor
5354 ConfigurationName : "Blazor" ,
5455 Extensions : ImmutableArray < RazorExtension > . Empty ) ;
5556
56- public static async Task InitAsync ( HttpClient httpClient )
57+ public static async Task InitAsync ( Func < IReadOnlyCollection < string > , ValueTask < IReadOnlyList < byte [ ] > > > getReferencedDllsBytesFunc )
5758 {
58-
5959 var basicReferenceAssemblyRoots = new [ ]
6060 {
6161 typeof ( Console ) . Assembly , // System.Console
@@ -71,23 +71,16 @@ public static async Task InitAsync(HttpClient httpClient)
7171 typeof ( WebAssemblyHostBuilder ) . Assembly , // Microsoft.AspNetCore.Components.WebAssembly
7272 typeof ( FluentValidation . AbstractValidator < > ) . Assembly ,
7373 } ;
74-
75- var assemblyNames = basicReferenceAssemblyRoots
76- . SelectMany ( assembly => assembly . GetReferencedAssemblies ( ) . Concat ( new [ ] { assembly . GetName ( ) } ) )
77- . Select ( x => x . Name )
78- . Distinct ( )
79- . ToList ( ) ;
80-
81- var assemblyStreams = await GetStreamFromHttpAsync ( httpClient , assemblyNames ) ;
82-
83- var allReferenceAssemblies = assemblyStreams . ToDictionary ( a => a . Key , a => MetadataReference . CreateFromStream ( a . Value ) ) ;
84-
85- var basicReferenceAssemblies = allReferenceAssemblies
86- . Where ( a => basicReferenceAssemblyRoots
87- . Select ( x => x . GetName ( ) . Name )
88- . Union ( basicReferenceAssemblyRoots . SelectMany ( y => y . GetReferencedAssemblies ( ) . Select ( z => z . Name ) ) )
89- . Any ( n => n == a . Key ) )
90- . Select ( a => a . Value )
74+ var assemblyNames = await getReferencedDllsBytesFunc ( basicReferenceAssemblyRoots
75+ . SelectMany ( assembly => assembly . GetReferencedAssemblies ( ) . Concat (
76+ [
77+ assembly . GetName ( )
78+ ] ) )
79+ . Select ( assemblyName => assemblyName . Name )
80+ . ToHashSet ( ) ) ;
81+
82+ var basicReferenceAssemblies = assemblyNames
83+ . Select ( peImage => MetadataReference . CreateFromImage ( peImage , MetadataReferenceProperties . Assembly ) )
9184 . ToList ( ) ;
9285
9386 _baseCompilation = CSharpCompilation . Create (
@@ -112,10 +105,7 @@ public async Task<CompileToAssemblyResult> CompileToAssemblyAsync(
112105 ICollection < CodeFile > codeFiles ,
113106 Func < string , Task > updateStatusFunc ) // TODO: try convert to event
114107 {
115- if ( codeFiles == null )
116- {
117- throw new ArgumentNullException ( nameof ( codeFiles ) ) ;
118- }
108+ ArgumentNullException . ThrowIfNull ( codeFiles ) ;
119109
120110 var cSharpResults = await this . CompileToCSharpAsync ( codeFiles , updateStatusFunc ) ;
121111
@@ -125,25 +115,6 @@ public async Task<CompileToAssemblyResult> CompileToAssemblyAsync(
125115 return result ;
126116 }
127117
128- private static async Task < IDictionary < string , Stream > > GetStreamFromHttpAsync (
129- HttpClient httpClient ,
130- IEnumerable < string > assemblyNames )
131- {
132- var streams = new ConcurrentDictionary < string , Stream > ( ) ;
133-
134- await Task . WhenAll (
135- assemblyNames . Select ( async assemblyName =>
136- {
137- var result = await httpClient . GetAsync ( $ "/_framework/{ assemblyName } .dll") ;
138-
139- result . EnsureSuccessStatusCode ( ) ;
140-
141- streams . TryAdd ( assemblyName , await result . Content . ReadAsStreamAsync ( ) ) ;
142- } ) ) ;
143-
144- return streams ;
145- }
146-
147118 private static CompileToAssemblyResult CompileToAssembly ( IReadOnlyList < CompileToCSharpResult > cSharpResults )
148119 {
149120 if ( cSharpResults . Any ( r => r . Diagnostics . Any ( d => d . Severity == DiagnosticSeverity . Error ) ) )
@@ -288,16 +259,16 @@ private async Task<IReadOnlyList<CompileToCSharpResult>> CompileToCSharpAsync(
288259 }
289260
290261 private RazorProjectEngine CreateRazorProjectEngine ( IReadOnlyList < MetadataReference > references ) =>
291- RazorProjectEngine . Create ( configuration , fileSystem , b =>
262+ RazorProjectEngine . Create ( configuration , fileSystem , builder =>
292263 {
293- b . SetRootNamespace ( DefaultRootNamespace ) ;
294- b . AddDefaultImports ( DefaultImports ) ;
264+ builder . SetRootNamespace ( DefaultRootNamespace ) ;
265+ builder . AddDefaultImports ( DefaultImports ) ;
295266
296267 // Features that use Roslyn are mandatory for components
297- CompilerFeatures . Register ( b ) ;
268+ CompilerFeatures . Register ( builder ) ;
298269
299- b . Features . Add ( new CompilationTagHelperFeature ( ) ) ;
300- b . Features . Add ( new DefaultMetadataReferenceFeature { References = references } ) ;
270+ builder . Features . Add ( new CompilationTagHelperFeature ( ) ) ;
271+ builder . Features . Add ( new DefaultMetadataReferenceFeature { References = references } ) ;
301272 } ) ;
302273 }
303274}
0 commit comments