run method

  1. @override
Future<ExitCode> run([
  1. List<String>? args
])
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<ExitCode> run([List<String>? args]) async {
  final argResults = args != null ? argParser.parse(args) : super.argResults!;

  final isRecursive = argResults['recursive'] as bool? ?? false;
  final isConcurrent = argResults['concurrent'] as bool? ?? false;
  final erasePubspecLock = argResults['pubspec-lock'] as bool? ?? false;

  final pubspecs = await pubspecYaml.all(recursive: isRecursive);

  logger.detail('Found ${pubspecs.length} pubspec.yaml files');
  for (final pubspec in pubspecs) {
    logger.detail(' - $pubspec');
  }

  if (pubspecs.isEmpty) {
    logger.err('No pubspec.yaml files found');
    return ExitCode.usage;
  }

  final packages = pubspecs.map(
    (e) => DetermineFlutterOrDart(
      pubspecYaml: e,
      pubspecLock: pubspecLock,
      findFile: findFile,
    ),
  );

  final baseRemoveCommands = [
    'rm -rf .dart_tool',
    'rm -rf build',
    if (erasePubspecLock) 'rm -f pubspec.lock',
  ];

  final commands = <CommandToRun>[];
  for (final package in packages) {
    final removeCommands = [...baseRemoveCommands];
    if (package.isFlutter) {
      removeCommands.add('flutter clean');
    }

    var label = darkGray.wrap('Cleaning (')!;
    label += cyan.wrap(package.tool())!;
    label += darkGray.wrap(') in ')!;
    label += yellow.wrap(package.directory(fromDirectory: cwd.path))!;

    final command = CommandToRun(
      command: removeCommands.join(' && '),
      workingDirectory: package.directory(),
      runConcurrently: isConcurrent,
      keys: const ['clean'],
      label: label,
    );

    commands.add(command);
  }

  ExitCode exitCode;

  if (isConcurrent) {
    final runner = RunManyScripts(
      commands: commands,
      bindings: bindings,
      logger: logger,
    );

    final results = await runner.run(bail: false);

    results.printErrors(commands, logger);

    exitCode = results.exitCode(logger);
  } else {
    exitCode = ExitCode.success;

    for (final command in commands) {
      final runner = RunOneScript(
        command: command,
        bindings: bindings,
        logger: logger,
        showOutput: true,
      );

      final result = await runner.run();

      result.printError(command, logger);

      if (result.exitCodeReason != ExitCode.success) {
        exitCode = result.exitCodeReason;
      }
    }
  }

  return exitCode;
}