App-Seacan

 view release on metacpan or  search on metacpan

lib/App/Seacan.pm  view on Meta::CPAN

package App::Seacan;

# Semantic Vesioning: http://semver.org/
# Not sure if I want to use v-string, but I do want to follow
# semvar as a convention.
our $VERSION = "0.1.0";

use Mo qw<required coerce>;
use File::Path qw<make_path>;
use TOML qw<from_toml>;

sub join_path { join "/", @_ };

has config => (
    required => 1,
    coerce => sub {
        my $c = $_[0];
        if (!ref($c) && -f $c) {
            open(my $fh, "<:utf8", $c) or die $!;
            local $/ = undef;
            $c = from_toml( scalar <$fh> );
        }
        $c->{perl}{installed_as} //= "seacan";

        return $c;
    }
);

sub seacan_perlbrew_root {
    my $self = shift;
    return join_path($self->config->{seacan}{output}, "perlbrew");
}

sub seacan_perl {
    my $self = shift;
    return join_path( $self->seacan_perlbrew_root, "perls", $self->config->{perl}{installed_as}, "bin", "perl" );
}

sub perl_is_installed {
    my $self = shift;
    my $perlbrew_root_path = join_path($self->config->{seacan}{output}, "perlbrew");
    return 0 unless -d $perlbrew_root_path;
    my $perl_executable = join_path($perlbrew_root_path, "perls", $self->config->{perl}{installed_as}, "bin", "perl");
    if (my $r = -f $perl_executable) {
        say STDERR "perl is installed: $perl_executable";
        return 1;
    }
    return 0;
}

sub install_perl {
    my $self = shift;

    my $perlbrew_root_path = $self->seacan_perlbrew_root;
    make_path( $perlbrew_root_path ) unless -d $perlbrew_root_path;

    for (keys %ENV) {
        delete $ENV{$_} if /\APERLBREW_/;
    }
    delete $ENV{PERL_CPANM_OPT};
    delete $ENV{PERL_LOCAL_LIB_ROOT};
    delete $ENV{PERL_MB_OPT};
    delete $ENV{PERL_MM_OPT};
    delete $ENV{PERL5LIB};

    $ENV{PERLBREW_ROOT} = $perlbrew_root_path;

    system("curl -L https://install.perlbrew.pl | bash") == 0 or die $!;
    my $perlbrew_command = join_path($perlbrew_root_path, "bin", "perlbrew");
    system($perlbrew_command, "install", $self->config->{perl}{version}, "--as", $self->config->{perl}{installed_as}) == 0 or die $!;
    system($perlbrew_command, "install-cpanm", "--force");
}

sub install_cpan {
    my $self = shift;
    my $cpanm_command = join_path( $self->seacan_perlbrew_root, "bin", "cpanm");
    my $perl_command = $self->seacan_perl;

    $, = " ";
    system($perl_command, $cpanm_command, "--notest", "-L", join_path($self->config->{seacan}{output}, "local"), "--installdeps", $self->config->{seacan}{app} ) == 0 or die $!;
}

sub copy_app {
    my $self = shift;
    my $target_directory = join_path($self->config->{seacan}{output}, "app");
    my $source_directory = $self->config->{seacan}{app};

    make_path($target_directory);
    $source_directory =~ s{/+$}{};

    system("rsync", "-8vPa", $source_directory, $target_directory) == 0 or die;
}

sub create_launcher {
    # Instead of giving a very long command to the user
    # a launcher script is generated.
    # app_name and main_script could be added to the configuration
    # so we can add the info directly instead of "guessing" it
    # through a regex.

    my $self = shift;
    my $output = $self->config->{seacan}{output};

    # The launcher script goes into bin of the target directory
    my $target_directory = join_path($output, 'bin');



( run in 1.425 second using v1.01-cache-2.11-cpan-5c0b1e786e0 )