-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,6 @@ | |
.dart_tool/ | ||
.packages | ||
build/ | ||
pubspec.lock | ||
pubspec.lock | ||
|
||
coverage/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/// Contains the functions related to the installation file | ||
library install; | ||
|
||
export 'src/install/install_completion.dart'; | ||
export 'src/install/shell_completion_installation.dart'; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/// {@template completion_installation_exception} | ||
/// Describes an exception during the installation of completion scripts. | ||
/// {@endtemplate} | ||
class CompletionInstallationException implements Exception { | ||
/// {@macro completion_installation_exception} | ||
CompletionInstallationException({ | ||
required this.message, | ||
required this.rootCommand, | ||
}); | ||
|
||
/// The error message for this exception | ||
final String message; | ||
|
||
/// The command for which the installation failed. | ||
final String rootCommand; | ||
|
||
@override | ||
String toString() => 'Could not install completion scripts for $rootCommand: ' | ||
'$message'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import 'package:cli_completion/src/exceptions.dart'; | ||
import 'package:cli_completion/src/install/shell_completion_installation.dart'; | ||
|
||
import 'package:mason_logger/mason_logger.dart'; | ||
|
||
/// Install completion configuration hooks for a [rootCommand] in the | ||
/// current shell. | ||
void installCompletion({ | ||
required Logger logger, | ||
required String rootCommand, | ||
bool? isWindowsOverride, | ||
Map<String, String>? environmentOverride, | ||
}) { | ||
logger | ||
..detail('Completion installation for $rootCommand started') | ||
..detail('Identifying system shell'); | ||
|
||
final completionInstallation = ShellCompletionInstallation.fromCurrentShell( | ||
logger: logger, | ||
isWindowsOverride: isWindowsOverride, | ||
environmentOverride: environmentOverride, | ||
); | ||
|
||
if (completionInstallation == null) { | ||
throw CompletionInstallationException( | ||
message: 'Unknown shell.', | ||
rootCommand: rootCommand, | ||
); | ||
} | ||
|
||
logger.detail( | ||
'Shell identified as ${completionInstallation.configuration.name}', | ||
); | ||
|
||
completionInstallation.install(rootCommand); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
/// A type definition for functions that creates the content of a | ||
/// completion script given a [rootCommand] | ||
typedef CompletionScriptTemplate = String Function(String rootCommand); | ||
|
||
/// A type definition for functions that describes | ||
/// the source line given a [scriptPath] | ||
typedef SourceStringTemplate = String Function(String scriptPath); | ||
|
||
/// {@template shell_completion_configuration} | ||
/// Describes the configuration of a completion script in a specific shell. | ||
/// | ||
/// See: | ||
/// - [zshConfiguration] for zsh | ||
@immutable | ||
class ShellCompletionConfiguration { | ||
/// {@macro shell_completion_configuration} | ||
@visibleForTesting | ||
const ShellCompletionConfiguration({ | ||
required this.name, | ||
required this.shellRCFile, | ||
required this.sourceLineTemplate, | ||
required this.scriptTemplate, | ||
}); | ||
|
||
/// A descriptive string to identify the shell among others. | ||
final String name; | ||
|
||
/// The location of a config file that is run upon shell start. | ||
/// Eg: .bashrc or .zshrc | ||
final String shellRCFile; | ||
|
||
/// Generates a line to sources of a script file. | ||
final SourceStringTemplate sourceLineTemplate; | ||
|
||
/// Generates the contents of a completion script. | ||
final CompletionScriptTemplate scriptTemplate; | ||
|
||
/// The name for the config file for this shell. | ||
String get completionConfigForShellFileName => '$name-config.$name'; | ||
} | ||
|
||
/// A [ShellCompletionConfiguration] for zsh. | ||
final zshConfiguration = ShellCompletionConfiguration( | ||
name: 'zsh', | ||
shellRCFile: '~/.zshrc', | ||
sourceLineTemplate: (String scriptPath) { | ||
return '[[ -f $scriptPath ]] && . $scriptPath || true'; | ||
}, | ||
scriptTemplate: (String rootCommand) { | ||
// Completion script for zsh. | ||
// | ||
// Based on https://github.com/mklabs/tabtab/blob/master/lib/scripts/zsh.sh | ||
return ''' | ||
if type compdef &>/dev/null; then | ||
_${rootCommand}_completion () { | ||
local reply | ||
local si=\$IFS | ||
IFS=\$'\n' reply=(\$(COMP_CWORD="\$((CURRENT-1))" COMP_LINE="\$BUFFER" COMP_POINT="\$CURSOR" $rootCommand completion -- "\${words[@]}")) | ||
IFS=\$si | ||
_describe 'values' reply | ||
} | ||
compdef _${rootCommand}_completion $rootCommand | ||
fi | ||
'''; | ||
}, | ||
); |