install method

void install(
  1. String rootCommand, {
  2. bool force = false,
})

Install completion configuration files for a rootCommand in the current shell.

It will create:

  • A completion script file in completionConfigDir that is named after the rootCommand and the current shell (e.g. very_good.bash).
  • A config file in completionConfigDir that is named after the current shell (e.g. bash-config.bash) that sources the aforementioned completion script file.
  • A line in the shell config file (e.g. .bash_profile) that sources the aforementioned config file.

If force is true, it will overwrite the command's completion files even if they already exist. If false, it will check if is already installed, or if it has been explicitly uninstalled before installing it.

Implementation

void install(String rootCommand, {bool force = false}) {
  final configuration = this.configuration;

  if (configuration == null) {
    throw CompletionInstallationException(
      message: 'Unknown shell.',
      rootCommand: rootCommand,
    );
  }

  if (!force && !_shouldInstall(rootCommand)) {
    return;
  }

  logger.detail(
    'Installing completion for the command $rootCommand '
    'on ${configuration.shell.name}',
  );

  createCompletionConfigDir();
  final completionFileCreated = writeCompletionScriptForCommand(rootCommand);
  writeCompletionConfigForShell(rootCommand);
  writeToShellConfigFile(rootCommand);

  if (completionFileCreated) {
    _logSourceInstructions(rootCommand);
  }

  final completionConfiguration =
      CompletionConfiguration.fromFile(completionConfigurationFile);
  completionConfiguration
      .copyWith(
        uninstalls: completionConfiguration.uninstalls.exclude(
          command: rootCommand,
          systemShell: configuration.shell,
        ),
        installs: completionConfiguration.installs.include(
          command: rootCommand,
          systemShell: configuration.shell,
        ),
      )
      .writeTo(completionConfigurationFile);
}