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 {
  final updateCheckProgress = _logger.progress('Checking for updates');
  late final String latestVersion;
  try {
    latestVersion = await _pubUpdater.getLatestVersion(packageName);
  } catch (error) {
    updateCheckProgress.fail();
    _logger.err('$error');
    return ExitCode.software.code;
  }
  updateCheckProgress.complete('Checked for updates');

  final isUpToDate = packageVersion == latestVersion;
  if (isUpToDate) {
    _logger.info('CLI is already at the latest version.');
    return ExitCode.success.code;
  }

  final updateProgress = _logger.progress('Updating to $latestVersion');

  late final ProcessResult result;
  try {
    result = await _pubUpdater.update(
      packageName: packageName,
      versionConstraint: latestVersion,
    );
  } catch (error) {
    updateProgress.fail();
    _logger.err('$error');
    return ExitCode.software.code;
  }

  if (result.exitCode != ExitCode.success.code) {
    updateProgress.fail();
    _logger.err('Error updating CLI: ${result.stderr}');
    return ExitCode.software.code;
  }

  updateProgress.complete('Updated to $latestVersion');

  return ExitCode.success.code;
}