-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathapp_options.dart
307 lines (269 loc) · 8.59 KB
/
app_options.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import 'dart:convert' show base64Encode, utf8;
import 'package:meta/meta.dart';
import 'package:path/path.dart' show basename;
import 'package:patrol_cli/src/devices.dart';
import 'package:patrol_cli/src/ios/ios_test_backend.dart';
import 'package:patrol_cli/src/runner/flutter_command.dart';
class FlutterAppOptions {
const FlutterAppOptions({
required this.command,
required this.target,
required this.flavor,
required this.buildMode,
required this.dartDefines,
});
final FlutterCommand command;
final String target;
final String? flavor;
final BuildMode buildMode;
final Map<String, String> dartDefines;
/// Translates these options into a proper `flutter attach`.
@nonVirtual
List<String> toFlutterAttachInvocation() {
final cmd = [
...[command.executable, ...command.arguments],
'attach',
'--no-version-check',
'--suppress-analytics',
'--debug',
...['--target', target],
for (final dartDefine in dartDefines.entries) ...[
'--dart-define',
'${dartDefine.key}=${dartDefine.value}',
],
];
return cmd;
}
}
class AndroidAppOptions {
const AndroidAppOptions({
required this.flutter,
this.packageName,
required this.appServerPort,
required this.testServerPort,
});
final FlutterAppOptions flutter;
final String? packageName;
final int appServerPort;
final int testServerPort;
String get description => 'apk with entrypoint ${basename(flutter.target)}';
List<String> toGradleAssembleInvocation({required bool isWindows}) {
// for example: assembleDevDebug, assembleRelease
return _toGradleInvocation(
isWindows: isWindows,
task: 'assemble$_effectiveFlavor$_buildMode',
);
}
List<String> toGradleAssembleTestInvocation({required bool isWindows}) {
// for example: assembleDevDebugAndroidTest, assembleReleaseAndroidTest
return _toGradleInvocation(
isWindows: isWindows,
task: 'assemble$_effectiveFlavor${_buildMode}AndroidTest',
);
}
List<String> toGradleConnectedTestInvocation({required bool isWindows}) {
// for example: connectedDevDebugAndroidTest, connectedReleaseAndroidTest
return _toGradleInvocation(
isWindows: isWindows,
task: 'connected$_effectiveFlavor${_buildMode}AndroidTest',
);
}
String get _buildMode => flutter.buildMode.androidName;
String get _effectiveFlavor {
var flavor = flutter.flavor ?? '';
if (flavor.isNotEmpty) {
flavor = flavor[0].toUpperCase() + flavor.substring(1);
}
return flavor;
}
/// Translates these options into a proper Gradle invocation.
List<String> _toGradleInvocation({
required bool isWindows,
required String task,
}) {
final List<String> cmd;
if (isWindows) {
cmd = <String>[r'.\gradlew.bat'];
} else {
cmd = <String>['./gradlew'];
}
// Add Gradle task
cmd.add(':app:$task');
// Add Dart test target
final target = '-Ptarget=${flutter.target}';
cmd.add(target);
// Add Dart defines encoded in base64
if (flutter.dartDefines.isNotEmpty) {
final dartDefinesString = StringBuffer();
for (var i = 0; i < flutter.dartDefines.length; i++) {
final entry = flutter.dartDefines.entries.elementAt(i);
final pair = utf8.encode('${entry.key}=${entry.value}');
dartDefinesString.write(base64Encode(pair));
if (i != flutter.dartDefines.length - 1) {
dartDefinesString.write(',');
}
}
cmd.add('-Pdart-defines=$dartDefinesString');
}
// Add app and test server ports
cmd
..add('-Papp-server-port=$appServerPort')
..add('-Ptest-server-port=$testServerPort');
return cmd;
}
}
class IOSAppOptions {
IOSAppOptions({
required this.flutter,
this.bundleId,
required this.scheme,
required this.configuration,
required this.simulator,
required this.appServerPort,
required this.testServerPort,
});
final FlutterAppOptions flutter;
final String? bundleId;
final String scheme;
final String configuration;
final bool simulator;
final int appServerPort;
final int testServerPort;
String get description {
final platform = simulator ? 'simulator' : 'device';
return 'app with entrypoint ${basename(flutter.target)} for iOS $platform';
}
/// Translates these options into a proper flutter build invocation, which
/// runs before xcodebuild and performs configuration.
List<String> toFlutterBuildInvocation(BuildMode buildMode) {
final cmd = [
...[flutter.command.executable, ...flutter.command.arguments],
...['build', 'ios'],
'--no-version-check',
'--suppress-analytics',
...[
'--config-only',
'--no-codesign',
'--${buildMode.name}', // for example '--debug',
if (simulator) '--simulator',
],
if (flutter.flavor != null) ...['--flavor', flutter.flavor!],
...['--target', flutter.target],
for (final dartDefine in flutter.dartDefines.entries) ...[
'--dart-define',
'${dartDefine.key}=${dartDefine.value}',
],
];
return cmd;
}
/// Translates these options into a proper `xcodebuild build-for-testing`
/// invocation.
List<String> buildForTestingInvocation() {
final cmd = [
...['xcodebuild', 'build-for-testing'],
...['-workspace', 'Runner.xcworkspace'],
...['-scheme', scheme],
...['-configuration', configuration],
...['-sdk', if (simulator) 'iphonesimulator' else 'iphoneos'],
...[
'-destination',
'generic/platform=${simulator ? 'iOS Simulator' : 'iOS'}',
],
'-quiet',
...['-derivedDataPath', '../build/ios_integ'],
r'OTHER_SWIFT_FLAGS=$(inherited) -D PATROL_ENABLED',
];
return cmd;
}
/// Translates these options into a proper `xcodebuild test-without-building`
/// invocation.
List<String> testWithoutBuildingInvocation(
Device device, {
required String xcTestRunPath,
required String resultBundlePath,
}) {
final cmd = [
...['xcodebuild', 'test-without-building'],
...['-xctestrun', xcTestRunPath],
...['-only-testing', 'RunnerUITests/RunnerUITests'],
...[
'-destination',
'platform=${device.real ? 'iOS' : 'iOS Simulator'},name=${device.name}',
],
...['-destination-timeout', '1'],
...['-resultBundlePath', resultBundlePath],
];
return cmd;
}
}
class MacOSAppOptions {
MacOSAppOptions({
required this.flutter,
this.bundleId,
required this.scheme,
required this.configuration,
required this.appServerPort,
required this.testServerPort,
});
final FlutterAppOptions flutter;
final String? bundleId;
final String scheme;
final String configuration;
final int appServerPort;
final int testServerPort;
String get description {
return 'app with entrypoint ${basename(flutter.target)} for macos';
}
/// Translates these options into a proper flutter build invocation, which
/// runs before xcodebuild and performs configuration.
List<String> toFlutterBuildInvocation(BuildMode buildMode) {
final cmd = [
...[flutter.command.executable, ...flutter.command.arguments],
...['build', 'macos'],
'--no-version-check',
'--suppress-analytics',
...[
'--config-only',
'--${buildMode.name}', // for example '--debug',
],
if (flutter.flavor != null) ...['--flavor', flutter.flavor!],
...['--target', flutter.target],
for (final dartDefine in flutter.dartDefines.entries) ...[
'--dart-define',
'${dartDefine.key}=${dartDefine.value}',
],
];
return cmd;
}
/// Translates these options into a proper `xcodebuild build-for-testing`
/// invocation.
List<String> buildForTestingInvocation() {
final cmd = [
...['xcodebuild', 'build-for-testing'],
...['-workspace', 'Runner.xcworkspace'],
...['-scheme', scheme],
...['-configuration', configuration],
'-quiet',
...['-derivedDataPath', '../build/macos_integ'],
r'OTHER_SWIFT_FLAGS=$(inherited) -D PATROL_ENABLED',
];
return cmd;
}
/// Translates these options into a proper `xcodebuild test-without-building`
/// invocation.
List<String> testWithoutBuildingInvocation(
Device device, {
required String xcTestRunPath,
required String resultBundlePath,
}) {
final cmd = [
...['xcodebuild', 'test-without-building'],
...['-xctestrun', xcTestRunPath],
...['-only-testing', 'RunnerUITests/RunnerUITests'],
...['-destination', 'platform=macOS'],
...['-resultBundlePath', resultBundlePath],
'-verbose',
];
return cmd;
}
}