3
3
using Microsoft . CodeAnalysis ;
4
4
using Microsoft . CodeAnalysis . Text ;
5
5
6
- namespace Microsoft . Maui . TestUtils . DeviceTests . Runners . SourceGen ;
7
-
8
- public class RunnerGenerator
6
+ namespace Microsoft . Maui . TestUtils . DeviceTests . Runners . SourceGen
9
7
{
10
- public RunnerGenerator ( GeneratorExecutionContext context , string targetFramework )
8
+ public class RunnerGenerator
11
9
{
12
- Context = context ;
10
+ public RunnerGenerator ( GeneratorExecutionContext context , string targetFramework )
11
+ {
12
+ Context = context ;
13
13
14
- TargetFramework = targetFramework ;
14
+ TargetFramework = targetFramework ;
15
15
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." ) ;
19
19
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" ;
23
23
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" ;
27
27
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 )
33
30
{
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
+ }
36
37
}
38
+ context . Log ( $ "ContainsSplashScreen: { ContainsSplashScreen } ") ;
37
39
}
38
- context . Log ( $ "ContainsSplashScreen: { ContainsSplashScreen } ") ;
39
- }
40
40
41
- public GeneratorExecutionContext Context { get ; }
41
+ public GeneratorExecutionContext Context { get ; }
42
42
43
- public string TargetFramework { get ; }
43
+ public string TargetFramework { get ; }
44
44
45
- public string RootNamespace { get ; }
45
+ public string RootNamespace { get ; }
46
46
47
- public string ApplicationId { get ; }
47
+ public string ApplicationId { get ; }
48
48
49
- public string ApplicationTitle { get ; }
49
+ public string ApplicationTitle { get ; }
50
50
51
- public bool ContainsSplashScreen { get ; }
51
+ public bool ContainsSplashScreen { get ; }
52
52
53
- public void Generate ( )
54
- {
55
- Context . Log ( $ "Generating runners...") ;
56
-
57
- if ( TargetFramework . IndexOf ( "-android" , StringComparison . OrdinalIgnoreCase ) != - 1 )
53
+ public void Generate ( )
58
54
{
59
- var code = GenerateAndroidSource ( ) ;
60
- var name = "TestRunner.Android.sg.cs" ;
55
+ Context . Log ( $ "Generating runners...") ;
61
56
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" ;
68
61
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" ;
75
68
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
+ }
77
78
}
78
- }
79
79
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
+ }
85
85
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""," : "" ;
91
91
92
- var appName = "MainApplication" ;
93
- var visualActivityName = "MainActivity" ;
92
+ var appName = "MainApplication" ;
93
+ var visualActivityName = "MainActivity" ;
94
94
95
- var instrumentationName = "TestInstrumentation" ;
96
- var headlessActivityName = "TestActivity" ;
95
+ var instrumentationName = "TestInstrumentation" ;
96
+ var headlessActivityName = "TestActivity" ;
97
97
98
- return @"
98
+ return @"
99
99
#if !SKIP_RUNNER_ENTRYPOINT_GENERATION && !SKIP_VISUAL_RUNNER_ENTRYPOINT_GENERATION && !SKIP_VISUAL_RUNNER_APPLICATION_GENERATION
100
100
namespace " + RootNamespace + @"
101
101
{
@@ -162,16 +162,16 @@ public partial class " + headlessActivityName + @" : global::Microsoft.Maui.Test
162
162
}
163
163
#endif
164
164
" ;
165
- }
165
+ }
166
166
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" ;
173
173
174
- return @"
174
+ return @"
175
175
#if !SKIP_RUNNER_ENTRYPOINT_GENERATION && !SKIP_VISUAL_RUNNER_ENTRYPOINT_GENERATION && !SKIP_RUNNER_PROGRAM_GENERATION
176
176
namespace " + RootNamespace + @"
177
177
{
@@ -219,5 +219,6 @@ partial class " + headlessDelegateName + @" : global::Microsoft.Maui.TestUtils.D
219
219
}
220
220
#endif
221
221
" ;
222
+ }
222
223
}
223
224
}
0 commit comments