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