start method

Future<int> start({
  1. String? workingDirectory,
  2. void progressOut(
    1. String line
    )?,
  3. void progressErr(
    1. String line
    )?,
  4. bool showLog = true,
})

Implementation

Future<int> start({
  String? workingDirectory,
  void Function(String line)? progressOut,
  void Function(String line)? progressErr,
  bool showLog = true,
}) 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,
      workingDirectory: workingDirectory,
      environment: Platform.environment,
    );

    process.stdout.transform(utf8.decoder).listen((line) {
      final cleanLine = line.replaceAll(RegExp(r'[\s\n]+$'), '');
      progressOut?.call(cleanLine);
      if (showLog) printMessage(cleanLine);
    });

    process.stderr.transform(utf8.decoder).listen((line) {
      final cleanLine = line.replaceAll(RegExp(r'[\s\n]+$'), '');
      progressErr?.call(cleanLine);
      if (showLog) printerrMessage(cleanLine);
    });

    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 start command "$this": ${e.message}');
    }
    rethrow;
  }
}