Debian Codegraph @

cli_ure/source/scripts/subst_template.pl

raw
1 #
2 # This file is part of the LibreOffice project.
3 #
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 #
8 # This file incorporates work covered by the following license notice:
9 #
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 #
18
19 use warnings;
20 use strict;
21 use diagnostics;
22
23 sub trim;
24 sub readRedirectionValues($);
25
26 my $usage =
27 "Usage is: \n subst_template.pl configTemplate redirections policyConfig
28
29 configTemplate: The config file which is used for the policy assembly. It
30 contains place holders for the binding redirection.
31
32 redirections: file containing the values for oldVersion and newVersion tags
33 which are used in the BindingRedirect element of the config files.
34
35 policyConfig: Name of the file in which we want to write the config file.
36 ";
37
38
39 if (scalar @ARGV < 3) {
40 print $usage;
41 exit -1;
42 }
43
44
45 my %redirectionValue = readRedirectionValues($ARGV[1]);
46 #print "|$_| |$redirectionValue{$_}|\n", for keys %redirectionValue;
47
48
49 #Read config file in which we will replace the versions
50 $/ = undef;
51 open TEMPLATE, $ARGV[0] or die $!;
52 my $templ = <TEMPLATE>;
53
54 #Open the config file we are goint to write to
55 open CONFIG, "> $ARGV[2]" or die "Cannot write to $ARGV[2] $!";
56
57 #No substitute the place holders for oldVersion and new Version in the config template with
58 #the values obtained from the redirections file
59 for (keys %redirectionValue) {
60 $templ=~ s/\b$_\b/$redirectionValue{$_}/;
61 }
62 #Write the config file
63 print CONFIG $templ;
64
65 #Reads the key value pairs from the files, which name must be passed in
66 #the parameter. The file contains lines of the form name=value, for example
67 #CLI_TYPES_OLD_VERSION=1.1.0.0-1.1.1.0
68 sub readRedirectionValues($)
69 {
70 #Read in the values for the version redirection
71 open REDIR, $_[0] or die $!;
72
73 my %redirectionValues;
74
75 while (<REDIR>)
76 {
77 chomp;
78 my $trimmed;
79 #Skip empty lines
80 if (length($trimmed = trim($_)) == 0) {
81 next;
82 }
83
84 #Skip comment symbol: #
85 if ($trimmed =~ /^#/) {
86 next;
87 }
88
89 my @lineParts = split /=/,$_;
90
91 #Check if we have valid name value pairs.
92 if (scalar @lineParts != 2) {
93 print "Error: Values in $ARGV[1] are not correct (Entries must have the form name=value). Invalid line: \n$_\n";
94 exit -1;
95 }
96
97 #Trim the strings and check if they still contain characters
98 my $name = trim($lineParts[0]);
99 my $value = trim($lineParts[1]);
100 if (length($name) == 0 || length($value) == 0) {
101 print "Error: Values in $ARGV[1] are not correct. Invalid line: \n$_\n";
102 exit -1;
103 }
104
105 #Check if we have duplicate key names
106 for (keys %redirectionValues) {
107 if ( $name eq $_) {
108 print "Error: Values in $ARGV[1] are not correct. The name $_ is not unique.\n";
109 exit -1;
110 }
111 }
112
113 $redirectionValues{$name} = $value;
114 }
115 return %redirectionValues;
116 }
117
118 sub trim($)
119 {
120 my $string = shift;
121 $string =~ s/^\s+//;
122 $string =~ s/\s+$//;
123 return $string;
124 }