33using Microsoft . CodeAnalysis ;
44using Microsoft . CodeAnalysis . Text ;
55
6- namespace Microsoft . Maui . TestUtils . DeviceTests . Runners . SourceGen ;
7-
8- public class RunnerGenerator
6+ namespace Microsoft . Maui . TestUtils . DeviceTests . Runners . SourceGen
97{
10- public RunnerGenerator ( GeneratorExecutionContext context , string targetFramework )
8+ public class RunnerGenerator
119 {
12- Context = context ;
10+ public RunnerGenerator ( GeneratorExecutionContext context , string targetFramework )
11+ {
12+ Context = context ;
1313
14- TargetFramework = targetFramework ;
14+ TargetFramework = targetFramework ;
1515
16- context . AnalyzerConfigOptions . GlobalOptions . TryGetValue ( "build_property.ApplicationId" , out var applicationId ) ;
17- context . Log ( $ "ApplicationId: { applicationId } ") ;
18- ApplicationId = applicationId ?? throw new Exception ( "ApplicationId needs to be set." ) ;
16+ context . AnalyzerConfigOptions . GlobalOptions . TryGetValue ( "build_property.ApplicationId" , out var applicationId ) ;
17+ context . Log ( $ "ApplicationId: { applicationId } ") ;
18+ ApplicationId = applicationId ?? throw new Exception ( "ApplicationId needs to be set." ) ;
1919
20- context . AnalyzerConfigOptions . GlobalOptions . TryGetValue ( "build_property.ApplicationTitle" , out var applicationTitle ) ;
21- context . Log ( $ "ApplicationTitle: { applicationTitle } ") ;
22- ApplicationTitle = applicationTitle ?? "Tests" ;
20+ context . AnalyzerConfigOptions . GlobalOptions . TryGetValue ( "build_property.ApplicationTitle" , out var applicationTitle ) ;
21+ context . Log ( $ "ApplicationTitle: { applicationTitle } ") ;
22+ ApplicationTitle = applicationTitle ?? "Tests" ;
2323
24- context . AnalyzerConfigOptions . GlobalOptions . TryGetValue ( "build_property.RootNamespace" , out var rootNamespace ) ;
25- context . Log ( $ "RootNamespace: { rootNamespace } ") ;
26- RootNamespace = rootNamespace ?? "TestRunnerNamespace" ;
24+ context . AnalyzerConfigOptions . GlobalOptions . TryGetValue ( "build_property.RootNamespace" , out var rootNamespace ) ;
25+ context . Log ( $ "RootNamespace: { rootNamespace } ") ;
26+ RootNamespace = rootNamespace ?? "TestRunnerNamespace" ;
2727
28- ContainsSplashScreen = false ;
29- foreach ( var file in context . AdditionalFiles )
30- {
31- var options = context . AnalyzerConfigOptions . GetOptions ( file ) ;
32- if ( options . TryGetValue ( "build_metadata.AdditionalFiles.IsMauiSplashScreen" , out var isMauiSplashScreen ) && bool . TryParse ( isMauiSplashScreen , out var isSplash ) && isSplash )
28+ ContainsSplashScreen = false ;
29+ foreach ( var file in context . AdditionalFiles )
3330 {
34- ContainsSplashScreen = true ;
35- break ;
31+ var options = context . AnalyzerConfigOptions . GetOptions ( file ) ;
32+ if ( options . TryGetValue ( "build_metadata.AdditionalFiles.IsMauiSplashScreen" , out var isMauiSplashScreen ) && bool . TryParse ( isMauiSplashScreen , out var isSplash ) && isSplash )
33+ {
34+ ContainsSplashScreen = true ;
35+ break ;
36+ }
3637 }
38+ context . Log ( $ "ContainsSplashScreen: { ContainsSplashScreen } ") ;
3739 }
38- context . Log ( $ "ContainsSplashScreen: { ContainsSplashScreen } ") ;
39- }
4040
41- public GeneratorExecutionContext Context { get ; }
41+ public GeneratorExecutionContext Context { get ; }
4242
43- public string TargetFramework { get ; }
43+ public string TargetFramework { get ; }
4444
45- public string RootNamespace { get ; }
45+ public string RootNamespace { get ; }
4646
47- public string ApplicationId { get ; }
47+ public string ApplicationId { get ; }
4848
49- public string ApplicationTitle { get ; }
49+ public string ApplicationTitle { get ; }
5050
51- public bool ContainsSplashScreen { get ; }
51+ public bool ContainsSplashScreen { get ; }
5252
53- public void Generate ( )
54- {
55- Context . Log ( $ "Generating runners...") ;
56-
57- if ( TargetFramework . IndexOf ( "-android" , StringComparison . OrdinalIgnoreCase ) != - 1 )
53+ public void Generate ( )
5854 {
59- var code = GenerateAndroidSource ( ) ;
60- var name = "TestRunner.Android.sg.cs" ;
55+ Context . Log ( $ "Generating runners...") ;
6156
62- AddSource ( name , code ) ;
63- }
64- else if ( TargetFramework . IndexOf ( "-ios" , StringComparison . OrdinalIgnoreCase ) != - 1 )
65- {
66- var code = GenerateIosSource ( ) ;
67- var name = "TestRunner.iOS.sg.cs" ;
57+ if ( TargetFramework . IndexOf ( "-android" , StringComparison . OrdinalIgnoreCase ) != - 1 )
58+ {
59+ var code = GenerateAndroidSource ( ) ;
60+ var name = "TestRunner.Android.sg.cs" ;
6861
69- AddSource ( name , code ) ;
70- }
71- else if ( TargetFramework . IndexOf ( "-maccatalyst " , StringComparison . OrdinalIgnoreCase ) != - 1 )
72- {
73- var code = GenerateIosSource ( ) ;
74- var name = "TestRunner.MacCatalyst .sg.cs" ;
62+ AddSource ( name , code ) ;
63+ }
64+ else if ( TargetFramework . IndexOf ( "-ios " , StringComparison . OrdinalIgnoreCase ) != - 1 )
65+ {
66+ var code = GenerateIosSource ( ) ;
67+ var name = "TestRunner.iOS .sg.cs" ;
7568
76- AddSource ( name , code ) ;
69+ AddSource ( name , code ) ;
70+ }
71+ else if ( TargetFramework . IndexOf ( "-maccatalyst" , StringComparison . OrdinalIgnoreCase ) != - 1 )
72+ {
73+ var code = GenerateIosSource ( ) ;
74+ var name = "TestRunner.MacCatalyst.sg.cs" ;
75+
76+ AddSource ( name , code ) ;
77+ }
7778 }
78- }
7979
80- protected void AddSource ( string filename , string contents )
81- {
82- Context . Log ( $ "AddSource: { filename } ") ;
83- Context . AddSource ( filename , SourceText . From ( contents , Encoding . UTF8 ) ) ;
84- }
80+ protected void AddSource ( string filename , string contents )
81+ {
82+ Context . Log ( $ "AddSource: { filename } ") ;
83+ Context . AddSource ( filename , SourceText . From ( contents , Encoding . UTF8 ) ) ;
84+ }
8585
86- string GenerateAndroidSource ( )
87- {
88- var mauiProgramName = "MauiProgram" ;
89- var mauiProgramFullName = @"global::" + RootNamespace + "." + mauiProgramName ;
90- var splash = ContainsSplashScreen ? @"Theme = ""@style/Maui.SplashTheme""," : "" ;
86+ string GenerateAndroidSource ( )
87+ {
88+ var mauiProgramName = "MauiProgram" ;
89+ var mauiProgramFullName = @"global::" + RootNamespace + "." + mauiProgramName ;
90+ var splash = ContainsSplashScreen ? @"Theme = ""@style/Maui.SplashTheme""," : "" ;
9191
92- var appName = "MainApplication" ;
93- var visualActivityName = "MainActivity" ;
92+ var appName = "MainApplication" ;
93+ var visualActivityName = "MainActivity" ;
9494
95- var instrumentationName = "TestInstrumentation" ;
96- var headlessActivityName = "TestActivity" ;
95+ var instrumentationName = "TestInstrumentation" ;
96+ var headlessActivityName = "TestActivity" ;
9797
98- return @"
98+ return @"
9999#if !SKIP_RUNNER_ENTRYPOINT_GENERATION && !SKIP_VISUAL_RUNNER_ENTRYPOINT_GENERATION && !SKIP_VISUAL_RUNNER_APPLICATION_GENERATION
100100namespace " + RootNamespace + @"
101101{
@@ -162,16 +162,16 @@ public partial class " + headlessActivityName + @" : global::Microsoft.Maui.Test
162162}
163163#endif
164164" ;
165- }
165+ }
166166
167- string GenerateIosSource ( )
168- {
169- var mauiProgramName = "MauiProgram" ;
170- var mauiProgramFullName = @"global::" + RootNamespace + "." + mauiProgramName ;
171- var visualDelegateName = "VisualRunnerAppDelegate" ;
172- var headlessDelegateName = "HeadlessRunnerAppDelegate" ;
167+ string GenerateIosSource ( )
168+ {
169+ var mauiProgramName = "MauiProgram" ;
170+ var mauiProgramFullName = @"global::" + RootNamespace + "." + mauiProgramName ;
171+ var visualDelegateName = "VisualRunnerAppDelegate" ;
172+ var headlessDelegateName = "HeadlessRunnerAppDelegate" ;
173173
174- return @"
174+ return @"
175175#if !SKIP_RUNNER_ENTRYPOINT_GENERATION && !SKIP_VISUAL_RUNNER_ENTRYPOINT_GENERATION && !SKIP_RUNNER_PROGRAM_GENERATION
176176namespace " + RootNamespace + @"
177177{
@@ -219,5 +219,6 @@ partial class " + headlessDelegateName + @" : global::Microsoft.Maui.TestUtils.D
219219}
220220#endif
221221" ;
222+ }
222223 }
223224}
0 commit comments