File Coverage

blib/lib/Dist/Iller/DocType/Cpanfile.pm
Criterion Covered Total %
statement 26 28 92.8
branch n/a
condition n/a
subroutine 11 12 91.6
pod 0 4 0.0
total 37 44 84.0


line stmt bran cond sub pod time code
1 1     1   611 use 5.14.0;
  1         4  
2 1     1   6 use strict;
  1         2  
  1         24  
3 1     1   5 use warnings;
  1         3  
  1         68  
4              
5             package Dist::Iller::DocType::Cpanfile;
6              
7             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
8             # ABSTRACT: Turn the Dist::Iller config into a .cpanfile file
9             our $VERSION = '0.1411';
10              
11 1     1   9 use Dist::Iller::Elk;
  1         2  
  1         16  
12 1     1   2370 use JSON::MaybeXS qw/decode_json/;
  1         2  
  1         69  
13 1     1   7 use Path::Tiny;
  1         3  
  1         45  
14 1     1   5 use Carp qw/croak/;
  1         2  
  1         60  
15 1     1   8 use Dist::Iller::Prereq;
  1         2  
  1         658  
16             with qw/
17             Dist::Iller::DocType
18             Dist::Iller::Role::HasPrereqs
19             /;
20              
21 2     2 0 11 sub comment_start { '#' }
22              
23 3     3 0 42 sub filename { 'cpanfile' }
24              
25 4     4 0 25 sub phase { 'after' }
26              
27             sub to_hash {
28 0     0 0   my $self = shift;
29 0           return { prereqs => $self->prereqs };
30             }
31              
32             sub parse {
33             my $self = shift;
34              
35             my $metapath = path('META.json');
36             if(!$metapath->exists) {
37             croak 'META.json does not exist';
38             }
39              
40             my $meta = decode_json($metapath->slurp)->{'prereqs'};
41              
42             for my $phase (keys %{ $meta }) {
43             my $phasedata = $meta->{ $phase };
44              
45             for my $relation (keys %{ $phasedata }) {
46             my $relationdata = $phasedata->{ $relation };
47              
48             for my $module (sort keys %{ $relationdata }) {
49             my $prereq = $meta->{ $phase }{ $relation };
50             $self->add_prereq(Dist::Iller::Prereq->new(
51             module => $module,
52             version => $meta->{ $phase }{ $relation }{ $module },
53             phase => $phase,
54             relation => $relation,
55             ));
56             }
57             }
58             }
59             }
60              
61             sub to_string {
62             my $self = shift;
63              
64             my @strings;
65              
66             for my $phase (qw/runtime test build configure develop/) {
67             RELATION:
68             for my $relation (qw/requires recommends suggests conflicts/) {
69              
70             my @prereqs = sort { $a->module cmp $b->module } $self->filter_prereqs(sub { $_->phase eq $phase && $_->relation eq $relation });
71             next RELATION if !scalar @prereqs;
72              
73             push @strings => "on $phase => sub {";
74             for my $prereq (@prereqs) {
75             push @strings => sprintf q{ %s '%s' => '%s';}, $relation, $prereq->module, $prereq->version;
76             }
77             push @strings => '};';
78             }
79             }
80             return join "\n" => (@strings, '');
81              
82             }
83              
84             __PACKAGE__->meta->make_immutable;
85              
86             1;
87              
88             __END__
89              
90             =pod
91              
92             =encoding UTF-8
93              
94             =head1 NAME
95              
96             Dist::Iller::DocType::Cpanfile - Turn the Dist::Iller config into a .cpanfile file
97              
98             =head1 VERSION
99              
100             Version 0.1411, released 2020-01-01.
101              
102             =head1 SOURCE
103              
104             L<https://github.com/Csson/p5-Dist-Iller>
105              
106             =head1 HOMEPAGE
107              
108             L<https://metacpan.org/release/Dist-Iller>
109              
110             =head1 AUTHOR
111              
112             Erik Carlsson <info@code301.com>
113              
114             =head1 COPYRIGHT AND LICENSE
115              
116             This software is copyright (c) 2021 by Erik Carlsson.
117              
118             This is free software; you can redistribute it and/or modify it under
119             the same terms as the Perl 5 programming language system itself.
120              
121             =cut