@@ -3,15 +3,17 @@ import 'dart:convert';
33import 'dart:io' ;
44
55import 'package:args/command_runner.dart' ;
6+ import 'package:celest_cli/src/cli/cli_runtime.dart' ;
67import 'package:celest_cli/src/context.dart' ;
78import 'package:celest_cli/src/models.dart' ;
9+ import 'package:celest_cli/src/releases/celest_release_info.dart' ;
810import 'package:celest_cli/src/releases/latest_release.dart' ;
911import 'package:celest_cli/src/sdk/dart_sdk.dart' ;
10- import 'package:cli_util/cli_util.dart' ;
12+ import 'package:celest_cli/src/version.dart' ;
13+ import 'package:collection/collection.dart' ;
1114import 'package:http/http.dart' as http;
1215import 'package:logging/logging.dart' ;
1316import 'package:meta/meta.dart' ;
14- import 'package:path/path.dart' as p;
1517import 'package:pub_semver/pub_semver.dart' ;
1618
1719/// Base class for all commands in this package providing common functionality.
@@ -24,16 +26,6 @@ abstract base class CelestCommand extends Command<int> {
2426 /// Whether verbose logging is enabled.
2527 bool get verbose => globalResults? ['verbose' ] as bool ? ?? false ;
2628
27- /// The path to the Flutter SDK, if installed.
28- late final String ? flutterRoot = () {
29- final dartSdkPath = getSdkPath ();
30- final flutterBin = p.dirname (p.dirname (dartSdkPath));
31- if (File (p.join (flutterBin, 'flutter' )).existsSync ()) {
32- return p.dirname (flutterBin);
33- }
34- return null ;
35- }();
36-
3729 /// Resolves the latest version information from `pub.dev` .
3830 Future <PubVersionInfo ?> resolveVersionInfo (String package) async {
3931 // Get the currently published version of the package.
@@ -66,23 +58,57 @@ abstract base class CelestCommand extends Command<int> {
6658 return PubVersionInfo (semvers..sort ());
6759 }
6860
69- Future <void > checkForLatestVersion () async {
61+ Future <void > checkForLatestVersion ({bool ? includeDev}) async {
62+ final (latestVersion, _) = await getLatestVersion (includeDev: includeDev);
63+ if (latestVersion < currentVersion) {
64+ cliLogger.warn (
65+ 'A new version of Celest is available! Run `celest upgrade` '
66+ 'to get the latest changes.' ,
67+ );
68+ }
69+ }
70+
71+ Future <(Version , CelestReleaseInfo ?)> getLatestVersion ({
72+ bool ? includeDev,
73+ }) async {
74+ includeDev ?? = currentVersion.isPreRelease;
7075 try {
71- final latestRelease = await performance.trace (
76+ final (latestVersion, releaseInfo) = await performance.trace (
7277 'CelestCommand' ,
7378 'retrieveLatestRelease' ,
74- () =>
75- retrieveLatestRelease (version).timeout (const Duration (seconds: 3 )),
79+ () => switch (CliRuntime .current) {
80+ CliRuntime .aot => retrieveLatestRelease (includeDev: includeDev! )
81+ .then ((release) => (release.version, release)),
82+ CliRuntime .local => Future .value ((currentVersion, null )),
83+ CliRuntime .pubGlobal => _latestVersionPub (includeDev: includeDev! )
84+ .then ((version) => (version, null )),
85+ }
86+ .timeout (const Duration (seconds: 3 )),
7687 );
77- if (latestRelease.version > Version .parse (version)) {
78- cliLogger.warn (
79- 'A new version of Celest is available! Run `celest upgrade` '
80- 'to get the latest changes.' ,
81- );
82- }
88+ return (latestVersion, releaseInfo);
8389 } on Object catch (e, st) {
8490 performance.captureError (e, stackTrace: st);
91+ return (currentVersion, null );
92+ }
93+ }
94+
95+ Future <Version > _latestVersionPub ({
96+ bool includeDev = false ,
97+ }) async {
98+ final versionInfo = await resolveVersionInfo ('celest_cli' );
99+ if (versionInfo == null ) {
100+ throw Exception (
101+ 'Failed to resolve version information for celest_cli.' ,
102+ );
85103 }
104+ return maxBy (
105+ [
106+ currentVersion,
107+ versionInfo.latestVersion,
108+ if (includeDev) versionInfo.latestPrerelease,
109+ ].nonNulls,
110+ (v) => v,
111+ )! ;
86112 }
87113
88114 @override
0 commit comments