Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6d12096

Browse files
author
Chris Yang
authored
[tool] combine run and runAndExitOnError (flutter#3827)
1 parent 5c1c0c0 commit 6d12096

File tree

7 files changed

+87
-75
lines changed

7 files changed

+87
-75
lines changed

script/tool/lib/src/common.dart

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -500,17 +500,33 @@ class ProcessRunner {
500500
///
501501
/// If [exitOnError] is set to `true`, then this will throw an error if
502502
/// the [executable] terminates with a non-zero exit code.
503+
/// Defaults to `false`.
504+
///
505+
/// If [logOnError] is set to `true`, it will print a formatted message about the error.
506+
/// Defaults to `false`
503507
///
504508
/// Returns the [io.ProcessResult] of the [executable].
505509
Future<io.ProcessResult> run(String executable, List<String> args,
506510
{Directory workingDir,
507511
bool exitOnError = false,
512+
bool logOnError = false,
508513
Encoding stdoutEncoding = io.systemEncoding,
509514
Encoding stderrEncoding = io.systemEncoding}) async {
510-
return io.Process.run(executable, args,
515+
final io.ProcessResult result = await io.Process.run(executable, args,
511516
workingDirectory: workingDir?.path,
512517
stdoutEncoding: stdoutEncoding,
513518
stderrEncoding: stderrEncoding);
519+
if (result.exitCode != 0) {
520+
if (logOnError) {
521+
final String error =
522+
_getErrorString(executable, args, workingDir: workingDir);
523+
print('$error Stderr:\n${result.stdout}');
524+
}
525+
if (exitOnError) {
526+
throw ToolExit(result.exitCode);
527+
}
528+
}
529+
return result;
514530
}
515531

516532
/// Starts the [executable] with [args].
@@ -526,31 +542,6 @@ class ProcessRunner {
526542
return process;
527543
}
528544

529-
/// Run the [executable] with [args], throwing an error on non-zero exit code.
530-
///
531-
/// Unlike [runAndStream], this does not stream the process output to stdout.
532-
/// It also unconditionally throws an error on a non-zero exit code.
533-
///
534-
/// The current working directory of [executable] can be overridden by
535-
/// passing [workingDir].
536-
///
537-
/// Returns the [io.ProcessResult] of running the [executable].
538-
Future<io.ProcessResult> runAndExitOnError(
539-
String executable,
540-
List<String> args, {
541-
Directory workingDir,
542-
}) async {
543-
final io.ProcessResult result = await io.Process.run(executable, args,
544-
workingDirectory: workingDir?.path);
545-
if (result.exitCode != 0) {
546-
final String error =
547-
_getErrorString(executable, args, workingDir: workingDir);
548-
print('$error Stderr:\n${result.stdout}');
549-
throw ToolExit(result.exitCode);
550-
}
551-
return result;
552-
}
553-
554545
String _getErrorString(String executable, List<String> args,
555546
{Directory workingDir}) {
556547
final String workdir = workingDir == null ? '' : ' in ${workingDir.path}';

script/tool/lib/src/firebase_test_lab_command.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,16 @@ class FirebaseTestLabCommand extends PluginCommand {
7373
} else {
7474
_firebaseProjectConfigured = Completer<void>();
7575
}
76-
await processRunner.runAndExitOnError('gcloud', <String>[
77-
'auth',
78-
'activate-service-account',
79-
'--key-file=${argResults['service-key']}',
80-
]);
76+
await processRunner.run(
77+
'gcloud',
78+
<String>[
79+
'auth',
80+
'activate-service-account',
81+
'--key-file=${argResults['service-key']}',
82+
],
83+
exitOnError: true,
84+
logOnError: true,
85+
);
8186
final int exitCode = await processRunner.runAndStream('gcloud', <String>[
8287
'config',
8388
'set',

script/tool/lib/src/format_command.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ class FormatCommand extends PluginCommand {
5656
}
5757

5858
Future<bool> _didModifyAnything() async {
59-
final io.ProcessResult modifiedFiles = await processRunner
60-
.runAndExitOnError('git', <String>['ls-files', '--modified'],
61-
workingDir: packagesDir);
59+
final io.ProcessResult modifiedFiles = await processRunner.run(
60+
'git',
61+
<String>['ls-files', '--modified'],
62+
workingDir: packagesDir,
63+
exitOnError: true,
64+
logOnError: true,
65+
);
6266

6367
print('\n\n');
6468

@@ -76,8 +80,13 @@ class FormatCommand extends PluginCommand {
7680
'this command into your terminal:');
7781

7882
print('patch -p1 <<DONE');
79-
final io.ProcessResult diff = await processRunner
80-
.runAndExitOnError('git', <String>['diff'], workingDir: packagesDir);
83+
final io.ProcessResult diff = await processRunner.run(
84+
'git',
85+
<String>['diff'],
86+
workingDir: packagesDir,
87+
exitOnError: true,
88+
logOnError: true,
89+
);
8190
print(diff.stdout);
8291
print('DONE');
8392
return true;

script/tool/lib/src/lint_podspecs_command.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,13 @@ class LintPodspecsCommand extends PluginCommand {
5858
return;
5959
}
6060

61-
await processRunner.runAndExitOnError('which', <String>['pod'],
62-
workingDir: packagesDir);
61+
await processRunner.run(
62+
'which',
63+
<String>['pod'],
64+
workingDir: packagesDir,
65+
exitOnError: true,
66+
logOnError: true,
67+
);
6368

6469
_print('Starting podspec lint test');
6570

script/tool/lib/src/publish_plugin_command.dart

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,13 @@ class PublishPluginCommand extends PluginCommand {
136136
@required bool shouldPushTag}) async {
137137
final String tag = _getTag(packageDir);
138138
_print('Tagging release $tag...');
139-
await processRunner.runAndExitOnError('git', <String>['tag', tag],
140-
workingDir: packageDir);
139+
await processRunner.run(
140+
'git',
141+
<String>['tag', tag],
142+
workingDir: packageDir,
143+
exitOnError: true,
144+
logOnError: true,
145+
);
141146
if (!shouldPushTag) {
142147
return;
143148
}
@@ -163,15 +168,13 @@ class PublishPluginCommand extends PluginCommand {
163168
}
164169

165170
Future<void> _checkGitStatus(Directory packageDir) async {
166-
final ProcessResult statusResult = await processRunner.runAndExitOnError(
167-
'git',
168-
<String>[
169-
'status',
170-
'--porcelain',
171-
'--ignored',
172-
packageDir.absolute.path
173-
],
174-
workingDir: packageDir);
171+
final ProcessResult statusResult = await processRunner.run(
172+
'git',
173+
<String>['status', '--porcelain', '--ignored', packageDir.absolute.path],
174+
workingDir: packageDir,
175+
logOnError: true,
176+
exitOnError: true,
177+
);
175178

176179
final String statusOutput = statusResult.stdout as String;
177180
if (statusOutput.isNotEmpty) {
@@ -184,9 +187,13 @@ class PublishPluginCommand extends PluginCommand {
184187
}
185188

186189
Future<String> _verifyRemote(String remote) async {
187-
final ProcessResult remoteInfo = await processRunner.runAndExitOnError(
188-
'git', <String>['remote', 'get-url', remote],
189-
workingDir: packagesDir);
190+
final ProcessResult remoteInfo = await processRunner.run(
191+
'git',
192+
<String>['remote', 'get-url', remote],
193+
workingDir: packagesDir,
194+
exitOnError: true,
195+
logOnError: true,
196+
);
190197
return remoteInfo.stdout as String;
191198
}
192199

@@ -239,7 +246,12 @@ class PublishPluginCommand extends PluginCommand {
239246
_print('Tag push canceled.');
240247
throw ToolExit(1);
241248
}
242-
await processRunner.runAndExitOnError('git', <String>['push', remote, tag],
243-
workingDir: packagesDir);
249+
await processRunner.run(
250+
'git',
251+
<String>['push', remote, tag],
252+
workingDir: packagesDir,
253+
exitOnError: true,
254+
logOnError: true,
255+
);
244256
}
245257
}

script/tool/test/publish_plugin_command_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,14 @@ class TestProcessRunner extends ProcessRunner {
328328
final List<String> pushTagsArgs = <String>[];
329329

330330
@override
331-
Future<io.ProcessResult> runAndExitOnError(
331+
Future<io.ProcessResult> run(
332332
String executable,
333333
List<String> args, {
334334
Directory workingDir,
335+
bool exitOnError = false,
336+
bool logOnError = false,
337+
Encoding stdoutEncoding = io.systemEncoding,
338+
Encoding stderrEncoding = io.systemEncoding,
335339
}) async {
336340
// Don't ever really push tags.
337341
if (executable == 'git' && args.isNotEmpty && args[0] == 'push') {

script/tool/test/util.dart

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -235,32 +235,18 @@ class RecordingProcessRunner extends ProcessRunner {
235235

236236
/// Returns [io.ProcessResult] created from [processToReturn], [resultStdout], and [resultStderr].
237237
@override
238-
Future<io.ProcessResult> run(String executable, List<String> args,
239-
{Directory workingDir,
240-
bool exitOnError = false,
241-
Encoding stdoutEncoding = io.systemEncoding,
242-
Encoding stderrEncoding = io.systemEncoding}) async {
243-
recordedCalls.add(ProcessCall(executable, args, workingDir?.path));
244-
io.ProcessResult result;
245-
246-
if (processToReturn != null) {
247-
result = io.ProcessResult(
248-
processToReturn.pid,
249-
await processToReturn.exitCode,
250-
resultStdout ?? processToReturn.stdout,
251-
resultStderr ?? processToReturn.stderr);
252-
}
253-
return Future<io.ProcessResult>.value(result);
254-
}
255-
256-
@override
257-
Future<io.ProcessResult> runAndExitOnError(
238+
Future<io.ProcessResult> run(
258239
String executable,
259240
List<String> args, {
260241
Directory workingDir,
242+
bool exitOnError = false,
243+
bool logOnError = false,
244+
Encoding stdoutEncoding = io.systemEncoding,
245+
Encoding stderrEncoding = io.systemEncoding,
261246
}) async {
262247
recordedCalls.add(ProcessCall(executable, args, workingDir?.path));
263248
io.ProcessResult result;
249+
264250
if (processToReturn != null) {
265251
result = io.ProcessResult(
266252
processToReturn.pid,

0 commit comments

Comments
 (0)