Line data Source code
1 : import 'dart:io'; 2 : 3 : import 'package:args/command_runner.dart'; 4 : import 'package:mason/mason.dart' hide packageVersion; 5 : import 'package:pub_updater/pub_updater.dart'; 6 : import 'package:very_good_cli/src/command_runner.dart'; 7 : import 'package:very_good_cli/src/version.dart'; 8 : 9 : /// {@template update_command} 10 : /// `very_good update` command which updates very_good cli. 11 : /// {@endtemplate} 12 : class UpdateCommand extends Command<int> { 13 : /// {@macro update_command} 14 15 : UpdateCommand({ 15 : required Logger logger, 16 : PubUpdater? pubUpdater, 17 : }) : _logger = logger, 18 2 : _pubUpdater = pubUpdater ?? PubUpdater(); 19 : 20 : final Logger _logger; 21 : final PubUpdater _pubUpdater; 22 : 23 1 : @override 24 : String get description => 'Update Very Good CLI.'; 25 : 26 : /// The [name] of the command. But static. 27 : static const String commandName = 'update'; 28 : 29 15 : @override 30 : String get name => commandName; 31 : 32 2 : @override 33 : Future<int> run() async { 34 4 : final updateCheckProgress = _logger.progress('Checking for updates'); 35 : late final String latestVersion; 36 : try { 37 4 : latestVersion = await _pubUpdater.getLatestVersion(packageName); 38 : } catch (error) { 39 1 : updateCheckProgress.fail(); 40 3 : _logger.err('$error'); 41 1 : return ExitCode.software.code; 42 : } 43 2 : updateCheckProgress.complete('Checked for updates'); 44 : 45 2 : final isUpToDate = packageVersion == latestVersion; 46 : if (isUpToDate) { 47 2 : _logger.info('Very Good CLI is already at the latest version.'); 48 1 : return ExitCode.success.code; 49 : } 50 : 51 6 : final updateProgress = _logger.progress('Updating to $latestVersion'); 52 : 53 : late ProcessResult result; 54 : 55 : try { 56 4 : result = await _pubUpdater.update( 57 : packageName: packageName, 58 : versionConstraint: latestVersion, 59 : ); 60 : } catch (error) { 61 1 : updateProgress.fail(); 62 3 : _logger.err('$error'); 63 1 : return ExitCode.software.code; 64 : } 65 : 66 6 : if (result.exitCode != ExitCode.success.code) { 67 1 : updateProgress.fail(); 68 4 : _logger.err('Error updating Very Good CLI: ${result.stderr}'); 69 1 : return ExitCode.software.code; 70 : } 71 : 72 4 : updateProgress.complete('Updated to $latestVersion'); 73 : 74 2 : return ExitCode.success.code; 75 : } 76 : }