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 ctx = await context;
  final channel = optional('channel');

  final info = await ctx.store.checkForUpdates(
    packageReference: require('package'),
    currentVersion: Versions.parse(require('current')),
    channel: channel == null ? null : ReleaseChannel.parse(channel),
    // Defaults to this machine, so "is there an update" means "one I can
    // actually install" without the caller having to say so.
    platform: switch (optional('platform')) {
      'any' => null,
      final explicit? => explicit,
      null => Platforms.current,
    },
  );

  if (ctx.jsonOutput) {
    ctx.writeJson(info.toJson());
  } else if (!info.updateAvailable) {
    ctx.info(
      '${info.packageName} ${info.currentVersion} is up to date on the '
      '${info.channel.name} channel.',
    );
  } else {
    ctx.info(
      '${info.packageName} ${info.latestVersion} is available '
      '(you have ${info.currentVersion}, ${info.channel.name} channel).',
    );
    if (info.notes != null && info.notes!.isNotEmpty) {
      ctx.info('');
      ctx.info(info.notes!);
    }
    if (info.asset == null) {
      ctx.info('');
      ctx.info(
        'No artifact matches this platform, so there is nothing to install.',
      );
    }
  }

  // Exit codes let a shell script branch without parsing output: 0 = current,
  // 10 = an update is available. A non-zero code that is not an error needs
  // to be distinct from the 1 a real failure produces.
  return info.updateAvailable ? 10 : 0;
}