writeCompletionScriptForCommand method

  1. @visibleForTesting
bool writeCompletionScriptForCommand(
  1. String rootCommand
)

Creates a configuration file exclusively to rootCommand and the identified shell.

The file will be named after the rootCommand and the current shell (e.g. very_good.bash).

The file will be created in completionConfigDir.

If the file already exists, it will do nothing.

Returns true if the file was created, false otherwise.

Implementation

@visibleForTesting
bool writeCompletionScriptForCommand(String rootCommand) {
  final configuration = this.configuration!;
  final completionConfigDirPath = completionConfigDir.path;
  final commandScriptName = '$rootCommand.${configuration.shell.name}';
  final commandScriptPath = path.join(
    completionConfigDirPath,
    commandScriptName,
  );
  logger.info(
    'Writing completion script for $rootCommand on $commandScriptPath',
  );

  final scriptFile = File(commandScriptPath);

  if (scriptFile.existsSync()) {
    logger.warn(
      'A script file for $rootCommand was already found on '
      '$commandScriptPath.',
    );
    return false;
  }

  scriptFile.writeAsStringSync(configuration.scriptTemplate(rootCommand));

  return true;
}