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: include uninstallation logic #70

Merged
merged 32 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
12eb748
refactor: added ScriptConfigurationEntry
alestiago May 10, 2023
000aa81
refactor: remove empty space
alestiago May 10, 2023
ac0a310
refactor: use completion installation to verify installation
alestiago May 10, 2023
4421964
refactor: removing unused code
alestiago May 10, 2023
8e6939c
feat: reformatted start and end comments
alestiago May 10, 2023
3397c16
refactor: used barrel file
alestiago May 10, 2023
6d37050
test: refactor expectation
alestiago May 10, 2023
c9cca0a
test: remove white space
alestiago May 11, 2023
ba6eaaf
Merge branch 'main' into refactor/script-configuration-entry
alestiago May 11, 2023
ac810c3
feat: fixed merge issue
alestiago May 11, 2023
98f4cf0
test: removed extra space
alestiago May 11, 2023
ccf8de3
feat: uninstall logic
alestiago May 11, 2023
111892c
Merge branch 'main' into refactor/uninstallation-logic
alestiago May 12, 2023
fd16cfa
test: removeFrom
alestiago May 12, 2023
2b19a14
fix: added break condition
alestiago May 12, 2023
817ae1b
refactor: used while instead of do while
alestiago May 12, 2023
c87eb66
feat: refined removedFrom
alestiago May 13, 2023
b571c14
feat: refined uninstall logic
alestiago May 13, 2023
e8335c9
feat: tested and configured uninstalling behaviour
alestiago May 13, 2023
c327c2c
test: included missing executable script deletion
alestiago May 13, 2023
bfccc39
feat: refined uninstallation logic
alestiago May 13, 2023
11b990e
feat: improved uninstall behaviour for multiple shells
alestiago May 13, 2023
b2db32a
test: added CompletionUnistallationException
alestiago May 13, 2023
ac3a066
docs: improved uninstall documentation
alestiago May 13, 2023
55ee7ec
test: improved names
alestiago May 13, 2023
e249a97
refactor: renamed executablName to rootCommand
alestiago May 16, 2023
96b2796
refactor: made shouldDelete false by default
alestiago May 16, 2023
db0b752
docs: fixed wrong documentation
alestiago May 16, 2023
cba7174
test: renamed file
alestiago May 16, 2023
0736ca5
test: include _test in test name
alestiago May 16, 2023
bc847d4
Update lib/src/installer/completion_installation.dart
alestiago May 17, 2023
da11c75
Merge branch 'main' into refactor/uninstallation-logic
renancaraujo May 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions lib/src/installer/completion_installation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,82 @@ ${configuration!.sourceLineTemplate(scriptPath)}''';

logger.info('Added config to $configFilePath');
}

/// Uninstalls the completion for the command [executableName] on the current
/// shell.
///
/// Before uninstalling, it checks if the completion is installed:
/// - The shell has an existing RCFile with a completion
/// [ScriptConfigurationEntry].
/// - The shell has an exisiting completion configuration file with a
renancaraujo marked this conversation as resolved.
Show resolved Hide resolved
alestiago marked this conversation as resolved.
Show resolved Hide resolved
/// [ScriptConfigurationEntry] for the [executableName].
///
/// If any of the above is not true, it throws a
/// [CompletionUnistallationException].
///
/// Upon a successful uninstallation the executable [ScriptConfigurationEntry]
/// is removed from the shell configuration file. If after this removal the
/// latter is empty, it is deleted together with the the executable completion
/// script and the completion [ScriptConfigurationEntry] from the shell RC
/// file. In the case that there are no other completion scripts installed on
/// other shells the completion config directory is deleted, leaving the
/// user's system as it was before the installation.
void uninstall(String executableName) {
alestiago marked this conversation as resolved.
Show resolved Hide resolved
final configuration = this.configuration!;
logger.detail(
'''Uninstalling completion for the command $executableName on ${configuration.shell.name}''',
);

final shellRCFile = File(_shellRCFilePath);
if (!shellRCFile.existsSync()) {
throw CompletionUnistallationException(
executableName: executableName,
message: 'No shell RC file found at ${shellRCFile.path}',
);
}

const completionEntry = ScriptConfigurationEntry('Completion');
if (!completionEntry.existsIn(shellRCFile)) {
throw CompletionUnistallationException(
executableName: executableName,
message: 'Completion is not installed at ${shellRCFile.path}',
);
}

final shellCompletionConfigurationFile = File(
path.join(
completionConfigDir.path,
configuration.completionConfigForShellFileName,
),
);
final executableEntry = ScriptConfigurationEntry(executableName);
if (!executableEntry.existsIn(shellCompletionConfigurationFile)) {
throw CompletionUnistallationException(
executableName: executableName,
message:
'''No shell script file found at ${shellCompletionConfigurationFile.path}''',
);
}

final executableShellCompletionScriptFile = File(
path.join(
completionConfigDir.path,
'$executableName.${configuration.shell.name}',
),
);
if (executableShellCompletionScriptFile.existsSync()) {
executableShellCompletionScriptFile.deleteSync();
}

executableEntry.removeFrom(shellCompletionConfigurationFile);
if (!shellCompletionConfigurationFile.existsSync()) {
completionEntry.removeFrom(shellRCFile, shouldDelete: false);
}

if (completionConfigDir.listSync().isEmpty) {
completionConfigDir.deleteSync();
}
}
}

/// Resolve the home from a path string
Expand Down
21 changes: 21 additions & 0 deletions lib/src/installer/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,24 @@ class CompletionInstallationException implements Exception {
String toString() => 'Could not install completion scripts for $rootCommand: '
'$message';
}

/// {@template completion_unistallation_exception}
/// Describes an exception during the installation of completion scripts.
alestiago marked this conversation as resolved.
Show resolved Hide resolved
/// {@endtemplate}
class CompletionUnistallationException implements Exception {
/// {@macro completion_unistallation_exception}
CompletionUnistallationException({
required this.message,
required this.executableName,
});

/// The error message for this exception
final String message;

/// The command for which the installation failed.
final String executableName;
alestiago marked this conversation as resolved.
Show resolved Hide resolved

@override
String toString() =>
'''Could not uninstall completion scripts for $executableName: $message''';
}
38 changes: 34 additions & 4 deletions lib/src/installer/script_configuration_entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,46 @@ class ScriptConfigurationEntry {
file.createSync(recursive: true);
}

final entry = StringBuffer()
final stringBuffer = StringBuffer()
..writeln()
..writeln(_startComment)
..writeln(content)
..writeln(_startComment);
if (content != null) stringBuffer.writeln(content);
stringBuffer
..writeln(_endComment)
..writeln();

file.writeAsStringSync(
entry.toString(),
stringBuffer.toString(),
mode: FileMode.append,
);
}

/// Removes the entry with [name] from the [file].
///
/// If the [file] does not exist, this will do nothing.
///
/// If a file has multiple entries with the same [name], all of them will be
/// removed.
///
/// If [shouldDelete] is true, the [file] will be deleted if it is empty after
/// removing the entry. Otherwise, the [file] will be left empty.
void removeFrom(File file, {bool shouldDelete = true}) {
alestiago marked this conversation as resolved.
Show resolved Hide resolved
if (!file.existsSync()) return;

final content = file.readAsStringSync();
final stringPattern = '\n$_startComment.*$_endComment\n\n'
.replaceAll('[', r'\[')
.replaceAll(']', r'\]');
final pattern = RegExp(
stringPattern,
multiLine: true,
dotAll: true,
);
final newContent = content.replaceAllMapped(pattern, (_) => '');
file.writeAsStringSync(newContent);

if (shouldDelete && newContent.trim().isEmpty) {
file.deleteSync();
}
}
}
Loading