| 1 |
#!/usr/bin/perl |
| 2 |
package Perl::PrereqScanner::App 1.100; |
| 3 |
# ABSTRACT: scan your working dir for likely prereqs |
| 4 |
|
| 5 |
use strict; |
| 6 |
use warnings; |
| 7 |
|
| 8 |
use lib (); |
| 9 |
|
| 10 |
use File::Find; |
| 11 |
use List::Util qw{ max }; |
| 12 |
use Perl::PrereqScanner; |
| 13 |
use CPAN::Meta::Requirements (); |
| 14 |
use Scalar::Util qw(looks_like_number); |
| 15 |
|
| 16 |
use Getopt::Long::Descriptive; |
| 17 |
|
| 18 |
my ($opt, $usage) = describe_options( |
| 19 |
'scan-perl-prereqs %o [DIR|FILES]', |
| 20 |
[ 'pretty' => 'format for human consumption, not toolchain' ], |
| 21 |
[ 'by-file' => 'emit one stanza per file' ], |
| 22 |
[ 'lib|I=s@' => 'specifies include paths, like perl\'s -I' ], |
| 23 |
|
| 24 |
[ 'version' => 'print usage message and exit' ], |
| 25 |
[ 'help' => 'print version and exit' ], |
| 26 |
); |
| 27 |
|
| 28 |
lib->import(@{ $opt->lib }) if $opt->lib; |
| 29 |
|
| 30 |
print('scan-perl-prereqs v' . (Perl::PrereqScanner->VERSION || 'DEV') . "\n") |
| 31 |
if $opt->version; |
| 32 |
print($usage->text) if $opt->help; |
| 33 |
exit if $opt->version or $opt->help; |
| 34 |
|
| 35 |
foreach my $file ( @ARGV ? @ARGV : '.' ) { |
| 36 |
-d $file ? scan_dir( $file ) : scan_file( $file ); |
| 37 |
} |
| 38 |
|
| 39 |
my %result; |
| 40 |
|
| 41 |
sub scan_dir { |
| 42 |
find( { |
| 43 |
no_chdir => 1, |
| 44 |
wanted => sub { scan_file($_) if /\.(pl|PL|pm|cgi|psgi|t)$/ && -f $_ } |
| 45 |
}, shift ); |
| 46 |
} |
| 47 |
|
| 48 |
sub scan_file { |
| 49 |
my $file = shift; |
| 50 |
|
| 51 |
my $key = $opt->by_file ? $file : ''; |
| 52 |
$result{ $key } ||= CPAN::Meta::Requirements->new; |
| 53 |
|
| 54 |
$result{ $key }->add_requirements( |
| 55 |
Perl::PrereqScanner->new->scan_file($file) |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
for my $file (sort keys %result) { |
| 60 |
print "## $file\n" if length $file and keys(%result) > 1; |
| 61 |
print_prereqs($result{$file}); |
| 62 |
} |
| 63 |
|
| 64 |
sub print_prereqs { |
| 65 |
my $prereqs = shift->as_string_hash; |
| 66 |
|
| 67 |
if( $opt->pretty ) { |
| 68 |
my $max = max map { length } keys %$prereqs; |
| 69 |
printf( "%-${max}s = %s\n", $_, $prereqs->{$_} ) |
| 70 |
for sort keys %$prereqs; |
| 71 |
} else { |
| 72 |
### FIXME grossly insufficient filtering, should refactor/backport |
| 73 |
### filtering from Dist::Zilla::Plugin::AutoPrereqs or something |
| 74 |
foreach ( sort keys %$prereqs ) { |
| 75 |
next if $_ eq 'perl'; |
| 76 |
my $v = $prereqs->{$_} || 0; |
| 77 |
$v = qq{"$v"} unless looks_like_number($v); |
| 78 |
print($v eq '0' ? qq{$_\n} : qq{$_~$v\n}); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
exit; |
| 84 |
|
| 85 |
#pod =head1 SYNOPSIS |
| 86 |
#pod |
| 87 |
#pod scan-perl-prereqs [--by-file] [--pretty] [DIRS | FILES] |
| 88 |
#pod |
| 89 |
#pod Directories are traversed with L<File::Find> to collect all C<.pl>, C<.pm>, |
| 90 |
#pod C<.psgi> and C<.t> files. If no directories or files are specified, the current |
| 91 |
#pod working directory is scanned. |
| 92 |
#pod |
| 93 |
#pod The default is to print a single combined list suitable for piping to C<cpanm> |
| 94 |
#pod or similar tools. |
| 95 |
#pod |
| 96 |
#pod The C<--by-file> switch will print results grouped by the files in which they |
| 97 |
#pod were found. |
| 98 |
#pod |
| 99 |
#pod The C<--pretty> swith will print results in a more human-friendly format, with |
| 100 |
#pod names and versions vertically aligned. |
| 101 |
#pod |
| 102 |
#pod =cut |
| 103 |
|
| 104 |
__END__ |
| 105 |
|
| 106 |
=pod |
| 107 |
|
| 108 |
=encoding UTF-8 |
| 109 |
|
| 110 |
=head1 NAME |
| 111 |
|
| 112 |
Perl::PrereqScanner::App - scan your working dir for likely prereqs |
| 113 |
|
| 114 |
=head1 VERSION |
| 115 |
|
| 116 |
version 1.100 |
| 117 |
|
| 118 |
=head1 SYNOPSIS |
| 119 |
|
| 120 |
scan-perl-prereqs [--by-file] [--pretty] [DIRS | FILES] |
| 121 |
|
| 122 |
Directories are traversed with L<File::Find> to collect all C<.pl>, C<.pm>, |
| 123 |
C<.psgi> and C<.t> files. If no directories or files are specified, the current |
| 124 |
working directory is scanned. |
| 125 |
|
| 126 |
The default is to print a single combined list suitable for piping to C<cpanm> |
| 127 |
or similar tools. |
| 128 |
|
| 129 |
The C<--by-file> switch will print results grouped by the files in which they |
| 130 |
were found. |
| 131 |
|
| 132 |
The C<--pretty> swith will print results in a more human-friendly format, with |
| 133 |
names and versions vertically aligned. |
| 134 |
|
| 135 |
=head1 PERL VERSION |
| 136 |
|
| 137 |
This library should run on perls released even a long time ago. It should work |
| 138 |
on any version of perl released in the last five years. |
| 139 |
|
| 140 |
Although it may work on older versions of perl, no guarantee is made that the |
| 141 |
minimum required version will not be increased. The version may be increased |
| 142 |
for any reason, and there is no promise that patches will be accepted to lower |
| 143 |
the minimum required perl. |
| 144 |
|
| 145 |
=head1 AUTHORS |
| 146 |
|
| 147 |
=over 4 |
| 148 |
|
| 149 |
=item * |
| 150 |
|
| 151 |
Jerome Quelin |
| 152 |
|
| 153 |
=item * |
| 154 |
|
| 155 |
Ricardo Signes <[email protected]> |
| 156 |
|
| 157 |
=back |
| 158 |
|
| 159 |
=head1 COPYRIGHT AND LICENSE |
| 160 |
|
| 161 |
This software is copyright (c) 2009 by Jerome Quelin. |
| 162 |
|
| 163 |
This is free software; you can redistribute it and/or modify it under |
| 164 |
the same terms as the Perl 5 programming language system itself. |
| 165 |
|
| 166 |
=cut |