File Coverage

blib/lib/Alien/Plotly/Kaleido.pm
Criterion Covered Total %
statement 50 64 78.1
branch 6 16 37.5
condition 3 6 50.0
subroutine 11 12 91.6
pod 2 3 66.6
total 72 101 71.2


line stmt bran cond sub pod time code
1             package Alien::Plotly::Kaleido;
2              
3             # ABSTRACT: Finds or installs plotly kaleido
4              
5 1     1   342849 use 5.010;
  1         12  
6 1     1   5 use strict;
  1         3  
  1         20  
7 1     1   5 use warnings;
  1         2  
  1         56  
8              
9             our $VERSION = '0.002'; # VERSION
10              
11 1     1   7 use parent 'Alien::Base';
  1         2  
  1         5  
12              
13 1     1   5203 use File::Which qw(which);
  1         2  
  1         47  
14 1     1   946 use IPC::Run ();
  1         25978  
  1         24  
15 1     1   700 use JSON ();
  1         10365  
  1         25  
16 1     1   7 use Path::Tiny ();
  1         2  
  1         367  
17              
18             sub bin_dir {
19 3     3 1 9170 my ($class) = @_;
20 3 50       14 if ( $class->install_type eq 'system' ) {
21 0         0 return Alien::Base::bin_dir($class);
22             }
23             else {
24 3         91 return Path::Tiny->new( $class->dist_dir );
25             }
26             }
27              
28             sub version {
29 0     0 1 0 my ($self) = @_;
30 0 0       0 if ( $self->install_type eq 'system' ) {
31 0         0 state $version;
32 0 0       0 unless ($version) {
33 0         0 $version = $self->detect_kaleido_version;
34             }
35 0         0 return $version;
36             }
37             else {
38 0         0 return $self->SUPER::version;
39             }
40             }
41              
42             sub detect_kaleido_version {
43 1     1 0 304 my ($class) = @_;
44              
45 1         5 my $kaleido = which('kaleido');
46 1 50       159 if ($kaleido) {
47             my $decode_json_safe = sub {
48 5     5   32 my ($out) = @_;
49 5         15 my $data;
50 5         15 eval { $data = JSON::decode_json($out); };
  5         119  
51 5 100       34 $@ = '' if ($@);
52 5         40 return $data;
53 1         6 };
54              
55 1         5 my @cmd = ( $kaleido, 'plotly', '--disable-gpu', '--no-sandbox' );
56 1         2 eval {
57 1         303 require Chart::Plotly;
58 0         0 my $plotlyjs =
59             File::ShareDir::dist_file( 'Chart-Plotly',
60             'plotly.js/plotly.min.js' );
61 0 0       0 if ( -r $plotlyjs ) {
62 0         0 push @cmd, "--plotlyjs=$plotlyjs";
63             }
64             };
65 1         6 my $h;
66             my $data;
67 1         18 eval {
68 1         4 my ( $in, $out, $err );
69 1         11 $h = IPC::Run::start( \@cmd, \$in, \$out, \$err,
70             my $t = IPC::Run::timer(30) );
71 1   66     9156 while ( not $data and not $t->is_expired ) {
72 5         107 $h->pump;
73 5         1148818 $data = $decode_json_safe->($out);
74             }
75 1         22 $h->finish;
76             };
77 1 50       22444 if ($@) {
78 0         0 warn $@;
79 0         0 $h->kill_kill;
80             }
81 1 50 33     48 if ( $data and exists $data->{version} ) {
82 1         32 return $data->{version};
83             }
84             }
85              
86 0           die "Failed to detect kaleido version";
87             }
88              
89             1;
90              
91             __END__