| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
package perlver; |
| 4 |
$perlver::VERSION = '1.44'; |
| 5 |
=pod |
| 6 |
|
| 7 |
=head1 NAME |
| 8 |
|
| 9 |
perlver - The Perl Minimum Version Analyzer |
| 10 |
|
| 11 |
=head1 SYNOPSIS |
| 12 |
|
| 13 |
adam@red:~$ perlver Perl-MinimumVersion |
| 14 |
Found directory '.' |
| 15 |
Searching for Perl files... found 3 file(s) |
| 16 |
Scanning lib/Perl/MinimumVersion.pm... done |
| 17 |
Scanning t/01_compile.t... done |
| 18 |
Scanning t/02_main.t... done |
| 19 |
|
| 20 |
--------------------------------------------------------- |
| 21 |
| file | explicit | syntax | external | |
| 22 |
| --------------------------------------------------------- | |
| 23 |
| lib/Perl/MinimumVersion.pm | 5.005 | ~ | n/a | |
| 24 |
| t/01_compile.t | ~ | ~ | n/a | |
| 25 |
| t/02_main.t | ~ | ~ | n/a | |
| 26 |
--------------------------------------------------------- |
| 27 |
|
| 28 |
Minimum version of Perl required: ... |
| 29 |
|
| 30 |
adam@red:~$ |
| 31 |
|
| 32 |
=head1 DESCRIPTION |
| 33 |
|
| 34 |
C<perlver> is a console script created to provide convenient access to the |
| 35 |
functionality provided by L<Perl::MinimumVersion>. |
| 36 |
|
| 37 |
--blame option shows code which requires this version of perl |
| 38 |
|
| 39 |
The synopsis above pretty much covers all you need to know at this point. |
| 40 |
|
| 41 |
=cut |
| 42 |
|
| 43 |
use 5.005; |
| 44 |
use strict; |
| 45 |
use version 'qv'; |
| 46 |
use File::Spec (); |
| 47 |
use Getopt::Long 'GetOptions'; |
| 48 |
use Params::Util '_INSTANCE'; |
| 49 |
use File::Find::Rule (); |
| 50 |
use File::Find::Rule::Perl (); |
| 51 |
use Perl::MinimumVersion 'PMV'; |
| 52 |
|
| 53 |
# Define prototypes |
| 54 |
sub verbose ($); |
| 55 |
sub message ($); |
| 56 |
sub error (@); |
| 57 |
sub format_version ($); |
| 58 |
sub dist ($); |
| 59 |
|
| 60 |
use vars qw{$VERBOSE $BLAME $EXPLAIN}; |
| 61 |
BEGIN { |
| 62 |
|
| 63 |
# Configuration globals |
| 64 |
$VERBOSE = ''; |
| 65 |
$BLAME = ''; |
| 66 |
$EXPLAIN = ''; |
| 67 |
|
| 68 |
# Unbuffer output |
| 69 |
$| = 1; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
##################################################################### |
| 77 |
# Configuration |
| 78 |
|
| 79 |
GetOptions( |
| 80 |
verbose => \$VERBOSE, |
| 81 |
blame => \$BLAME, |
| 82 |
explain => \$EXPLAIN, |
| 83 |
); |
| 84 |
|
| 85 |
# Get the target |
| 86 |
my $target = shift @ARGV; |
| 87 |
unless ( $target ) { |
| 88 |
error("You did not provide a file or directory to check"); |
| 89 |
} |
| 90 |
|
| 91 |
print "\n"; |
| 92 |
if ( $BLAME ) { |
| 93 |
blame($target); |
| 94 |
} else { |
| 95 |
summary($target); |
| 96 |
} |
| 97 |
|
| 98 |
exit(0); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
##################################################################### |
| 105 |
# Regular Mode |
| 106 |
|
| 107 |
sub summary { |
| 108 |
my $target = shift; |
| 109 |
my @files = (); |
| 110 |
if ( -d $target ) { |
| 111 |
verbose "Found directory '$target'\n"; |
| 112 |
verbose "Searching for Perl files... "; |
| 113 |
@files = find($target); |
| 114 |
verbose "found " . scalar(@files) . " file(s)\n"; |
| 115 |
} elsif ( -f $target ) { |
| 116 |
verbose "Found file '$target'\n"; |
| 117 |
@files = $target; |
| 118 |
} else { |
| 119 |
error "File or directory '$target' does not exist"; |
| 120 |
} |
| 121 |
|
| 122 |
# Scan the files |
| 123 |
verbose "Processing files...\n"; |
| 124 |
my @results = (); |
| 125 |
my $file_len = 12 + List::Util::max map { length $_ } @files; |
| 126 |
foreach my $file ( @files ) { |
| 127 |
# Set up the results data |
| 128 |
verbose sprintf("%-${file_len}s", "Scanning $file..."); |
| 129 |
my $result = [ $file, undef, undef ]; |
| 130 |
push @results, $result; |
| 131 |
|
| 132 |
# Load the document standalone first so we store the file name |
| 133 |
my $document = PPI::Document::File->new( |
| 134 |
$file, |
| 135 |
readonly => 1, |
| 136 |
); |
| 137 |
unless ( $document ) { |
| 138 |
verbose "[error]\n"; |
| 139 |
next; |
| 140 |
} |
| 141 |
|
| 142 |
# Create the version checker |
| 143 |
my $pmv = PMV->new( $document ); |
| 144 |
unless ( $pmv ) { |
| 145 |
verbose "[error]\n"; |
| 146 |
next; |
| 147 |
} |
| 148 |
|
| 149 |
# Check the explicit version |
| 150 |
$result->[1] = $pmv->minimum_explicit_version; |
| 151 |
|
| 152 |
# Check the syntax version |
| 153 |
$result->[2] = $pmv->minimum_syntax_version; |
| 154 |
|
| 155 |
$result->[4] = $pmv->{syntax_check_failed} if exists $pmv->{syntax_check_failed}; |
| 156 |
|
| 157 |
verbose "[ok]\n"; |
| 158 |
} |
| 159 |
|
| 160 |
# Calculate the minimum explicit, syntax and total versions |
| 161 |
verbose "Compiling results...\n"; |
| 162 |
my $pmv_explicit = PMV->_max( map { $_->[1] } @results ); |
| 163 |
my $pmv_syntax = PMV->_max( map { $_->[2] } @results ); |
| 164 |
my $pmv_bug = !! ($pmv_explicit and $pmv_syntax and $pmv_syntax > $pmv_explicit); |
| 165 |
my $pmv_total = PMV->_max( $pmv_explicit, $pmv_syntax ); |
| 166 |
|
| 167 |
# Generate the output values |
| 168 |
my @outputs = ( [ 'file', 'explicit', 'syntax', 'external' ] ); |
| 169 |
foreach my $result ( @results ) { |
| 170 |
my $output = []; |
| 171 |
$output->[0] = $result->[0]; |
| 172 |
$output->[1] = format_version($result->[1]); |
| 173 |
$output->[2] = format_version($result->[2]); |
| 174 |
# $output->[3] = format_version($result->[3]); |
| 175 |
$output->[3] = 'n/a'; |
| 176 |
$output->[4] = $result->[4] || '' if ($EXPLAIN); |
| 177 |
push @outputs, $output; |
| 178 |
} |
| 179 |
|
| 180 |
# Complete the output preperation work |
| 181 |
$pmv_explicit = format_version( $pmv_explicit ); |
| 182 |
$pmv_syntax = format_version( $pmv_syntax ); |
| 183 |
$pmv_total = format_version( $pmv_total ); |
| 184 |
if ( $pmv_total eq '~' ) { |
| 185 |
$pmv_total = format_version( qv(5.004) ) . ' (default)'; |
| 186 |
} |
| 187 |
my $len0 = List::Util::max map { length $_->[0] } @outputs; |
| 188 |
my $len1 = List::Util::max map { length $_->[1] } @outputs; |
| 189 |
my $len2 = List::Util::max map { length $_->[2] } @outputs; |
| 190 |
my $len3 = List::Util::max map { length $_->[3] } @outputs; |
| 191 |
my $len_all = $len0 + $len1 + $len2 + $len3 + 9; |
| 192 |
my $len_totals = $len1 + $len2 + $len3 + 6; |
| 193 |
|
| 194 |
my $error_message = "ERROR DETECTED : ACTUAL DEPENDENCY HIGHER THAN SPECIFIED"; |
| 195 |
my $error_length = length $error_message; |
| 196 |
if ( $error_length > $len_all ) { |
| 197 |
my $diff = $error_length - $len_all; |
| 198 |
$len_all += $diff; |
| 199 |
$len0 += $diff; |
| 200 |
} |
| 201 |
|
| 202 |
# Prepare formatting parts |
| 203 |
my $line_format = " | %-${len0}s | %-${len1}s | %-${len2}s | %-${len3}s |\n"; |
| 204 |
if ($EXPLAIN) { |
| 205 |
chomp($line_format); |
| 206 |
$line_format.=" (%s)\n"; |
| 207 |
} |
| 208 |
my $spacer = '-' x $len_all; |
| 209 |
my $divider = " | $spacer |\n"; |
| 210 |
my $capline = " $spacer \n"; |
| 211 |
my $rowline = " | %-${len_all}s |\n"; |
| 212 |
|
| 213 |
# Print the results |
| 214 |
print "\n"; |
| 215 |
print $capline; |
| 216 |
printf( $line_format, @{shift(@outputs)} ); |
| 217 |
print $divider; |
| 218 |
foreach my $result ( @outputs ) { |
| 219 |
printf( $line_format, @$result ); |
| 220 |
} |
| 221 |
print $divider; |
| 222 |
printf( $rowline, "Minimum explicit version : $pmv_explicit" ); |
| 223 |
printf( $rowline, "Minimum syntax version : $pmv_syntax" ); |
| 224 |
printf( $rowline, "Minimum version of perl : $pmv_total" ); |
| 225 |
if ( $pmv_bug ) { |
| 226 |
print $divider; |
| 227 |
printf( $rowline, "ERROR : ACTUAL DEPENDENCY HIGHER THAN SPECIFIED" ); |
| 228 |
printf( $rowline, "DETAILS : perlver --blame $target" ); |
| 229 |
} |
| 230 |
print $capline; |
| 231 |
print "\n"; |
| 232 |
} |
| 233 |
|
| 234 |
sub blame { |
| 235 |
my $target = shift; |
| 236 |
my @files = (); |
| 237 |
if ( -d $target ) { |
| 238 |
verbose "Found directory '$target'\n"; |
| 239 |
verbose "Searching for Perl files... "; |
| 240 |
@files = find($target); |
| 241 |
verbose "found " . scalar(@files) . " file(s)\n"; |
| 242 |
} elsif ( -f $target ) { |
| 243 |
verbose "Found file '$target'\n"; |
| 244 |
@files = $target; |
| 245 |
} else { |
| 246 |
error "File or directory '$target' does not exist"; |
| 247 |
} |
| 248 |
|
| 249 |
# Scan the files |
| 250 |
verbose "Processing files...\n"; |
| 251 |
my $maximum = undef; |
| 252 |
my $blame = undef; |
| 253 |
my $file_len = 12 + List::Util::max map { length $_ } @files; |
| 254 |
my $max = undef; |
| 255 |
my $maxpmv = undef; |
| 256 |
foreach my $file ( @files ) { |
| 257 |
# Set up the results data |
| 258 |
verbose sprintf("%-${file_len}s", "Scanning $file..."); |
| 259 |
|
| 260 |
# Load the document standalone first so we store the file name |
| 261 |
my $document = PPI::Document::File->new( |
| 262 |
$file, |
| 263 |
readonly => 1, |
| 264 |
); |
| 265 |
unless ( $document ) { |
| 266 |
verbose "[error]\n"; |
| 267 |
next; |
| 268 |
} |
| 269 |
|
| 270 |
# Create the version checker |
| 271 |
my $pmv = PMV->new( $document ); |
| 272 |
unless ( $pmv ) { |
| 273 |
verbose "[error]\n"; |
| 274 |
next; |
| 275 |
} |
| 276 |
|
| 277 |
# Check the syntax version |
| 278 |
my $reason = $pmv->minimum_syntax_reason( $max ? $max->version : undef ); |
| 279 |
if ( $reason ) { |
| 280 |
verbose $reason->version . "\n"; |
| 281 |
} else { |
| 282 |
verbose "~\n"; |
| 283 |
next; |
| 284 |
} |
| 285 |
|
| 286 |
# Handle the first successful case |
| 287 |
if ( ! $max or $reason->version > $max->version ) { |
| 288 |
$max = $reason; |
| 289 |
$maxpmv = $pmv; |
| 290 |
next; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
# Anything? |
| 295 |
unless ( $max ) { |
| 296 |
print "Nothing obvious to blame\n"; |
| 297 |
exit(0); |
| 298 |
} |
| 299 |
|
| 300 |
# Index and prepare |
| 301 |
my $element = $max->element or die "Reason element unknown"; |
| 302 |
my $document = $element->top or die "Reason document unknown"; |
| 303 |
$document->index_locations; |
| 304 |
|
| 305 |
# Generate the location message |
| 306 |
my $file = $document->filename; |
| 307 |
my $line = $element->line_number; |
| 308 |
my $char = $element->column_number; |
| 309 |
my $content = $element->content; |
| 310 |
my $rule = $max->rule; |
| 311 |
my $version = $max->version; |
| 312 |
print " ------------------------------------------------------------\n"; |
| 313 |
print " File : $file\n"; |
| 314 |
print " Line : $line\n"; |
| 315 |
print " Char : $char\n"; |
| 316 |
print " Rule : $rule\n"; |
| 317 |
print " Version : $version\n"; |
| 318 |
print " ------------------------------------------------------------\n"; |
| 319 |
print " $content\n"; |
| 320 |
print " ------------------------------------------------------------\n"; |
| 321 |
} |
| 322 |
|
| 323 |
sub find { |
| 324 |
my $dir = shift; |
| 325 |
my $perl = File::Find::Rule->perl_file; |
| 326 |
my $build = File::Find::Rule->name('blib', '_build')->directory; |
| 327 |
return dist($dir) |
| 328 |
? File::Find::Rule->any( |
| 329 |
$build->prune->discard, |
| 330 |
$perl, |
| 331 |
)->in( $dir ) |
| 332 |
: $perl->in( $dir ); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
##################################################################### |
| 340 |
# Support Functions |
| 341 |
|
| 342 |
sub verbose ($) { |
| 343 |
return 1 unless $VERBOSE; |
| 344 |
print ' ' . $_[0]; |
| 345 |
} |
| 346 |
|
| 347 |
sub message ($) { |
| 348 |
print ' ' . $_[0]; |
| 349 |
} |
| 350 |
|
| 351 |
sub error (@) { |
| 352 |
print ' ' . join '', map { "$_\n" } ('', @_, ''); |
| 353 |
exit(255); |
| 354 |
} |
| 355 |
|
| 356 |
sub format_version ($) { |
| 357 |
my $version = shift; |
| 358 |
if ( _INSTANCE($version, 'Perl::MinimumVersion::Reason') ) { |
| 359 |
$version = $version->version->normal; |
| 360 |
} elsif ( _INSTANCE($version, 'version') ) { |
| 361 |
return $version->normal; |
| 362 |
} elsif ( $version ) { |
| 363 |
return "$version"; |
| 364 |
} elsif ( defined $version ) { |
| 365 |
return '~'; |
| 366 |
} else { |
| 367 |
return 'undef'; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
sub format_reason ($) { |
| 372 |
my $reason = shift; |
| 373 |
|
| 374 |
# Index the document of the worse offender |
| 375 |
my $element = $reason->element; |
| 376 |
my $document = $element->top; |
| 377 |
$document->index_locations; |
| 378 |
|
| 379 |
$DB::single = 1; |
| 380 |
|
| 381 |
1; |
| 382 |
} |
| 383 |
|
| 384 |
sub dist ($) { |
| 385 |
my $dir = shift; |
| 386 |
if ( -f File::Spec->catfile($dir, 'Makefile.PL') ) { |
| 387 |
return 1; |
| 388 |
} |
| 389 |
if ( -f File::Spec->catfile($dir, 'Build.PL') ) { |
| 390 |
return 1; |
| 391 |
} |
| 392 |
return ''; |
| 393 |
} |
| 394 |
|
| 395 |
=pod |
| 396 |
|
| 397 |
=head1 TO DO |
| 398 |
|
| 399 |
- Add PPI::Cache integration |
| 400 |
|
| 401 |
- Add PPI::Metrics integration (once it exists) |
| 402 |
|
| 403 |
- Add some sort of parseable output |
| 404 |
|
| 405 |
=head1 SUPPORT |
| 406 |
|
| 407 |
All bugs should be filed via the bug tracker at |
| 408 |
|
| 409 |
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Perl-MinimumVersion> |
| 410 |
|
| 411 |
For other issues, or commercial enhancement and support, contact the author |
| 412 |
|
| 413 |
=head1 AUTHORS |
| 414 |
|
| 415 |
Adam Kennedy <[email protected]> |
| 416 |
|
| 417 |
=head1 SEE ALSO |
| 418 |
|
| 419 |
L<PPI>, L<Perl::MinimumVersion> |
| 420 |
|
| 421 |
=head1 COPYRIGHT |
| 422 |
|
| 423 |
Copyright 2005 - 2012 Adam Kennedy. |
| 424 |
|
| 425 |
This program is free software; you can redistribute |
| 426 |
it and/or modify it under the same terms as Perl itself. |
| 427 |
|
| 428 |
The full text of the license can be found in the |
| 429 |
LICENSE file included with this module. |
| 430 |
|
| 431 |
=cut |