File Coverage

blib/lib/Module/Build/Prereqs/FromCPANfile.pm
Criterion Covered Total %
statement 44 44 100.0
branch 9 10 90.0
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 64 65 98.4


line stmt bran cond sub pod time code
1             package Module::Build::Prereqs::FromCPANfile;
2 3     3   118369 use strict;
  3         8  
  3         142  
3 3     3   18 use warnings;
  3         7  
  3         138  
4 3     3   23914 use Module::CPANfile 1.0000;
  3         88195  
  3         126  
5 3     3   28 use CPAN::Meta::Prereqs 2.132830;
  3         53  
  3         72  
6 3     3   15 use Exporter qw(import);
  3         5  
  3         113  
7 3     3   15 use version 0.80;
  3         53  
  3         22  
8              
9             our $VERSION = "0.02";
10              
11             our @EXPORT = our @EXPORT_OK = qw(mb_prereqs_from_cpanfile);
12              
13             sub mb_prereqs_from_cpanfile {
14 9     9 1 829734 my (%args) = @_;
15 9         18 my $version = $args{version};
16 9 100       33 $version = _get_mb_version() if not defined $version;
17 9         15 my $cpanfile_path = $args{cpanfile};
18 9 50       22 $cpanfile_path = "cpanfile" if not defined $cpanfile_path;
19              
20 9         62 my $file = Module::CPANfile->load($cpanfile_path);
21 9         29640 return _prereqs_to_mb($file->prereqs, $version);
22             }
23              
24             sub _get_mb_version {
25 2     2   1260 require Module::Build;
26 2         10 return $Module::Build::VERSION;
27             }
28              
29             sub _prereqs_to_mb {
30 9     9   11580 my ($prereqs, $mb_version_str) = @_;
31 9         25 my %result = ();
32 9         79 my $mb_version = version->parse($mb_version_str);
33 9         56 _put_result($prereqs->requirements_for("runtime", "requires"), \%result, "requires");
34 9         36 _put_result($prereqs->requirements_for("runtime", "recommends"), \%result, "recommends");
35 9         32 _put_result($prereqs->requirements_for("runtime", "conflicts"), \%result, "conflicts");
36 9 100       244 if($mb_version < version->parse("0.30")) {
    100          
37 3         69 _put_result($prereqs->merged_requirements(["configure", "build", "test"], ["requires"]),
38             \%result, "build_requires");
39             }elsif($mb_version < version->parse("0.4004")) {
40 2         11 _put_result($prereqs->merged_requirements(["build", "test"], ["requires"]),
41             \%result, "build_requires");
42 2         11 _put_result($prereqs->requirements_for("configure", "requires"), \%result, "configure_requires");
43             }else {
44 4         9 foreach my $phase (qw(configure build test)) {
45 12         36 _put_result($prereqs->requirements_for($phase, "requires"), \%result, "${phase}_requires");
46             }
47             }
48 9         130 return %result;
49             }
50              
51             sub _put_result {
52 46     46   3653 my ($requirements, $result_hashref, $result_key) = @_;
53 46         113 my $reqs_hashref = $requirements->as_string_hash;
54 46 100       1200 if(keys %$reqs_hashref) {
55 32         103 $result_hashref->{$result_key} = $reqs_hashref;
56             }
57             }
58              
59              
60             1;
61             __END__