Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 33 additions & 24 deletions sidekick_core/lib/sidekick_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,20 @@ class SidekickCommandRunner<T> extends CompletionCommandRunner<T> {
final unmount = mount(debugName: args.join(' '));

ArgResults? parsedArgs;
Future<String?>? 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;
Expand All @@ -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');
Expand All @@ -320,24 +322,19 @@ class SidekickCommandRunner<T> extends CompletionCommandRunner<T> {
}

try {
if (_isUpdateCheckEnabled &&
!_isSidekickCliUpdateCommand(parsedArgs) &&
!isRunningTabCompletionCommand()) {
await updateCheck?.then((result) {
// 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();
}
if (result != null) printerr(result);
});
} finally {
unmount();
}
}
}

/// Print a warning if the CLI isn't up to date
Future<void> _checkForUpdates() async {
Future<String?> _checkForUpdates() async {
try {
final updateFuture = VersionChecker.isDependencyUpToDate(
package: SidekickContext.sidekickPackage,
Expand All @@ -346,15 +343,15 @@ 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.',
);
return '${yellow('Update available!')}\n'
'Run ${cyan('${SidekickContext.cliName} sidekick update')} to update your CLI.';
}
} catch (_) {
/* ignore */
}
return null;
}

/// Print a warning if the user manually updated the sidekick_core
Expand Down Expand Up @@ -397,6 +394,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
Expand Down