Skip to content

Commit

Permalink
Item15203: improved detection of module versions
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDaum committed Jul 14, 2023
1 parent fcf1c86 commit 65bed44
Showing 1 changed file with 61 additions and 49 deletions.
110 changes: 61 additions & 49 deletions core/lib/Foswiki/Configure/Dependency.pm
Expand Up @@ -18,6 +18,7 @@ use strict;
use warnings;

use version 0.77;
use Safe;

use Assert;

Expand Down Expand Up @@ -777,67 +778,78 @@ sub extractModuleVersion {
my $mod_version = '0';
my $mod_release = '0';

my $filePath;
foreach my $dir (@INC) {
open( my $mf, '<', "$dir/$file" ) or next;
local $/ = "\n";
local $_;
my $pod;
while (<$mf>) {
chomp;
if (/^=cut/) {
$pod = 0;
next;
}
if (/^=/) {
$pod = 1;
next;
}
next if ($pod);
next if m/eval/; # Some modules issue $VERSION = eval $VERSION ... bypass that line
s/\s*#.*$//;
if ($FoswikiPM) {
last if ( $mod_version && $mod_release );
if (/^\s*(?:our\s+)?\$(?:\w*::)*VERSION\s*=~\s*(.*?);/) {
my $exp = $1;
$exp =~ s/\$RELEASE/\$mod_release/g;
eval("\$mod_version =~ $exp;");
print STDERR
if ( -e "$dir/$file" ) {
$filePath = "$dir/$file";
last;
}
}
return 0 unless defined $filePath;

my $safe = Safe->new;
open( my $mf, '<', $filePath ) or return 0;
local $/ = "\n";
local $_;
my $pod;
while (<$mf>) {
chomp;
if (/^=cut/) {
$pod = 0;
next;
}
if (/^=/) {
$pod = 1;
next;
}
next if ($pod);
next if m/eval/; # Some modules issue $VERSION = eval $VERSION ... bypass that line
s/\s*#.*$//;
if ($FoswikiPM) {
last if ( $mod_version && $mod_release );
if (/^\s*(?:our\s+)?\$(?:\w*::)*VERSION\s*=~\s*(.*?);/) {
my $exp = $1;
$exp =~ s/\$RELEASE/\$mod_release/g;
$mod_version = $safe->reval($exp);
print STDERR
"Dependency.pm 1-Failed to eval $1 from $_ in $file at line $.: $@\n"
if ($@);
last;
}
if ($@);
last;
}

if (
if (
/\$VERSION\s*=\s*version->(?:new|parse|declare)\s*\(\s*['"]([vV]?\d+\.\d+(?:\.\d+)?(?:_\d+)?)['"]\s*\)/
)
{
$mod_version = $1;
)
{
$mod_version = $1;
}
if (/\s*(?:our\s+)?\$(?:\w*::)*(RELEASE|VERSION)\s*=(?!~)\s*(.*);/)
{
if ( $1 eq 'RELEASE' ) {
$mod_release = $safe->reval($2);
}
if (
/\s*(?:our\s+)?\$(?:\w*::)*(RELEASE|VERSION)\s*=(?!~)\s*(.*);/
)
{
eval( "\$mod_" . lc($1) . " = $2;" );
print STDERR
"Dependency.pm 2-Failed to eval $2 from $_ in $file at line $.: $@\n"
if ($@);
next;
else {
$mod_version = $safe->reval($2);
}
print STDERR
"Dependency.pm 2-Failed to eval $2 from $_ in $file at line $.: $@\n"
if ($@);
next;
}
next
unless (
/\s*(?:our\s+)?\$(?:\w*::)*VERSION\s*=\s*(?:qv\()?(.*?)(?:\))?;/
);
eval("\$mod_version = $1;");

# die "Failed to eval $1 from $_ in $file at line $. $@\n" if( $@ ); # DEBUG
next;
}
if (/\s*(?:our\s+)?\$(?:\w*::)*VERSION\s*=\s*(?:qv\()?(.*?)(?:\))?;/) {
$mod_version = $safe->reval($1);
last;
}
if (/package $module (.*);/) {
$mod_version = $safe->reval($1);
last;
}
close $mf;
return ( 1, $mod_version, "$dir/$file", $mod_release );
}

close $mf;
return ( 1, $mod_version, $filePath, $mod_release ) if $mod_version;
return ( 0, undef );
}

Expand Down

0 comments on commit 65bed44

Please sign in to comment.