run method

Future<int> run([
  1. List<String> extraArgs = const []
])

Runs the current script with the given extra arguments.

Implementation

Future<int> run([List<String> extraArgs = const []]) async {
  final effectiveArgs = args + extraArgs;
  final config = await ScriptRunnerConfig.get(_fileSystem);

  final scrContents = _getScriptContents(config, extraArgs: extraArgs);
  final scrPath = _getScriptPath();
  final scrFile = _fileSystem.file(scrPath);

  await scrFile.writeAsString(scrContents);

  if (config.shell.os != OS.windows && _fileSystem is! MemoryFileSystem) {
    final result = await io.Process.run("chmod", ["u+x", scrPath]);
    if (result.exitCode != 0) throw Exception(result.stderr);
  }

  final origCmd = [cmd, ...effectiveArgs.map(_utils.quoteWrap)].join(' ');

  if (displayCmd) {
    print(_utils.colorize('\$ $origCmd', [_utils.TerminalColor.gray]));
  }

  try {
    final exitCode = await _runShellScriptFile(config, scrPath);
    if (appendNewline) {
      print('');
    }
    if (exitCode != 0) {
      final e = io.ProcessException(
        cmd,
        args,
        'Process exited with error code: $exitCode',
        exitCode,
      );
      throw e;
    }
    return exitCode;
  } finally {
    await _fileSystem.file(scrPath).delete();
  }
}