3
3
// found in the LICENSE file.
4
4
5
5
import 'dart:async' ;
6
- import 'dart:io' as io;
7
6
8
7
import 'package:file/file.dart' ;
9
8
import 'package:path/path.dart' as p;
10
9
import 'package:platform/platform.dart' ;
11
10
12
11
import 'common/core.dart' ;
13
- import 'common/plugin_command .dart' ;
12
+ import 'common/package_looping_command .dart' ;
14
13
import 'common/plugin_utils.dart' ;
15
14
import 'common/process_runner.dart' ;
16
15
17
- /// Key for IPA.
18
- const String kIpa = 'ipa' ;
19
-
20
16
/// Key for APK.
21
- const String kApk = 'apk' ;
17
+ const String _platformFlagApk = 'apk' ;
18
+
19
+ const int _exitNoPlatformFlags = 2 ;
22
20
23
21
/// A command to build the example applications for packages.
24
- class BuildExamplesCommand extends PluginCommand {
22
+ class BuildExamplesCommand extends PackageLoopingCommand {
25
23
/// Creates an instance of the build command.
26
24
BuildExamplesCommand (
27
25
Directory packagesDir, {
28
26
ProcessRunner processRunner = const ProcessRunner (),
29
27
}) : super (packagesDir, processRunner: processRunner) {
30
- argParser.addFlag (kPlatformLinux, defaultsTo : false );
31
- argParser.addFlag (kPlatformMacos, defaultsTo : false );
32
- argParser.addFlag (kPlatformWeb, defaultsTo : false );
33
- argParser.addFlag (kPlatformWindows, defaultsTo : false );
34
- argParser.addFlag (kIpa, defaultsTo : io. Platform .isMacOS );
35
- argParser.addFlag (kApk );
28
+ argParser.addFlag (kPlatformLinux);
29
+ argParser.addFlag (kPlatformMacos);
30
+ argParser.addFlag (kPlatformWeb);
31
+ argParser.addFlag (kPlatformWindows);
32
+ argParser.addFlag (kPlatformIos );
33
+ argParser.addFlag (_platformFlagApk );
36
34
argParser.addOption (
37
35
kEnableExperiment,
38
36
defaultsTo: '' ,
@@ -49,164 +47,125 @@ class BuildExamplesCommand extends PluginCommand {
49
47
'This command requires "flutter" to be in your path.' ;
50
48
51
49
@override
52
- Future <void > run () async {
50
+ Future <void > initializeRun () async {
53
51
final List <String > platformSwitches = < String > [
54
- kApk ,
55
- kIpa ,
52
+ _platformFlagApk ,
53
+ kPlatformIos ,
56
54
kPlatformLinux,
57
55
kPlatformMacos,
58
56
kPlatformWeb,
59
57
kPlatformWindows,
60
58
];
61
59
if (! platformSwitches.any ((String platform) => getBoolArg (platform))) {
62
- print (
60
+ printError (
63
61
'None of ${platformSwitches .map ((String platform ) => '--$platform ' ).join (', ' )} '
64
- 'were specified, so not building anything .' );
65
- return ;
62
+ 'were specified. At least one platform must be provided .' );
63
+ throw ToolExit (_exitNoPlatformFlags) ;
66
64
}
67
- final String flutterCommand =
68
- const LocalPlatform ().isWindows ? 'flutter.bat' : 'flutter' ;
69
-
70
- final String enableExperiment = getStringArg (kEnableExperiment);
65
+ }
71
66
72
- final List <String > failingPackages = < String > [];
73
- await for (final Directory plugin in getPlugins ()) {
74
- for (final Directory example in getExamplesForPlugin (plugin)) {
75
- final String packageName =
76
- p.relative (example.path, from: packagesDir.path);
77
-
78
- if (getBoolArg (kPlatformLinux)) {
79
- print ('\n BUILDING Linux for $packageName ' );
80
- if (isLinuxPlugin (plugin)) {
81
- final int buildExitCode = await processRunner.runAndStream (
82
- flutterCommand,
83
- < String > [
84
- 'build' ,
85
- kPlatformLinux,
86
- if (enableExperiment.isNotEmpty)
87
- '--enable-experiment=$enableExperiment ' ,
88
- ],
89
- workingDir: example);
90
- if (buildExitCode != 0 ) {
91
- failingPackages.add ('$packageName (linux)' );
92
- }
93
- } else {
94
- print ('Linux is not supported by this plugin' );
67
+ @override
68
+ Future <List <String >> runForPackage (Directory package) async {
69
+ final List <String > errors = < String > [];
70
+
71
+ for (final Directory example in getExamplesForPlugin (package)) {
72
+ final String packageName =
73
+ p.relative (example.path, from: packagesDir.path);
74
+
75
+ if (getBoolArg (kPlatformLinux)) {
76
+ print ('\n BUILDING $packageName for Linux' );
77
+ if (isLinuxPlugin (package)) {
78
+ if (! await _buildExample (example, kPlatformLinux)) {
79
+ errors.add ('$packageName (Linux)' );
95
80
}
81
+ } else {
82
+ printSkip ('Linux is not supported by this plugin' );
96
83
}
84
+ }
97
85
98
- if (getBoolArg (kPlatformMacos)) {
99
- print ('\n BUILDING macOS for $packageName ' );
100
- if (isMacOsPlugin (plugin)) {
101
- final int exitCode = await processRunner.runAndStream (
102
- flutterCommand,
103
- < String > [
104
- 'build' ,
105
- kPlatformMacos,
106
- if (enableExperiment.isNotEmpty)
107
- '--enable-experiment=$enableExperiment ' ,
108
- ],
109
- workingDir: example);
110
- if (exitCode != 0 ) {
111
- failingPackages.add ('$packageName (macos)' );
112
- }
113
- } else {
114
- print ('macOS is not supported by this plugin' );
86
+ if (getBoolArg (kPlatformMacos)) {
87
+ print ('\n BUILDING $packageName for macOS' );
88
+ if (isMacOsPlugin (package)) {
89
+ if (! await _buildExample (example, kPlatformMacos)) {
90
+ errors.add ('$packageName (macOS)' );
115
91
}
92
+ } else {
93
+ printSkip ('macOS is not supported by this plugin' );
116
94
}
95
+ }
117
96
118
- if (getBoolArg (kPlatformWeb)) {
119
- print ('\n BUILDING web for $packageName ' );
120
- if (isWebPlugin (plugin)) {
121
- final int buildExitCode = await processRunner.runAndStream (
122
- flutterCommand,
123
- < String > [
124
- 'build' ,
125
- kPlatformWeb,
126
- if (enableExperiment.isNotEmpty)
127
- '--enable-experiment=$enableExperiment ' ,
128
- ],
129
- workingDir: example);
130
- if (buildExitCode != 0 ) {
131
- failingPackages.add ('$packageName (web)' );
132
- }
133
- } else {
134
- print ('Web is not supported by this plugin' );
97
+ if (getBoolArg (kPlatformWeb)) {
98
+ print ('\n BUILDING $packageName for web' );
99
+ if (isWebPlugin (package)) {
100
+ if (! await _buildExample (example, kPlatformWeb)) {
101
+ errors.add ('$packageName (web)' );
135
102
}
103
+ } else {
104
+ printSkip ('Web is not supported by this plugin' );
136
105
}
106
+ }
137
107
138
- if (getBoolArg (kPlatformWindows)) {
139
- print ('\n BUILDING Windows for $packageName ' );
140
- if (isWindowsPlugin (plugin)) {
141
- final int buildExitCode = await processRunner.runAndStream (
142
- flutterCommand,
143
- < String > [
144
- 'build' ,
145
- kPlatformWindows,
146
- if (enableExperiment.isNotEmpty)
147
- '--enable-experiment=$enableExperiment ' ,
148
- ],
149
- workingDir: example);
150
- if (buildExitCode != 0 ) {
151
- failingPackages.add ('$packageName (windows)' );
152
- }
153
- } else {
154
- print ('Windows is not supported by this plugin' );
108
+ if (getBoolArg (kPlatformWindows)) {
109
+ print ('\n BUILDING $packageName for Windows' );
110
+ if (isWindowsPlugin (package)) {
111
+ if (! await _buildExample (example, kPlatformWindows)) {
112
+ errors.add ('$packageName (Windows)' );
155
113
}
114
+ } else {
115
+ printSkip ('Windows is not supported by this plugin' );
156
116
}
117
+ }
157
118
158
- if (getBoolArg (kIpa)) {
159
- print ('\n BUILDING IPA for $packageName ' );
160
- if (isIosPlugin (plugin)) {
161
- final int exitCode = await processRunner.runAndStream (
162
- flutterCommand,
163
- < String > [
164
- 'build' ,
165
- 'ios' ,
166
- '--no-codesign' ,
167
- if (enableExperiment.isNotEmpty)
168
- '--enable-experiment=$enableExperiment ' ,
169
- ],
170
- workingDir: example);
171
- if (exitCode != 0 ) {
172
- failingPackages.add ('$packageName (ipa)' );
173
- }
174
- } else {
175
- print ('iOS is not supported by this plugin' );
119
+ if (getBoolArg (kPlatformIos)) {
120
+ print ('\n BUILDING $packageName for iOS' );
121
+ if (isIosPlugin (package)) {
122
+ if (! await _buildExample (
123
+ example,
124
+ kPlatformIos,
125
+ extraBuildFlags: < String > ['--no-codesign' ],
126
+ )) {
127
+ errors.add ('$packageName (iOS)' );
176
128
}
129
+ } else {
130
+ printSkip ('iOS is not supported by this plugin' );
177
131
}
132
+ }
178
133
179
- if (getBoolArg (kApk)) {
180
- print ('\n BUILDING APK for $packageName ' );
181
- if (isAndroidPlugin (plugin)) {
182
- final int exitCode = await processRunner.runAndStream (
183
- flutterCommand,
184
- < String > [
185
- 'build' ,
186
- 'apk' ,
187
- if (enableExperiment.isNotEmpty)
188
- '--enable-experiment=$enableExperiment ' ,
189
- ],
190
- workingDir: example);
191
- if (exitCode != 0 ) {
192
- failingPackages.add ('$packageName (apk)' );
193
- }
194
- } else {
195
- print ('Android is not supported by this plugin' );
134
+ if (getBoolArg (_platformFlagApk)) {
135
+ print ('\n BUILDING APK for $packageName ' );
136
+ if (isAndroidPlugin (package)) {
137
+ if (! await _buildExample (example, _platformFlagApk)) {
138
+ errors.add ('$packageName (apk)' );
196
139
}
140
+ } else {
141
+ printSkip ('Android is not supported by this plugin' );
197
142
}
198
143
}
199
144
}
200
- print ('\n\n ' );
201
145
202
- if (failingPackages.isNotEmpty) {
203
- print ('The following build are failing (see above for details):' );
204
- for (final String package in failingPackages) {
205
- print (' * $package ' );
206
- }
207
- throw ToolExit (1 );
208
- }
146
+ return errors;
147
+ }
209
148
210
- print ('All builds successful!' );
149
+ Future <bool > _buildExample (
150
+ Directory example,
151
+ String flutterBuildType, {
152
+ List <String > extraBuildFlags = const < String > [],
153
+ }) async {
154
+ final String flutterCommand =
155
+ const LocalPlatform ().isWindows ? 'flutter.bat' : 'flutter' ;
156
+ final String enableExperiment = getStringArg (kEnableExperiment);
157
+
158
+ final int exitCode = await processRunner.runAndStream (
159
+ flutterCommand,
160
+ < String > [
161
+ 'build' ,
162
+ flutterBuildType,
163
+ ...extraBuildFlags,
164
+ if (enableExperiment.isNotEmpty)
165
+ '--enable-experiment=$enableExperiment ' ,
166
+ ],
167
+ workingDir: example,
168
+ );
169
+ return exitCode == 0 ;
211
170
}
212
171
}
0 commit comments