Update release scripts for branches
[policy/parent.git] / integration / src / release_scripts / updateParentRef.sh
1 #!/bin/bash
2
3 #
4 # ============LICENSE_START================================================
5 # ONAP
6 # =========================================================================
7 # Copyright (C) 2021-2022 Nordix Foundation.
8 # =========================================================================
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 #      http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 # ============LICENSE_END==================================================
21 #
22
23 set -e
24
25 SCRIPT_NAME=$(basename "$0")
26
27 # Use the bash internal OSTYPE variable to check for MacOS
28 if [[ "$OSTYPE" == "darwin"* ]]
29 then
30     SED="gsed"
31 else
32     SED="sed"
33 fi
34
35 usage()
36 {
37     echo ""
38     echo "$SCRIPT_NAME - update the parent reference in a POM file"
39     echo ""
40     echo "       usage:  $SCRIPT_NAME [-options]"
41     echo ""
42     echo "       options"
43     echo "         -h             - this help message"
44     echo "         -f pom_file    - the POM file to update"
45     echo "         -g group_id    - the parent group ID"
46     echo "         -a artifact_id - the parent artifact ID"
47     echo "         -v version     - the parent version"
48     exit 255;
49 }
50
51 while getopts "hf:g:a:v:" opt
52 do
53     case $opt in
54     h)
55         usage
56         ;;
57     f)
58         pom_file=$OPTARG
59         ;;
60     g)
61         group_id=$OPTARG
62         ;;
63     a)
64         artifact_id=$OPTARG
65         ;;
66     v)
67         version=$OPTARG
68         ;;
69     \?)
70         usage
71         exit 1
72         ;;
73     :)
74       echo "Option -$OPTARG requires an argument." >&2
75       exit 1
76       ;;
77     esac
78 done
79
80 if [ $OPTIND -eq 1 ]
81 then
82     echo "no arguments were specified"
83     usage
84 fi
85
86 if [ ! -f "$pom_file" ]
87 then
88     echo "POM file '$pom_file' specified on '-f' flag not found"
89     exit 1
90 fi
91
92 if [ -z "$group_id" ]
93 then
94     echo "group ID not specified on '-g' flag"
95     exit 1
96 fi
97
98 if [ -z "$artifact_id" ]
99 then
100     echo "artifact ID not specified on '-a' flag"
101     exit 1
102 fi
103
104 if [ -z "$version" ]
105 then
106     echo "version not specified on '-v' flag"
107     exit 1
108 fi
109
110 pom_lines=$(wc -l "$pom_file" | $SED 's/^[ \t]*//' | cut -f1 -d' ')
111 parent_start_line=$(grep -n '^[\t ]*<parent>[\t ]*$' "$pom_file" | cut -f1 -d':')
112 parent_end_line=$(grep -n '^[\t ]*</parent>[\t ]*$' "$pom_file" | cut -f1 -d':')
113
114 pom_head_lines=$((parent_start_line-1))
115 pom_tail_lines=$((pom_lines-parent_end_line))
116
117 pom_temp_file=$(mktemp)
118
119 {
120     head -$pom_head_lines "$pom_file"
121     echo "    <parent>"
122     echo "        <groupId>$group_id</groupId>"
123     echo "        <artifactId>$artifact_id</artifactId>"
124     echo "        <version>$version</version>"
125     echo "        <relativePath />"
126     echo "    </parent>"
127     tail -$pom_tail_lines "$pom_file"
128 } > "$pom_temp_file"
129
130 mv "$pom_temp_file" "$pom_file"