| 1 | # ---------------------------------------------------------------------- copyright and license --- | ||||||||||||
| 2 | # | ||||||||||||
| 3 | # file: lib/Perl/PrereqScanner/Scanner/Hint.pm | ||||||||||||
| 4 | # | ||||||||||||
| 5 | # Copyright © 2015, 2016 Van de Bugger. | ||||||||||||
| 6 | # | ||||||||||||
| 7 | # This file is part of perl-Perl-PrereqScanner-Scanner-Hint. | ||||||||||||
| 8 | # | ||||||||||||
| 9 | # perl-Perl-PrereqScanner-Scanner-Hint is free software: you can redistribute it and/or modify it | ||||||||||||
| 10 | # under the terms of the GNU General Public License as published by the Free Software Foundation, | ||||||||||||
| 11 | # either version 3 of the License, or (at your option) any later version. | ||||||||||||
| 12 | # | ||||||||||||
| 13 | # perl-Perl-PrereqScanner-Scanner-Hint is distributed in the hope that it will be useful, but | ||||||||||||
| 14 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||||||||||||
| 15 | # PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||||||||||||
| 16 | # | ||||||||||||
| 17 | # You should have received a copy of the GNU General Public License along with | ||||||||||||
| 18 | # perl-Perl-PrereqScanner-Scanner-Hint. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||
| 19 | # | ||||||||||||
| 20 | # ---------------------------------------------------------------------- copyright and license --- | ||||||||||||
| 21 | |||||||||||||
| 22 | #pod =for :this This is C<Perl::PrereqScanner::Scanner::Hint> module documentation. Read this if you are going to hack or | ||||||||||||
| 23 | #pod extend C<Manifest::Write>. | ||||||||||||
| 24 | #pod | ||||||||||||
| 25 | #pod =for :those If you want to specify implicit prerequisites directly in Perl code, read the L<user manual|Perl::PrereqScanner::Scanner::Hint::Manual>. | ||||||||||||
| 26 | #pod General topics like getting source, building, installing, bug reporting and some others are covered | ||||||||||||
| 27 | #pod in the F<README>. | ||||||||||||
| 28 | #pod | ||||||||||||
| 29 | #pod =for test_synopsis my $path; | ||||||||||||
| 30 | #pod | ||||||||||||
| 31 | #pod =head1 SYNOPSIS | ||||||||||||
| 32 | #pod | ||||||||||||
| 33 | #pod use Perl::PrereqScanner; | ||||||||||||
| 34 | #pod my $scanner = Perl::PrereqScanner->new( { | ||||||||||||
| 35 | #pod extra_scanners => [ qw{ Hint } ], | ||||||||||||
| 36 | #pod } ); | ||||||||||||
| 37 | #pod my $prereqs = $scanner->scan_file( $path ); | ||||||||||||
| 38 | #pod | ||||||||||||
| 39 | #pod =head1 DESCRIPTION | ||||||||||||
| 40 | #pod | ||||||||||||
| 41 | #pod This is a trivial scanner which utilizes power of C<Perl::PrereqScanner> and C<PPI>. | ||||||||||||
| 42 | #pod | ||||||||||||
| 43 | #pod =head1 SEE ALSO | ||||||||||||
| 44 | #pod | ||||||||||||
| 45 | #pod =for :list | ||||||||||||
| 46 | #pod * L<Perl::PrereqScanner> | ||||||||||||
| 47 | #pod * L<PPI> | ||||||||||||
| 48 | #pod | ||||||||||||
| 49 | #pod =cut | ||||||||||||
| 50 | |||||||||||||
| 51 | # -------------------------------------------------------------------------------------------------- | ||||||||||||
| 52 | |||||||||||||
| 53 | package Perl::PrereqScanner::Scanner::Hint; | ||||||||||||
| 54 | |||||||||||||
| 55 | 2 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 56 | 2 | ▶ | use namespace::autoclean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 57 | 2 | ▶ | use version 0.77; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 58 | 2 | ▶ | use warnings::register; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 59 | |||||||||||||
| 60 | # ABSTRACT: Plugin for C<Perl::PrereqScanner> looking for C<## REQUIRE:> comments | ||||||||||||
| 61 | 2 | our $VERSION = 'v0.1.1'; # VERSION | |||||||||||
| 62 | |||||||||||||
| 63 | 2 | ▶ | use Module::Runtime qw{ is_module_name }; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 64 | 2 | ▶ | use Try::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 65 | |||||||||||||
| 66 | 2 | with 'Perl::PrereqScanner::Scanner'; | |||||||||||
| 67 | |||||||||||||
| 68 | # -------------------------------------------------------------------------------------------------- | ||||||||||||
| 69 | |||||||||||||
| 70 | 3 | ▶ | my $error = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 71 | 3 | my ( $self, $what, $elem ) = @_; | |||||||||||
| 72 | 3 | my $line = $elem->logical_line_number; | |||||||||||
| 73 | 3 | ▶ | my $file = $elem->logical_filename || '(*UNKNOWN*)'; | ||||||||||
|
Condition100%2/2
$elem->logical_filename || '(*UNKNOWN*)'
MC/DC100%1/1
$elem->logical_filename || '(*UNKNOWN*)'
$elem->logical_filename
Truth table100%2/2
A
A= $elem->logical_filename
| |||||||||||||
| 74 | 3 | die "$what at $file line $line.\n"; | |||||||||||
| 75 | 2 | }; | |||||||||||
| 76 | |||||||||||||
| 77 | 1 | ▶ | my $warning = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||
| 78 | 1 | my ( $self, $what, $elem ) = @_; | |||||||||||
| 79 | 1 | my $line = $elem->logical_line_number; | |||||||||||
| 80 | 1 | ▶ | my $file = $elem->logical_filename || '(*UNKNOWN*)'; | ||||||||||
|
Condition50%1/2
$elem->logical_filename || '(*UNKNOWN*)'
MC/DC0%0/1
$elem->logical_filename || '(*UNKNOWN*)'
$elem->logical_filename
Truth table50%1/2
A
A= $elem->logical_filename
| |||||||||||||
| 81 | 1 | warnings::warnif( $self, "$what at $file line $line.\n" ); | |||||||||||
| 82 | 2 | }; | |||||||||||
| 83 | |||||||||||||
| 84 | # -------------------------------------------------------------------------------------------------- | ||||||||||||
| 85 | |||||||||||||
| 86 | #pod =method scan_for_prereqs | ||||||||||||
| 87 | #pod | ||||||||||||
| 88 | #pod my $doc = PPI::Document->new( ... ); | ||||||||||||
| 89 | #pod my $req = CPAN::Meta::Requirements->new; | ||||||||||||
| 90 | #pod $self->scan_for_prereqs( $doc, $req ); | ||||||||||||
| 91 | #pod | ||||||||||||
| 92 | #pod The method scans document C<$doc>, which is expected to be an objects of C<PPI::Document> class. | ||||||||||||
| 93 | #pod The methods looks for comments starting with C<# REQUIRE:>, and adds found requirements to C<$req>, | ||||||||||||
| 94 | #pod by calling C<< $req->add_string+requirement >>. C<$req> is expected to be an object of | ||||||||||||
| 95 | #pod C<CPAN::Meta::Requirements> class. | ||||||||||||
| 96 | #pod | ||||||||||||
| 97 | #pod =cut | ||||||||||||
| 98 | |||||||||||||
| 99 | 7 | ▶ | sub scan_for_prereqs { | ||||||||||
|
Subroutine
scan_for_prereqs: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 100 | 7 | my ( $self, $doc, $req ) = @_; | |||||||||||
| 101 | 7 | ▶ | my $comments = $doc->find( 'Token::Comment' ) || []; | ||||||||||
|
Condition50%1/2
$doc->find('Token::Comment') || []
MC/DC0%0/1
$doc->find('Token::Comment') || []
$doc->find('Token::Comment')
Truth table50%1/2
A
A= $doc->find('Token::Comment')
| |||||||||||||
| 102 | 7 | for my $comment ( @$comments ) { | |||||||||||
| 103 | 19 | ▶ | if ( $comment->content =~ m{ ^ \h* (\#{1,2}) \h* REQUIRES? \h* : \h* (.*) \h* $ }x ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | 11 | my ( $hashes, $requirement ) = ( $1, $2 ); | |||||||||||
| 105 | 11 | ▶ | if ( length( $hashes ) == 1 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 106 | 1 | $self->$warning( | |||||||||||
| 107 | "Starting a hint with one hash is deprecated, use two hashes instead", | ||||||||||||
| 108 | $comment | ||||||||||||
| 109 | ); | ||||||||||||
| 110 | }; | ||||||||||||
| 111 | 11 | $requirement =~ s{ \h* \# .* \z }{}x; # Strip trailing comment, if any. | |||||||||||
| 112 | 11 | my ( $mod, $ver ) = ( split( m{\h+}x, $requirement, 2 ), '0' ); | |||||||||||
| 113 | 11 | ▶ | if ( is_module_name( $mod ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | 9 | ▶ | try { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 115 | 9 | $req->add_string_requirement( $mod, $ver ); | |||||||||||
| 116 | 1 | ▶ | } catch { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 117 | 1 | my $ex = "$_"; | |||||||||||
| 118 | 1 | chomp( $ex ); | |||||||||||
| 119 | 1 | $self->$error( "$ex", $comment ); | |||||||||||
| 120 | 9 | }; | |||||||||||
| 121 | } else { | ||||||||||||
| 122 | 2 | $self->$error( "'$mod' is not a valid module name", $comment ); | |||||||||||
| 123 | }; | ||||||||||||
| 124 | }; | ||||||||||||
| 125 | }; | ||||||||||||
| 126 | 4 | return; | |||||||||||
| 127 | }; | ||||||||||||
| 128 | |||||||||||||
| 129 | # -------------------------------------------------------------------------------------------------- | ||||||||||||
| 130 | |||||||||||||
| 131 | 2 | __PACKAGE__->meta->make_immutable(); | |||||||||||
| 132 | |||||||||||||
| 133 | 2 | 1; | |||||||||||
| 134 | |||||||||||||
| 135 | # -------------------------------------------------------------------------------------------------- | ||||||||||||
| 136 | |||||||||||||
| 137 | #pod =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 138 | #pod | ||||||||||||
| 139 | #pod Copyright (C) 2015, 2016 Van de Bugger | ||||||||||||
| 140 | #pod | ||||||||||||
| 141 | #pod License GPLv3+: The GNU General Public License version 3 or later | ||||||||||||
| 142 | #pod <http://www.gnu.org/licenses/gpl-3.0.txt>. | ||||||||||||
| 143 | #pod | ||||||||||||
| 144 | #pod This is free software: you are free to change and redistribute it. There is | ||||||||||||
| 145 | #pod NO WARRANTY, to the extent permitted by law. | ||||||||||||
| 146 | #pod | ||||||||||||
| 147 | #pod | ||||||||||||
| 148 | #pod =cut | ||||||||||||
| 149 | |||||||||||||
| 150 | # ------------------------------------------------------------------------------------------------ | ||||||||||||
| 151 | # | ||||||||||||
| 152 | # file: doc/what.pod | ||||||||||||
| 153 | # | ||||||||||||
| 154 | # This file is part of perl-Perl-PrereqScanner-Scanner-Hint. | ||||||||||||
| 155 | # | ||||||||||||
| 156 | # ------------------------------------------------------------------------------------------------ | ||||||||||||
| 157 | |||||||||||||
| 158 | #pod =pod | ||||||||||||
| 159 | #pod | ||||||||||||
| 160 | #pod =encoding UTF-8 | ||||||||||||
| 161 | #pod | ||||||||||||
| 162 | #pod =head1 WHAT? | ||||||||||||
| 163 | #pod | ||||||||||||
| 164 | #pod C<Perl::PrereqScanner::Scanner::Hint> (or just C<Scanner::Hint> for brevity) is a plugin for C<Perl::PrereqScanner> | ||||||||||||
| 165 | #pod tool. C<Scanner::Hint> looks for C<# REQUIRE: I<ModuleName> I<VersionRange>> comments in the code. | ||||||||||||
| 166 | #pod | ||||||||||||
| 167 | #pod =cut | ||||||||||||
| 168 | |||||||||||||
| 169 | # end of file # | ||||||||||||
| 170 | |||||||||||||
| 171 | |||||||||||||
| 172 | # end of file # | ||||||||||||
| 173 | |||||||||||||
| 174 | __END__ | ||||||||||||