run property

Future<int> get run

Implementation

Future<int> get run async {
  final commandCli = CommandlineConverter().convert(this);

  if (commandCli.isEmpty) {
    throw ArgumentError('Command cannot be empty');
  }

  final executable = commandCli.first;
  final arguments = commandCli.sublist(1);

  try {
    final process = await Process.start(
      executable,
      arguments,
      runInShell: true,
      environment: Platform.environment,
    );

    process.stdout.transform(utf8.decoder).listen(
      (line) {
        printMessage(line.replaceAll(RegExp(r'[\s\n]+$'), ''));
      },
    );

    process.stderr.transform(utf8.decoder).listen(
      (line) {
        printerrMessage(line.replaceAll(RegExp(r'[\s\n]+$'), ''));
      },
    );

    final exitCode = await process.exitCode;

    if (exitCode > 0) {
      throw Exception('Command "$this" exited with code $exitCode');
    }

    return exitCode;
  } catch (e) {
    if (e is ProcessException) {
      throw Exception('Failed to execute command "$this": ${e.message}');
    }
    rethrow;
  }
}