Skip to content

Commit 617d79d

Browse files
committed
Updated completion install so that it supports multiple ShellRC file options and will use the first one it finds.
1 parent a12a06c commit 617d79d

File tree

4 files changed

+100
-11
lines changed

4 files changed

+100
-11
lines changed

lib/src/installer/completion_installation.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,23 @@ class CompletionInstallation {
274274
);
275275
}
276276

277-
String get _shellRCFilePath =>
278-
_resolveHome(configuration!.shellRCFile, environment);
277+
278+
String? __shellRCFilePath;
279+
String get _shellRCFilePath {
280+
String? firstPath;
281+
if(__shellRCFilePath == null){
282+
for(final fileName in configuration!.shellRCFiles){
283+
final filePath = _resolveHome(fileName, environment);
284+
firstPath ??= filePath;
285+
if(File(filePath).existsSync()){
286+
__shellRCFilePath = filePath;
287+
return __shellRCFilePath!;
288+
}
289+
}
290+
__shellRCFilePath = firstPath;
291+
}
292+
return __shellRCFilePath!;
293+
}
279294

280295
/// Write a source to the completion global script in the shell configuration
281296
/// file, which its location is described by the [configuration].

lib/src/installer/shell_completion_configuration.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ShellCompletionConfiguration {
2222
/// {@macro shell_completion_configuration}
2323
const ShellCompletionConfiguration._({
2424
required this.shell,
25-
required this.shellRCFile,
25+
required this.shellRCFiles,
2626
required this.sourceLineTemplate,
2727
required this.scriptTemplate,
2828
});
@@ -42,9 +42,12 @@ class ShellCompletionConfiguration {
4242
/// {@macro system_shell}
4343
final SystemShell shell;
4444

45-
/// The location of a config file that is run upon shell start.
45+
/// A preferential ordered list of locations of a config file that is run upon
46+
/// shell start. The list is to allow multiple options eg both .bash_profile
47+
/// and .bashrc. The first option will be tried first and and if the file
48+
/// doesn't exist the next one will be tried.
4649
/// Eg: .bash_profile or .zshrc
47-
final String shellRCFile;
50+
final List<String> shellRCFiles;
4851

4952
/// Generates a line to sources of a script file.
5053
final SourceStringTemplate sourceLineTemplate;
@@ -61,7 +64,7 @@ class ShellCompletionConfiguration {
6164
@visibleForTesting
6265
final zshConfiguration = ShellCompletionConfiguration._(
6366
shell: SystemShell.zsh,
64-
shellRCFile: '~/.zshrc',
67+
shellRCFiles: const ['~/.zshrc'],
6568
sourceLineTemplate: (String scriptPath) {
6669
return '[[ -f $scriptPath ]] && . $scriptPath || true';
6770
},
@@ -94,7 +97,7 @@ fi
9497
@visibleForTesting
9598
final bashConfiguration = ShellCompletionConfiguration._(
9699
shell: SystemShell.bash,
97-
shellRCFile: '~/.bash_profile',
100+
shellRCFiles: const ['~/.bashrc','~/.bash_profile'],
98101
sourceLineTemplate: (String scriptPath) {
99102
return '[ -f $scriptPath ] && . $scriptPath || true';
100103
},

test/src/installer/completion_installation_test.dart

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,73 @@ void main() {
727727
);
728728
},
729729
);
730+
731+
test(
732+
'installing completion for .bashrc',
733+
() {
734+
735+
final bashInstallation = CompletionInstallation(
736+
logger: logger,
737+
isWindows: false,
738+
environment: {
739+
'HOME': tempDir.path,
740+
},
741+
configuration: bashConfiguration,
742+
);
743+
744+
final configDir = bashInstallation.completionConfigDir;
745+
746+
final bashProfile = File(path.join(tempDir.path, '.bash_profile'))
747+
..createSync();
748+
749+
bashInstallation
750+
.install('very_good');
751+
752+
// Different format needed for matching cli output
753+
expect(bashProfile.readAsStringSync(), '''
754+
\n## [Completion]
755+
## Completion scripts setup. Remove the following line to uninstall
756+
[ -f ${configDir.path}/bash-config.bash ] && . ${configDir.path}/bash-config.bash || true
757+
## [/Completion]
758+
759+
''');
760+
761+
},
762+
);
763+
764+
test(
765+
'installing completion for .bash_profile',
766+
() {
767+
768+
final bashInstallation = CompletionInstallation(
769+
logger: logger,
770+
isWindows: false,
771+
environment: {
772+
'HOME': tempDir.path,
773+
},
774+
configuration: bashConfiguration,
775+
);
776+
777+
final configDir = bashInstallation.completionConfigDir;
778+
779+
final bashRc = File(path.join(tempDir.path, '.bashrc'))
780+
..createSync();
781+
782+
bashInstallation
783+
.install('very_good');
784+
785+
// Different format needed for matching cli output
786+
expect(bashRc.readAsStringSync(), '''
787+
\n## [Completion]
788+
## Completion scripts setup. Remove the following line to uninstall
789+
[ -f ${configDir.path}/bash-config.bash ] && . ${configDir.path}/bash-config.bash || true
790+
## [/Completion]
791+
792+
''');
793+
794+
},
795+
);
796+
730797
});
731798

732799
group('uninstall', () {

test/src/installer/shell_completion_configuration_test.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ void main() {
1616
expect(zshConfiguration.shell, SystemShell.zsh);
1717
});
1818

19-
test('shellRCFile', () {
20-
expect(zshConfiguration.shellRCFile, '~/.zshrc');
19+
test('shellRCFiles', () {
20+
expect(zshConfiguration.shellRCFiles.first, '~/.zshrc');
2121
});
2222

2323
test('sourceStringTemplate', () {
@@ -68,8 +68,12 @@ fi
6868
expect(bashConfiguration.shell, SystemShell.bash);
6969
});
7070

71-
test('shellRCFile', () {
72-
expect(bashConfiguration.shellRCFile, '~/.bash_profile');
71+
test('shellRCFiles first', () {
72+
expect(bashConfiguration.shellRCFiles.first, '~/.bashrc');
73+
});
74+
75+
test('shellRCFiles last', () {
76+
expect(bashConfiguration.shellRCFiles.last, '~/.bash_profile');
7377
});
7478

7579
test('sourceStringTemplate', () {

0 commit comments

Comments
 (0)