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

feat: Tell the user to source the config file when instalaltion is done #41

Merged
merged 4 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 26 additions & 3 deletions lib/src/installer/completion_installation.dart
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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