runTaskInTerminal method

Future<ProcessResult> runTaskInTerminal({
  1. required String name,
  2. required String command,
  3. required List<String> arguments,
  4. bool isAdminCmd = false,
  5. String? workingDirectory,
  6. Map<String, String>? environment,
  7. bool includeParentEnvironment = true,
  8. bool runInShell = true,
  9. Encoding? stdoutEncoding = systemEncoding,
  10. Encoding? stderrEncoding = systemEncoding,
  11. required Logger logger,
})

Implementation

Future<io.ProcessResult> runTaskInTerminal({
  required String name,
  required String command,
  required List<String> arguments,
  bool isAdminCmd = false,
  String? workingDirectory,
  Map<String, String>? environment,
  bool includeParentEnvironment = true,
  bool runInShell = true,
  Encoding? stdoutEncoding = systemEncoding,
  Encoding? stderrEncoding = systemEncoding,
  required Logger logger,
}) async {
  logger.finest('running os task $name: $command ${arguments.join(' ')}');

  _ProcessParams params;
  if (Platform.isLinux || Platform.isMacOS) {
    params = _ProcessParams(
      isAdminCmd ? 'sudo' : '/bin/sh',
      [
        if (isAdminCmd) '/bin/sh',
        '-c',
        [
          command,
          ...arguments,
        ].join(' '),
      ],
    );
  } else if (Platform.isWindows) {
    params = _ProcessParams(
      'cmd',
      [
        '/c',
        [
          command,
          ...arguments,
        ].join(' '),
      ],
    );
  } else {
    throw UnsupportedError('current os is not supported');
  }
  final process = await Process.start(
    params.exe,
    params.args,
    workingDirectory: workingDirectory,
    environment: environment,
    includeParentEnvironment: includeParentEnvironment,
    runInShell: runInShell,
  );

  final stdOutFuture = process.stdout.fold<List<String>>(
    <String>[],
    (previous, element) {
      final newVal = String.fromCharCodes(element);
      logger.config(
        newVal,
      );
      return <String>[
        ...previous,
        newVal,
      ];
    },
  );
  final stdErrFuture = process.stderr.fold<List<String>>(
    <String>[],
    (previous, element) {
      final newVal = String.fromCharCodes(element);
      logger.severe(
        newVal,
      );
      return <String>[
        ...previous,
        newVal,
      ];
    },
  );
  final exitCode = await process.exitCode;
  final stdOut = (await stdOutFuture).join();
  final stdErr = (await stdErrFuture).join();

  return io.ProcessResult(
    process.pid,
    exitCode,
    stdOut,
    stdErr,
  );
}