@@ -304,6 +304,85 @@ ${configuration!.sourceLineTemplate(scriptPath)}''';
304304
305305 logger.info ('Added config to $configFilePath ' );
306306 }
307+
308+ /// Uninstalls the completion for the command [rootCommand] on the current
309+ /// shell.
310+ ///
311+ /// Before uninstalling, it checks if the completion is installed:
312+ /// - The shell has an existing RCFile with a completion
313+ /// [ScriptConfigurationEntry] .
314+ /// - The shell has an existing completion configuration file with a
315+ /// [ScriptConfigurationEntry] for the [rootCommand] .
316+ ///
317+ /// If any of the above is not true, it throws a
318+ /// [CompletionUnistallationException] .
319+ ///
320+ /// Upon a successful uninstallation the executable [ScriptConfigurationEntry]
321+ /// is removed from the shell configuration file. If after this removal the
322+ /// latter is empty, it is deleted together with the the executable completion
323+ /// script and the completion [ScriptConfigurationEntry] from the shell RC
324+ /// file. In the case that there are no other completion scripts installed on
325+ /// other shells the completion config directory is deleted, leaving the
326+ /// user's system as it was before the installation.
327+ void uninstall (String rootCommand) {
328+ final configuration = this .configuration! ;
329+ logger.detail (
330+ '''Uninstalling completion for the command $rootCommand on ${configuration .shell .name }''' ,
331+ );
332+
333+ final shellRCFile = File (_shellRCFilePath);
334+ if (! shellRCFile.existsSync ()) {
335+ throw CompletionUnistallationException (
336+ executableName: rootCommand,
337+ message: 'No shell RC file found at ${shellRCFile .path }' ,
338+ );
339+ }
340+
341+ const completionEntry = ScriptConfigurationEntry ('Completion' );
342+ if (! completionEntry.existsIn (shellRCFile)) {
343+ throw CompletionUnistallationException (
344+ executableName: rootCommand,
345+ message: 'Completion is not installed at ${shellRCFile .path }' ,
346+ );
347+ }
348+
349+ final shellCompletionConfigurationFile = File (
350+ path.join (
351+ completionConfigDir.path,
352+ configuration.completionConfigForShellFileName,
353+ ),
354+ );
355+ final executableEntry = ScriptConfigurationEntry (rootCommand);
356+ if (! executableEntry.existsIn (shellCompletionConfigurationFile)) {
357+ throw CompletionUnistallationException (
358+ executableName: rootCommand,
359+ message:
360+ '''No shell script file found at ${shellCompletionConfigurationFile .path }''' ,
361+ );
362+ }
363+
364+ final executableShellCompletionScriptFile = File (
365+ path.join (
366+ completionConfigDir.path,
367+ '$rootCommand .${configuration .shell .name }' ,
368+ ),
369+ );
370+ if (executableShellCompletionScriptFile.existsSync ()) {
371+ executableShellCompletionScriptFile.deleteSync ();
372+ }
373+
374+ executableEntry.removeFrom (
375+ shellCompletionConfigurationFile,
376+ shouldDelete: true ,
377+ );
378+ if (! shellCompletionConfigurationFile.existsSync ()) {
379+ completionEntry.removeFrom (shellRCFile);
380+ }
381+
382+ if (completionConfigDir.listSync ().isEmpty) {
383+ completionConfigDir.deleteSync ();
384+ }
385+ }
307386}
308387
309388/// Resolve the home from a path string
0 commit comments