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
    )?,
})

Implementation

static Future<void> execute(
  List<String> commands, {
  int concurrent = defaultConcurrent,
  void Function(String)? customCommand,
  void Function(String line)? stdout,
}) async {
  final workingDirectoryFlutter = find('pubspec.yaml', workingDirectory: '.')
      .toList()
      .map((e) => e.replaceAll('${separator}pubspec.yaml', ''))
      .sorted((a, b) =>
          b.split(separator).length.compareTo(a.split(separator).length));

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

  for (var e in workingDirectoryFlutter) {
    futures.add(() async {
      final path = e.replaceAll(current, '.');
      try {
        print('🚀 $path: ${commands.join(', ')}');
        for (var command in commands) {
          command.start(
            workingDirectory: e,
            progress: Progress(
              (line) {
                stdout?.call(line);
              },
              stderr: (line) {
                if (line.isEmpty) return;
                if (line.contains('Waiting for another flutter command')) {
                  return;
                }
                printerr(red(line));
              },
            ),
          );
        }
        customCommand?.call(e);
        print('✅  $path: ${commands.join(', ')}');
      } catch (e) {
        print('❌  $path: ${commands.join(', ')}');
        rethrow;
      }
    });
  }

  int runnable = 0;
  final length = futures.length;
  print('📦 Total Packages: $length');
  print('---------------------------------------');
  for (runnable = 0; runnable < length; runnable += concurrent) {
    int take =
        runnable + concurrent > length ? length % concurrent : concurrent;
    final isolate = futures
        .getRange(runnable, runnable + take)
        .map((e) => Isolate.run(e));
    await Future.wait(isolate);
  }
}