blib/lib/Java/Maven/Artifact/Version.pm

filter: Statementstmt 89.6% 174 / 194 Branchbran 79.7% 67 / 84 Conditioncond 71.4% 15 / 21 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 89.4% 51 / 57 Podpod 100% 2 / 2 totaltotal 85.4% 318 / 372 SCAR 40.5
CC
50
Cov
86%
_check_comparison_settings
20.0
_identify_qualifier
9.0
_compare_listitems
9.0
CRAP 57.4
↑ Summary
1 package Java::Maven::Artifact::Version;
2  
3 8 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 our @ISA = qw/Exporter/;
11 8 our @EXPORT_OK = qw/&version_parse &version_compare/;
12 =head1 NAME
13  
14 Java::Maven::Artifact::Version - a perl module for comparing Artifact versions exactly like Maven does.
15  
16 =head1 VERSION
17  
18 Version 1.00
19  
20 see L</MAVEN VERSION COMPATIBILITY>.
21  
22 =cut
23  
24 8 our $VERSION = '1.00';
25  
26 =head1 SYNOPSIS
27  
28 Note that this documentation is intended as a reference to the module.
29  
30 use Java::Maven::Artifact::Version qw/version_compare version_parse/;
31  
32 my $y = version_compare('1-alpha', '1-beta'); # $y = -1
33 my $x = version_compare('1.0', '1-0.alpha'); # $x = 0
34  
35 my $z = version_parse('1-1.2-alpha'); # $z = '(1,(1,2,alpha))'
36 my @l = version_parse('1-1.2-alpha'); # [1,[1,2,'alpha']]
37  
38 =head1 DESCRIPTION
39  
40 L<Apache Maven|http://maven.apache.org/> has a peculiar way to compare Artifact versions.
41 The aim of this module is to exactly reproduce this way in hope that it could be usefull to someone that wants to write utils like SCM hooks. It may quickly ensure an Artifact version respect a grow order without to have to install Java and Maven on the system in charge of this checking.
42  
43 The official Apache document that describes it is here L<http://docs.codehaus.org/display/MAVEN/Versioning>.
44 But don't blindly believe everything. Take the red pill, and I show you how deep the rabbit-hole goes.
45 Because there is a gap between the truth coded in C<org.apache.maven.artifact.versioning.ComparableVersion.java> that can be found L<here|https://github.com/apache/maven/blob/master/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/ComparableVersion.java> and that Maven official document.
46  
47 Lucky for you this module cares about the real comparison differences hard coded in C<ComparableVersion> and reproduces it.
48  
49 see L</FAQ> for details.
50  
51 =cut
52  
53 use constant {
54 8 _ALPHA => 'alpha',
55 _BETA => 'beta',
56 _DEBUG => 0,
57 _INTEGER_ITEM => 'integeritem',
58 _LIST_ITEM => 'listitem',
59 _MILESTONE => 'milestone',
60 _NULL_ITEM => 'nullitem',
61 _RC => 'rc',
62 _SNAPSHOT => 'snapshot',
63 _SP => 'sp',
64 _STRING_ITEM => 'stringitem',
65 _UNDEF => 'undef'
66 8 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 =head1 SUBROUTINES
69  
70 =cut
71  
72 # replace all following separators ('..', '--', '-.' or '.-') by .0.
73 # or replace leading separator by '0.'
74 # example : '-1..1' -> '0.1.0.1'
75 sub _append_zero {
76 203 ▶ join '.', map { $_ eq '' ? '0' : $_ } split /\-|\./, shift;
Subroutine _append_zero: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '' ? : 6 277 283
77 }
78  
79 sub _check_comparison_settings {
80 0 ▶ my ($settings) = @_;
Subroutine _check_comparison_settings: not called CC=4 SCAR=30.0
81 0 ▶ croak("'version' mandatory parameter is missing") if not exists $settings->{version};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $settings->{"version"} 0 0 0
82 0 ▶ carp("'max_depth' should be >= 0") if (exists $settings->{max_depth} && $settings->{max_depth} <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if exists $settings->{"max_depth"} and $settings->{"max_depth"} <= 0 0 0 0
Condition0%0/3
exists $settings->{"max_depth"} and $settings->{"max_depth"} <= 0
!ll&&!rl&&r
000
MC/DC0%0/2
exists $settings->{"max_depth"} and $settings->{"max_depth"} <= 0
exists $settings->{"max_depth"} $settings->{"max_depth"} <= 0
Truth table0%0/3
A and B
A= exists $settings->{"max_depth"}B= $settings->{"max_depth"} <= 0
ABresult
0X0
100
111
83 }
84  
85 sub _compare_integeritem_to {
86 75 ▶ my ($integeritem, $item, $depth) = @_;
Subroutine _compare_integeritem_to: called CC=1 SCAR=0.0
87 my $dispatch = {
88 &_NULL_ITEM => sub {
89 5 ▶ print("comparing $integeritem to nullitem\n") if (_DEBUG);
Subroutine __ANON__: called CC=2 SCAR=6.9
90 5 $$depth++;
91 5 ▶ $integeritem =~ m/^0+$/ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$integeritem =~ /^0+$/ ? : 1 4 5
92 },
93 &_LIST_ITEM => sub {
94 1 ▶ print("comparing $integeritem to listitem\n") if (_DEBUG);
Subroutine __ANON__: called CC=1 SCAR=0.0
95 1 1;
96 },
97 &_INTEGER_ITEM => sub {
98 64 ▶ print("comparing $integeritem to $item\n") if (_DEBUG);
Subroutine __ANON__: called CC=1 SCAR=0.0
99 64 $$depth++;
100 64 $integeritem <=> $item;
101 },
102 &_STRING_ITEM => sub {
103 5 ▶ print("comparing $integeritem to stringitem\n") if (_DEBUG);
Subroutine __ANON__: called CC=1 SCAR=0.0
104 5 1;
105 }
106 75 };
107 75 $dispatch->{_identify_item_type($item)}->();
108 }
109  
110 sub _compare_items {
111 126 ▶ my ($item1, $item2, $max_depth, $depth) = @_;
Subroutine _compare_items: called CC=1 SCAR=0.8
112 my $dispatch = {
113 &_NULL_ITEM => sub {
114 0 ▶ print("_compare_items(nullitem, ?)\n") if (_DEBUG);
Subroutine __ANON__: not called CC=2 SCAR=17.9
115 0 ▶ unless (defined($item2)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $item2) 0 0 0
116 0 $$depth++;
117 0 return 0 ;
118 }
119 0 _compare_items($item2, undef, $depth) * -1;
120 },
121 &_LIST_ITEM => sub {
122 8 ▶ print("_compare_items(listitem, ?)\n") if (_DEBUG);
Subroutine __ANON__: called CC=1 SCAR=0.0
123 8 _compare_listitem_to($item1, $item2, $max_depth, $depth);
124 },
125 &_INTEGER_ITEM => sub {
126 44 ▶ print("_compare_items(integeritem, ?)\n") if (_DEBUG);
Subroutine __ANON__: called CC=1 SCAR=0.0
127 44 _compare_integeritem_to($item1, $item2, $depth);
128 },
129 &_STRING_ITEM => sub {
130 17 ▶ print("_compare_items(stringitem, ?)\n") if (_DEBUG);
Subroutine __ANON__: called CC=1 SCAR=0.0
131 17 _compare_stringitem_to($item1, $item2, $depth);
132 }
133 126 };
134 126 $dispatch->{_identify_item_type($item1)}->();
135 }
136  
137 sub _compare_listitem_to {
138 33 ▶ my ($listitem, $item, $max_depth, $depth) = @_;
Subroutine _compare_listitem_to: called CC=1 SCAR=0.0
139 my $dispatch = {
140 1 ▶ &_NULL_ITEM => sub { _compare_listitem_to_nullitem($listitem, $max_depth, $depth) },
Subroutine __ANON__: called CC=1 SCAR=0.0
141 6 ▶ &_LIST_ITEM => sub { _compare_listitems($listitem, $item, $max_depth, $depth) },
Subroutine __ANON__: called CC=1 SCAR=0.0
142 0 ▶ &_INTEGER_ITEM => sub { -1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 2 ▶ &_STRING_ITEM => sub { 1 }
Subroutine __ANON__: called CC=1 SCAR=0.0
144 33 };
145 33 $dispatch->{_identify_item_type($item)}->();
146 }
147  
148 sub _compare_listitem_to_nullitem {
149 3 ▶ my ($listitem, $max_depth, $depth) = @_;
Subroutine _compare_listitem_to_nullitem: called CC=2 SCAR=8.4
150 3 ▶ if (not @$listitem) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$listitem) { } 0 3 3
151 0 warn("comparing listitem with empty listitem should never occur. Check your code boy...");
152 0 0; #empty listitem (theoricaly impossible) equals null item
153 } else {
154 #only compare first element with null item (yes they did that...)
155 3 _compare_items(@$listitem[0], undef, $max_depth, $depth);
156 }
157 }
158  
159 sub _compare_listitems {
160 70 ▶ my ($list1, $list2, $max_depth, $depth) = @_;
Subroutine _compare_listitems: called CC=9 SCAR=22.0
161 70 my @l = @$list1;
162 70 my @r = @$list2;
163 70 ▶ while (@l || @r) {
Branch: 1 branch
BranchTrueFalseTotal
if (@l or @r) 128 10 138
Condition100%3/3
@l or @r
l!l&&r!l&&!r
126210
MC/DC100%2/2
@l or @r
@l @r
Truth table100%3/3
A or B
A= @lB= @r
ABresult
1X1
011
000
164 128 ▶ last if ($max_depth && $$depth >= $max_depth);
Branch: 1 branch
BranchTrueFalseTotal
if $max_depth and $$depth >= $max_depth 5 123 128
Condition100%3/3
$max_depth and $$depth >= $max_depth
!ll&&!rl&&r
89345
MC/DC100%2/2
$max_depth and $$depth >= $max_depth
$max_depth $$depth >= $max_depth
Truth table100%3/3
A and B
A= $max_depthB= $$depth >= $max_depth
ABresult
0X0
100
111
165 123 ▶ my $li = @l ? shift(@l) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@l ? : 121 2 123
166 123 ▶ my $ri = @r ? shift(@r) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@r ? : 115 8 123
167 123 ▶ my $c = defined($li) ? _compare_items($li, $ri, $max_depth, $depth) : _compare_items($ri, $li, $max_depth, $depth) * -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $li ? : 121 2 123
168 123 print("depth is $$depth\n") if (_DEBUG);
169 123 ▶ $c and return $c;
Branch: 1 branch
BranchTrueFalseTotal
$c and return $c 55 68 123
170 }
171 15 0;
172 }
173  
174 sub _compare_stringitem_to {
175 20 ▶ my ($stringitem, $item , $max_depth, $depth) = @_;
Subroutine _compare_stringitem_to: called CC=1 SCAR=0.0
176 my $dispatch = {
177 5 ▶ &_NULL_ITEM => sub { _compare_stringitem_to_stringitem($stringitem, $item, $depth) },
Subroutine __ANON__: called CC=1 SCAR=0.0
178 1 ▶ &_LIST_ITEM => sub { _compare_listitem_to($item, $stringitem, $max_depth, $depth) * -1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
179 1 ▶ &_INTEGER_ITEM => sub { _compare_integeritem_to($item, $stringitem, $depth) * -1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
180 13 ▶ &_STRING_ITEM => sub { _compare_stringitem_to_stringitem($stringitem, $item, $depth) }
Subroutine __ANON__: called CC=1 SCAR=0.0
181 20 };
182 20 $dispatch->{_identify_item_type($item)}->();
183 }
184  
185 sub _compare_stringitem_to_stringitem {
186 18 ▶ my ($stringitem1, $stringitem2, $depth) = @_;
Subroutine _compare_stringitem_to_stringitem: called CC=1 SCAR=0.0
187 18 $$depth++;
188 18 _substitute_to_qualifier($stringitem1) cmp _substitute_to_qualifier($stringitem2);
189 }
190  
191 sub _compare_to_mvn_version {
192 0 ▶ my ($this, $another_version, $max_depth) = @_;
Subroutine _compare_to_mvn_version: not called CC=2 SCAR=17.9
193 0 ▶ die("parameter is not a Java::Maven::Artifact::Version") unless ($another_version->isa('Java::Maven::Artifact::Version'));
Branch: 1 branch
BranchTrueFalseTotal
unless $another_version->isa("Java::Maven::Artifact::Version") 0 0 0
194 0 my $depth = 0;
195 0 _compare_listitems($this->{items}, $another_version->{items}, $max_depth, \$depth);
196 }
197  
198 sub _get_version {
199 0 ▶ my ($version) = @_;
Subroutine _get_version: not called CC=2 SCAR=17.9
200 0 ▶ ref($version) eq 'Java::Maven::Artifact::Version' ? $version : Java::Maven::Artifact::Version->new(version => $version);
Branch: 1 branch
BranchTrueFalseTotal
ref $version eq 'Java::Maven::Artifact::Version' ? : 0 0 0
201 }
202  
203 sub _getref {
204 482 ▶ my ($var) = @_;
Subroutine _getref: called CC=3 SCAR=11.1
205 482 ▶ (ref($var) || not defined($var)) ? $var : \$var; # var may already be a ref
Branch: 1 branch
BranchTrueFalseTotal
ref $var || !defined($var) ? : 120 362 482
Condition66%2/3
ref $var or not defined $var
l!l&&r!l&&!r
1200362
MC/DC50%1/2
ref $var or not defined $var
ref $var not defined $var
Truth table66%2/3
A or not(B)
A= ref $varB= not defined $var
ABresult
1X1
011
000
206 }
207  
208 sub _identify_item_type {
209 254 ▶ my ($item) = @_;
Subroutine _identify_item_type: called CC=2 SCAR=7.1
210 my $types = {
211 13 ▶ _UNDEF() => sub { _NULL_ITEM },
Subroutine __ANON__: called CC=1 SCAR=0.0
212 181 ▶ 'SCALAR' => sub { _identify_scalar_item_type($item) },
Subroutine __ANON__: called CC=1 SCAR=0.0
213 60 ▶ 'ARRAY' => sub { _LIST_ITEM },
Subroutine __ANON__: called CC=1 SCAR=0.0
214 0 ▶ _DEFAULT_ => sub { die "unable to identify item type of item $item ." }
Subroutine __ANON__: not called CC=1 SCAR=6.9
215 254 };
216 254 my $t = _reftype($item);
217 254 print("_identify_item_type($t)\n") if (_DEBUG);
218 254 ▶ exists $types->{$t} ? $types->{$t}->() : $types->{_DEFAULT_}->();
Branch: 1 branch
BranchTrueFalseTotal
exists $types->{$t} ? : 254 0 254
219 }
220  
221 sub _identify_qualifier {
222 36 ▶ my ($stringitem) = @_;
Subroutine _identify_qualifier: called CC=9 SCAR=22.0
223 36 ▶ return _NULL_ITEM unless defined($stringitem);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stringitem 5 31 36
224 31 ▶ return _ALPHA if $stringitem =~ m/^(alpha|a\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $stringitem =~ /^(alpha|a\d+)$/ 3 28 31
225 28 ▶ return _BETA if $stringitem =~ m/^(beta|b\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $stringitem =~ /^(beta|b\d+)$/ 3 25 28
226 25 ▶ return _MILESTONE if $stringitem =~ m/^(milestone|m\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $stringitem =~ /^(milestone|m\d+)$/ 4 21 25
227 21 ▶ return _RC if $stringitem =~ m/^rc$/;
Branch: 1 branch
BranchTrueFalseTotal
if $stringitem =~ /^rc$/ 4 17 21
228 17 ▶ return _SNAPSHOT if $stringitem =~ m/^snapshot$/;
Branch: 1 branch
BranchTrueFalseTotal
if $stringitem =~ /^snapshot$/ 0 17 17
229 17 ▶ return _NULL_ITEM if $stringitem =~ m/^$/;
Branch: 1 branch
BranchTrueFalseTotal
if $stringitem =~ /^$/ 2 15 17
230 15 ▶ return _SP if $stringitem =~ m/^sp$/;
Branch: 1 branch
BranchTrueFalseTotal
if $stringitem =~ /^sp$/ 4 11 15
231 11 '_DEFAULT_';
232 }
233  
234 sub _identify_scalar_item_type {
235 181 ▶ my ($scalar) = @_;
Subroutine _identify_scalar_item_type: called CC=2 SCAR=6.9
236 181 ▶ $scalar =~ m/^\d+$/ ? _INTEGER_ITEM : _STRING_ITEM;
Branch: 1 branch
BranchTrueFalseTotal
$scalar =~ /^\d+$/ ? : 140 41 181
237 }
238  
239 sub _is_nullitem {
240 254 ▶ my ($item) = @_;
Subroutine _is_nullitem: called CC=2 SCAR=6.9
241 254 ▶ (not defined($item)) ? 1 : _UNDEF eq reftype(_getref($item));
Branch: 1 branch
BranchTrueFalseTotal
!defined($item) ? : 13 241 254
242 }
243  
244 sub _normalize {
245 382 ▶ my ($listitems) = @_;
Subroutine _normalize: called CC=6 SCAR=17.9
246 382 my $norm_sublist;
247 382 ▶ if (ref(@$listitems[-1]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref @$listitems[-1] eq "ARRAY") 71 311 382
248 71 my $sublist = pop(@$listitems);
249 71 $norm_sublist = _normalize($sublist);
250 }
251 382 ▶ pop(@$listitems) while (@$listitems && @$listitems[-1] =~ m/^(0+|ga|final)?$/ );
Branch: 1 branch
BranchTrueFalseTotal
@$listitems and @$listitems[-1] =~ /^(0+|ga|final)?$/ and do { pop @$listitems } 20 382 402
Condition100%3/3
@$listitems and @$listitems[-1] =~ /^(0+|ga|final)?$/
!ll&&!rl&&r
1536720
MC/DC100%2/2
@$listitems and @$listitems[-1] =~ /^(0+|ga|final)?$/
@$listitems @$listitems[-1] =~ /^(0+|ga|final)?$/
Truth table100%3/3
A and B
A= @$listitemsB= @$listitems[-1] =~ /^(0+|ga|final)?$/
ABresult
0X0
100
111
252 382 ▶ push(@$listitems, $norm_sublist) if (defined($norm_sublist) && @$norm_sublist);
Branch: 1 branch
BranchTrueFalseTotal
if defined $norm_sublist and @$norm_sublist 70 312 382
Condition100%3/3
defined $norm_sublist and @$norm_sublist
!ll&&!rl&&r
311170
MC/DC100%2/2
defined $norm_sublist and @$norm_sublist
defined $norm_sublist @$norm_sublist
Truth table100%3/3
A and B
A= defined $norm_sublistB= @$norm_sublist
ABresult
0X0
100
111
253 382 $listitems;
254 }
255  
256 sub _reftype {
257 254 ▶ my ($item) = @_;
Subroutine _reftype: called CC=2 SCAR=6.9
258 254 ▶ _is_nullitem($item) ? _UNDEF : reftype(_getref($item));
Branch: 1 branch
BranchTrueFalseTotal
_is_nullitem($item) ? : 13 241 254
259 }
260  
261 sub _replace_alias {
262 291 ▶ my ($string) = @_;
Subroutine _replace_alias: called CC=4 SCAR=15.2
263 291 ▶ if ($string eq '') {
Branch: 3 branches
BranchTrueFalseTotal
if ($string eq '') { } 0 291 291
elsif ($string =~ /^(ga|final)$/) { } 15 276 291
elsif ($string eq 'cr') { } 0 276 276
264 0 return 0;
265 } elsif ($string =~ m/^(ga|final)$/) {
266 15 return '';
267 } elsif ($string eq 'cr') {
268 0 return 'rc';
269 }
270 276 $string;
271 }
272  
273 sub _replace_special_aliases {
274 203 ▶ my ($string) = @_;
Subroutine _replace_special_aliases: called CC=1 SCAR=0.0
275 203 $string =~ s/((?:^)|(?:\.|\-))a(\d)/$1alpha.$2/g; # a1 = alpha.1
276 203 $string =~ s/((?:^)|(?:\.|\-))b(\d)/$1beta.$2/g; # b11 = beta.11
277 203 $string =~ s/((?:^)|(?:\.|\-))m(\d)/$1milestone.$2/g; # m7 = milestone.7
278 203 $string;
279 }
280  
281 # split 'xxx12' to ['xxx',12] and vice versa
282 sub _split_hybrid_items {
283 276 ▶ my ($string) = @_;
Subroutine _split_hybrid_items: called CC=1 SCAR=0.0
284 276 $string =~ s/(\D)(\d)/$1.$2/g;
285 276 $string =~ s/(\d)(\D)/$1.$2/g;
286 276 split /\./, $string;
287 }
288  
289 # _split_to_items must only be called when version has been splitted into listitems
290 # Then it works only on a single listitem
291 sub _split_to_items {
292 179 ▶ my ($string) = @_;
Subroutine _split_to_items: called CC=1 SCAR=0.0
293 179 my @items = ();
294 179 my @tonormalize = _split_to_to_normalize($string);
295 #at this time we must replace aliases with their values
296 my $closure = sub {
297 203 ▶ my ($i) = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
298 203 $i = _append_zero($i);
299 203 $i = _replace_special_aliases($i); #must be replaced BEFORE items splitting
300 203 my @xs = split(/\-|\./, $i);
301 203 @xs = map({ _replace_alias($_) } @xs); #must be replaced after items splitting
302 203 ▶ @xs = map({ $_ !~ /^\s*$/ ? _split_hybrid_items($_) : $_ } @xs);
Branch: 1 branch
BranchTrueFalseTotal
!($_ =~ /^\s*$/) ? : 276 15 291
303 203 push(@items, @{_normalize(\@xs)} );
304 179 };
305 179 map { $closure->($_) } @tonormalize;
306 179 @items;
307 }
308  
309 sub _split_to_lists {
310 179 ▶ my ($string, @items) = @_;
Subroutine _split_to_lists: called CC=2 SCAR=6.9
311 #listitems are created every encountered dash when there are a digits in front and after it
312 179 ▶ if (my ($a, $b) = ($string =~ m/(.*?\d)\-(\d.*)/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $string =~ /(.*?\d)\-(\d.*)/) { } 71 108 179
313 71 push(@items, _split_to_items($a), _split_to_lists($b, ()));
314 } else {
315 108 push(@items, _split_to_items($string));
316 }
317 179 \@items;
318 }
319  
320 #_normalize must be called each time a digit is followed by a dash
321 sub _split_to_to_normalize {
322 179 ▶ my ($string) = @_;
Subroutine _split_to_to_normalize: called CC=1 SCAR=0.0
323 179 $string =~ s#(\d)\-#$1</version>#g; # use '</version>' as seperator because it cannot be a part of an artifact version...
324 179 split('</version>', $string);
325 }
326  
327 sub _substitute_to_qualifier {
328 36 ▶ my ($stringitem) = @_;
Subroutine _substitute_to_qualifier: called CC=2 SCAR=6.9
329 36 ▶ my $qualifier_cmp_values = {
Branch: 1 branch
BranchTrueFalseTotal
$stringitem ? : 29 7 36
330 &_ALPHA => '0',
331 &_BETA => '1',
332 &_MILESTONE => '2',
333 &_RC => '3',
334 &_SNAPSHOT => '4',
335 &_NULL_ITEM => '5',
336 &_SP => '6',
337 _DEFAULT_ => $stringitem ? "7-$stringitem" : '7-' #yes they really did that in ComparableVersion...
338 };
339 36 $qualifier_cmp_values->{_identify_qualifier($stringitem)};
340 }
341  
342  
343 sub _to_normalized_string {
344 28 ▶ my ($items) = @_;
Subroutine _to_normalized_string: called CC=2 SCAR=6.9
345 28 my $s = '(';
346 my $append = sub {
347 76 ▶ my ($i) = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
348 76 ▶ ref($i) eq 'ARRAY' ? $s .= _to_normalized_string($i) : ($s .= "$i");
Branch: 1 branch
BranchTrueFalseTotal
ref $i eq 'ARRAY' ? : 8 68 76
349 76 $s .= ',';
350 28 };
351 28 map { $append->($_) } @$items ;
352 28 ▶ chop($s) if (length($s) > 1);
Branch: 1 branch
BranchTrueFalseTotal
if length $s > 1 27 1 28
353 28 $s .= ')';
354 }
355  
356 =head2 version_compare
357  
358 By default C<version_compare> compares a version string to another one exactly like Maven does.
359  
360 See L<http://docs.codehaus.org/display/MAVEN/Versioning> for general comparison description, and L</DESCRIPTION> for more details about mechanisms not described in that official Maven doc but occur during Maven Artifact versions comparison in Java.
361  
362 This function will return :
363  
364 =over 4
365  
366 =item * C<0> if versions compared are equal
367  
368 =item * C<1> if version is greater than version that is compared to
369  
370 =item * C<-1> if version is lower than version that is compared to
371  
372 =back
373  
374 $v = version_compare('1.0', '1.1'); # $v = -1
375  
376 C<version_compare> can go further. You can set C<max_depth> to stop comparison before the whole version comparison has processed.
377  
378 Suppose you have to code SCM hook which enforce that pushed artifact source must always begin by the same two version items and new version must be greater than the old one.
379  
380 my ($old, $new) = ('1.1.12', '1.1.13');
381 my $common = version_compare($old, $new, 2); # returns 0 here
382 die "you did not respect the version policy" if $common;
383 die "you must increment artifact version" if version_compare($old, $new) >= 0;
384  
385 Note that C<max_depth> cares about sub C<listitems>.
386
387 $v = '1-1.0.sp; # normalized to (1,(1,0,'sp'))
388 $o = '1-1-SNAPSHOT'; # normalized to (1,(1,'SNAPSHOT'))
389 $x = version_compare($v, $o, 3); # will compare '0' to 'SNAPSHOT' and will return 1
390  
391 Of course understand that this computation is done B<after> normalization.
392
393 $x = version_compare('1-1.0-1-ga-0-1.2', '1-1.0-1-ga-0-1.3', 4); #only last item will be ignored during this comparison
394 # ^ ^ ^ ^ ^ ^ ^ ^
395  
396 Note that set negative C<max_depth> will always return 0, because no comparison will be done at all
397  
398 $x = version_compare(1, 2, -1); # $x = 0
399  
400 =cut
401  
402 sub version_compare {
403 44 ▶ my ($v1, $v2, $max_depth) = @_;
Subroutine version_compare: called CC=4 SCAR=14.2
Pod documented
404 44 ▶ return unless defined($v1) || defined($v2);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v1 or defined $v2 0 44 44
Condition33%1/3
defined $v1 or defined $v2
l!l&&r!l&&!r
4400
MC/DC0%0/2
defined $v1 or defined $v2
defined $v1 defined $v2
Truth table33%1/3
A or B
A= defined $v1B= defined $v2
ABresult
1X1
011
000
405 44 ▶ $max_depth = defined $max_depth ? $max_depth : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $max_depth ? : 12 32 44
406 44 my $depth = 0;
407 44 my @listitem1 = version_parse($v1);
408 44 my @listitem2 = version_parse($v2);
409 44 _compare_listitems(\@listitem1, \@listitem2, $max_depth, \$depth);
410 }
411  
412 =head2 version_parse
413  
414 will return normalized version representation (see L</"Normalization">).
415  
416 In B<scalar context>, it will return string representation :
417  
418 $s = version_parse('1.0-final-1'); # $s = '(1,(,1))'
419  
420 You would have the same string if you had call C<org.apache.maven.artifact.versioning.ComparableVersion.ListItem.toString()> private method of C<org.apache.maven.artifact.versioning.ComparableVersion.java> on the main C<ListItem>.
421  
422 In B<list context>, it will return the data structure representation :
423  
424 @l = version_parse('1.0-final-1'); # [1,['',1]]
425  
426 =cut
427  
428 sub version_parse {
429 108 ▶ my ($v) = @_;
Subroutine version_parse: called CC=3 SCAR=11.0
Pod documented
430 108 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 108 108
431 108 my $listitem = _normalize(_split_to_lists(lc($v), ()));
432 108 ▶ wantarray ? @$listitem : _to_normalized_string($listitem);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 88 20 108
433 }
434  
435 =head1 FAQ
436  
437 =head2 What are differences between actual Maven comparison algo and that described in the official Maven doc ?
438  
439 =head3 zero appending on blank separator
440  
441 zero ('C<0>') will be appended on each blank separator char (dot '.' or dash '-')
442 During parsing if separator char is encountered and it was not preceded by C<stringitem> or C<listitem>, zero char ('C<0>') is automatically appended.
443 Then version that begins with separator is automatically prefixed by zero.
444  
445 'C<-1>' will be internally moved to 'C<0-1>'.
446  
447 'C<1....1>' will be internally moved to 'C<1.0.0.0.1>'.
448  
449 =head3 The dash separator "B<->"
450  
451 The dash separator "B<->" will create C<listitem> only if it is preceeded by an C<integeritem> and it is followed by digit.
452  
453 Then when they say C<1-alpha10-SNAPSHOT =E<gt> [1,["alpha",10,["SNAPSHOT"]]]> understand that it's wrong.
454  
455 C<1-alpha10-SNAPSHOT> is internally represented by C<[1,"alpha",10,"SNAPSHOT"]>. Which has a fully different comparison behavior because no sub C<listitem> is created.
456  
457 Please note that L</zero appending on blank separator> has been done B<after> C<listitem> splitting.
458  
459 Then understand that 'C<-1--1>' will B<NOT> be internally represented by 'C<(0,(1,(0,(1))>', but by 'C<(0,1,0,1)>'.
460  
461  
462 =head3 Normalization
463  
464 Normalization is one of the most important part of version comparison but it is not described at all in the official Maven document.
465 So what is I<normalization> ?
466 It's kind of reducing version components function.
467 Its aim is to shoot useless version components in artifact version. To simplify it, understand that C<1.0> must be internally represented by C<1> during comparison.
468 But I<normalization> appends in specific times during artifact version parsing.
469  
470 It appends:
471  
472 =over 4
473  
474 =item 1. each time a dash 'C<->' separator is preceded by digit but B<before> any alias substitution (except when any of these digits is a L<zero appended|/zero appending on blank separator>, because C<listitem> splitting is done before 'zero appending').
475  
476  
477 =item 2. at the end of each parsed C<listitem>, then B<after> all alias substitution
478  
479 =back
480  
481 And I<normalization> process current parsed C<listitem> from current position when normalization is called, back to the beginning of this current C<listitem>.
482  
483 Each encountered C<nullitem> will be shot until a non C<nullitem> is encountered or until the begining of this C<listitem> is reached if all its items are C<nullitems>.
484 In this last case precisely, the empty C<listitem> will be shot except if it is the main one.
485  
486 Then understand that :
487  
488 =over 4
489  
490 =item * C<1.0.alpha.0> becomes C<(1,0,alpha)> #because when main C<listitem> parsing has ended, I<normalization> has been called. Last item was 0, 0 is the C<nullitem> of C<integeritem>, then it has been shooted. Next last item was C<alpha> that is not C<nullitem> then normalization process stopped.
491  
492 =item * C<1.0-final-1> becomes C<(1,,1)> #because a dash has been encoutered during parsing. Then normalization has been called because it was preceded by a digit and last item in the current C<listitem> is 0. Then it has been shot. C<final> has been substituted by C<''> but when next normalization has been called, at the end of the parsing, the last item was not C<nullitem>, then normalization did not meet C<''>.
493  
494 =item * C<0.0.ga> becomes C<()> # because 'ga' has been substituted by C<''> and when C<listitem> has been normalized at the end, all items where C<nullitem>s
495  
496 =item * C<final-0.1 becomes> (,0,1) # because normalization has not been called after first dash because it was not been preceded by digit.
497  
498 =back
499  
500 If you told me I<WTF ?>, I would answer I am not responsible of drug consumption...
501  
502 In C<org.apache.maven.artifact.versioning.ComparableVersion.java>, the representation of normalized version is only displayable with the call of C<org.apache.maven.artifact.versioning.ComparableVersion.ListItem.toString()> private method on the main C<ListItem>.
503  
504 Comma "C<,>" is used as items separator, and enclosing braces are used to represent C<ListItem>.
505  
506 For example:
507 in Java world C<org.apache.maven.artifact.versioning.ComparableVersion.ListItem.toString()> on C<"1-0.1"> gives C<"(1,(0,1))">.
508  
509 L</version_parse> function reproduces this algo for the whole set C<Java::Maven::Artifact::Version>.
510  
511 $v = version_parse('1-0.1'); # $v = '(1,(O,1))'
512  
513 =head3 listitem and nullitem comparison
514  
515 It is not very clear in the official Maven doc.
516  
517 Comparing C<listitem> with C<nullitem> will just compare first C<item> of the C<listitem> with C<nullitem>.
518  
519 =head1 MAVEN VERSION COMPATIBILITY
520  
521 This version is fully compatible with the C<org.apache.maven.artifact.versioning.ComparableVersion.java> algo of C<org.apache.maven:maven-artifact:3.2.2> embedded with Maven 3.2.2
522  
523 All L<Test::More|http://search.cpan.org/~exodist/Test-Simple-1.001003/lib/Test/More.pm> tests are also available with Java Junit tests to ensure comparison results are similars.
524  
525 See L</SOURCE> if you want to check them.
526  
527 I will do my best to check the Maven compatibility on each Maven new release.
528  
529 =head1 AUTHOR
530  
531 Thomas Cazali, C<< <pandragon at cpan.org> >>
532  
533 =head1 SOURCE
534  
535 The source code repository for C<Java::Maven::Artifact::Version> can be found at L<https://github.com/apendragon/Java-Maven-Artifact-Version/>
536  
537 =head1 BUGS
538  
539 Please report any bugs or feature requests to L<https://github.com/apendragon/Java-Maven-Artifact-Version/issues>.
540  
541 =head1 SUPPORT
542  
543 You can find documentation for this module with the perldoc command.
544  
545 perldoc Java::Maven::Artifact::Version
546  
547  
548 You can also look for information at:
549  
550 L<https://github.com/apendragon/Java-Maven-Artifact-Version/wiki>
551  
552 =over 4
553  
554 =item * github repository issues tracker (report bugs here)
555  
556 L<https://github.com/apendragon/Java-Maven-Artifact-Version/issues>
557  
558 =item * AnnoCPAN: Annotated CPAN documentation
559  
560 L<http://annocpan.org/dist/Java-Maven-Artifact-Version>
561  
562 =item * CPAN Ratings
563  
564 L<http://cpanratings.perl.org/d/Java-Maven-Artifact-Version>
565  
566 =item * Search CPAN
567  
568 L<http://search.cpan.org/dist/Java-Maven-Artifact-Version/>
569  
570 =back
571  
572 =head1 LICENSE AND COPYRIGHT
573  
574 Copyright 2014 Thomas Cazali.
575  
576 This program is free software; you can redistribute it and/or modify it
577 under the terms of the the Artistic License (2.0). You may obtain a
578 copy of the full license at:
579  
580 L<http://www.perlfoundation.org/artistic_license_2_0>
581  
582 Any use, modification, and distribution of the Standard or Modified
583 Versions is governed by this Artistic License. By using, modifying or
584 distributing the Package, you accept this license. Do not use, modify,
585 or distribute the Package, if you do not accept this license.
586  
587 If your Modified Version has been derived from a Modified Version made
588 by someone other than you, you are nevertheless required to ensure that
589 your Modified Version complies with the requirements of this license.
590  
591 This license does not grant you the right to use any trademark, service
592 mark, tradename, or logo of the Copyright Holder.
593  
594 This license includes the non-exclusive, worldwide, free-of-charge
595 patent license to make, have made, use, offer to sell, sell, import and
596 otherwise transfer the Package with respect to any patent claims
597 licensable by the Copyright Holder that are necessarily infringed by the
598 Package. If you institute patent litigation (including a cross-claim or
599 counterclaim) against any party alleging that the Package constitutes
600 direct or contributory patent infringement, then this Artistic License
601 to you shall terminate on the date that such litigation is filed.
602  
603 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
604 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
605 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
606 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
607 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
608 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
609 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
610 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
611  
612  
613 =cut
614  
615 8 1; # End of Java::Maven::Artifact::Version
↑ Summary