blob: 9f51f2242d875954d67a7bb53c45be70040af7aa [file] [log] [blame]
James E. King IIIecebd772018-12-28 08:50:58 -05001#!/usr/bin/env bash
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# The veralign script sets the appropriate versions in all of
23# the package configuration files for all of the supported
24# languages. It is used to prepare a release or move master
25# forward to the next anticipated version.
26#
27# USAGE
28# -----------------------------------------------------------
29# usage: veralign.sh <oldVersion> <newVersion>
30#
31# EXAMPLE
32# -----------------------------------------------------------
33# $ ./veralign.sh 0.12.0 1.0.0
34# $ ./veralign.sh 1.0.0 1.1.0
35#
36# IMPORTANT USAGE NOTE
37# -----------------------------------------------------------
38# Define the environment variable DRYRUN to have the script
39# print out all matches to the oldVersion hilighted so that
40# you can verify it will change the right things.
41#
42
43declare -A FILES
44
45# These files require a manual touch:
James E. King IIIe824efc2019-01-07 16:50:54 -050046FILES[CHANGES.md]=manual
James E. King IIIecebd772018-12-28 08:50:58 -050047FILES[debian/changelog]=manual
48FILES[doap.rdf]=manual
49
50# These files can be updated automatically:
51FILES[ApacheThrift.nuspec]=simpleReplace
James E. King IIIecebd772018-12-28 08:50:58 -050052FILES[appveyor.yml]=simpleReplace
53FILES[bower.json]=jsonReplace
Jens Geyer56700e42020-02-22 16:51:51 +010054FILES[CMakeLists.txt]=simpleReplace
Jens Geyer71997412019-10-19 21:22:59 +020055FILES[compiler/cpp/src/thrift/version.h]=simpleReplace
Jens Geyer56700e42020-02-22 16:51:51 +010056FILES[configure.ac]=configureReplace
57FILES[contrib/Rebus/Properties/AssemblyInfo.cs]=simpleReplace
James E. King IIIecebd772018-12-28 08:50:58 -050058FILES[contrib/thrift.spec]=simpleReplace
Jens Geyer56700e42020-02-22 16:51:51 +010059FILES[contrib/zeromq/csharp/AssemblyInfo.cs]=simpleReplace
James Z.M. Gao65fb49b2021-01-06 11:51:41 +080060FILES[contrib/thrift-maven-plugin/pom.xml]=pomReplace
James E. King III234fb472019-01-13 23:19:18 -050061FILES[doc/specs/idl.md]=simpleReplace
James E. King IIIb1d63e72019-01-22 14:16:39 -050062FILES[lib/as3/gradle.properties]=simpleReplace
James E. King IIIecebd772018-12-28 08:50:58 -050063FILES[lib/d/src/thrift/base.d]=simpleReplace
64FILES[lib/dart/pubspec.yaml]=pubspecReplace
65FILES[lib/delphi/src/Thrift.pas]=simpleReplace
66FILES[lib/erl/src/thrift.app.src]=simpleReplace
67FILES[lib/haxe/haxelib.json]=simpleReplace
68FILES[lib/hs/thrift.cabal]=simpleReplace
69FILES[lib/java/gradle.properties]=simpleReplace
Jens Geyer616df982019-10-19 22:08:13 +020070FILES[lib/js/package-lock.json]=jsonReplace
Jens Geyer56700e42020-02-22 16:51:51 +010071FILES[lib/js/package.json]=jsonReplace
James E. King IIIecebd772018-12-28 08:50:58 -050072FILES[lib/js/src/thrift.js]=simpleReplace
73FILES[lib/lua/Thrift.lua]=simpleReplace
Jens Geyer56700e42020-02-22 16:51:51 +010074FILES[lib/netstd/Thrift/Properties/AssemblyInfo.cs]=simpleReplace
Jens Geyeraa0c8b32019-01-28 23:27:45 +010075FILES[lib/netstd/Thrift/Properties/AssemblyInfo.cs]=simpleReplace
Jens Geyerb75e88a2019-10-17 21:56:39 +020076FILES[lib/netstd/Thrift/Thrift.csproj]=simpleReplace
James E. King IIIecebd772018-12-28 08:50:58 -050077FILES[lib/ocaml/_oasis]=simpleReplace
78FILES[lib/perl/lib/Thrift.pm]=simpleReplace
79FILES[lib/py/setup.py]=simpleReplace
80FILES[lib/rb/thrift.gemspec]=simpleReplace
81FILES[lib/rs/Cargo.toml]=simpleReplace
82FILES[lib/st/package.xml]=simpleReplace
James E. King IIId7c11ad2019-01-11 19:19:44 -050083FILES[lib/swift/Sources/Thrift.swift]=simpleReplace
James E. King III17355422019-01-11 23:06:08 -050084FILES[lib/swift/Tests/ThriftTests/ThriftTests.swift]=simpleReplace
Jens Geyer616df982019-10-19 22:08:13 +020085FILES[lib/ts/package-lock.json]=jsonReplace
Jens Geyer56700e42020-02-22 16:51:51 +010086FILES[lib/ts/package.json]=jsonReplace
Jens Geyer616df982019-10-19 22:08:13 +020087FILES[package-lock.json]=jsonReplace
Jens Geyer56700e42020-02-22 16:51:51 +010088FILES[package.json]=jsonReplace
James E. King IIIecebd772018-12-28 08:50:58 -050089FILES[sonar-project.properties]=simpleReplace
James E. King IIIecebd772018-12-28 08:50:58 -050090FILES[test/dart/test_client/pubspec.yaml]=pubspecReplace
91FILES[test/erl/src/thrift_test.app.src]=simpleReplace
Jens Geyer56700e42020-02-22 16:51:51 +010092FILES[Thrift.podspec]=simpleReplace
James E. King IIIecebd772018-12-28 08:50:58 -050093FILES[tutorial/dart/client/pubspec.yaml]=pubspecReplace
94FILES[tutorial/dart/console_client/pubspec.yaml]=pubspecReplace
95FILES[tutorial/dart/server/pubspec.yaml]=pubspecReplace
96FILES[tutorial/delphi/DelphiClient/DelphiClient.dproj]=simpleReplace
97FILES[tutorial/delphi/DelphiServer/DelphiServer.dproj]=simpleReplace
98FILES[tutorial/hs/ThriftTutorial.cabal]=simpleReplace
99FILES[tutorial/ocaml/_oasis]=simpleReplace
100
Jens Geyer56700e42020-02-22 16:51:51 +0100101
102
James E. King IIIc9ac8d22019-01-07 16:46:45 -0500103if [ ! -f "CHANGES.md" ]; then
James E. King IIIecebd772018-12-28 08:50:58 -0500104 >&2 echo "error: run veralign.sh while in the thrift root directory"
105 exit 1
106fi
107
108if [ $# -ne 2 ]; then
109 >&2 echo "usage: veralign.sh <oldVersion> <newVersion>"
110 exit 1
111fi
112
113jq --version 1>/dev/null 2>/dev/null
114if [ $? -ne 0 ]; then
115 >&2 echo "error: the 'jq' package is not installed"
116 exit 1
117fi
118
119#
120# validateVersion: check that a version matches the major.minor.patch
121# format which is the lowest common denominator supported by all
122# project systems.
123# \param $1 the version
124# \returns 0 if the version is compliant
125#
126function validateVersion
127{
128 local result
129 local valid
130 valid=$(echo "$1" | sed '/^[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+$/!{q22}')
131 result=$?
132 if [ $result -eq 22 ]; then
133 >&2 echo "error: version '$1' does not conform to the required major.minor.patch format"
134 return ${result}
135 fi
136}
137
138OLDVERSION=$1
139NEWVERSION=$2
140validateVersion "${OLDVERSION}" || exit $?
141validateVersion "${NEWVERSION}" || exit $?
142
143#
144# escapeVersion: escape the version for use as a sed search
145# \param $1 the version to escape
146# \output the escaped string
147# \returns 0
148# \example VERSEARCH=$(escapeVersion "[1.0.0]"); echo $VERSEARCH; => "\[1\.0\.0\]"
149#
150function escapeVersion
151{
James Z.M. Gao65fb49b2021-01-06 11:51:41 +0800152 echo "$(echo "$1" | sed 's/\./\\./g' | sed 's/\[/\\\[/g' | sed 's/\]/\\\]/g')"
James E. King IIIecebd772018-12-28 08:50:58 -0500153}
154
155# Set up verbose hilighting if running interactive
156if [ "$(tput colors)" -ne 0 ]; then
157 reverse=$(tput rev)
158 red=$(tput setaf 1)
159 green=$(tput setaf 2)
160 yellow=$(tput setaf 3)
161 normal=$(tput sgr0)
162fi
163
164declare -A MANUAL
165
166#
167# manual: note that update of said file is manual
168# \param $1 filename to do replacements on
169# \returns 0
170#
171function manual
172{
173 MANUAL["$1"]=""
174 return 0
175}
176
177#
178# configureReplace: replace the AC_INIT field in configure.ac
179# \param $1 filename to do replacements on
180# \returns 0 on success
181#
182
183function configureReplace
184{
185 replace "$1" "[thrift], [${OLDVERSION}]" "[thrift], [${NEWVERSION}]"
186}
187
188#
189# jsonReplace: replace a specific version field in a JSON file
190# must be a top level "version" field in the json structure
191# \param $1 filename to do replacements on
192# \returns 0 on success
193#
194
195function jsonReplace
196{
197 local result
198 local output
199 if [ ! -z "$DRYRUN" ]; then
200 output=$(jq -e ".version" "$1")
201 else
202 output=$(jq -e ".version = \"${NEWVERSION}\"" "$1" > tmp.$$.json && mv tmp.$$.json "$1")
203 fi
204 result=$?
205 if [ $? -ne 0 ]; then
206 printf "%-60s | %5d | ${red}ERROR${normal}: version tag not found" "$1" "$count"
207 echo
208 return 1
209 elif [ ! -z "$DRYRUN" ]; then
210 output=${output%\"}
211 output=${output#\"}
212 printf "%-60s | %5d | MATCHES: version: \"${reverse}${green}${output}${normal}\"" "$1" 1
213 echo
214 return 0
215 fi
216 printf "%-60s | %5d | ${green}OK${normal}" "$1" 1
217 echo
218 return 0
219}
220
221#
222# pubspecReplace: replace a specific version field in a YAML file
223# must be a top level "version" field in the yaml structure
224# did not find a package that preserves comments so this is
225# somewhat brain-dead, but it gets the job done
226# \param $1 filename to do replacements on
227# \returns 0 on success
228#
229
230function pubspecReplace
231{
232 replace "$1" "version: ${OLDVERSION}" "version: ${NEWVERSION}"
233}
234
235#
James Z.M. Gao65fb49b2021-01-06 11:51:41 +0800236# pomReplace: replace a specific version field in a maven pom file
237# must be a top level "version" field in the xml structure
238# \param $1 filename to do replacements on
239# \returns 0 on success
240#
241
242function pomReplace
243{
244 replace "$1" "^ <version>${OLDVERSION}<\/version>" " <version>${NEWVERSION}<\/version>"
245}
246
247#
James E. King IIIecebd772018-12-28 08:50:58 -0500248# replace: replace occurrences of one string with another
249# the file specified must contain the old string at least once
250# in order to be successful.
251# \param $1 filename to do replacements on
252# \param $2 the "old" string to be replaced
253# \param $3 the "new" striing to replace it with
254# \returns 0 on success
255#
256function replace
257{
258 local result
259 local output
260 local oldString="$2"
261 local newString="$3"
262 local oldRegex=$(escapeVersion "${oldString}")
263 local count=$(grep -Ec "${oldRegex}" "$1")
264 local verbose
265 if [ $count -eq 0 ]; then
266 printf "%-60s | %5d | ${red}NOT FOUND${normal}: ${oldString}" "$1" 0
267 echo
268 return 1
269 elif [ ! -z "$DRYRUN" ]; then
270 printf "%-60s | %5d | MATCHES:" "$1" "$count"
271 echo
272 while read -r line; do
273 echo " > $(echo "$line" | sed "s/${oldRegex}/${reverse}${green}${oldString}${normal}/g")"
274 done < <(grep -E "${oldRegex}" "$1")
275 return 0
276 fi
277 output=$(sed -i "s/${oldRegex}/${newString}/g" "$1")
278 result=$?
279 if [ $result -ne 0 ]; then
280 printf "%-60s | %5d | ${red}ERROR${normal}: %s" "$1" "$count" "$output"
281 echo
282 return 1
283 fi
284 printf "%-60s | %5d | ${green}OK${normal}" "$1" "$count"
285 echo
286 return 0
287}
288
289#
290# simpleReplace: replace occurrences of ${OLDVERSION} with ${NEWVERSION}
291# the file specified must contain OLDVERSION at least once
292# in order to be successful.
293# \param $1 filename to do replacements on
294# \param $2 the "old" string to be replaced
295# \param $3 the "new" striing to replace it with
296# \returns 0 on success
297#
298function simpleReplace
299{
300 replace "$1" "${OLDVERSION}" "${NEWVERSION}"
301}
302
303echo ""
304echo "Apache Thrift Version Alignment Tool"
305echo "------------------------------------"
306echo ""
307echo "Previous Version: ${OLDVERSION}"
308echo " New Version: ${NEWVERSION}"
309echo ""
310echo "-------------------------------------------------------------+-------+----------------------"
311echo "Filename | Count | Status "
312echo "-------------------------------------------------------------+-------+----------------------"
313
314for file in $(echo "${!FILES[@]}" | sort); do
315 ${FILES[$file]} $file || exit $?
316done
317
318echo
319echo "Files that must be modified manually:"
320echo
321for manu in $(echo "${!MANUAL[@]}" | sort); do
322 echo " > ${yellow}${manu}${normal}"
323done
324
325exit 0