-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make update check faster #249
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -284,8 +284,20 @@ class SidekickCommandRunner<T> extends CompletionCommandRunner<T> { | |
final unmount = mount(debugName: args.join(' ')); | ||
|
||
ArgResults? parsedArgs; | ||
Future<void Function()?>? updateCheck; | ||
|
||
try { | ||
parsedArgs = parse(args); | ||
|
||
if (_isUpdateCheckEnabled && | ||
!_isSidekickCliUpdateCommand(parsedArgs) && | ||
!_isRunningTabCompletionCommand(parsedArgs)) { | ||
// print warning if CLI update is available | ||
// TODO prevent multiple update checks when a command start another command | ||
|
||
updateCheck = _checkForUpdates(); | ||
} | ||
|
||
if (parsedArgs['version'] == true) { | ||
print('${SidekickContext.cliName} is using sidekick version $version'); | ||
return null; | ||
|
@@ -294,23 +306,13 @@ class SidekickCommandRunner<T> extends CompletionCommandRunner<T> { | |
final result = await super.runCommand(parsedArgs); | ||
return result; | ||
} finally { | ||
// don't print anything additionally when running the hidden tab completion command (runs in the background when pressing tab), | ||
// otherwise anything that is printed will also be used as suggestion | ||
bool isRunningTabCompletionCommand() { | ||
final reservedCommands = [ | ||
HandleCompletionRequestCommand.commandName, | ||
InstallCompletionFilesCommand.commandName, | ||
]; | ||
return reservedCommands.contains(parsedArgs?.command?.name); | ||
} | ||
|
||
// don't show the install-global suggesting when running the install-global command | ||
bool isInstallGlobalCommand() => | ||
parsedArgs?.command?.name == 'sidekick' && | ||
parsedArgs!.arguments.contains('install-global'); | ||
|
||
if (!enableAutoInstall && | ||
!isRunningTabCompletionCommand() && | ||
!_isRunningTabCompletionCommand(parsedArgs) && | ||
!isInstallGlobalCommand()) { | ||
final command = | ||
yellow('./${SidekickContext.cliName} sidekick install-global'); | ||
|
@@ -320,24 +322,16 @@ class SidekickCommandRunner<T> extends CompletionCommandRunner<T> { | |
} | ||
|
||
try { | ||
if (_isUpdateCheckEnabled && | ||
!_isSidekickCliUpdateCommand(parsedArgs) && | ||
!isRunningTabCompletionCommand()) { | ||
// print warning if the user didn't fully update their CLI | ||
_checkCliVersionIntegrity(); | ||
// print warning if CLI update is available | ||
// TODO start the update check in the background at command start | ||
// TODO prevent multiple update checks when a command start another command | ||
await _checkForUpdates(); | ||
} | ||
(await updateCheck)?.call(); | ||
} finally { | ||
unmount(); | ||
} | ||
} | ||
} | ||
|
||
/// Print a warning if the CLI isn't up to date | ||
Future<void> _checkForUpdates() async { | ||
Future<void Function()> _checkForUpdates() async { | ||
void Function()? updateCheck; | ||
try { | ||
final updateFuture = VersionChecker.isDependencyUpToDate( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The try catch is now useless, because the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Future itself is awaited in Line 345 in combination with the timeout |
||
package: SidekickContext.sidekickPackage, | ||
|
@@ -346,15 +340,21 @@ class SidekickCommandRunner<T> extends CompletionCommandRunner<T> { | |
); | ||
// If it takes too long, don't wait for it | ||
final isUpToDate = await updateFuture.timeout(const Duration(seconds: 3)); | ||
|
||
if (!isUpToDate) { | ||
printerr( | ||
'${yellow('Update available!')}\n' | ||
'Run ${cyan('${SidekickContext.cliName} sidekick update')} to update your CLI.', | ||
); | ||
updateCheck = () => printerr( | ||
'${yellow('Update available!')}\n' | ||
'Run ${cyan('${SidekickContext.cliName} sidekick update')} to update your CLI.', | ||
); | ||
} | ||
} catch (_) { | ||
/* ignore */ | ||
} | ||
return () { | ||
// print warning if the user didn't fully update their CLI | ||
_checkCliVersionIntegrity(); | ||
updateCheck?.call(); | ||
}; | ||
} | ||
|
||
/// Print a warning if the user manually updated the sidekick_core | ||
|
@@ -397,6 +397,18 @@ class SidekickCommandRunner<T> extends CompletionCommandRunner<T> { | |
} | ||
} | ||
|
||
/// Returns true if the executed command is a tab completion command | ||
/// | ||
/// don't print anything additionally when running the hidden tab completion command (runs in the background when pressing tab), | ||
/// otherwise anything that is printed will also be used as suggestion | ||
bool _isRunningTabCompletionCommand(ArgResults? parsedArgs) { | ||
final reservedCommands = [ | ||
HandleCompletionRequestCommand.commandName, | ||
InstallCompletionFilesCommand.commandName, | ||
]; | ||
return reservedCommands.contains(parsedArgs?.command?.name); | ||
} | ||
|
||
/// Returns true if the command executed from [parsedArgs] is [UpdateCommand] | ||
/// | ||
/// Copied and adapted from CommandRunner.runCommand | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A
Future<void>
should be sufficient. Thevoid Function()
seems redundantThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated it to a String? :)