App-GhaInstall
view release on metacpan or search on metacpan
lib/App/GhaInstall.pm view on Meta::CPAN
install_module( 'CPAN' ) if INSTALLER eq 'cpan';
}
elsif ( /--configure/ ) {
install_configure_dependencies();
}
elsif ( /--auto/ ) {
install_dependencies();
}
else {
push @modules, $_;
}
}
if ( @modules ) {
install_modules( @modules );
}
return 0;
}
sub slurp {
my $file = shift;
open my $fh, '<', $file
or die "$file exists but cannot be read\n";
local $/;
return <$fh>;
}
sub read_json {
my $file = shift;
return unless -f $file;
my $hash;
if ( eval { require JSON::MaybeXS; 1 } ) {
my $json = 'JSON::MaybeXS'->new;
$hash = $json->decode( slurp($file) );
}
elsif ( eval { require JSON::PP; 1 } ) {
my $json = 'JSON::PP'->new;
$hash = $json->decode( slurp($file) );
}
else {
# Bundled version of JSON::Tiny
require App::GhaInstall::JSON;
$hash = JSON::Tiny::decode_json( slurp($file) );
}
return unless ref($hash) eq 'HASH';
$hash->{metatype} = 'JSON';
return $hash;
}
sub read_yaml {
my $file = shift;
return unless -f $file;
my $hash;
if ( eval { require JSON::XS; 1 } ) {
$hash = YAML::XS::Load( slurp($file) );
}
else {
# Bundled version of YAML::Tiny
require App::GhaInstall::YAML;
$hash = YAML::Tiny::Load( slurp($file) );
}
return unless ref($hash) eq 'HASH';
$hash->{metatype} = 'YAML';
return $hash;
}
sub install_configure_dependencies {
my $meta =
read_json('META.json') || read_yaml('META.yml')
or die("Cannot read META.json or META.yml");
my ( @need, @want );
if ( $meta->{metatype} eq 'JSON' ) {
for my $phase ( qw( configure build ) ) {
push @need, keys %{ $meta->{prereqs}{$phase}{requires} or {} };
push @want, keys %{ $meta->{prereqs}{$phase}{recommends} or {} };
push @want, keys %{ $meta->{prereqs}{$phase}{suggests} or {} };
}
}
else {
push @need, keys %{ $meta->{configure_requires} or {} };
push @need, keys %{ $meta->{build_requires} or {} };
}
if ( @need ) {
install_modules( @need );
}
if ( @want and SHOULD_INSTALL_OPTIONAL_DEPS ) {
local $ALLOW_FAIL = 1;
install_modules( @want );
}
return;
}
sub install_dependencies {
my $meta =
read_json('MYMETA.json') || read_yaml('MYMETA.yml') || read_json('META.json') || read_yaml('META.yml')
or die("Cannot read MYMETA.json or MYMETA.yml");
my ( @need, @want );
if ( $meta->{metatype} eq 'JSON' ) {
for my $phase ( qw( configure build runtime test ) ) {
push @need, keys %{ $meta->{prereqs}{$phase}{requires} or {} };
push @want, keys %{ $meta->{prereqs}{$phase}{recommends} or {} };
push @want, keys %{ $meta->{prereqs}{$phase}{suggests} or {} };
}
}
else {
( run in 1.799 second using v1.01-cache-2.11-cpan-39bf76dae61 )