diff --git a/script/tool/lib/src/common.dart b/script/tool/lib/src/common.dart index d8826c7930..c16ba8e957 100644 --- a/script/tool/lib/src/common.dart +++ b/script/tool/lib/src/common.dart @@ -500,17 +500,33 @@ class ProcessRunner { /// /// If [exitOnError] is set to `true`, then this will throw an error if /// the [executable] terminates with a non-zero exit code. + /// Defaults to `false`. + /// + /// If [logOnError] is set to `true`, it will print a formatted message about the error. + /// Defaults to `false` /// /// Returns the [io.ProcessResult] of the [executable]. Future run(String executable, List args, {Directory workingDir, bool exitOnError = false, + bool logOnError = false, Encoding stdoutEncoding = io.systemEncoding, Encoding stderrEncoding = io.systemEncoding}) async { - return io.Process.run(executable, args, + final io.ProcessResult result = await io.Process.run(executable, args, workingDirectory: workingDir?.path, stdoutEncoding: stdoutEncoding, stderrEncoding: stderrEncoding); + if (result.exitCode != 0) { + if (logOnError) { + final String error = + _getErrorString(executable, args, workingDir: workingDir); + print('$error Stderr:\n${result.stdout}'); + } + if (exitOnError) { + throw ToolExit(result.exitCode); + } + } + return result; } /// Starts the [executable] with [args]. @@ -526,31 +542,6 @@ class ProcessRunner { return process; } - /// Run the [executable] with [args], throwing an error on non-zero exit code. - /// - /// Unlike [runAndStream], this does not stream the process output to stdout. - /// It also unconditionally throws an error on a non-zero exit code. - /// - /// The current working directory of [executable] can be overridden by - /// passing [workingDir]. - /// - /// Returns the [io.ProcessResult] of running the [executable]. - Future runAndExitOnError( - String executable, - List args, { - Directory workingDir, - }) async { - final io.ProcessResult result = await io.Process.run(executable, args, - workingDirectory: workingDir?.path); - if (result.exitCode != 0) { - final String error = - _getErrorString(executable, args, workingDir: workingDir); - print('$error Stderr:\n${result.stdout}'); - throw ToolExit(result.exitCode); - } - return result; - } - String _getErrorString(String executable, List args, {Directory workingDir}) { final String workdir = workingDir == null ? '' : ' in ${workingDir.path}'; diff --git a/script/tool/lib/src/firebase_test_lab_command.dart b/script/tool/lib/src/firebase_test_lab_command.dart index 2998522da0..ff7d05bf4e 100644 --- a/script/tool/lib/src/firebase_test_lab_command.dart +++ b/script/tool/lib/src/firebase_test_lab_command.dart @@ -73,11 +73,16 @@ class FirebaseTestLabCommand extends PluginCommand { } else { _firebaseProjectConfigured = Completer(); } - await processRunner.runAndExitOnError('gcloud', [ - 'auth', - 'activate-service-account', - '--key-file=${argResults['service-key']}', - ]); + await processRunner.run( + 'gcloud', + [ + 'auth', + 'activate-service-account', + '--key-file=${argResults['service-key']}', + ], + exitOnError: true, + logOnError: true, + ); final int exitCode = await processRunner.runAndStream('gcloud', [ 'config', 'set', diff --git a/script/tool/lib/src/format_command.dart b/script/tool/lib/src/format_command.dart index 19b6004d24..9c29f0f8c6 100644 --- a/script/tool/lib/src/format_command.dart +++ b/script/tool/lib/src/format_command.dart @@ -56,9 +56,13 @@ class FormatCommand extends PluginCommand { } Future _didModifyAnything() async { - final io.ProcessResult modifiedFiles = await processRunner - .runAndExitOnError('git', ['ls-files', '--modified'], - workingDir: packagesDir); + final io.ProcessResult modifiedFiles = await processRunner.run( + 'git', + ['ls-files', '--modified'], + workingDir: packagesDir, + exitOnError: true, + logOnError: true, + ); print('\n\n'); @@ -76,8 +80,13 @@ class FormatCommand extends PluginCommand { 'this command into your terminal:'); print('patch -p1 <['diff'], workingDir: packagesDir); + final io.ProcessResult diff = await processRunner.run( + 'git', + ['diff'], + workingDir: packagesDir, + exitOnError: true, + logOnError: true, + ); print(diff.stdout); print('DONE'); return true; diff --git a/script/tool/lib/src/lint_podspecs_command.dart b/script/tool/lib/src/lint_podspecs_command.dart index 1fe6b71cf1..ebcefd6c23 100644 --- a/script/tool/lib/src/lint_podspecs_command.dart +++ b/script/tool/lib/src/lint_podspecs_command.dart @@ -58,8 +58,13 @@ class LintPodspecsCommand extends PluginCommand { return; } - await processRunner.runAndExitOnError('which', ['pod'], - workingDir: packagesDir); + await processRunner.run( + 'which', + ['pod'], + workingDir: packagesDir, + exitOnError: true, + logOnError: true, + ); _print('Starting podspec lint test'); diff --git a/script/tool/lib/src/publish_plugin_command.dart b/script/tool/lib/src/publish_plugin_command.dart index 0dae3a502b..201130db75 100644 --- a/script/tool/lib/src/publish_plugin_command.dart +++ b/script/tool/lib/src/publish_plugin_command.dart @@ -136,8 +136,13 @@ class PublishPluginCommand extends PluginCommand { @required bool shouldPushTag}) async { final String tag = _getTag(packageDir); _print('Tagging release $tag...'); - await processRunner.runAndExitOnError('git', ['tag', tag], - workingDir: packageDir); + await processRunner.run( + 'git', + ['tag', tag], + workingDir: packageDir, + exitOnError: true, + logOnError: true, + ); if (!shouldPushTag) { return; } @@ -163,15 +168,13 @@ class PublishPluginCommand extends PluginCommand { } Future _checkGitStatus(Directory packageDir) async { - final ProcessResult statusResult = await processRunner.runAndExitOnError( - 'git', - [ - 'status', - '--porcelain', - '--ignored', - packageDir.absolute.path - ], - workingDir: packageDir); + final ProcessResult statusResult = await processRunner.run( + 'git', + ['status', '--porcelain', '--ignored', packageDir.absolute.path], + workingDir: packageDir, + logOnError: true, + exitOnError: true, + ); final String statusOutput = statusResult.stdout as String; if (statusOutput.isNotEmpty) { @@ -184,9 +187,13 @@ class PublishPluginCommand extends PluginCommand { } Future _verifyRemote(String remote) async { - final ProcessResult remoteInfo = await processRunner.runAndExitOnError( - 'git', ['remote', 'get-url', remote], - workingDir: packagesDir); + final ProcessResult remoteInfo = await processRunner.run( + 'git', + ['remote', 'get-url', remote], + workingDir: packagesDir, + exitOnError: true, + logOnError: true, + ); return remoteInfo.stdout as String; } @@ -239,7 +246,12 @@ class PublishPluginCommand extends PluginCommand { _print('Tag push canceled.'); throw ToolExit(1); } - await processRunner.runAndExitOnError('git', ['push', remote, tag], - workingDir: packagesDir); + await processRunner.run( + 'git', + ['push', remote, tag], + workingDir: packagesDir, + exitOnError: true, + logOnError: true, + ); } } diff --git a/script/tool/test/publish_plugin_command_test.dart b/script/tool/test/publish_plugin_command_test.dart index 03e7858d3b..0cf709adc0 100644 --- a/script/tool/test/publish_plugin_command_test.dart +++ b/script/tool/test/publish_plugin_command_test.dart @@ -328,10 +328,14 @@ class TestProcessRunner extends ProcessRunner { final List pushTagsArgs = []; @override - Future runAndExitOnError( + Future run( String executable, List args, { Directory workingDir, + bool exitOnError = false, + bool logOnError = false, + Encoding stdoutEncoding = io.systemEncoding, + Encoding stderrEncoding = io.systemEncoding, }) async { // Don't ever really push tags. if (executable == 'git' && args.isNotEmpty && args[0] == 'push') { diff --git a/script/tool/test/util.dart b/script/tool/test/util.dart index 5a2c42bd31..4dc019c968 100644 --- a/script/tool/test/util.dart +++ b/script/tool/test/util.dart @@ -235,32 +235,18 @@ class RecordingProcessRunner extends ProcessRunner { /// Returns [io.ProcessResult] created from [processToReturn], [resultStdout], and [resultStderr]. @override - Future run(String executable, List args, - {Directory workingDir, - bool exitOnError = false, - Encoding stdoutEncoding = io.systemEncoding, - Encoding stderrEncoding = io.systemEncoding}) async { - recordedCalls.add(ProcessCall(executable, args, workingDir?.path)); - io.ProcessResult result; - - if (processToReturn != null) { - result = io.ProcessResult( - processToReturn.pid, - await processToReturn.exitCode, - resultStdout ?? processToReturn.stdout, - resultStderr ?? processToReturn.stderr); - } - return Future.value(result); - } - - @override - Future runAndExitOnError( + Future run( String executable, List args, { Directory workingDir, + bool exitOnError = false, + bool logOnError = false, + Encoding stdoutEncoding = io.systemEncoding, + Encoding stderrEncoding = io.systemEncoding, }) async { recordedCalls.add(ProcessCall(executable, args, workingDir?.path)); io.ProcessResult result; + if (processToReturn != null) { result = io.ProcessResult( processToReturn.pid,