uninstall method

void uninstall(
  1. String rootCommand
)

Uninstalls the completion for the command rootCommand on the current shell.

Before uninstalling, it checks if the completion is installed:

If any of the above is not true, it throws a CompletionUninstallationException.

Upon a successful uninstallation the executable ScriptConfigurationEntry is removed from the shell configuration file. If after this removal the latter is empty, it is deleted together with the the executable completion script and the completion ScriptConfigurationEntry from the shell RC file. In the case that there are no other completion scripts installed on other shells the completion config directory is deleted, leaving the user's system as it was before the installation.

Implementation

void uninstall(String rootCommand) {
  final configuration = this.configuration!;
  logger.detail(
    '''Uninstalling completion for the command $rootCommand on ${configuration.shell.name}''',
  );

  final shellRCFile = File(_shellRCFilePath);
  if (!shellRCFile.existsSync()) {
    throw CompletionUninstallationException(
      rootCommand: rootCommand,
      message: 'No shell RC file found at ${shellRCFile.path}',
    );
  }

  const completionEntry = ScriptConfigurationEntry('Completion');
  if (!completionEntry.existsIn(shellRCFile)) {
    throw CompletionUninstallationException(
      rootCommand: rootCommand,
      message: 'Completion is not installed at ${shellRCFile.path}',
    );
  }

  final shellCompletionConfigurationFile = File(
    path.join(
      completionConfigDir.path,
      configuration.completionConfigForShellFileName,
    ),
  );
  final executableEntry = ScriptConfigurationEntry(rootCommand);
  if (!executableEntry.existsIn(shellCompletionConfigurationFile)) {
    throw CompletionUninstallationException(
      rootCommand: rootCommand,
      message:
          '''No shell script file found at ${shellCompletionConfigurationFile.path}''',
    );
  }

  final executableShellCompletionScriptFile = File(
    path.join(
      completionConfigDir.path,
      '$rootCommand.${configuration.shell.name}',
    ),
  );
  if (executableShellCompletionScriptFile.existsSync()) {
    executableShellCompletionScriptFile.deleteSync();
  }

  executableEntry.removeFrom(
    shellCompletionConfigurationFile,
    shouldDelete: true,
  );
  if (!shellCompletionConfigurationFile.existsSync()) {
    completionEntry.removeFrom(shellRCFile);
  }
  final completionConfigDirContent = completionConfigDir.listSync();
  final onlyHasConfigurationFile = completionConfigDirContent.length == 1 &&
      path.absolute(completionConfigDirContent.first.path) ==
          path.absolute(completionConfigurationFile.path);
  if (completionConfigDirContent.isEmpty || onlyHasConfigurationFile) {
    completionConfigDir.deleteSync(recursive: true);
  } else {
    final completionConfiguration =
        CompletionConfiguration.fromFile(completionConfigurationFile);
    completionConfiguration
        .copyWith(
          uninstalls: completionConfiguration.uninstalls.include(
            command: rootCommand,
            systemShell: configuration.shell,
          ),
          installs: completionConfiguration.installs.exclude(
            command: rootCommand,
            systemShell: configuration.shell,
          ),
        )
        .writeTo(completionConfigurationFile);
  }
}