App-Rakubrew

 view release on metacpan or  search on metacpan

lib/App/Rakubrew/Shell.pm  view on Meta::CPAN

package App::Rakubrew::Shell;
use strict;
use warnings;
use 5.010;
use Encode::Locale qw(env);
use File::Spec::Functions qw(catdir catfile updir splitpath);
use Try::Tiny;
use App::Rakubrew::Tools;
use App::Rakubrew::Variables;
use App::Rakubrew::VersionHandling;

# Turn on substring-based command line completion where possible contrary to the
# "start of the line completion". I.e., to visualize the difference, 'ver'
# string would result in the following command candidates:
#   SUBSTRING_COMPLETION==1 -> version versions rakubrew-version
#   SUBSTRING_COMPLETION==0 -> version versions
use constant SUBSTRING_COMPLETION => 1;

my $shell_hook;

sub initialize {
    my $class = shift;
    my $shell = shift;

    if (!shell_exists('Dummy self', $shell) || $shell eq 'auto') {
        $shell = detect_shell();
    }

    eval "require App::Rakubrew::Shell::$shell";
    if ($@) {
        die "Loading shell hook failed: " . $@;
    }
    $shell_hook = bless {}, "App::Rakubrew::Shell::$shell";
    return $shell_hook;
}

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;
    }
}



( run in 0.657 second using v1.01-cache-2.11-cpan-39bf76dae61 )