run method Null safety

  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 {
  final accountCredentialsArg = _credentials;
  final packageName = _packageName;
  final upgradeMode = _upgradeMode;
  final next = _next;
  final outputDir = _outputDirectory;

  _logger.write('\n');

  late AutoRefreshingAuthClient baseClient;

  try {
    baseClient = await clientViaServiceAccount(
      ServiceAccountCredentials.fromJson(
        (json.decode(accountCredentialsArg) as Map).cast<String, dynamic>(),
      ),
      [AndroidPublisherApi.androidpublisherScope],
    );
  } catch (e) {
    _logger.err('Error getting client: $e');
    usageException(_invalidCredentialsError);
  }

  final appLatestVersionProgress =
      _logger.progress('Getting latest version...');

  final publisherApi = AndroidPublisherApi(baseClient);
  final edit = await _createAppEdit(publisherApi, packageName);

  final list = await publisherApi.edits.tracks.list(
    packageName,
    edit.id!,
  );

  final latestVersion = list.latestVersion;

  appLatestVersionProgress.update('Cleaning up');
  await _deleteAppEdit(publisherApi, packageName, edit);

  appLatestVersionProgress.complete(
    'The latest version in Google Play Console is ${green.wrap(
      list.latestVersion.toString(),
    )}',
  );

  if (upgradeMode == UpgradeMode.never) {
    exit(ExitCode.success.code);
  }

  late final PubSpec pubspec;

  try {
    pubspec = await PubSpec.load(outputDir);
  } catch (e) {
    _logger.err(
      'An error occured loading the pubspec.yaml file. '
      'Check that you are in the root of the project and '
      'that the file is properly formatted.',
    );
    exit(ExitCode.ioError.code);
  }

  final currentVersion = pubspec.version ?? Version.none;
  final hasNewerVersion = currentVersion <= latestVersion;
  final shouldUpgrade = upgradeMode == UpgradeMode.always ||
      hasNewerVersion && upgradeMode == UpgradeMode.outdated;

  final latestIsPreRelease = latestVersion.isPreRelease;

  if (!shouldUpgrade) {
    _logger.success(
      'The app version is already higher than the one '
      'in Google Cloud Console.',
    );
    exit(ExitCode.success.code);
  }

  final versionUpgradingProgress =
      _logger.progress('Upgrading the app to the latest version');

  late final Version nextVersion;
  if (next == NextVersion.build) {
    if (latestIsPreRelease) {
      nextVersion = latestVersion.next(next);
    } else {
      nextVersion = latestVersion.next(NextVersion.patch).next(next);
    }
  } else {
    nextVersion = latestVersion.next(next).next(next);
  }

  try {
    final updatedPubspc = pubspec.copy(version: nextVersion);
    await updatedPubspc.save(outputDir);

    versionUpgradingProgress
        .complete(green.wrap('Version upgraded to $nextVersion'));
    // _logger.success('The app version has been upgraded to $nextVersion.');
  } catch (e) {
    versionUpgradingProgress.fail(
      'An error occured updating the pubspec.yaml file. '
      'Check that you are in the root of the project and '
      'that the file is properly formatted.',
    );

    exit(ExitCode.ioError.code);
  }

  exit(ExitCode.success.code);
}