1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22
23
24use warnings;
25use strict;
26use diagnostics;
27
28sub trim;
29sub readIncVersions($);
30sub processLine($$);
31sub checkName($);
32sub incrementNewVersion($);
33sub incrementOldVersion($);
34sub incrementPolicyVersion($);
35my $usage =
36"The tool increments the minor version of assemblies and the major version of ".
37"the respective policy files. This is only done if new uno types have been added since".
38"the last product upate. This information is obtained from the file which is passed as ".
39"argument changedTypes. The names in the version file must have a particular form. ".
40"They must end on one of folling terms: NEW_VERSION, OLD_VERSION, POLICY_VERSION\n".
41"If no new published types habe been added then no output, argument  newVersions, is written".
42"Usage is: \n increment_version.pl oldVersions  incVersions newVersions changedTypes\n\n".
43"oldVersion: Contains name value pairs, which are used for forming the config files of ".
44"the policy assemblies, for building the assemblies. \n\n".
45"incVersions: File containing the names of which the versions are to be incremented. ".
46"Every line may only contain one name. The names must exactly match those from the ".
47"oldVersion file.\n\n".
48"newVersions: Contains all entries from oldVersions, but the values of the names,".
49"which occur in selection, have been incremented.\n\n".
50"changedTypes: File that contains the information if new published types have been added ".
51"since the last product update.\n\n" ;
52
53my $sNameForm =
54"The names must end on one of these names: NEW_VERSION, OLD_VERSION, POLICY_VERSION\n".
55"For example, valid names are: \n".
56"CLI_URETYPES_NEW_VERSION\nCLI_URETYPES_OLD_VERSION\nCLI_URETYPES_POLICY_VERSION\n";
57
58if (scalar @ARGV < 4) {
59   print $usage;
60   exit -1;
61}
62
63-e "$ARGV[0]" or die "Error: wrong arguments. \n".$usage;
64-e "$ARGV[1]" or die "Error: wrong arguments. \n".$usage;
65-e "$ARGV[3]" or die "Error: wrong arguments. \n".$usage;
66
67#check if new types have been added since last release.
68#If not, then there is nothing to be done.
69#read in oldVersions line by line and apply the increment operation
70open TYPES, "$ARGV[3]" or die "Cannot open to $ARGV[3] $!";
71
72my $newTypes;
73
74#We look for the line that contains the number of new types
75while(<TYPES>)
76{
77    if (/New and published types/i)
78    {
79	$_ =~ /=\s*(\d+)/;
80	if ( ! defined $1)
81	{
82	    print "\n###$ARGV[3] contains an invalid entry for 'New and published types'.  \n\n";
83	    exit -1;
84	}
85	$newTypes = $1;
86    }
87}
88
89#Check if changeTypes contained the line we are looking for
90if (! defined $newTypes)
91{
92    print "\n###$ARGV[3] does not contain entry about the new types ".
93	"or we are looking for the wrong string! \n\n";
94    exit -1;
95}
96
97if ( $newTypes == 0)
98{
99    print "\nNo new UNO types since las product update.\n";
100    exit 0;
101}
102else
103{
104    print "\nNew UNO types were addes since last release. The version will be increased.\n\n";
105}
106
107#read in incVersions in a list
108my @incVersions = readIncVersions($ARGV[1]);
109#print "@incVersions";
110
111#read in oldVersions line by line and apply the increment operation
112open OLDVERSION, "$ARGV[0]" or die "Cannot open to $ARGV[0] $!";
113
114#open file we want to write to
115open NEWVERSION, "> $ARGV[2]" or die "Cannot write to $ARGV[2] $!";
116
117print NEWVERSION processLine($_, @incVersions) while(<OLDVERSION>);
118
119close NEWVERSION;
120close OLDVERSION;
121
122exit 0;
123
124sub processLine($$)
125{
126    my $line = $_[0];
127    #skip empty lines
128    my $trimmed;
129    return $line if (length($trimmed = trim($line)) == 0);
130    #Skip comment symbol: #
131    return $line if ($trimmed =~ /^#/);
132
133    #Get the left part of '='
134    my $i = index($line, "=");
135    if( $i == -1)
136    {
137	print "Error: No '=' found in line:,: \n $line \n";
138	exit -1;
139    }
140    my $name = substr($line, 0, $i);
141    $name = trim($name);
142    #We do not check the names here because the file can contain
143    #other names, e.g. CLI_URETYPES_POLICY_ASSEMBLY
144    if (length($name) == 0) {
145	print "Wrong line in $ARGV[0]\n", $sNameForm;
146	exit -1;
147    }
148    my $value = substr($line, $i + 1);
149    $value = trim($value);
150
151    #Check if the entry shall be incremented, this information is in the second
152    #argument
153    my $found;
154    for(@incVersions) {
155	if ($_ eq $name) {
156	    $found = 1;
157	    last;
158	}
159    }
160    if ( ! defined($found)) {
161	 return $line;
162    }
163
164    #Check if the name represents a version we need to change
165    if ($name =~ /NEW_VERSION$/)
166    {
167	$value = incrementNewVersion($value);
168    }
169    elsif ($name =~ /OLD_VERSION$/)
170    {
171	$value = incrementOldVersion($value);
172    }
173    elsif ($name =~ /POLICY_VERSION$/)
174    {
175	$value = incrementPolicyVersion($value);
176    }
177    else
178    {
179	#other name which we ignore
180	return $line;
181    }
182    return "${name}=${value}\n";
183}
184
185#The value of a new version has the form x.x.x.x
186#We increment the third position from the left.
187#Te argument must already be trimmed.
188sub incrementNewVersion($)
189{
190   my @parts = split /\./,$_[0];
191   if (scalar @parts != 4)
192   {
193       print "Error, no valid version given in $ARGV[0]\n. A 'new version' has four parts.";
194       exit -1;
195   }
196   $parts[2]++;
197   #build the version string and return
198   return "$parts[0].$parts[1].$parts[2].$parts[3]";
199}
200
201#The value of a new version has the form x.x.x.x-x.x.x.x
202#We increment the third position of the second part.
203#Te argument must already be trimmed.
204sub incrementOldVersion($)
205{
206    my @parts = split /[\.-]/,$_[0];
207    if (scalar @parts != 8)
208    {
209	print "Error, no valid version given in $ARGV[0]\n. A 'old version' has the form
210x.x.x.x-x.x.x.x\n.";
211	exit -1;
212    }
213    $parts[6]++;
214    return "$parts[0].$parts[1].$parts[2].$parts[3]-$parts[4].$parts[5].$parts[6].$parts[7]";
215    return $_[0];
216}
217
218sub incrementPolicyVersion($)
219{
220   my @parts = split /\./,$_[0];
221   if (scalar @parts != 4)
222   {
223       print "Error, no valid version given in $ARGV[0]\n. A 'policy version' has four parts.";
224       exit -1;
225   }
226   $parts[0]++;
227   #build the version string and return
228   return "$parts[0].$parts[1].$parts[2].$parts[3]";
229}
230
231
232sub readIncVersions($)
233{
234    open INC, $_[0] or die "Could not open $_[0] $!";
235    my $arg = $_[0];
236    my @names;
237
238    while(<INC>)
239    {
240	chomp;
241	#Skip empty lines
242	my $line;
243	if (length($line = trim($_)) == 0) {
244	    next;
245	}
246	#Skip comment symbol: #
247	if ($line =~ /^#/) {
248	    next;
249	}
250	if (!checkName($line)) {
251	    print "Wrong entry in file $_[0]\n", $sNameForm;
252	    exit -1;
253	}
254	push @names, $line;
255    }
256    print "No entries found in $arg\n" if(scalar @names == 0);
257    return @names;
258}
259
260#The argument must already be trimmed
261#returns 1 if ok
262sub checkName($)
263{
264    my $name = $_[0];
265    if ( $name !~/NEW_VERSION$|OLD_VERSION$|POLICY_VERSION$/) {
266	return 0;
267    }
268    return 1;
269}
270
271sub trim($)
272{
273	my $string = shift;
274	$string =~ s/^\s+//;
275	$string =~ s/\s+$//;
276	return $string;
277}
278