blib/lib/App/Mver.pm untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 187 CC 14 SCAR 36.7
CC
14
Cov
0%
mver
90.0
run
12.0
get_changes_between
12.0
CRAP 39.4
1 package App::Mver;
2  
3 use strict;
4 use version;
5 use warnings;
6  
7 use ExtUtils::MakeMaker;
8  
9 our $VERSION = '0.09';
10  
11 my $module_corelist = eval 'require Module::CoreList; 1';
12 my $lwp_useragent = eval 'require LWP::Simple; 1';
13 my $json_any = eval 'use JSON::Any; 1';
14 my $changes_parser = eval 'require CPAN::Changes; 1';
15 my $can_do_requests = $lwp_useragent && $json_any;
16 my $can_parse_changes = $can_do_requests && $changes_parser;
17  
18 my $api_host = 'http://api.metacpan.org';
19 my $module_search = "$api_host/module/%s";
20 my $changelog_search = "$api_host/v0/file/_search?q=release:%s-%s AND (name:Changes OR name:ChangeLog OR name:CHANGES OR name:CHANGELOG OR name:Changelog)&fields=path";
21 my $source_search = "$api_host/source/%s/%s/%s";
22  
23 sub run {
24 my($modules, $opts) = @_;
25  
26 $can_do_requests = 0 if $opts->{'no-internet'};
27 $can_parse_changes = 0 unless $opts->{changes};
28  
29 mver($_) for @$modules;
30 }
31  
32 sub mver {
33 my $arg = shift;
34 $arg =~ s{-}{::}g;
35  
36 print "$arg: ";
37 if(lc $arg eq 'perl') {
38 require Config;
39 print $Config::Config{version};
40 }
41 else {
42 my $file = MM->_installed_file_for_module($arg);
43 if(defined $file) {
44 my $version = eval { version->parse(MM->parse_version($file)) };
45 if($version) {
46 print $version;
47  
48 if($module_corelist and is_core($arg)) {
49 print ' (core module)';
50 }
51 }
52 else {
53 print 'installed, but $VERSION is not defined';
54 }
55  
56 if($can_do_requests and $version) {
57 my($latest, $author) = get_latest_version_and_author($arg);
58 if($latest and $latest <= $version) {
59 print ' (latest)';
60 }
61 else {
62 print " (latest: $latest)";
63  
64 if($can_parse_changes and $latest and $author) {
65 my $changes = get_changes_between($arg, $author, $version, $latest);
66 if($changes) {
67 print "$/Changes:$/$changes";
68 }
69 }
70 }
71 }
72 }
73 else {
74 print 'not installed';
75 }
76 }
77 print $/;
78 }
79  
80 sub is_core {
81 my $arg = shift;
82  
83 my($found_in_core) = Module::CoreList->find_modules(qr/^\Q$arg\E$/, $]);
84  
85 !!$found_in_core;
86 }
87  
88 sub get_latest_version_and_author {
89 my $arg = shift;
90  
91 my $json = LWP::Simple::get(sprintf $module_search, $arg) or return;
92 my $response = eval { JSON::Any->from_json($json) } or return;
93  
94 if($response->{status} eq 'latest') {
95 my $version = version->parse($response->{version}) or return ();
96 return ($version, $response->{author});
97 }
98  
99 return ();
100 }
101  
102 sub get_changes_between {
103 my($arg, $author, $ver_start, $ver_stop) = @_;
104  
105 $arg =~ s/::/-/g;
106  
107 my $json = LWP::Simple::get(sprintf $changelog_search, $arg, $ver_stop) or return;
108 my $response = eval { JSON::Any->from_json($json) } or return;
109 my $first_hit = $response->{hits}{hits}[0]{fields} or return;
110  
111 my $raw = LWP::Simple::get(
112 sprintf $source_search, $author,
113 "$arg-$ver_stop",
114 $first_hit->{path},
115 ) or return;
116  
117 my $changes;
118 if($raw) {
119 my $parser = CPAN::Changes->load_string($raw);
120 for my $release ($parser->releases) {
121 my $curr = eval { version->parse($release->version) } or next;
122 if($curr > $ver_start and $curr <= $ver_stop) {
123 $changes .= $release->serialize;
124 }
125 }
126 }
127  
128 $changes;
129 }
130  
131 1;
132  
133 __END__