run function

String run(
  1. String executable, {
  2. List<String> arguments = const [],
  3. RunOptions? runOptions,
  4. bool quiet = false,
  5. String? workingDirectory,
})

Synchronously run an executable.

If quiet is false, logs the stdout. The stderr is always logged.

Returns the stdout.

All other optional parameters are forwarded to Process.runSync.

Implementation

String run(String executable,
    {List<String> arguments = const [],
    RunOptions? runOptions,
    bool quiet = false,
    String? workingDirectory}) {
  runOptions = mergeWorkingDirectory(workingDirectory, runOptions);
  if (!quiet) log("$executable ${arguments.join(' ')}");

  final result = Process.runSync(executable, arguments,
      workingDirectory: runOptions.workingDirectory,
      environment: runOptions.environment,
      includeParentEnvironment: runOptions.includeParentEnvironment,
      runInShell: runOptions.runInShell,
      stdoutEncoding: runOptions.stdoutEncoding,
      stderrEncoding: runOptions.stderrEncoding);

  var stdout = result.stdout as String;
  var stderr = result.stderr as String;

  if (!quiet && stdout.isNotEmpty) log(stdout.trim());
  if (stderr.isNotEmpty) log(result.stderr);

  if (result.exitCode != 0) {
    throw ProcessException._(executable, result.exitCode, stdout, stderr);
  }

  return stdout;
}