Skip to content

Commit f8a79b0

Browse files
authored
refactor: used enhanced enum for system shell name (#67)
1 parent 44b7927 commit f8a79b0

File tree

7 files changed

+41
-20
lines changed

7 files changed

+41
-20
lines changed

lib/src/installer/completion_installation.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class CompletionInstallation {
113113

114114
logger.detail(
115115
'Installing completion for the command $rootCommand '
116-
'on ${configuration.name}',
116+
'on ${configuration.shell.name}',
117117
);
118118

119119
createCompletionConfigDir();
@@ -164,7 +164,7 @@ class CompletionInstallation {
164164
bool writeCompletionScriptForCommand(String rootCommand) {
165165
final configuration = this.configuration!;
166166
final completionConfigDirPath = completionConfigDir.path;
167-
final commandScriptName = '$rootCommand.${configuration.name}';
167+
final commandScriptName = '$rootCommand.${configuration.shell.name}';
168168
final commandScriptPath = path.join(
169169
completionConfigDirPath,
170170
commandScriptName,
@@ -207,7 +207,7 @@ class CompletionInstallation {
207207
logger.info('No file found at $configPath, creating one now');
208208
configFile.createSync();
209209
}
210-
final commandScriptName = '$rootCommand.${configuration.name}';
210+
final commandScriptName = '$rootCommand.${configuration.shell.name}';
211211

212212
final containsLine =
213213
configFile.readAsStringSync().contains(commandScriptName);

lib/src/installer/shell_completion_configuration.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ typedef SourceStringTemplate = String Function(String scriptPath);
2020
class ShellCompletionConfiguration {
2121
/// {@macro shell_completion_configuration}
2222
const ShellCompletionConfiguration._({
23-
required this.name,
23+
required this.shell,
2424
required this.shellRCFile,
2525
required this.sourceLineTemplate,
2626
required this.scriptTemplate,
@@ -38,8 +38,8 @@ class ShellCompletionConfiguration {
3838
}
3939
}
4040

41-
/// A descriptive string to identify the shell among others.
42-
final String name;
41+
/// {@macro system_shell}
42+
final SystemShell shell;
4343

4444
/// The location of a config file that is run upon shell start.
4545
/// Eg: .bash_profile or .zshrc
@@ -52,13 +52,14 @@ class ShellCompletionConfiguration {
5252
final CompletionScriptTemplate scriptTemplate;
5353

5454
/// The name for the config file for this shell.
55-
String get completionConfigForShellFileName => '$name-config.$name';
55+
String get completionConfigForShellFileName =>
56+
'${shell.name}-config.${shell.name}';
5657
}
5758

5859
/// A [ShellCompletionConfiguration] for zsh.
5960
@visibleForTesting
6061
final zshConfiguration = ShellCompletionConfiguration._(
61-
name: 'zsh',
62+
shell: SystemShell.zsh,
6263
shellRCFile: '~/.zshrc',
6364
sourceLineTemplate: (String scriptPath) {
6465
return '[[ -f $scriptPath ]] && . $scriptPath || true';
@@ -91,7 +92,7 @@ fi
9192
/// A [ShellCompletionConfiguration] for bash.
9293
@visibleForTesting
9394
final bashConfiguration = ShellCompletionConfiguration._(
94-
name: 'bash',
95+
shell: SystemShell.bash,
9596
shellRCFile: '~/.bash_profile',
9697
sourceLineTemplate: (String scriptPath) {
9798
return '[ -f $scriptPath ] && . $scriptPath || true';

lib/src/system_shell.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@ import 'dart:io';
22

33
import 'package:path/path.dart' as path;
44

5-
/// The supported shells.
5+
/// {@template system_shell}
6+
/// A type definition for a shell.
7+
///
8+
/// The enumerated shells are the supported shells.
9+
/// {@endtemplate}
610
enum SystemShell {
711
/// The Zsh shell: https://www.zsh.org/
8-
zsh,
12+
zsh('zsh'),
913

1014
/// GNU Bash shell: https://www.gnu.org/software/bash/
11-
bash;
15+
bash('bash');
16+
17+
/// {@macro system_shell}
18+
const SystemShell(this.name);
19+
20+
/// A descriptive string to identify the shell among others.
21+
final String name;
1222

1323
/// Identifies the current shell based on the [Platform.environment].
1424
///

test/src/command_runner/completion_command_runner_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ void main() {
9393
expect(
9494
commandRunner.completionInstallation,
9595
isA<CompletionInstallation>().having(
96-
(e) => e.configuration?.name,
96+
(e) => e.configuration?.shell,
9797
'chosen shell',
98-
equals('zsh'),
98+
equals(SystemShell.zsh),
9999
),
100100
);
101101
});

test/src/installer/completion_installation_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ void main() {
2525
systemShell: SystemShell.bash,
2626
logger: logger,
2727
);
28-
expect(installation.configuration?.name, 'bash');
28+
expect(installation.configuration?.shell, SystemShell.bash);
2929
});
3030

3131
test('zsh', () {
3232
final installation = CompletionInstallation.fromSystemShell(
3333
systemShell: SystemShell.zsh,
3434
logger: logger,
3535
);
36-
expect(installation.configuration?.name, 'zsh');
36+
expect(installation.configuration?.shell, SystemShell.zsh);
3737
});
3838

3939
test('proxies overrides', () {

test/src/installer/shell_completion_configuration_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ void main() {
1111
ShellCompletionConfiguration.fromSystemShell(SystemShell.zsh);
1212
});
1313

14-
test('name', () {
15-
expect(zshConfiguration.name, 'zsh');
14+
test('shell', () {
15+
expect(zshConfiguration.shell, SystemShell.zsh);
1616
});
1717

1818
test('shellRCFile', () {
@@ -62,8 +62,8 @@ fi
6262
ShellCompletionConfiguration.fromSystemShell(SystemShell.bash);
6363
});
6464

65-
test('name', () {
66-
expect(bashConfiguration.name, 'bash');
65+
test('shell', () {
66+
expect(bashConfiguration.shell, SystemShell.bash);
6767
});
6868

6969
test('shellRCFile', () {

test/src/system_shell_test.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import 'package:test/test.dart';
33

44
void main() {
55
group('SystemShell', () {
6+
group('name', () {
7+
test('bash is correct', () {
8+
expect(SystemShell.bash.name, equals('bash'));
9+
});
10+
11+
test('zsh is correct', () {
12+
expect(SystemShell.zsh.name, equals('zsh'));
13+
});
14+
});
15+
616
group('current', () {
717
test('instantiated without env', () {
818
expect(

0 commit comments

Comments
 (0)