Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
refactor: defined runCommandFile for SystemShell
  • Loading branch information
alestiago committed May 10, 2023
commit c4997ab98957cf06546d0dfb1e65e7a82ea6e2b9
36 changes: 13 additions & 23 deletions lib/src/installer/completion_installation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,16 @@ class CompletionInstallation {
);
}

String get _shellRCFilePath =>
_resolveHome(configuration!.shellRCFile, environment);

/// Write a source to the completion global script in the shell configuration
/// file, which its location is described by the [configuration].
@visibleForTesting
void writeToShellConfigFile(String rootCommand) {
final configuration = this.configuration!;
final shellRunCommandFile = configuration.shell.runCommandFile();

logger.info(
'Adding dart cli completion config entry '
'to $_shellRCFilePath',
'to ${shellRunCommandFile.path}',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What good is having run command doing here? Most people know those files as RC files. Unnecessary change.

);

final completionConfigDirPath = completionConfigDir.path;
Expand All @@ -247,26 +245,25 @@ class CompletionInstallation {
configuration.completionConfigForShellFileName,
);

final shellRCFile = File(_shellRCFilePath);

if (!shellRCFile.existsSync()) {
if (!shellRunCommandFile.existsSync()) {
throw CompletionInstallationException(
rootCommand: rootCommand,
message: 'No configuration file found at ${shellRCFile.path}',
message: 'No configuration file found at ${shellRunCommandFile.path}',
);
}

final containsLine =
shellRCFile.readAsStringSync().contains(completionConfigPath);
shellRunCommandFile.readAsStringSync().contains(completionConfigPath);

if (containsLine) {
logger.warn('A completion config entry was already found on'
' $_shellRCFilePath.');
logger.warn(
'''A completion config entry was already found on ${shellRunCommandFile.path}.''',
);
return;
}

_sourceScriptOnFile(
configFile: shellRCFile,
configFile: shellRunCommandFile,
scriptName: 'Completion',
description: 'Completion scripts setup. '
'Remove the following line to uninstall',
Expand All @@ -280,13 +277,16 @@ class CompletionInstallation {
/// Tells the user to source the shell configuration file.
void _logSourceInstructions(String rootCommand) {
final level = logger.level;
final configuration = this.configuration!;
final shellRunCommandFile = configuration.shell.runCommandFile();

logger
..level = Level.info
..info(
'\n'
'Completion files installed. To enable completion, run the following '
'command in your shell:\n'
'${lightCyan.wrap('source $_shellRCFilePath')}'
'${lightCyan.wrap('source ${shellRunCommandFile.path}}')}'
'\n',
)
..level = level;
Expand Down Expand Up @@ -321,13 +321,3 @@ ${configuration!.sourceLineTemplate(scriptPath)}
logger.info('Added config to $configFilePath');
}
}

/// Resolve the home from a path string
String _resolveHome(
String originalPath,
Map<String, String> environment,
) {
final after = originalPath.split('~/').last;
final home = path.absolute(environment['HOME']!);
return path.join(home, after);
}
7 changes: 0 additions & 7 deletions lib/src/installer/shell_completion_configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class ShellCompletionConfiguration {
/// {@macro shell_completion_configuration}
const ShellCompletionConfiguration._({
required this.shell,
required this.shellRCFile,
required this.sourceLineTemplate,
required this.scriptTemplate,
});
Expand All @@ -41,10 +40,6 @@ class ShellCompletionConfiguration {
/// {@macro system_shell}
final SystemShell shell;

/// The location of a config file that is run upon shell start.
/// Eg: .bash_profile or .zshrc
final String shellRCFile;

/// Generates a line to sources of a script file.
final SourceStringTemplate sourceLineTemplate;

Expand All @@ -60,7 +55,6 @@ class ShellCompletionConfiguration {
@visibleForTesting
final zshConfiguration = ShellCompletionConfiguration._(
shell: SystemShell.zsh,
shellRCFile: '~/.zshrc',
sourceLineTemplate: (String scriptPath) {
return '[[ -f $scriptPath ]] && . $scriptPath || true';
},
Expand Down Expand Up @@ -93,7 +87,6 @@ fi
@visibleForTesting
final bashConfiguration = ShellCompletionConfiguration._(
shell: SystemShell.bash,
shellRCFile: '~/.bash_profile',
sourceLineTemplate: (String scriptPath) {
return '[ -f $scriptPath ] && . $scriptPath || true';
},
Expand Down
30 changes: 27 additions & 3 deletions lib/src/system_shell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,41 @@ import 'package:path/path.dart' as path;
/// {@endtemplate}
enum SystemShell {
/// The Zsh shell: https://www.zsh.org/
zsh('zsh'),
zsh(
'zsh',
runCommandFileName: '.zshrc',
),

/// GNU Bash shell: https://www.gnu.org/software/bash/
bash('bash');
bash(
'bash',
runCommandFileName: '.bash_profile',
);

/// {@macro system_shell}
const SystemShell(this.name);
const SystemShell(this.name, {required String runCommandFileName})
: _runCommandFileName = runCommandFileName;

/// A descriptive string to identify the shell among others.
final String name;

/// The name of a config file that is run upon shell start.
///
/// For example: `.bash_profile` or `.zshrc`.
final String _runCommandFileName;

/// Retrieves, from the current environment, the run command (RC) [File]
/// for the shell.
///
/// It is assumed that the RC file is located at the user's home directory.
File runCommandFile({Map<String, String>? environmentOverride}) {
final environment = environmentOverride ?? Platform.environment;

final home = path.absolute(environment['HOME']!);
final runCommandFilePath = path.join(home, _runCommandFileName);
return File(runCommandFilePath);
}

/// Identifies the current shell based on the [Platform.environment].
///
/// Pass [environmentOverride] to override the default value of
Expand Down
8 changes: 0 additions & 8 deletions test/src/installer/shell_completion_configuration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ void main() {
expect(zshConfiguration.shell, SystemShell.zsh);
});

test('shellRCFile', () {
expect(zshConfiguration.shellRCFile, '~/.zshrc');
});

test('sourceStringTemplate', () {
final result = zshConfiguration.sourceLineTemplate('./pans/snaps');
expect(result, '[[ -f ./pans/snaps ]] && . ./pans/snaps || true');
Expand Down Expand Up @@ -66,10 +62,6 @@ fi
expect(bashConfiguration.shell, SystemShell.bash);
});

test('shellRCFile', () {
expect(bashConfiguration.shellRCFile, '~/.bash_profile');
});

test('sourceStringTemplate', () {
final result = bashConfiguration.sourceLineTemplate('./pans/snaps');
expect(result, '[ -f ./pans/snaps ] && . ./pans/snaps || true');
Expand Down
21 changes: 21 additions & 0 deletions test/src/system_shell_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:cli_completion/parser.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';

void main() {
Expand All @@ -13,6 +14,26 @@ void main() {
});
});

group('runCommandFile', () {
const environment = {'HOME': '/foo/bar'};

test('bash path is correct', () {
const shell = SystemShell.bash;
final file = shell.runCommandFile(environmentOverride: environment);

final expectedPath = path.join(environment['HOME']!, '.bash_profile');
expect(file.path, equals(expectedPath));
});

test('zsh path is correct', () {
const shell = SystemShell.zsh;
final file = shell.runCommandFile(environmentOverride: environment);

final expectedPath = path.join(environment['HOME']!, '.zshrc');
expect(file.path, equals(expectedPath));
});
});

group('current', () {
test('instantiated without env', () {
expect(
Expand Down