-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathrun_process.dart
130 lines (116 loc) · 3.53 KB
/
run_process.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
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:logging/logging.dart';
/// Runs a [Process].
///
/// If [logger] is provided, stream stdout and stderr to it.
///
/// If [captureOutput], captures stdout and stderr.
// TODO(dacoharkes): Share between package:native_toolchain_c and here.
Future<RunProcessResult> runProcess({
required Uri executable,
List<String> arguments = const [],
Uri? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
required Logger? logger,
bool captureOutput = true,
int expectedExitCode = 0,
bool throwOnUnexpectedExitCode = false,
}) async {
if (Platform.isWindows && !includeParentEnvironment) {
const winEnvKeys = [
'SYSTEMROOT',
];
environment = {
for (final winEnvKey in winEnvKeys)
winEnvKey: Platform.environment[winEnvKey]!,
...?environment,
};
}
final printWorkingDir =
workingDirectory != null && workingDirectory != Directory.current.uri;
final commandString = [
if (printWorkingDir) '(cd ${workingDirectory.toFilePath()};',
...?environment?.entries.map((entry) => '${entry.key}=${entry.value}'),
executable.toFilePath(),
...arguments.map((a) => a.contains(' ') ? "'$a'" : a),
if (printWorkingDir) ')',
].join(' ');
logger?.info('Running `$commandString`.');
final stdoutBuffer = StringBuffer();
final stderrBuffer = StringBuffer();
final process = await Process.start(
executable.toFilePath(),
arguments,
workingDirectory: workingDirectory?.toFilePath(),
environment: environment,
includeParentEnvironment: includeParentEnvironment,
runInShell: Platform.isWindows &&
(!includeParentEnvironment || workingDirectory != null),
);
final stdoutSub = process.stdout
.transform(utf8.decoder)
.transform(const LineSplitter())
.listen(captureOutput
? (s) {
logger?.fine(s);
stdoutBuffer.writeln(s);
}
: logger?.fine);
final stderrSub = process.stderr
.transform(utf8.decoder)
.transform(const LineSplitter())
.listen(captureOutput
? (s) {
logger?.severe(s);
stderrBuffer.writeln(s);
}
: logger?.severe);
final (exitCode, _, _) = await (
process.exitCode,
stdoutSub.asFuture<void>(),
stderrSub.asFuture<void>()
).wait;
final result = RunProcessResult(
pid: process.pid,
command: commandString,
exitCode: exitCode,
stdout: stdoutBuffer.toString(),
stderr: stderrBuffer.toString(),
);
if (throwOnUnexpectedExitCode && expectedExitCode != exitCode) {
throw ProcessException(
executable.toFilePath(),
arguments,
"Full command string: '$commandString'.\n"
"Exit code: '$exitCode'.\n"
'For the output of the process check the logger output.',
);
}
return result;
}
/// Drop in replacement of [ProcessResult].
class RunProcessResult {
final int pid;
final String command;
final int exitCode;
final String stderr;
final String stdout;
RunProcessResult({
required this.pid,
required this.command,
required this.exitCode,
required this.stderr,
required this.stdout,
});
@override
String toString() => '''command: $command
exitCode: $exitCode
stdout: $stdout
stderr: $stderr''';
}