App-Rakubrew
view release on metacpan or search on metacpan
lib/App/Rakubrew/Shell.pm view on Meta::CPAN
sub detect_shell {
if ($^O =~ /win32/i) {
# https://stackoverflow.com/a/8547234
my $psmodpath = env('PSMODULEPATH');
if ($psmodpath =~ /\\Documents\\(?:Windows)?PowerShell\\Modules(?:;|$)/) {
return 'PowerShell';
}
else {
return 'Cmd';
}
}
else {
my $shell = env('SHELL') || '/bin/bash';
$shell = (splitpath( $shell))[2];
$shell =~ s/[^a-z]+$//; # remove version numbers
# tcsh claims it's csh on FreeBSD. Try to detect that.
if ($shell eq 'csh' && env('tcsh')) {
$shell = 'tcsh';
}
$shell = ucfirst $shell;
if (!shell_exists('Dummy self', $shell)) {
$shell = 'Sh';
}
return $shell;
}
}
sub get {
my $self = shift;
return $shell_hook;
}
sub available_shells {
<Bash Zsh Fish Tcsh Sh PowerShell Cmd>
}
sub shell_exists {
my $self = shift;
my $shell = shift;
eval "require App::Rakubrew::Shell::$shell";
return $@ ? 0 : 1;
}
sub print_shellmod_code {
my $self = shift;
my @params = @_;
my $command = shift(@params) // '';
my $mode = get_brew_mode(1);
my $version;
my $sep = $^O =~ /win32/i ? ';' : ':';
if ($command eq 'shell' && @params) {
$version = $params[0];
if ($params[0] eq '--unset') {
say $self->get_shell_unsetter_code();
}
elsif (! is_version_broken($params[0])) {
say $self->get_shell_setter_code($params[0]);
}
}
elsif ($command eq 'mode' && $mode eq 'shim') { # just switched to shim mode
my $path = env('PATH');
$path = $self->clean_path($path);
$path = $shim_dir . $sep . $path;
say $self->get_path_setter_code($path);
}
elsif ($mode eq 'env') {
$version = get_version();
}
if ($mode eq 'env') {
my $path = env('PATH');
$path = $self->clean_path($path);
if ($version ne 'system') {
if ($version eq '--unset') {
# Get version ignoring the still set shell version.
$version = get_version('shell');
}
return if is_version_broken($version);
$path = join($sep, get_bin_paths($version), $path);
}
# In env mode several commands require changing PATH, so we just always
# construct a new PATH and see if it's different.
if ($path ne env('PATH')) {
say $self->get_path_setter_code($path);
}
}
}
sub clean_path {
my $self = shift;
my $path = shift;
my $also_clean_path = shift;
my $sep = $^O =~ /win32/i ? ';' : ':';
my @paths;
for my $version (get_versions()) {
next if $version eq 'system';
next if is_version_broken($version);
try {
push @paths, get_bin_paths($version);
}
catch {
# Version is broken. So it's likely not in path anyways.
# -> ignore it
};
}
push @paths, $versions_dir;
push @paths, $shim_dir;
push @paths, $also_clean_path if $also_clean_path;
@paths = map { "\Q$_\E" } @paths;
my $paths_regex = join "|", @paths;
my $old_path;
do {
$old_path = $path;
$path =~ s/^($paths_regex)[^$sep]*$//g;
$path =~ s/^($paths_regex)[^$sep]*$sep//g;
$path =~ s/$sep($paths_regex)[^$sep]*$//g;
$path =~ s/$sep($paths_regex)[^$sep]*$sep/$sep/g;
} until $path eq $old_path;
return $path;
}
# Strips out all elements in arguments array up to and including $bre_name
# command. The first argument is index where the completion should look for the
# word to be completed.
sub strip_executable {
my $self = shift;
my $index = shift;
my $cmd_pos = 0;
foreach my $word (@_) {
++$cmd_pos;
--$index;
last if $word =~ /(^|\W)$brew_name$/;
}
return ($index, @_[$cmd_pos..$#_])
}
=pod
Returns a list of completion candidates.
This function takes two parameters:
( run in 0.685 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )