Alien-Plotly-Kaleido
view release on metacpan or search on metacpan
lib/Alien/Plotly/Kaleido.pm view on Meta::CPAN
package Alien::Plotly::Kaleido;
# ABSTRACT: Finds or installs plotly kaleido
use 5.010;
use strict;
use warnings;
our $VERSION = '0.003'; # VERSION
use parent 'Alien::Base';
use File::Which qw(which);
use IPC::Run ();
use JSON ();
use Path::Tiny ();
sub bin_dir {
my ($class) = @_;
if ( $class->install_type eq 'system' ) {
return Alien::Base::bin_dir($class);
}
else {
return Path::Tiny->new( $class->dist_dir );
}
}
sub version {
my ($self) = @_;
if ( $self->install_type eq 'system' ) {
state $version;
unless ($version) {
$version = $self->detect_kaleido_version;
}
return $version;
}
else {
return $self->SUPER::version;
}
}
sub detect_kaleido_version {
my ($class) = @_;
my $kaleido = which('kaleido');
if ($kaleido) {
my $decode_json_safe = sub {
my ($out) = @_;
my $data;
eval { $data = JSON::decode_json($out); };
$@ = '' if ($@);
return $data;
};
my @cmd = ( $kaleido, 'plotly', '--disable-gpu', '--no-sandbox' );
eval {
require Chart::Plotly;
my $plotlyjs =
File::ShareDir::dist_file( 'Chart-Plotly',
'plotly.js/plotly.min.js' );
if ( -r $plotlyjs ) {
push @cmd, "--plotlyjs=$plotlyjs";
}
};
my $h;
my $data;
eval {
my ( $in, $out, $err );
$h = IPC::Run::start( \@cmd, \$in, \$out, \$err,
my $t = IPC::Run::timer(30) );
while ( not $data and not $t->is_expired ) {
$h->pump;
$data = $decode_json_safe->($out);
}
$h->finish;
};
if ($@) {
warn $@;
$h->kill_kill;
}
if ( $data and exists $data->{version} ) {
return $data->{version};
}
}
die "Failed to detect kaleido version";
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
( run in 0.719 second using v1.01-cache-2.11-cpan-39bf76dae61 )