run method

  1. @override
Future<int> run()
override

Runs this command.

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

Implementation

@override
Future<int> run() async {
  var dependency = argResults[_argDependency] as String;
  if (dependency?.isEmpty ?? true) {
    printInfo('Enter package name to update:');
    dependency = console.readLineSync();

    if (dependency?.isEmpty ?? true) {
      return error(1,
          message: 'Nothing to update - dependency name is not provider. '
              'You can pass package name as '
              '--$_argDependency=PACKAGE_NAME or '
              '-${_argDependencyAbbr}PACKAGE_NAME');
    }
  }

  printInfo('Updating <$dependency>...');

  try {
    final pubspecFiles = await getPubspecs();
    if (pubspecFiles.isNotEmpty) {
      printVerbose('Sort pubspec files consider mutual dependencies');
      _sortPubpecs(pubspecFiles);

      printVerbose('Update pubspec files');
      var updated = 0;
      for (final file in pubspecFiles) {
        if (await _updatePubspec(file, dependency)) {
          printInfo('Dependency updated for ${file.path}');
          updated++;
        }
      }

      if (updated == 0) {
        printInfo(
            'Dependency <$dependency> is not found in any of pubspec files.');
      } else {
        printInfo('Updated $updated pubspec files.');
      }
    }

    return success(message: 'Bye 👋');
  } on RunException catch (e) {
    return errorBy(e);
  } catch (e) {
    return error(2, message: 'Failed by: $e');
  }
}