Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | #!/usr/bin/perl -w |
michael@0 | 2 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 5 | |
michael@0 | 6 | package Moz::Milestone; |
michael@0 | 7 | use strict; |
michael@0 | 8 | |
michael@0 | 9 | use vars qw($officialMilestone |
michael@0 | 10 | $milestone); |
michael@0 | 11 | |
michael@0 | 12 | local $Moz::Milestone::milestone; |
michael@0 | 13 | local $Moz::Milestone::officialMilestone; |
michael@0 | 14 | |
michael@0 | 15 | # |
michael@0 | 16 | # Usage: getOfficialMilestone($milestoneFile) |
michael@0 | 17 | # Returns full milestone (x.x.x.x[ab12pre+]) |
michael@0 | 18 | # |
michael@0 | 19 | sub getOfficialMilestone($) { |
michael@0 | 20 | my $mfile = $_[0]; |
michael@0 | 21 | open(FILE,"$mfile") || |
michael@0 | 22 | die ("Can't open $mfile for reading!"); |
michael@0 | 23 | |
michael@0 | 24 | my $num = <FILE>; |
michael@0 | 25 | while($num =~ /^\s*#/ || $num !~ /^\d/) { |
michael@0 | 26 | $num = <FILE>; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | close(FILE); |
michael@0 | 30 | if ($num !~ /^\d/) { return; } |
michael@0 | 31 | chomp($num); |
michael@0 | 32 | # Remove extra ^M caused by using dos-mode line-endings |
michael@0 | 33 | chop $num if (substr($num, -1, 1) eq "\r"); |
michael@0 | 34 | $Moz::Milestone::officialMilestone = $num; |
michael@0 | 35 | $Moz::Milestone::milestone = &getMilestoneNum; |
michael@0 | 36 | return $num; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | # |
michael@0 | 40 | # Usage: getMilestoneNum($num) |
michael@0 | 41 | # Returns: milestone without a + if it exists. |
michael@0 | 42 | # |
michael@0 | 43 | sub getMilestoneNum { |
michael@0 | 44 | if (defined($Moz::Milestone::milestone)) { |
michael@0 | 45 | return $Moz::Milestone::milestone; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | if (defined($Moz::Milestone::officialMilestone)) { |
michael@0 | 49 | $Moz::Milestone::milestone = $Moz::Milestone::officialMilestone; |
michael@0 | 50 | } else { |
michael@0 | 51 | $Moz::Milestone::milestone = $_[0]; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | if ($Moz::Milestone::milestone =~ /\+$/) { # for x.x.x+, strip off the + |
michael@0 | 55 | $Moz::Milestone::milestone =~ s/\+$//; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | return $Moz::Milestone::milestone; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | # |
michael@0 | 62 | # Usage: getMilestoneQualifier($num) |
michael@0 | 63 | # Returns: + if it exists. |
michael@0 | 64 | # |
michael@0 | 65 | sub getMilestoneQualifier { |
michael@0 | 66 | my $milestoneQualifier; |
michael@0 | 67 | if (defined($Moz::Milestone::officialMilestone)) { |
michael@0 | 68 | $milestoneQualifier = $Moz::Milestone::officialMilestone; |
michael@0 | 69 | } else { |
michael@0 | 70 | $milestoneQualifier = $_[0]; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | if ($milestoneQualifier =~ /\+$/) { |
michael@0 | 74 | return "+"; |
michael@0 | 75 | } |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | sub getMilestoneMajor { |
michael@0 | 79 | my $milestoneMajor; |
michael@0 | 80 | if (defined($Moz::Milestone::milestone)) { |
michael@0 | 81 | $milestoneMajor = $Moz::Milestone::milestone; |
michael@0 | 82 | } else { |
michael@0 | 83 | $milestoneMajor = $_[0]; |
michael@0 | 84 | } |
michael@0 | 85 | my @parts = split(/\./,$milestoneMajor); |
michael@0 | 86 | return $parts[0]; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | sub getMilestoneMinor { |
michael@0 | 90 | my $milestoneMinor; |
michael@0 | 91 | if (defined($Moz::Milestone::milestone)) { |
michael@0 | 92 | $milestoneMinor = $Moz::Milestone::milestone; |
michael@0 | 93 | } else { |
michael@0 | 94 | $milestoneMinor = $_[0]; |
michael@0 | 95 | } |
michael@0 | 96 | my @parts = split(/\./,$milestoneMinor); |
michael@0 | 97 | |
michael@0 | 98 | if ($#parts < 1 ) { return 0; } |
michael@0 | 99 | return $parts[1]; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | sub getMilestoneMini { |
michael@0 | 103 | my $milestoneMini; |
michael@0 | 104 | if (defined($Moz::Milestone::milestone)) { |
michael@0 | 105 | $milestoneMini = $Moz::Milestone::milestone; |
michael@0 | 106 | } else { |
michael@0 | 107 | $milestoneMini = $_[0]; |
michael@0 | 108 | } |
michael@0 | 109 | my @parts = split(/\./,$milestoneMini); |
michael@0 | 110 | |
michael@0 | 111 | if ($#parts < 2 ) { return 0; } |
michael@0 | 112 | return $parts[2]; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | sub getMilestoneMicro { |
michael@0 | 116 | my $milestoneMicro; |
michael@0 | 117 | if (defined($Moz::Milestone::milestone)) { |
michael@0 | 118 | $milestoneMicro = $Moz::Milestone::milestone; |
michael@0 | 119 | } else { |
michael@0 | 120 | $milestoneMicro = $_[0]; |
michael@0 | 121 | } |
michael@0 | 122 | my @parts = split(/\./,$milestoneMicro); |
michael@0 | 123 | |
michael@0 | 124 | if ($#parts < 3 ) { return 0; } |
michael@0 | 125 | return $parts[3]; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | sub getMilestoneAB { |
michael@0 | 129 | my $milestoneAB; |
michael@0 | 130 | if (defined($Moz::Milestone::milestone)) { |
michael@0 | 131 | $milestoneAB = $Moz::Milestone::milestone; |
michael@0 | 132 | } else { |
michael@0 | 133 | $milestoneAB = $_[0]; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | if ($milestoneAB =~ /a/) { return "alpha"; } |
michael@0 | 137 | if ($milestoneAB =~ /b/) { return "beta"; } |
michael@0 | 138 | return "final"; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | # |
michael@0 | 142 | # Usage: getMilestoneABWithNum($milestoneFile) |
michael@0 | 143 | # Returns the alpha and beta tag with its number (a1, a2, b3, ...) |
michael@0 | 144 | # |
michael@0 | 145 | sub getMilestoneABWithNum { |
michael@0 | 146 | my $milestoneABNum; |
michael@0 | 147 | if (defined($Moz::Milestone::milestone)) { |
michael@0 | 148 | $milestoneABNum = $Moz::Milestone::milestone; |
michael@0 | 149 | } else { |
michael@0 | 150 | $milestoneABNum = $_[0]; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | if ($milestoneABNum =~ /([ab]\d+)/) { |
michael@0 | 154 | return $1; |
michael@0 | 155 | } else { |
michael@0 | 156 | return ""; |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | # |
michael@0 | 161 | # build_file($template_file,$output_file) |
michael@0 | 162 | # |
michael@0 | 163 | sub build_file($$) { |
michael@0 | 164 | my @FILE; |
michael@0 | 165 | my @MILESTONE_PARTS; |
michael@0 | 166 | my $MINI_VERSION = 0; |
michael@0 | 167 | my $MICRO_VERSION = 0; |
michael@0 | 168 | my $OFFICIAL = 0; |
michael@0 | 169 | my $QUALIFIER = ""; |
michael@0 | 170 | |
michael@0 | 171 | if (!defined($Moz::Milestone::milestone)) { die("$0: no milestone file set!\n"); } |
michael@0 | 172 | @MILESTONE_PARTS = split(/\./, &getMilestoneNum); |
michael@0 | 173 | if ($#MILESTONE_PARTS >= 2) { |
michael@0 | 174 | $MINI_VERSION = 1; |
michael@0 | 175 | } else { |
michael@0 | 176 | $MILESTONE_PARTS[2] = 0; |
michael@0 | 177 | } |
michael@0 | 178 | if ($#MILESTONE_PARTS >= 3) { |
michael@0 | 179 | $MICRO_VERSION = 1; |
michael@0 | 180 | } else { |
michael@0 | 181 | $MILESTONE_PARTS[3] = 0; |
michael@0 | 182 | } |
michael@0 | 183 | if (! &getMilestoneQualifier) { |
michael@0 | 184 | $OFFICIAL = 1; |
michael@0 | 185 | } else { |
michael@0 | 186 | $QUALIFIER = "+"; |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | if (-e $_[0]) { |
michael@0 | 190 | open(FILE, "$_[0]") || die("$0: Can't open $_[0] for reading!\n"); |
michael@0 | 191 | @FILE = <FILE>; |
michael@0 | 192 | close(FILE); |
michael@0 | 193 | |
michael@0 | 194 | open(FILE, ">$_[1]") || die("$0: Can't open $_[1] for writing!\n"); |
michael@0 | 195 | |
michael@0 | 196 | # |
michael@0 | 197 | # There will be more of these based on what we need for files. |
michael@0 | 198 | # |
michael@0 | 199 | foreach(@FILE) { |
michael@0 | 200 | s/__MOZ_MAJOR_VERSION__/$MILESTONE_PARTS[0]/g; |
michael@0 | 201 | s/__MOZ_MINOR_VERSION__/$MILESTONE_PARTS[1]/g; |
michael@0 | 202 | s/__MOZ_MINI_VERSION__/$MILESTONE_PARTS[2]/g; |
michael@0 | 203 | s/__MOZ_MICRO_VERSION__/$MILESTONE_PARTS[3]/g; |
michael@0 | 204 | if ($MINI_VERSION) { |
michael@0 | 205 | s/__MOZ_OPTIONAL_MINI_VERSION__/.$MILESTONE_PARTS[2]/g; |
michael@0 | 206 | } |
michael@0 | 207 | if ($MICRO_VERSION) { |
michael@0 | 208 | s/__MOZ_OPTIONAL_MICRO_VERSION__/.$MILESTONE_PARTS[3]/g; |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | print FILE $_; |
michael@0 | 212 | } |
michael@0 | 213 | close(FILE); |
michael@0 | 214 | } else { |
michael@0 | 215 | die("$0: $_[0] doesn't exist for autoversioning!\n"); |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | 1; |