File Coverage

blib/lib/Perl/PrereqScanner/Scanner/Hint.pm
Criterion Covered Total %
statement 30 30 100.0
branch 4 4 100.0
condition 1 2 50.0
subroutine 8 8 100.0
pod 1 1 100.0
total 44 45 97.7


line stmt bran cond sub pod time code
1             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2             #
3             # file: lib/Perl/PrereqScanner/Scanner/Hint.pm
4             #
5              
6             #pod =encoding UTF-8
7             #pod
8             #pod =head1 COPYRIGHT AND LICENSE
9             #pod
10             #pod Copyright © 2015 Van de Bugger
11             #pod
12             #pod This file is part of perl-Perl-PrereqScanner-Scanner-Hint.
13             #pod
14             #pod perl-Perl-PrereqScanner-Scanner-Hint is free software: you can redistribute it and/or modify it
15             #pod under the terms of the GNU General Public License as published by the Free Software Foundation,
16             #pod either version 3 of the License, or (at your option) any later version.
17             #pod
18             #pod perl-Perl-PrereqScanner-Scanner-Hint is distributed in the hope that it will be useful, but
19             #pod WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
20             #pod PARTICULAR PURPOSE. See the GNU General Public License for more details.
21             #pod
22             #pod You should have received a copy of the GNU General Public License along with
23             #pod perl-Perl-PrereqScanner-Scanner-Hint. If not, see <http://www.gnu.org/licenses/>.
24             #pod
25             #pod =cut
26              
27             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
28              
29             #pod =for :this This is C<Perl::PrereqScanner::Scanner::Hint> module documentation. Read this if you are going to hack or
30             #pod extend C<Manifest::Write>.
31             #pod
32             #pod =for :those If you want to specify implicit prerequisites directly in your Perl code, read the L<user manual|Perl::PrereqScanner::Scanner::Hint::Manual>.
33             #pod General topics like getting source, building, installing, bug reporting and some others are covered
34             #pod in the F<README>.
35             #pod
36             #pod =for test_synopsis my $path;
37             #pod
38             #pod =head1 SYNOPSIS
39             #pod
40             #pod use Perl::PrereqScanner;
41             #pod my $scanner = Perl::PrereqScanner->new( {
42             #pod extra_scanners => [ qw{ Hint } ],
43             #pod } );
44             #pod my $prereqs = $scanner->scan_file( $path );
45             #pod
46             #pod =head1 DESCRIPTION
47             #pod
48             #pod This is a trivial scanner which utilizes power of C<Perl::PrereqScanner> and C<PPI>.
49             #pod
50             #pod =head1 SEE ALSO
51             #pod
52             #pod =for :list
53             #pod * L<Perl::PrereqScanner>
54             #pod * L<PPI>
55             #pod
56             #pod =cut
57              
58             # --------------------------------------------------------------------------------------------------
59              
60             package Perl::PrereqScanner::Scanner::Hint;
61              
62 1     1   35314 use Moose;
  1         2  
  1         9  
63 1     1   6530 use namespace::autoclean;
  1         2  
  1         11  
64 1     1   80 use version 0.77;
  1         34  
  1         10  
65              
66             # ABSTRACT: Plugin for C<Perl::PrereqScanner> looking for C<# REQUIRE:> comments
67             our $VERSION = 'v0.1.0'; # VERSION
68              
69 1     1   92 use Module::Runtime qw{ is_module_name };
  1         2  
  1         8  
70 1     1   40 use Try::Tiny;
  1         2  
  1         582  
