-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathcli.dart
More file actions
48 lines (42 loc) · 1.16 KB
/
cli.dart
File metadata and controls
48 lines (42 loc) · 1.16 KB
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
import 'package:path/path.dart' as p;
import 'package:universal_io/io.dart';
part 'dart_cli.dart';
part 'flutter_cli.dart';
/// Abstraction for running commands via command-line.
class _Cmd {
/// Runs the specified [cmd] with the provided [args].
static Future<ProcessResult> run(
String cmd,
List<String> args, {
bool throwOnError = true,
String? workingDirectory,
}) async {
final result = await Process.run(
cmd,
args,
workingDirectory: workingDirectory,
runInShell: true,
);
if (throwOnError) {
_throwIfProcessFailed(result, cmd, args);
}
return result;
}
static void _throwIfProcessFailed(
ProcessResult pr,
String process,
List<String> args,
) {
if (pr.exitCode != 0) {
final values = {
'Standard out': pr.stdout.toString().trim(),
'Standard error': pr.stderr.toString().trim()
}..removeWhere((k, v) => v.isEmpty);
var message = 'Unknown error';
if (values.isNotEmpty) {
message = values.entries.map((e) => '${e.key}\n${e.value}').join('\n');
}
throw ProcessException(process, args, message, pr.exitCode);
}
}
}