-
Notifications
You must be signed in to change notification settings - Fork 0
/
max-version.pl
executable file
·91 lines (78 loc) · 2.95 KB
/
max-version.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env perl
###############################################################################
# This script evaluates a list containing strings conforming to a SemVer, and #
# finds the entry indicating the most-recent release. #
# #
# For example: #
# 1.0.0-beta < 1.0.0 < 1.1 < 1.2.1 #
# #
# Input: A list containing entries with semantic versions. #
# Output: A single line of text conforming to semantic versioning. #
# #
# Note: this script should not be relied on for discerning the max version #
# between versions that differ only by tag. For instance, the behavior #
# between 1.0.1-foo and 1.0.1-bar is undefined. #
###############################################################################
use strict;
use warnings;
my $maxMajor = 0;
my $maxMinor = 0;
my $maxPatch = 0;
my $maxTag = "";
while(my $row = <STDIN>) {
if ($row =~ /^[vV]?(?<major>\d+)(?:\.(?<minor>\d+))?(?:\.(?<patch>\d+))?(?:-(?<tag>\S+))?$/) {
my $currentMajor = int($+{major});
my $currentMinor = int($+{minor});
my $currentPatch = int($+{patch});
my $currentTag = $+{tag};
if (not defined $currentMinor) {
$currentMinor = 0;
}
if (not defined $currentPatch) {
$currentPatch = 0;
}
if (not defined $currentTag) {
$currentTag = "";
}
if ($currentMajor > $maxMajor) {
$maxMajor = $currentMajor;
$maxMinor = $currentMinor;
$maxPatch = $currentPatch;
$maxTag = $currentTag;
next;
} elsif ($currentMajor < $maxMajor) {
next;
}
if ($currentMinor > $maxMinor) {
$maxMinor = $currentMinor;
$maxPatch = $currentPatch;
$maxTag = $currentTag;
next;
} elsif ($currentMinor < $maxMinor) {
next;
}
if ($currentPatch > $maxPatch) {
$maxPatch = $currentPatch;
$maxTag = $currentTag;
next;
} elsif ($currentPatch < $maxPatch) {
next;
}
if ($currentTag eq "" and $maxTag ne "") {
$maxTag = $currentTag;
next;
} elsif ($maxTag eq "" and $currentTag ne ""){
next;
} elsif ($currentTag gt $maxTag) {
$maxTag = $currentTag;
next
} elsif ($currentTag le $maxTag) {
next;
}
}
}
my $formatted = "v${maxMajor}.${maxMinor}.${maxPatch}";
if ($maxTag ne "") {
$formatted = $formatted . "-${maxTag}";
}
print($formatted . "\n");