blib/lib/Pandoc/Release.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 42 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 206 SCAR 60.4
CC
20
Cov
0%
download
110.0
list
42.0
_api_request
12.0
CRAP 420.0
1 package Pandoc::Release;
2 use 5.014;
3 use warnings;
4  
5 our $VERSION = '0.9.1';
6  
7 use HTTP::Tiny;
8 use JSON::PP;
9  
10 use Cwd;
11 use File::Path qw(make_path remove_tree);
12 use File::Copy 'move';
13 use File::Temp 'tempdir';
14  
15 use Pandoc;
16 use Pandoc::Version;
17  
18 use parent 'Exporter';
19  
20 our @EXPORT = qw(get list latest);
21  
22 =head1 NAME
23  
24 Pandoc::Release - get pandoc releases from GitHub
25  
26 =cut
27  
28 our $CLIENT = HTTP::Tiny->new;
29  
30 sub _api_request {
31 my ( $url, %opts ) = @_;
32  
33 say $url if $opts{verbose};
34  
35 my %headers =
36 $opts{token} ? ( Authorization => "token $opts{token}" ) : ();
37 my $res = $CLIENT->get( $url, { headers => \%headers } );
38  
39 $res->{success} or die "failed to fetch $url";
40 $res->{content} = JSON::PP::decode_json( $res->{content} );
41  
42 $res;
43 }
44  
45 sub get {
46 shift if __PACKAGE__ eq ( $_[0] // '' ); # can also be used as method
47  
48 my ( $version, %opts ) = @_;
49 warn "Pandoc release 1.17 had a bug, please don't use!\n"
50 if "$version" eq "1.17";
51 my $url = "https://api.github.com/repos/jgm/pandoc/releases/tags/$version";
52 bless _api_request( $url, %opts )->{content}, __PACKAGE__;
53 }
54  
55 sub list {
56 shift if __PACKAGE__ eq ( $_[0] // '' ); # can also be used as method
57  
58 my %opts = @_;
59  
60 my $range = $opts{range};
61 my $limit = $opts{limit} // -1;
62 my $since = Pandoc::Version->new( $opts{since} // 0 );
63 my $url = "https://api.github.com/repos/jgm/pandoc/releases";
64 my @releases;
65  
66 LOOP: while ($url) {
67 my $res = _api_request( $url, %opts );
68 foreach ( @{ $res->{content} } ) {
69 my $version = Pandoc::Version->new( $_->{tag_name} );
70 last LOOP unless $since < $version; # abort if possible
71 next if $version == '1.17'; # version had a bug
72 if ( !$range || $version->fulfills($range) ) {
73 push @releases, bless $_, __PACKAGE__;
74 last LOOP if --$limit == 0;
75 }
76 }
77  
78 my $link = $res->{headers}{link} // '';
79 $link =~ /<([^>]+)>; rel="next"/ or last;
80 $url = $1;
81 }
82  
83 @releases;
84 }
85  
86 sub latest {
87 shift if __PACKAGE__ eq ( $_[0] // '' ); # can also be used as method
88  
89 ( list( @_, limit => 1 ) )[0];
90 }
91  
92 sub download {
93 my ( $self, %opts ) = @_;
94  
95 my $version = Pandoc::Version->new( $self->{tag_name} );
96 my $bin = $opts{bin} // pandoc_data_dir('bin');
97  
98 if ($bin) {
99 make_path($bin);
100 -d $bin or die "missing directory $bin";
101 $bin = "$bin/pandoc-$version";
102 if ( -f $bin ) {
103 say "skipping existing $bin" if $opts{verbose};
104 my $pandoc = Pandoc->new($bin);
105 $pandoc = $pandoc->symlink( $opts{symlink}, %opts )
106 if exists $opts{symlink};
107 return $pandoc;
108 }
109 }
110  
111 my $arch = $opts{arch} // `dpkg --print-architecture`;
112 chomp $arch;
113 my $dir = $opts{dir} // tempdir( CLEANUP => 1 );
114  
115 my ($asset) = grep { $_->{name} =~ /-$arch\.deb$/ } @{ $self->{assets} };
116  
117 my $url = ( $asset // {} )->{browser_download_url} or do {
118 say "release $self->{tag_name} contains no $arch Debian package"
119 if $opts{verbose};
120 return;
121 };
122  
123 make_path($dir);
124 -d $dir or die "missing directory $dir";
125  
126 my $deb = "$dir/" . $asset->{name};
127 say $deb if $CLIENT->mirror( $url, $deb )->{success} and $opts{verbose};
128  
129 if ($bin) {
130 my $cmd =
131 "dpkg --fsys-tarfile '$deb'"
132 . "| tar -x ./usr/bin/pandoc -O > '$bin'"
133 . "&& chmod +x '$bin'";
134 system($cmd) and die "failed to extract pandoc from $deb:\n $cmd";
135 say $bin if $opts{verbose};
136  
137 my $pandoc = Pandoc->new($bin);
138 $pandoc = $pandoc->symlink( $opts{symlink}, %opts )
139 if exists $opts{symlink};
140 return $pandoc;
141 }
142 else {
143 return $version;
144 }
145 }
146  
147 1;
148  
149 __END__