blob: 380aa48311ee7b1582e78cac73712a8919c6180f [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:
46FILES[CHANGES]=manual
47FILES[debian/changelog]=manual
48FILES[doap.rdf]=manual
49
50# These files can be updated automatically:
51FILES[ApacheThrift.nuspec]=simpleReplace
52FILES[Thrift-swift3.podspec]=simpleReplace
53FILES[Thrift.podspec]=simpleReplace
54FILES[appveyor.yml]=simpleReplace
55FILES[bower.json]=jsonReplace
56FILES[build/cmake/DefineCMakeDefaults.cmake]=simpleReplace
57FILES[configure.ac]=configureReplace
58FILES[contrib/thrift.spec]=simpleReplace
59FILES[lib/cocoa/src/Thrift.h]=simpleReplace
60FILES[lib/csharp/ThriftMSBuildTask/Properties/AssemblyInfo.cs]=simpleReplace
61FILES[lib/csharp/ThriftMSBuildTask/ThriftMSBuildTask.csproj]=simpleReplace
62FILES[lib/csharp/src/Properties/AssemblyInfo.cs]=simpleReplace
63FILES[lib/csharp/src/Thrift.csproj]=simpleReplace
64FILES[lib/csharp/test/Multiplex/Client/MultiplexClient.csproj]=simpleReplace
65FILES[lib/csharp/test/Multiplex/Client/Properties/AssemblyInfo.cs]=simpleReplace
66FILES[lib/csharp/test/Multiplex/Server/MultiplexServer.csproj]=simpleReplace
67FILES[lib/csharp/test/Multiplex/Server/Properties/AssemblyInfo.cs]=simpleReplace
68FILES[lib/csharp/test/ThriftMVCTest/Properties/AssemblyInfo.cs]=simpleReplace
69FILES[lib/d/src/thrift/base.d]=simpleReplace
70FILES[lib/dart/pubspec.yaml]=pubspecReplace
71FILES[lib/delphi/src/Thrift.pas]=simpleReplace
72FILES[lib/erl/src/thrift.app.src]=simpleReplace
73FILES[lib/haxe/haxelib.json]=simpleReplace
74FILES[lib/hs/thrift.cabal]=simpleReplace
75FILES[lib/java/gradle.properties]=simpleReplace
76FILES[lib/js/package.json]=jsonReplace
77FILES[lib/js/src/thrift.js]=simpleReplace
78FILES[lib/lua/Thrift.lua]=simpleReplace
79FILES[lib/netcore/Thrift/Properties/AssemblyInfo.cs]=simpleReplace
80FILES[lib/netcore/Thrift/Transports/Client/THttpClientTransport.cs]=simpleReplace
81FILES[lib/ocaml/_oasis]=simpleReplace
82FILES[lib/perl/lib/Thrift.pm]=simpleReplace
83FILES[lib/py/setup.py]=simpleReplace
84FILES[lib/rb/thrift.gemspec]=simpleReplace
85FILES[lib/rs/Cargo.toml]=simpleReplace
86FILES[lib/st/package.xml]=simpleReplace
87FILES[package.json]=jsonReplace
88FILES[sonar-project.properties]=simpleReplace
89FILES[test/csharp/Properties/AssemblyInfo.cs]=simpleReplace
90FILES[test/csharp/ThriftTest.csproj]=simpleReplace
91FILES[test/dart/test_client/pubspec.yaml]=pubspecReplace
92FILES[test/erl/src/thrift_test.app.src]=simpleReplace
93FILES[tutorial/csharp/CsharpClient/Properties/AssemblyInfo.cs]=simpleReplace
94FILES[tutorial/csharp/CsharpServer/Properties/AssemblyInfo.cs]=simpleReplace
95FILES[tutorial/dart/client/pubspec.yaml]=pubspecReplace
96FILES[tutorial/dart/console_client/pubspec.yaml]=pubspecReplace
97FILES[tutorial/dart/server/pubspec.yaml]=pubspecReplace
98FILES[tutorial/delphi/DelphiClient/DelphiClient.dproj]=simpleReplace
99FILES[tutorial/delphi/DelphiServer/DelphiServer.dproj]=simpleReplace
100FILES[tutorial/hs/ThriftTutorial.cabal]=simpleReplace
101FILES[tutorial/ocaml/_oasis]=simpleReplace
102
103if [ ! -f "CHANGES" ]; then
104 >&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{
152 echo "$(echo $1 | sed 's/\./\\./g' | sed 's/\[/\\\[/g' | sed 's/\]/\\\]/g')"
153}
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#
236# replace: replace occurrences of one string with another
237# the file specified must contain the old string at least once
238# in order to be successful.
239# \param $1 filename to do replacements on
240# \param $2 the "old" string to be replaced
241# \param $3 the "new" striing to replace it with
242# \returns 0 on success
243#
244function replace
245{
246 local result
247 local output
248 local oldString="$2"
249 local newString="$3"
250 local oldRegex=$(escapeVersion "${oldString}")
251 local count=$(grep -Ec "${oldRegex}" "$1")
252 local verbose
253 if [ $count -eq 0 ]; then
254 printf "%-60s | %5d | ${red}NOT FOUND${normal}: ${oldString}" "$1" 0
255 echo
256 return 1
257 elif [ ! -z "$DRYRUN" ]; then
258 printf "%-60s | %5d | MATCHES:" "$1" "$count"
259 echo
260 while read -r line; do
261 echo " > $(echo "$line" | sed "s/${oldRegex}/${reverse}${green}${oldString}${normal}/g")"
262 done < <(grep -E "${oldRegex}" "$1")
263 return 0
264 fi
265 output=$(sed -i "s/${oldRegex}/${newString}/g" "$1")
266 result=$?
267 if [ $result -ne 0 ]; then
268 printf "%-60s | %5d | ${red}ERROR${normal}: %s" "$1" "$count" "$output"
269 echo
270 return 1
271 fi
272 printf "%-60s | %5d | ${green}OK${normal}" "$1" "$count"
273 echo
274 return 0
275}
276
277#
278# simpleReplace: replace occurrences of ${OLDVERSION} with ${NEWVERSION}
279# the file specified must contain OLDVERSION at least once
280# in order to be successful.
281# \param $1 filename to do replacements on
282# \param $2 the "old" string to be replaced
283# \param $3 the "new" striing to replace it with
284# \returns 0 on success
285#
286function simpleReplace
287{
288 replace "$1" "${OLDVERSION}" "${NEWVERSION}"
289}
290
291echo ""
292echo "Apache Thrift Version Alignment Tool"
293echo "------------------------------------"
294echo ""
295echo "Previous Version: ${OLDVERSION}"
296echo " New Version: ${NEWVERSION}"
297echo ""
298echo "-------------------------------------------------------------+-------+----------------------"
299echo "Filename | Count | Status "
300echo "-------------------------------------------------------------+-------+----------------------"
301
302for file in $(echo "${!FILES[@]}" | sort); do
303 ${FILES[$file]} $file || exit $?
304done
305
306echo
307echo "Files that must be modified manually:"
308echo
309for manu in $(echo "${!MANUAL[@]}" | sort); do
310 echo " > ${yellow}${manu}${normal}"
311done
312
313exit 0