[tool] combine run and runAndExitOnError (#3827)

This commit is contained in:
Chris Yang
2021-04-27 09:35:05 -07:00
committed by GitHub
parent 3c57df37c0
commit 189845bb8a
7 changed files with 87 additions and 75 deletions

View File

@ -500,17 +500,33 @@ class ProcessRunner {
/// ///
/// If [exitOnError] is set to `true`, then this will throw an error if /// If [exitOnError] is set to `true`, then this will throw an error if
/// the [executable] terminates with a non-zero exit code. /// 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]. /// Returns the [io.ProcessResult] of the [executable].
Future<io.ProcessResult> run(String executable, List<String> args, Future<io.ProcessResult> run(String executable, List<String> args,
{Directory workingDir, {Directory workingDir,
bool exitOnError = false, bool exitOnError = false,
bool logOnError = false,
Encoding stdoutEncoding = io.systemEncoding, Encoding stdoutEncoding = io.systemEncoding,
Encoding stderrEncoding = io.systemEncoding}) async { Encoding stderrEncoding = io.systemEncoding}) async {
return io.Process.run(executable, args, final io.ProcessResult result = await io.Process.run(executable, args,
workingDirectory: workingDir?.path, workingDirectory: workingDir?.path,
stdoutEncoding: stdoutEncoding, stdoutEncoding: stdoutEncoding,
stderrEncoding: stderrEncoding); 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]. /// Starts the [executable] with [args].
@ -526,31 +542,6 @@ class ProcessRunner {
return process; 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<io.ProcessResult> runAndExitOnError(
String executable,
List<String> 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<String> args, String _getErrorString(String executable, List<String> args,
{Directory workingDir}) { {Directory workingDir}) {
final String workdir = workingDir == null ? '' : ' in ${workingDir.path}'; final String workdir = workingDir == null ? '' : ' in ${workingDir.path}';

View File

@ -73,11 +73,16 @@ class FirebaseTestLabCommand extends PluginCommand {
} else { } else {
_firebaseProjectConfigured = Completer<void>(); _firebaseProjectConfigured = Completer<void>();
} }
await processRunner.runAndExitOnError('gcloud', <String>[ await processRunner.run(
'auth', 'gcloud',
'activate-service-account', <String>[
'--key-file=${argResults['service-key']}', 'auth',
]); 'activate-service-account',
'--key-file=${argResults['service-key']}',
],
exitOnError: true,
logOnError: true,
);
final int exitCode = await processRunner.runAndStream('gcloud', <String>[ final int exitCode = await processRunner.runAndStream('gcloud', <String>[
'config', 'config',
'set', 'set',

View File

@ -56,9 +56,13 @@ class FormatCommand extends PluginCommand {
} }
Future<bool> _didModifyAnything() async { Future<bool> _didModifyAnything() async {
final io.ProcessResult modifiedFiles = await processRunner final io.ProcessResult modifiedFiles = await processRunner.run(
.runAndExitOnError('git', <String>['ls-files', '--modified'], 'git',
workingDir: packagesDir); <String>['ls-files', '--modified'],
workingDir: packagesDir,
exitOnError: true,
logOnError: true,
);
print('\n\n'); print('\n\n');
@ -76,8 +80,13 @@ class FormatCommand extends PluginCommand {
'this command into your terminal:'); 'this command into your terminal:');
print('patch -p1 <<DONE'); print('patch -p1 <<DONE');
final io.ProcessResult diff = await processRunner final io.ProcessResult diff = await processRunner.run(
.runAndExitOnError('git', <String>['diff'], workingDir: packagesDir); 'git',
<String>['diff'],
workingDir: packagesDir,
exitOnError: true,
logOnError: true,
);
print(diff.stdout); print(diff.stdout);
print('DONE'); print('DONE');
return true; return true;

View File

@ -58,8 +58,13 @@ class LintPodspecsCommand extends PluginCommand {
return; return;
} }
await processRunner.runAndExitOnError('which', <String>['pod'], await processRunner.run(
workingDir: packagesDir); 'which',
<String>['pod'],
workingDir: packagesDir,
exitOnError: true,
logOnError: true,
);
_print('Starting podspec lint test'); _print('Starting podspec lint test');

View File

@ -136,8 +136,13 @@ class PublishPluginCommand extends PluginCommand {
@required bool shouldPushTag}) async { @required bool shouldPushTag}) async {
final String tag = _getTag(packageDir); final String tag = _getTag(packageDir);
_print('Tagging release $tag...'); _print('Tagging release $tag...');
await processRunner.runAndExitOnError('git', <String>['tag', tag], await processRunner.run(
workingDir: packageDir); 'git',
<String>['tag', tag],
workingDir: packageDir,
exitOnError: true,
logOnError: true,
);
if (!shouldPushTag) { if (!shouldPushTag) {
return; return;
} }
@ -163,15 +168,13 @@ class PublishPluginCommand extends PluginCommand {
} }
Future<void> _checkGitStatus(Directory packageDir) async { Future<void> _checkGitStatus(Directory packageDir) async {
final ProcessResult statusResult = await processRunner.runAndExitOnError( final ProcessResult statusResult = await processRunner.run(
'git', 'git',
<String>[ <String>['status', '--porcelain', '--ignored', packageDir.absolute.path],
'status', workingDir: packageDir,
'--porcelain', logOnError: true,
'--ignored', exitOnError: true,
packageDir.absolute.path );
],
workingDir: packageDir);
final String statusOutput = statusResult.stdout as String; final String statusOutput = statusResult.stdout as String;
if (statusOutput.isNotEmpty) { if (statusOutput.isNotEmpty) {
@ -184,9 +187,13 @@ class PublishPluginCommand extends PluginCommand {
} }
Future<String> _verifyRemote(String remote) async { Future<String> _verifyRemote(String remote) async {
final ProcessResult remoteInfo = await processRunner.runAndExitOnError( final ProcessResult remoteInfo = await processRunner.run(
'git', <String>['remote', 'get-url', remote], 'git',
workingDir: packagesDir); <String>['remote', 'get-url', remote],
workingDir: packagesDir,
exitOnError: true,
logOnError: true,
);
return remoteInfo.stdout as String; return remoteInfo.stdout as String;
} }
@ -239,7 +246,12 @@ class PublishPluginCommand extends PluginCommand {
_print('Tag push canceled.'); _print('Tag push canceled.');
throw ToolExit(1); throw ToolExit(1);
} }
await processRunner.runAndExitOnError('git', <String>['push', remote, tag], await processRunner.run(
workingDir: packagesDir); 'git',
<String>['push', remote, tag],
workingDir: packagesDir,
exitOnError: true,
logOnError: true,
);
} }
} }

View File

@ -328,10 +328,14 @@ class TestProcessRunner extends ProcessRunner {
final List<String> pushTagsArgs = <String>[]; final List<String> pushTagsArgs = <String>[];
@override @override
Future<io.ProcessResult> runAndExitOnError( Future<io.ProcessResult> run(
String executable, String executable,
List<String> args, { List<String> args, {
Directory workingDir, Directory workingDir,
bool exitOnError = false,
bool logOnError = false,
Encoding stdoutEncoding = io.systemEncoding,
Encoding stderrEncoding = io.systemEncoding,
}) async { }) async {
// Don't ever really push tags. // Don't ever really push tags.
if (executable == 'git' && args.isNotEmpty && args[0] == 'push') { if (executable == 'git' && args.isNotEmpty && args[0] == 'push') {

View File

@ -235,32 +235,18 @@ class RecordingProcessRunner extends ProcessRunner {
/// Returns [io.ProcessResult] created from [processToReturn], [resultStdout], and [resultStderr]. /// Returns [io.ProcessResult] created from [processToReturn], [resultStdout], and [resultStderr].
@override @override
Future<io.ProcessResult> run(String executable, List<String> args, Future<io.ProcessResult> run(
{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<io.ProcessResult>.value(result);
}
@override
Future<io.ProcessResult> runAndExitOnError(
String executable, String executable,
List<String> args, { List<String> args, {
Directory workingDir, Directory workingDir,
bool exitOnError = false,
bool logOnError = false,
Encoding stdoutEncoding = io.systemEncoding,
Encoding stderrEncoding = io.systemEncoding,
}) async { }) async {
recordedCalls.add(ProcessCall(executable, args, workingDir?.path)); recordedCalls.add(ProcessCall(executable, args, workingDir?.path));
io.ProcessResult result; io.ProcessResult result;
if (processToReturn != null) { if (processToReturn != null) {
result = io.ProcessResult( result = io.ProcessResult(
processToReturn.pid, processToReturn.pid,