71              
72             with 'Perl::PrereqScanner::Scanner';
73              
74             # --------------------------------------------------------------------------------------------------
75              
76             my $error = sub {
77             my ( $what, $elem ) = @_;
78             my ( undef, undef, undef, $line, $file ) = @{ $elem->location };
79             if ( not defined( $file ) ) {
80             $file = '(*UNKNOWN*)';
81             };
82             die "$what at $file line $line.\n";
83             };
84              
85             # --------------------------------------------------------------------------------------------------
86              
87             #pod =method scan_for_prereqs
88             #pod
89             #pod my $doc = PPI::Document->new( ... );
90             #pod my $req = CPAN::Meta::Requirements->new;
91             #pod $self->scan_for_prereqs( $doc, $req );
92             #pod
93             #pod The method scans document C<$doc>, which is expected to be an objects of C<PPI::Document> class.
94             #pod The methods looks for comments starting with C<# REQUIRE:>, and adds found requirements to C<$req>,
95             #pod by calling C<< $req->add_string+requirement >>. C<$req> is expected to be an object of
96             #pod C<CPAN::Meta::Requirements> class.
97             #pod
98             #pod =cut
99              
100             sub scan_for_prereqs {
101 5     5 1 15000 my ( $self, $doc, $req ) = @_;
102 5   50     26 my $comments = $doc->find( 'Token::Comment' ) || [];
103 5         3916 for my $comment ( @$comments ) {
104 13 100       1038 if ( $comment->content =~ m{ ^ \s* \# \s* REQUIRES? \s* : \s* (.*) \s* $ }x ) {
105 9         82 my $requirement = $1;
106 9         27 $requirement =~ s{ \s* \# .* \z }{}x; # Strip trailing comment, if any.
107 9         33 my ( $mod, $ver ) = ( split( m{\s+}x, $requirement, 2 ), '0' );
108 9 100       30 if ( is_module_name( $mod ) ) {
109             try {
110 8     8   315 $req->add_string_requirement( $mod, $ver );
111             } catch {
112 1     1   172 my $ex = "$_";
113 1         4 chomp( $ex );
114 1         5 $error->( "$ex", $comment );
115 8         146 };
116             } else {
117 1         17 $error->( "'$mod' is not a valid module name", $comment );
118             };
119             };
120             };
121 3         112 return;
122             };
123              
124             # --------------------------------------------------------------------------------------------------
125              
126             __PACKAGE__->meta->make_immutable();
127              
128             1;
129              
130             # --------------------------------------------------------------------------------------------------
131              
132             #pod =pod
133             #pod
134             #pod =encoding UTF-8
135             #pod
136             #pod =head1 WHAT?
137             #pod
138             #pod C<Perl::PrereqScanner::Scanner::Hint> (or just C<Scanner::Hint> for brevity) is a plugin for C<Perl::PrereqScanner>
139             #pod tool. C<Scanner::Hint> looks for C<# REQUIRE: I<ModuleName> I<VersionRange>> comments in the code.
140             #pod
141             #pod =cut
142              
143              
144             # end of file #
145              
146             __END__
147              
148             =pod
149              
150             =encoding UTF-8
151              
152             =head1 NAME
153              
154             Perl::PrereqScanner::Scanner::Hint - Plugin for C<Perl::PrereqScanner> looking for C<# REQUIRE:> comments
155              
156             =head1 VERSION
157              
158             Version v0.1.0, released on 2015-10-22 11:14 UTC.
159              
160             =head1 WHAT?
161              
162             C<Perl::PrereqScanner::Scanner::Hint> (or just C<Scanner::Hint> for brevity) is a plugin for C<Perl::PrereqScanner>
163             tool. C<Scanner::Hint> looks for C<# REQUIRE: I<ModuleName> I<VersionRange>> comments in the code.
164              
165             This is C<Perl::PrereqScanner::Scanner::Hint> module documentation. Read this if you are going to hack or
166             extend C<Manifest::Write>.
167              
168             If you want to specify implicit prerequisites directly in your Perl code, read the L<user manual|Perl::PrereqScanner::Scanner::Hint::Manual>.
169             General topics like getting source, building, installing, bug reporting and some others are covered
170             in the F<README>.
171              
172             =head1 SYNOPSIS
173              
174             use Perl::PrereqScanner;
175             my $scanner = Perl::PrereqScanner->new( {
176             extra_scanners => [ qw{ Hint } ],
177             } );
178             my $prereqs = $scanner->scan_file( $path );
179              
180             =head1 DESCRIPTION
181              
182             This is a trivial scanner which utilizes power of C<Perl::PrereqScanner> and C<PPI>.
183              
184             =head1 OBJECT METHODS
185              
186             =head2 scan_for_prereqs
187              
188             my $doc = PPI::Document->new( ... );
189             my $req = CPAN::Meta::Requirements->new;
190             $self->scan_for_prereqs( $doc, $req );
191              
192             The method scans document C<$doc>, which is expected to be an objects of C<PPI::Document> class.
193             The methods looks for comments starting with C<# REQUIRE:>, and adds found requirements to C<$req>,
194             by calling C<< $req->add_string+requirement >>. C<$req> is expected to be an object of
195             C<CPAN::Meta::Requirements> class.
196              
197             =for test_synopsis my $path;
198              
199             =head1 SEE ALSO
200              
201             =over 4
202              
203             =item *
204              
205             L<Perl::PrereqScanner>
206              
207             =item *
208              
209             L<PPI>
210              
211             =back
212              
213             =head1 AUTHOR
214              
215             Van de Bugger <van.de.bugger@gmail.com>
216              
217             =head1 COPYRIGHT AND LICENSE
218              
219             Copyright © 2015 Van de Bugger
220              
221             This file is part of perl-Perl-PrereqScanner-Scanner-Hint.
222              
223             perl-Perl-PrereqScanner-Scanner-Hint is free software: you can redistribute it and/or modify it
224             under the terms of the GNU General Public License as published by the Free Software Foundation,
225             either version 3 of the License, or (at your option) any later version.
226              
227             perl-Perl-PrereqScanner-Scanner-Hint is distributed in the hope that it will be useful, but
228             WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
229             PARTICULAR PURPOSE. See the GNU General Public License for more details.
230              
231             You should have received a copy of the GNU General Public License along with
232             perl-Perl-PrereqScanner-Scanner-Hint. If not, see <http://www.gnu.org/licenses/>.
233              
234             =cut