execute static method

Future<void> execute(
  1. List<String> commands, {
  2. int concurrent = defaultConcurrent,
  3. void customCommand(
    1. String
    )?,
  4. void stdout(
    1. String line
    )?,
  5. void stdoutErr(
    1. String line
    )?,
  6. bool ignorePubWorkspaces = false,
})

Implementation

static Future<void> execute(
  List<String> commands, {
  int concurrent = defaultConcurrent,
  void Function(String)? customCommand,
  void Function(String line)? stdout,
  void Function(String line)? stdoutErr,
  bool ignorePubWorkspaces = false,
}) async {
  final workingDirectoryFlutter = find('pubspec.yaml', workingDirectory: '.')
      .toList()
      .where(
        (element) {
          if (ignorePubWorkspaces) {
            return true;
          }

          final resolution = 'resolution';

          final yaml = YamlHelper.loadFileYaml(element);
          final hasResolution = yaml.containsKey(resolution);

          return !hasResolution;
        },
      )
      .map((e) => e.replaceAll('${separator}pubspec.yaml', ''))
      .sorted((a, b) =>
          b.split(separator).length.compareTo(a.split(separator).length));

  List<(String, Future<(String, bool, List<(bool, String)>)> Function())>
      futures = [];

  for (var e in workingDirectoryFlutter) {
    final path = e.replaceAll(current, '.');

    futures.add(
      (
        path,
        () async {
          final path = e.replaceAll(current, '.');
          List<(bool, String)> logs = [];
          bool isSuccess = false;

          try {
            for (var command in commands) {
              await command.start(
                workingDirectory: e,
                showLog: false,
                progressOut: (line) {
                  stdout?.call(line);
                  logs.add((false, line));
                },
                progressErr: (line) {
                  if (line.isEmpty) return;
                  if (line.contains('Waiting for another flutter command')) {
                    return;
                  }
                  stdoutErr?.call(line);
                  logs.add((true, line));
                },
              );
            }
            customCommand?.call(e);

            isSuccess = true;
          } catch (e) {
            isSuccess = false;
          }

          return (path, isSuccess, logs);
        }
      ),
    );
  }

  bool isAllExecutedSuccess = true;
  int runnable = 0;
  final length = futures.length;
  printMessage('πŸ“¦ Total Packages: $length');
  printMessage('---------------------------------------');
  for (runnable = 0; runnable < length; runnable += concurrent) {
    int take =
        runnable + concurrent > length ? length % concurrent : concurrent;

    final isolate = futures.getRange(runnable, runnable + take).map((e) {
      final path = e.$1;
      printMessage('πŸš€ $path: ${commands.join(', ')}');

      return Isolate.run<(String, bool, List<(bool, String)>)>(e.$2);
    });

    final results = await Future.wait(isolate);
    for (var i = 0; i < results.length; i++) {
      final path = results[i].$1;
      final isSuccess = results[i].$2;
      final logs = results[i].$3;
      if (isSuccess) {
        printMessage('βœ…  $path: ${commands.join(', ')}');
      } else {
        isAllExecutedSuccess = false;
        printMessage('❌  $path: ${commands.join(', ')}');
        printMessage('πŸ“  Logs: $path');

        for (var element in logs) {
          final isErrorMessage = element.$1;
          final line = element.$2;

          if (line.isEmpty ||
              RegExp(r'^\d{2}:\d{2}\s+\+\d+:').hasMatch(line) ||
              RegExp(r'^(\s)+$').hasMatch(line)) {
            continue;
          }

          if (isErrorMessage) {
            printerrMessage(red(element.$2));
            continue;
          }

          if (line.contains(RegExp(r'error'))) {
            printMessage(red(line));
          } else if (line.contains(RegExp(r'info'))) {
            printMessage(blue(line));
          } else if (line.contains(RegExp(r'warning'))) {
            printMessage(orange(line));
          } else {
            printMessage(element.$2);
          }
        }
      }
    }
  }

  if (!isAllExecutedSuccess) {
    throw Exception('Some packages failed to execute');
  }
}