1978e33dc651af4c6d74d7dd752f09f171f1c9b6
[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 usage()
28 {
29     echo ""
30     echo "$SCRIPT_NAME - update the parent reference in a POM file"
31     echo ""
32     echo "       usage:  $SCRIPT_NAME [-options]"
33     echo ""
34     echo "       options"
35     echo "         -h             - this help message"
36     echo "         -f pom_file    - the POM file to update"
37     echo "         -g group_id    - the parent group ID"
38     echo "         -a artifact_id - the parent artifact ID"
39     echo "         -v version     - the parent version"
40     exit 255;
41 }
42
43 while getopts "hf:g:a:v:" opt
44 do
45     case $opt in
46     h)
47         usage
48         ;;
49     f)
50         pom_file=$OPTARG
51         ;;
52     g)
53         group_id=$OPTARG
54         ;;
55     a)
56         artifact_id=$OPTARG
57         ;;
58     v)
59         version=$OPTARG
60         ;;
61     \?)
62         usage
63         exit 1
64         ;;
65     :)
66       echo "Option -$OPTARG requires an argument." >&2
67       exit 1
68       ;;
69     esac
70 done
71
72 if [ $OPTIND -eq 1 ]
73 then
74     echo "no arguments were specified"
75     usage
76 fi
77
78 if [ ! -f "$pom_file" ]
79 then
80     echo "POM file '$pom_file' specified on '-f' flag not found"
81     exit 1
82 fi
83
84 if [ -z "$group_id" ]
85 then
86     echo "group ID not specified on '-g' flag"
87     exit 1
88 fi
89
90 if [ -z "$artifact_id" ]
91 then
92     echo "artifact ID not specified on '-a' flag"
93     exit 1
94 fi
95
96 if [ -z "$version" ]
97 then
98     echo "version not specified on '-v' flag"
99     exit 1
100 fi
101
102 pom_lines=`wc -l $pom_file | sed 's/^[ \t]*//' | cut -f1 -d' '`
103 parent_start_line=`grep -n '^[\t ]*<parent>[\t*]*$' $pom_file | cut -f1 -d':'`
104 parent_end_line=`grep -n '^[\t ]*</parent>[\t*]*$' $pom_file | cut -f1 -d':'`
105
106 pom_head_lines=$((parent_start_line-1))
107 pom_tail_lines=$((pom_lines-parent_end_line))
108
109 pom_temp_file=$(mktemp)
110
111 head -$pom_head_lines $pom_file                      >  $pom_temp_file
112 echo "    <parent>"                                  >> $pom_temp_file
113 echo "        <groupId>$group_id</groupId>"          >> $pom_temp_file
114 echo "        <artifactId>$artifact_id</artifactId>" >> $pom_temp_file
115 echo "        <version>$version</version>"           >> $pom_temp_file
116 echo "        <relativePath />"                      >> $pom_temp_file
117 echo "    </parent>"                                 >> $pom_temp_file
118 tail -$pom_tail_lines $pom_file                      >> $pom_temp_file
119
120 mv $pom_temp_file $pom_file