Skip to content

Commit

Permalink
feat: Tell the user to source the config file when instalaltion is do…
Browse files Browse the repository at this point in the history
…ne (#41)
  • Loading branch information
renancaraujo committed Dec 16, 2022
1 parent 796eeda commit 53d1f08
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 7 deletions.
29 changes: 26 additions & 3 deletions lib/src/installer/completion_installation.dart
Expand Up @@ -113,9 +113,13 @@ class CompletionInstallation {
);

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

if (completionFileCreated) {
_logSourceInstructions(rootCommand);
}
}

/// Create a directory in which the completion config files shall be saved.
Expand Down Expand Up @@ -150,8 +154,10 @@ class CompletionInstallation {
/// 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.
@visibleForTesting
void writeCompletionScriptForCommand(String rootCommand) {
bool writeCompletionScriptForCommand(String rootCommand) {
final configuration = this.configuration!;
final completionConfigDirPath = completionConfigDir.path;
final commandScriptName = '$rootCommand.${configuration.name}';
Expand All @@ -170,10 +176,12 @@ class CompletionInstallation {
'A script file for $rootCommand was already found on '
'$commandScriptPath.',
);
return;
return false;
}

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

return true;
}

/// Adds a reference for the command-specific config file created on
Expand Down Expand Up @@ -265,6 +273,21 @@ class CompletionInstallation {
);
}

/// Tells the user to source the shell configuration file.
void _logSourceInstructions(String rootCommand) {
final level = logger.level;
logger
..level = Level.info
..info(
'\n'
'Completion files installed. To enable completion, run the following '
'command in your shell:\n'
'${lightCyan.wrap('source $_shellRCFilePath')}'
'\n',
)
..level = level;
}

void _sourceScriptOnFile({
required File configFile,
required String scriptName,
Expand Down
Expand Up @@ -14,6 +14,7 @@ void main() {

setUp(() {
logger = MockLogger();
when(() => logger.level).thenReturn(Level.quiet);
tempDir = Directory.systemTemp.createTempSync();
});

Expand Down Expand Up @@ -145,11 +146,11 @@ void main() {

expect(configFile.existsSync(), false);

installation
..createCompletionConfigDir()
..writeCompletionScriptForCommand('very_good');
installation.createCompletionConfigDir();
var result = installation.writeCompletionScriptForCommand('very_good');

expect(configFile.existsSync(), true);
expect(result, true);

expect(
configFile.readAsStringSync(),
Expand All @@ -166,7 +167,9 @@ void main() {
),
);

installation.writeCompletionScriptForCommand('very_good');
result = installation.writeCompletionScriptForCommand('very_good');

expect(result, false);

verify(
() => logger.warn(
Expand Down Expand Up @@ -286,6 +289,17 @@ void main() {

installation.install('very_good');

verify(() => logger.level = Level.info).called(1);

verify(
() => logger.info(
'\n'
'Completion files installed. To enable completion, run the '
'following command in your shell:\n'
'source ${path.join(tempDir.path, '.zshrc')}\n',
),
).called(1);

reset(logger);

// install again
Expand Down Expand Up @@ -321,6 +335,17 @@ void main() {
'${path.join(tempDir.path, '.zshrc')}.',
),
).called(1);

verifyNever(() => logger.level = Level.debug);

verifyNever(
() => logger.info(
'\n'
'Completion files installed. To enable completion, run the '
'following command in your shell:\n'
'source ${path.join(tempDir.path, '.zshrc')}\n',
),
);
},
);

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 53d1f08

Please sign in to comment.