@@ -149,24 +149,20 @@ void exitCallback(int ret, string msg, bool silent)
149149 logger . LogDebug ( $ "Unresolved reference { r . Key } in project { r . Value } ") ;
150150 }
151151
152- var webViewExtractionOption = Environment . GetEnvironmentVariable ( EnvironmentVariableNames . WebViewGeneration ) ;
153- if ( webViewExtractionOption == null ||
154- bool . TryParse ( webViewExtractionOption , out var shouldExtractWebViews ) &&
155- shouldExtractWebViews )
152+ var sourceGenerators = new ISourceGenerator [ ]
156153 {
157- CompilationInfos . Add ( ( "WebView extraction enabled" , "1" ) ) ;
158- GenerateSourceFilesFromWebViews ( ) ;
159- }
160- else
154+ new ImplicitUsingsGenerator ( fileContent , logger , tempWorkingDirectory ) ,
155+ new WebViewGenerator ( fileProvider , fileContent , dotnet , this , logger , tempWorkingDirectory , usedReferences . Keys )
156+ } ;
157+
158+ foreach ( var sourceGenerator in sourceGenerators )
161159 {
162- CompilationInfos . Add ( ( "WebView extraction enabled" , "0" ) ) ;
160+ this . generatedSources . AddRange ( sourceGenerator . Generate ( ) ) ;
163161 }
164162
165163 CompilationInfos . Add ( ( "UseWPF set" , fileContent . UseWpf ? "1" : "0" ) ) ;
166164 CompilationInfos . Add ( ( "UseWindowsForms set" , fileContent . UseWindowsForms ? "1" : "0" ) ) ;
167165
168- GenerateSourceFileFromImplicitUsings ( ) ;
169-
170166 const int align = 6 ;
171167 logger . LogInfo ( "" ) ;
172168 logger . LogInfo ( "Build analysis summary:" ) ;
@@ -365,14 +361,9 @@ private void RemoveNugetPackageReference(string packagePrefix, ISet<AssemblyLook
365361 }
366362 }
367363
368- private bool IsAspNetCoreDetected ( )
369- {
370- return fileContent . IsNewProjectStructureUsed && fileContent . UseAspNetCoreDlls ;
371- }
372-
373364 private void AddAspNetCoreFrameworkDlls ( ISet < AssemblyLookupLocation > dllLocations , ISet < string > frameworkLocations )
374365 {
375- if ( ! IsAspNetCoreDetected ( ) )
366+ if ( ! fileContent . IsAspNetCoreDetected )
376367 {
377368 return ;
378369 }
@@ -413,103 +404,6 @@ private void AddMicrosoftWindowsDesktopDlls(ISet<AssemblyLookupLocation> dllLoca
413404 . FullName ;
414405 }
415406
416- private void GenerateSourceFileFromImplicitUsings ( )
417- {
418- var usings = new HashSet < string > ( ) ;
419- if ( ! fileContent . UseImplicitUsings )
420- {
421- return ;
422- }
423-
424- // Hardcoded values from https://learn.microsoft.com/en-us/dotnet/core/project-sdk/overview#implicit-using-directives
425- usings . UnionWith ( [ "System" , "System.Collections.Generic" , "System.IO" , "System.Linq" , "System.Net.Http" , "System.Threading" ,
426- "System.Threading.Tasks" ] ) ;
427-
428- if ( fileContent . UseAspNetCoreDlls )
429- {
430- usings . UnionWith ( [ "System.Net.Http.Json" , "Microsoft.AspNetCore.Builder" , "Microsoft.AspNetCore.Hosting" ,
431- "Microsoft.AspNetCore.Http" , "Microsoft.AspNetCore.Routing" , "Microsoft.Extensions.Configuration" ,
432- "Microsoft.Extensions.DependencyInjection" , "Microsoft.Extensions.Hosting" , "Microsoft.Extensions.Logging" ] ) ;
433- }
434-
435- if ( fileContent . UseWindowsForms )
436- {
437- usings . UnionWith ( [ "System.Drawing" , "System.Windows.Forms" ] ) ;
438- }
439-
440- usings . UnionWith ( fileContent . CustomImplicitUsings ) ;
441-
442- logger . LogInfo ( $ "Generating source file for implicit usings. Namespaces: { string . Join ( ", " , usings . OrderBy ( u => u ) ) } ") ;
443-
444- if ( usings . Count > 0 )
445- {
446- var tempDir = GetTemporaryWorkingDirectory ( "implicitUsings" ) ;
447- var path = Path . Combine ( tempDir , "GlobalUsings.g.cs" ) ;
448- using ( var writer = new StreamWriter ( path ) )
449- {
450- writer . WriteLine ( "// <auto-generated/>" ) ;
451- writer . WriteLine ( "" ) ;
452-
453- foreach ( var u in usings . OrderBy ( u => u ) )
454- {
455- writer . WriteLine ( $ "global using global::{ u } ;") ;
456- }
457- }
458-
459- this . generatedSources . Add ( path ) ;
460- }
461- }
462-
463- private void GenerateSourceFilesFromWebViews ( )
464- {
465- var views = fileProvider . RazorViews ;
466- if ( views . Count == 0 )
467- {
468- return ;
469- }
470-
471- logger . LogInfo ( $ "Found { views . Count } cshtml and razor files.") ;
472-
473- if ( ! IsAspNetCoreDetected ( ) )
474- {
475- logger . LogInfo ( "Generating source files from cshtml files is only supported for new (SDK-style) project files" ) ;
476- return ;
477- }
478-
479- logger . LogInfo ( "Generating source files from cshtml and razor files..." ) ;
480-
481- var sdk = new Sdk ( dotnet ) . GetNewestSdk ( ) ;
482- if ( sdk != null )
483- {
484- try
485- {
486- var razor = new Razor ( sdk , dotnet , logger ) ;
487- var targetDir = GetTemporaryWorkingDirectory ( "razor" ) ;
488- var generatedFiles = razor . GenerateFiles ( views , usedReferences . Keys , targetDir ) ;
489- this . generatedSources . AddRange ( generatedFiles ) ;
490- }
491- catch ( Exception ex )
492- {
493- // It's okay, we tried our best to generate source files from cshtml files.
494- logger . LogInfo ( $ "Failed to generate source files from cshtml files: { ex . Message } ") ;
495- }
496- }
497- }
498-
499- /// <summary>
500- /// Creates a temporary directory with the given subfolder name.
501- /// The created directory might be inside the repo folder, and it is deleted when the object is disposed.
502- /// </summary>
503- /// <param name="subfolder"></param>
504- /// <returns></returns>
505- private string GetTemporaryWorkingDirectory ( string subfolder )
506- {
507- var temp = Path . Combine ( tempWorkingDirectory . ToString ( ) , subfolder ) ;
508- Directory . CreateDirectory ( temp ) ;
509-
510- return temp ;
511- }
512-
513407 /// <summary>
514408 /// Resolves conflicts between all of the resolved references.
515409 /// If the same assembly name is duplicated with different versions,
0 commit comments