| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Pod::Readme::Plugin::requires; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 850 | use Moo::Role; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 21 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | our $VERSION = 'v1.2.3'; | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 1 |  |  | 1 |  | 1298 | use CPAN::Meta; | 
|  | 1 |  |  |  |  | 31270 |  | 
|  | 1 |  |  |  |  | 38 |  | 
| 8 | 1 |  |  | 1 |  | 2946 | use Module::CoreList; | 
|  | 1 |  |  |  |  | 104759 |  | 
|  | 1 |  |  |  |  | 16 |  | 
| 9 | 1 |  |  | 1 |  | 710 | use Path::Tiny; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 127 |  | 
| 10 | 1 |  |  | 1 |  | 8 | use Types::Standard qw/ Bool Str /; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 45 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 1 |  |  | 1 |  | 1360 | use Pod::Readme::Types qw/ File HeadingLevel /; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 1049 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | =head1 NAME | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | Pod::Readme::Plugin::requires - Include requirements in README | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | =for readme plugin requires | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | This is a plugin for L that includes module requirements | 
| 25 |  |  |  |  |  |  | from the F file. | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | Because this depends on the F file, the F should be | 
| 28 |  |  |  |  |  |  | generated after that file has been updated. | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | =head1 ARGUMENTS | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =head2 C | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | =for readme plugin version from-file='MYMETA.yml' | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | By default, it will extract the version from the F file. If, | 
| 37 |  |  |  |  |  |  | for some reason, this file is in a non-standard location, then you | 
| 38 |  |  |  |  |  |  | should specify it here. | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | The file will be ignored if the C attribute is set, and instead | 
| 41 |  |  |  |  |  |  | obtain metadata from the L object (since the F | 
| 42 |  |  |  |  |  |  | file may not exist.) | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | =head2 C | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | By default, core modules for the version of Perl specified in the | 
| 47 |  |  |  |  |  |  | F file are omitted from this list.  If you prefer to lise | 
| 48 |  |  |  |  |  |  | all requirements, then specify this option. | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | =head2 C | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =for readme plugin version title='REQUIREMENTS' | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | This argument allows you to change the title of the heading. | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | =head1 KNOWN ISSUES | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | =over | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | =item * | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | Trailing zeros in module versions may be dropped. | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | If you specify a minimum version of a module with a trailing zero, | 
| 65 |  |  |  |  |  |  | e.g. "0.30", then it may be shown as "0.3".  A workaround is to | 
| 66 |  |  |  |  |  |  | specify the module version in your F as a string instead | 
| 67 |  |  |  |  |  |  | of number: | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | requires( | 
| 70 |  |  |  |  |  |  | 'CPAN::Changes' => '0.30', | 
| 71 |  |  |  |  |  |  | ... | 
| 72 |  |  |  |  |  |  | ); | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | =back | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | =cut | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | requires 'parse_cmd_args'; | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | has 'requires_from_file' => ( | 
| 81 |  |  |  |  |  |  | is      => 'rw', | 
| 82 |  |  |  |  |  |  | isa     => File, | 
| 83 |  |  |  |  |  |  | coerce  => sub { File->coerce(@_) }, | 
| 84 |  |  |  |  |  |  | default => 'META.yml', | 
| 85 |  |  |  |  |  |  | lazy    => 1, | 
| 86 |  |  |  |  |  |  | ); | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | has 'requires_title' => ( | 
| 89 |  |  |  |  |  |  | is      => 'rw', | 
| 90 |  |  |  |  |  |  | isa     => Str, | 
| 91 |  |  |  |  |  |  | default => 'REQUIREMENTS', | 
| 92 |  |  |  |  |  |  | lazy    => 1, | 
| 93 |  |  |  |  |  |  | ); | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | has 'requires_omit_core' => ( | 
| 96 |  |  |  |  |  |  | is      => 'rw', | 
| 97 |  |  |  |  |  |  | isa     => Bool, | 
| 98 |  |  |  |  |  |  | default => 1, | 
| 99 |  |  |  |  |  |  | lazy    => 1, | 
| 100 |  |  |  |  |  |  | ); | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | has 'requires_heading_level' => ( | 
| 103 |  |  |  |  |  |  | is      => 'rw', | 
| 104 |  |  |  |  |  |  | isa     => HeadingLevel, | 
| 105 |  |  |  |  |  |  | default => 1, | 
| 106 |  |  |  |  |  |  | lazy    => 1, | 
| 107 |  |  |  |  |  |  | ); | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | has 'requires_run' => ( | 
| 110 |  |  |  |  |  |  | is      => 'rw', | 
| 111 |  |  |  |  |  |  | isa     => Bool, | 
| 112 |  |  |  |  |  |  | default => 0, | 
| 113 |  |  |  |  |  |  | lazy    => 1, | 
| 114 |  |  |  |  |  |  | ); | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | around 'depends_on' => sub { | 
| 117 |  |  |  |  |  |  | my ( $orig, $self ) = @_; | 
| 118 |  |  |  |  |  |  | return ( $self->requires_from_file, $self->$orig ); | 
| 119 |  |  |  |  |  |  | }; | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | sub cmd_requires { | 
| 122 | 3 |  |  | 3 | 0 | 11 | my ( $self, @args ) = @_; | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 3 | 50 |  |  |  | 70 | die "The requires plugin can only be used once" if $self->requires_run; | 
| 125 |  |  |  |  |  |  |  | 
| 126 | 3 |  |  |  |  | 87 | my $res = $self->parse_cmd_args( | 
| 127 |  |  |  |  |  |  | [qw/ from-file title omit-core no-omit-core heading-level /], @args ); | 
| 128 | 2 |  |  |  |  | 7 | foreach my $key ( keys %{$res} ) { | 
|  | 2 |  |  |  |  | 9 |  | 
| 129 | 2 |  |  |  |  | 45 | ( my $name = "requires_${key}" ) =~ s/-/_/g; | 
| 130 | 2 | 50 |  |  |  | 13 | if ( my $method = $self->can($name) ) { | 
| 131 | 2 |  |  |  |  | 45 | $self->$method( $res->{$key} ); | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  | else { | 
| 134 | 0 |  |  |  |  | 0 | die "Invalid key: '${key}'"; | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 2 |  |  |  |  | 37 | my $meta; | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 2 | 50 |  |  |  | 18 | if ( $self->zilla ) { | 
| 141 |  |  |  |  |  |  |  | 
| 142 | 0 |  |  |  |  | 0 | $meta = CPAN::Meta->create( $self->zilla->distmeta, | 
| 143 |  |  |  |  |  |  | { lazy_validation => 1 } ); | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  | else { | 
| 147 |  |  |  |  |  |  |  | 
| 148 | 2 |  |  |  |  | 52 | my $file = | 
| 149 |  |  |  |  |  |  | path( $self->base_dir, $self->requires_from_file )->stringify; | 
| 150 | 2 | 50 |  |  |  | 267 | unless ( -e $file ) { | 
| 151 | 0 |  |  |  |  | 0 | die "Cannot find META.yml file at '${file}"; | 
| 152 |  |  |  |  |  |  | } | 
| 153 |  |  |  |  |  |  |  | 
| 154 | 2 |  |  |  |  | 25 | $meta = CPAN::Meta->load_file($file); | 
| 155 |  |  |  |  |  |  | } | 
| 156 |  |  |  |  |  |  |  | 
| 157 | 2 |  |  |  |  | 41399 | my ( $prereqs, $perl ) = $self->_get_prereqs( $meta, 'requires' ); | 
| 158 | 2 | 50 |  |  |  | 7 | if ( %{$prereqs} ) { | 
|  | 2 |  |  |  |  | 10 |  | 
| 159 |  |  |  |  |  |  |  | 
| 160 | 2 | 50 |  |  |  | 106 | my $heading = $self->can( "write_head" . $self->requires_heading_level ) | 
| 161 |  |  |  |  |  |  | or die "Invalid heading level: " . $self->requires_heading_level; | 
| 162 |  |  |  |  |  |  |  | 
| 163 | 2 |  |  |  |  | 126 | $self->$heading( $self->requires_title ); | 
| 164 |  |  |  |  |  |  |  | 
| 165 | 2 | 50 |  |  |  | 111 | if ($perl) { | 
| 166 | 2 |  |  |  |  | 49 | $self->write_para( | 
| 167 |  |  |  |  |  |  | sprintf( 'This distribution requires Perl %s.', | 
| 168 |  |  |  |  |  |  | version->parse($perl)->normal ) | 
| 169 |  |  |  |  |  |  | ); | 
| 170 |  |  |  |  |  |  | } | 
| 171 |  |  |  |  |  |  |  | 
| 172 | 2 |  |  |  |  | 58 | $self->write_para('This distribution requires the following modules:'); | 
| 173 |  |  |  |  |  |  |  | 
| 174 | 2 |  |  |  |  | 54 | $self->_write_modules($prereqs); | 
| 175 |  |  |  |  |  |  |  | 
| 176 | 2 |  |  |  |  | 54 | my ($recommends) = $self->_get_prereqs( $meta, 'recommends' ); | 
| 177 | 2 | 100 |  |  |  | 4 | if ( %{$recommends} ) { | 
|  | 2 |  |  |  |  | 9 |  | 
| 178 |  |  |  |  |  |  |  | 
| 179 | 1 |  |  |  |  | 5 | $self->write_para( | 
| 180 |  |  |  |  |  |  | 'This distribution recommends the following modules:'); | 
| 181 |  |  |  |  |  |  |  | 
| 182 | 1 |  |  |  |  | 26 | $self->_write_modules($recommends); | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | } | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | } | 
| 187 |  |  |  |  |  |  |  | 
| 188 | 2 |  |  |  |  | 67 | $self->requires_run(1); | 
| 189 |  |  |  |  |  |  | } | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | sub _get_prereqs { | 
| 192 | 4 |  |  | 4 |  | 14 | my ( $self, $meta, $key ) = @_; | 
| 193 |  |  |  |  |  |  |  | 
| 194 | 4 |  |  |  |  | 9 | my %prereqs; | 
| 195 | 4 |  |  |  |  | 6 | foreach my $type ( values %{ $meta->prereqs } ) { | 
|  | 4 |  |  |  |  | 20 |  | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | # TODO: max version | 
| 198 | 10 |  |  |  |  | 1002 | $prereqs{$_} = $type->{$key}->{$_} for ( keys %{ $type->{$key} } ); | 
|  | 10 |  |  |  |  | 105 |  | 
| 199 |  |  |  |  |  |  | } | 
| 200 | 4 |  |  |  |  | 33 | my $perl = delete $prereqs{perl}; | 
| 201 | 4 | 100 | 66 |  |  | 114 | if ( $self->requires_omit_core && $perl ) { | 
| 202 | 2 |  |  |  |  | 90 | foreach ( keys %prereqs ) { | 
| 203 | 46 |  |  |  |  | 53129 | my $ver = $prereqs{$_}; | 
| 204 |  |  |  |  |  |  | delete $prereqs{$_} | 
| 205 | 46 | 100 | 100 |  |  | 173 | if Module::CoreList->first_release( $_, $prereqs{$ver} ) | 
| 206 |  |  |  |  |  |  | && version->parse( Module::CoreList->first_release($_) ) <= | 
| 207 |  |  |  |  |  |  | version->parse($perl); | 
| 208 |  |  |  |  |  |  | } | 
| 209 |  |  |  |  |  |  | } | 
| 210 | 4 |  |  |  |  | 727 | return ( \%prereqs, $perl ); | 
| 211 |  |  |  |  |  |  | } | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | sub _write_modules { | 
| 214 | 3 |  |  | 3 |  | 9 | my ( $self, $prereqs ) = @_; | 
| 215 | 3 |  |  |  |  | 21 | $self->write_over(4); | 
| 216 | 3 |  |  |  |  | 71 | foreach my $module ( sort { lc($a) cmp lc($b) } keys %{$prereqs} ) { | 
|  | 109 |  |  |  |  | 210 |  | 
|  | 3 |  |  |  |  | 29 |  | 
| 217 | 40 |  |  |  |  | 959 | my $version = $prereqs->{$module}; | 
| 218 | 40 | 100 |  |  |  | 88 | my $text = $version ? " (version ${version})" : ''; | 
| 219 | 40 |  |  |  |  | 191 | $self->write_item( sprintf( '* L<%s>', $module ) . $text ); | 
| 220 |  |  |  |  |  |  | } | 
| 221 | 3 |  |  |  |  | 92 | $self->write_back; | 
| 222 |  |  |  |  |  |  | } | 
| 223 |  |  |  |  |  |  |  | 
| 224 | 1 |  |  | 1 |  | 8 | use namespace::autoclean; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 29 |  | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | 1; |