doRun method

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

Implementation

@override
Future<int> doRun() async {
  final args = argResults!;
  var dependency = _getDepName(args[_argDependency] as String?);

  if (dependency == null) {
    const optionHint = '--$_argDependency=PACKAGE_NAME or '
        '-${_argDependencyAbbr}PACKAGE_NAME';

    if (isNonInteractive) {
      return errorNoAnswer('package name to update', optionHint);
    }

    printInfo('Enter package name to update:');
    dependency = _getDepName(console.readLineSync());

    if (dependency == null) {
      return error(1,
          message: 'Nothing to update - dependency name is not provider. '
              'You can pass package name as $optionHint');
    }
  }

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

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

    printVerbose('Update pubspec files');
    final updatedFiles = <String>[];
    for (final file in pubspecFiles) {
      if (await _updatePubspec(file, dependency)) {
        printInfo('Dependency updated for ${file.path}');
        updatedFiles.add(p.relative(file.path));
      }
    }

    if (updatedFiles.isEmpty) {
      return error(1,
          message:
              'Dependency <$dependency> is not found in any of pubspec files.');
    }

    printInfo('Updated ${updatedFiles.length} pubspec files.');

    if (isJsonFormat) {
      return jsonResult(
        exitCode: 0,
        summary: '$dependency updated in ${updatedFiles.length} file(s)',
        data: <String, dynamic>{
          'dependency': dependency,
          'updatedFiles': updatedFiles,
        },
      );
    }
  }

  return success(message: 'Bye 👋');
}