Skip to content

Commit 3458c64

Browse files
committed
feat: Tell the user to source the config file when installation is done (#41)
1 parent 796eeda commit 3458c64

File tree

8 files changed

+55
-7
lines changed

8 files changed

+55
-7
lines changed

lib/src/installer/completion_installation.dart

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,13 @@ class CompletionInstallation {
113113
);
114114

115115
createCompletionConfigDir();
116-
writeCompletionScriptForCommand(rootCommand);
116+
final completionFileCreated = writeCompletionScriptForCommand(rootCommand);
117117
writeCompletionConfigForShell(rootCommand);
118118
writeToShellConfigFile(rootCommand);
119+
120+
if (completionFileCreated) {
121+
_logSourceInstructions(rootCommand);
122+
}
119123
}
120124

121125
/// Create a directory in which the completion config files shall be saved.
@@ -150,8 +154,10 @@ class CompletionInstallation {
150154
/// The file will be created in [completionConfigDir].
151155
///
152156
/// If the file already exists, it will do nothing.
157+
///
158+
/// Returns true if the file was created, false otherwise.
153159
@visibleForTesting
154-
void writeCompletionScriptForCommand(String rootCommand) {
160+
bool writeCompletionScriptForCommand(String rootCommand) {
155161
final configuration = this.configuration!;
156162
final completionConfigDirPath = completionConfigDir.path;
157163
final commandScriptName = '$rootCommand.${configuration.name}';
@@ -170,10 +176,12 @@ class CompletionInstallation {
170176
'A script file for $rootCommand was already found on '
171177
'$commandScriptPath.',
172178
);
173-
return;
179+
return false;
174180
}
175181

176182
scriptFile.writeAsStringSync(configuration.scriptTemplate(rootCommand));
183+
184+
return true;
177185
}
178186

179187
/// Adds a reference for the command-specific config file created on
@@ -265,6 +273,21 @@ class CompletionInstallation {
265273
);
266274
}
267275

276+
/// Tells the user to source the shell configuration file.
277+
void _logSourceInstructions(String rootCommand) {
278+
final level = logger.level;
279+
logger
280+
..level = Level.info
281+
..info(
282+
'\n'
283+
'Completion files installed. To enable completion, run the following '
284+
'command in your shell:\n'
285+
'${lightCyan.wrap('source $_shellRCFilePath')}'
286+
'\n',
287+
)
288+
..level = level;
289+
}
290+
268291
void _sourceScriptOnFile({
269292
required File configFile,
270293
required String scriptName,

test/src/install/completion_installation_test.dart renamed to test/src/installer/completion_installation_test.dart

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void main() {
1414

1515
setUp(() {
1616
logger = MockLogger();
17+
when(() => logger.level).thenReturn(Level.quiet);
1718
tempDir = Directory.systemTemp.createTempSync();
1819
});
1920

@@ -145,11 +146,11 @@ void main() {
145146

146147
expect(configFile.existsSync(), false);
147148

148-
installation
149-
..createCompletionConfigDir()
150-
..writeCompletionScriptForCommand('very_good');
149+
installation.createCompletionConfigDir();
150+
var result = installation.writeCompletionScriptForCommand('very_good');
151151

152152
expect(configFile.existsSync(), true);
153+
expect(result, true);
153154

154155
expect(
155156
configFile.readAsStringSync(),
@@ -166,7 +167,9 @@ void main() {
166167
),
167168
);
168169

169-
installation.writeCompletionScriptForCommand('very_good');
170+
result = installation.writeCompletionScriptForCommand('very_good');
171+
172+
expect(result, false);
170173

171174
verify(
172175
() => logger.warn(
@@ -286,6 +289,17 @@ void main() {
286289

287290
installation.install('very_good');
288291

292+
verify(() => logger.level = Level.info).called(1);
293+
294+
verify(
295+
() => logger.info(
296+
'\n'
297+
'Completion files installed. To enable completion, run the '
298+
'following command in your shell:\n'
299+
'source ${path.join(tempDir.path, '.zshrc')}\n',
300+
),
301+
).called(1);
302+
289303
reset(logger);
290304

291305
// install again
@@ -321,6 +335,17 @@ void main() {
321335
'${path.join(tempDir.path, '.zshrc')}.',
322336
),
323337
).called(1);
338+
339+
verifyNever(() => logger.level = Level.debug);
340+
341+
verifyNever(
342+
() => logger.info(
343+
'\n'
344+
'Completion files installed. To enable completion, run the '
345+
'following command in your shell:\n'
346+
'source ${path.join(tempDir.path, '.zshrc')}\n',
347+
),
348+
);
324349
},
325350
);
326351

test/src/install/shell_completion_configuration_test.dart renamed to test/src/installer/shell_completion_configuration_test.dart

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)