runSyncProcess method

SimpleProcessResult runSyncProcess(
  1. String exec, {
  2. List<String>? args = const <String>[],
  3. bool? showOutput,
  4. HandlerFunction? handlerFn,
  5. bool? skipOnError,
})

runSyncProcess

Implementation

SimpleProcessResult runSyncProcess(
  String exec, {
  List<String>? args = const <String>[],
  bool? showOutput,
  HandlerFunction? handlerFn,
  bool? skipOnError,
}) {
  skipOnError = skipOnError ?? false;
  showOutput = showOutput ?? simpleProcessOptions.showOutput;

  try {
    var result = Process.runSync(exec, _injectConfiguration(args),
        workingDirectory: simpleProcessOptions.baseDir,
        runInShell: simpleProcessOptions.runInShell,
        stdoutEncoding: simpleProcessOptions.stdoutEncoding,
        stderrEncoding: simpleProcessOptions.stderrEncoding);

    var simpleResult = SimpleProcessResult(processResult: result);
    if (showOutput == true) stdout.write(simpleResult.resultMessage);
    handlerFn?.call(simpleResult);

    if (skipOnError == false && simpleResult.exitCode != 0) {
      simpleResult.processException = ProcessException(
          exec, [...?args], result.stderr ?? result.stdout, result.exitCode);
      throw simpleResult;
    }

    return simpleResult;
  } on ProcessException catch (e) {
    var simpleResultException = SimpleProcessResult(processException: e);
    if (showOutput == true) stderr.write(simpleResultException.resultMessage);
    handlerFn?.call(simpleResultException);

    if (skipOnError == false) {
      throw simpleResultException;
    } else {
      return simpleResultException;
    }
  } catch (e) {
    rethrow;
  }
}