Skip to content

Commit

Permalink
Release versioning helper script(s) (OpenAPITools#3110)
Browse files Browse the repository at this point in the history
* Release versioning script with marker tags.

Introduces bump.sh which supports moving from version to version but
only within delimiting marker tags in the target file.

This script currently doesn't do validations or anything fancy.

* Allow bumping version according to type (major,minor,build,revision)

* bump.sh will display error if file contents are unchanged
  • Loading branch information
jimschubert authored Jun 24, 2019
1 parent 034064b commit 6b428d6
Show file tree
Hide file tree
Showing 22 changed files with 508 additions and 68 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ The OpenAPI Specification has undergone 3 revisions since initial creation in 20
OpenAPI Generator Version | Release Date | Notes
---------------------------- | ------------ | -----
5.0.0 (upcoming major release) [SNAPSHOT](https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/5.0.0-SNAPSHOT/)| 13.05.2020 | Major release with breaking changes (no fallback)
4.1.0 (upcoming minor release) [SNAPSHOT](https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/4.1.0-SNAPSHOT/)| 31.07.2019 | Minor release (breaking changes with fallbacks)
4.1.0 (upcoming minor release) [SNAPSHOT](https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/4.1.0-SNAPSHOT/)| 15.07.2019 | Minor release (breaking changes with fallbacks)
4.0.3 (upcoming patch release) [SNAPSHOT](https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/4.0.3-SNAPSHOT/)| 04.07.2019 | Patch release (minor bug fixes, etc)
[4.0.2](https://github.com/OpenAPITools/openapi-generator/releases/tag/v4.0.2) (latest stable release) | 20.06.2019 | Patch release (bug fixes, minor enhancements, etc)
<!-- RELEASE_VERSION -->[4.0.2](https://github.com/OpenAPITools/openapi-generator/releases/tag/v4.0.2) (latest stable release) | 20.06.2019 | Patch release (bug fixes, minor enhancements, etc)<!-- /RELEASE_VERSION -->

OpenAPI Spec compatibility: 1.0, 1.1, 1.2, 2.0, 3.0

Expand Down Expand Up @@ -153,7 +153,7 @@ See the different versions of the [openapi-generator-cli](https://mvnrepository.
* [Readme](https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-gradle-plugin/README.adoc)

### [1.3 - Download JAR](#table-of-contents)

<!-- RELEASE_VERSION -->
If you're looking for the latest stable version, you can grab it directly from Maven.org (Java 8 runtime at a minimum):

JAR location: `http://central.maven.org/maven2/org/openapitools/openapi-generator-cli/4.0.2/openapi-generator-cli-4.0.2.jar`
Expand All @@ -175,7 +175,7 @@ For Mac users, please make sure Java 8 is installed (Tips: run `java -version` t
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
export PATH=${JAVA_HOME}/bin:$PATH
```

<!-- /RELEASE_VERSION -->
### Launcher Script

One downside to manual jar downloads is that you don't keep up-to-date with the latest released version. We have a Bash launcher script at [bin/utils/openapi-generator.cli.sh](./bin/utils/openapi-generator-cli.sh) which resolves this issue.
Expand Down Expand Up @@ -368,7 +368,8 @@ npm install @openapitools/openapi-generator-cli -g
openapi-generator version
```

Or install a particualar OpenAPI Generator version (e.g. v4.0.2):
<!-- RELEASE_VERSION -->
Or install a particular OpenAPI Generator version (e.g. v4.0.2):

```sh
npm install @openapitools/[email protected] -g
Expand All @@ -379,7 +380,7 @@ Or install it as dev-dependency:
```sh
npm install @openapitools/openapi-generator-cli -D
```

<!-- /RELEASE_VERSION -->
## [2 - Getting Started](#table-of-contents)

To generate a PHP client for [petstore.yaml](https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml), please run the following
Expand All @@ -394,7 +395,9 @@ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generat
```
(if you're on Windows, replace the last command with `java -jar modules\openapi-generator-cli\target\openapi-generator-cli.jar generate -i https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g php -o c:\temp\php_api_client`)

<!-- RELEASE_VERSION -->
You can also download the JAR (latest release) directly from [maven.org](http://central.maven.org/maven2/org/openapitools/openapi-generator-cli/4.0.2/openapi-generator-cli-4.0.2.jar)
<!-- /RELEASE_VERSION -->

To get a list of **general** options available, please run `java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar help generate`

Expand Down
221 changes: 221 additions & 0 deletions bin/utils/release/bump.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
#!/usr/bin/env bash
#
# This script bumps from one version to another
#
# Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

declare -r version_regex="([0-9]+).([0-9]+).([0-9]+)-?(SNAPSHOT){0,1}"
declare start="<!-- RELEASE_VERSION -->"
declare end="<!-- \/RELEASE_VERSION -->"
declare from="${version_regex}"
declare to=""
declare debug=${debug:-false}
declare -a from_parts=()
declare -a to_parts=()
declare -ar inc=(major minor build snapshot)

USAGE="
USAGE: $0 OPTIONS input_file
This script will bump a version number (or other value) between marker tags.
OPTIONS:
-f The 'from' version
-t The 'to' version
-s The start tag regex
default: $start
-e The end tag regex
default: $end
-i Increase by one of: ${inc[@]}
-h Print this message
EXAMPLES:
Update to next snapshot version:
$0 -f 3.0.0 -t 3.0.1-SNAPSHOT pom.xml
Update build version only (useful for docs)
$0 -f 3.0.0 -t 3.0.1 pom.xml
Update from any version to any other version
$0 -f 1.2.3 -t 9.9.9-SNAPSHOT pom.xml
Customize the start/end tags
$0 -f 1.0.0 1.0.1-SNAPSHOT -s \"<!-- START -->\" -e \"<!-- END -->\" pom.xml
"


## print an error message and exit
err() {
>&2 echo -e "$1"
exit 1
}

## debug log messages. Run with debug=true ./bump.sh
d() {
if [[ true = "${debug}" ]]; then
echo "$1"
fi
}

## outputs usage and exits
usage()
{
err "${USAGE}"
exit 1
}

## usage: version input extracted_array
## - Checks that 'input' is a valid version
## - Extracts the version parts into 'extracted_array'
version()
{
if [[ "$#" -ne 2 ]]; then
err "Call function version with two parameters: version string arr"
fi
local v=$1
if [[ "${v}" =~ $version_regex ]]; then
local major=${BASH_REMATCH[1]}
local minor=${BASH_REMATCH[2]}
local build=${BASH_REMATCH[3]}
local snapshot=false
if [[ "SNAPSHOT" = "${BASH_REMATCH[4]}" ]]; then
snapshot=true
fi

d "major=$major minor=$minor build=$build snapshot=$snapshot"

eval "$2=(${major} ${minor} ${build} ${snapshot})"
else
err "Invalid version: $v"
fi
}

while getopts "hf:t:s:e:i:" OPTION
do
case ${OPTION} in
f)
from=${OPTARG}
;;
t)
to=${OPTARG}
;;
s)
start=${OPTARG}
;;
e)
end=${OPTARG}
;;
i)
increase=${OPTARG}
if [[ ! "${inc[@]}" =~ ${increase} ]];then
err "Only support increasing by one of: ${inc[@]}"
fi
;;
h)
usage
;;
esac
done

shift $((OPTIND-1))
file=( "$@" )

if [[ ${#file[@]} -eq 0 ]];then
echo "No file specified" >&2
usage
fi

if [[ -z "${from}" ]]; then
echo "No 'from' version specified." >&2
usage
fi

# TODO: compare steps in from_parts and to_parts.
version "${from}" from_parts

if [[ -z "${to}" ]]; then
if [[ -z "${increase}" ]]; then
err "No 'to' version specified."
else
case ${increase} in
major)
to="$(( ${from_parts[0]} + 1 )).0.0"
version "$to" to_parts
;;
minor)
to="${from_parts[0]}.$(( ${from_parts[1]} + 1 )).0"
version "$to" to_parts
;;
build)
to="${from_parts[0]}.${from_parts[1]}.$(( ${from_parts[2]} + 1 ))"
version "$to" to_parts
;;
snapshot)
if [[ true = ${from_parts[3]} ]]; then
err "Can't move from SNAPSHOT to SNAPSHOT (from=${from})."
else
to="${from_parts[0]}.${from_parts[1]}.$(( ${from_parts[2]} + 1 ))-SNAPSHOT"
version "$to" to_parts
fi
;;
esac
fi
else
version "${to}" to_parts
fi

if [[ ${from_parts[3]} = true && ${to_parts[3]} = true ]]; then
err "Moving from SNAPSHOT to SNAPSHOT is not supported."
fi

cat <<EOF > sedscript.sed
/${start}/,/${end}/{
s/${from}/${to}/g
}
EOF

d "Moving from=${from} to=${to}"

trap 'rm -f sedscript.sed' EXIT

sed_cross() {
# Cross-platform sed invocation. OSX has no option to show a version number in sed.
local target=$1
sed --version >/dev/null 2>&1 && sed -e -i '' -f sedscript.sed "$target" || sed -i '' -E -f sedscript.sed "$target"
}

update_file() {
local filename=$1
local error_message="ERROR: Failed to update $filename to target version ${to}"
local original_hash=$(ruby -r digest -e "p Digest::SHA2.file(\"$filename\").hexdigest")
local final_hash=""
if ! sed_cross ${filename}; then
# occurs if, for example, the file doesn't exist.
echo "ERROR: Failed to update $filename to target version ${to}" >&2
fi

local final_hash=$(ruby -r digest -e "p Digest::SHA2.file(\"$filename\").hexdigest")

if [[ "${original_hash}" = "${final_hash}" ]]; then
# occurs if, for example, the file doesn't have expected marker tags for replacement
echo "ERROR: $filename was not modified." >&2
else
echo "Updated $filename successfully!"
fi
}

for filename in "${file[@]}"; do
update_file ${filename}
done

96 changes: 96 additions & 0 deletions bin/utils/release/release_version_update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env bash
#
# This script is used to update files to the "latest" version.
#
# usage: ./bin/utils/release_version_update.sh <from> <to>
# example: ./bin/utils/release_version_update.sh 3.0.1-SNAPSHOT 3.0.1
#
# Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

declare cwd=$(cd $(dirname "${BASH_SOURCE}") && pwd)
declare root=$(cd "$cwd" && cd ../../../ && pwd)

USAGE="
USAGE: $0 target
This script will convert the current version in target files to the specified 'target'
where target is one of:
major
minor
build
snapshot
EXAMPLES:
Update to new snapshot (1.0.0 -> 1.0.1-SNAPSHOT):
$0 snapshot
Update build version (1.0.0 -> 1.0.1)
$0 build
Update minor version (1.2.3 -> 1.3.0)
$0 minor
Update major version (1.2.3 -> 2.0.0)
$0 major
"

version=$(ruby -r rexml/document -e 'include REXML;
p XPath.first(Document.new($stdin), "/project/version/text()")' < ${cwd}/../../../pom.xml | tr -d '"')

if [[ -n "$1" ]]; then
case $1 in
--help|-h)
echo -e "$USAGE" >&2
exit 1
;;
major|minor|build|snapshot)
inc="$1"
;;
*)
echo "Invalid target.Must be one of: major minor build or snapshot" >&2
exit 1
;;
esac
else
inc="snapshot"
fi

echo "Release preparation: Moving from $version to next $inc version."

# These files should wrap target version replacement blocks with <!-- RELEASE_VERSION --> and <!-- /RELEASE_VERSION -->
# We can include xml and md files here.
declare -a xml_files=(
"${root}/modules/openapi-generator-cli/pom.xml"
"${root}/modules/openapi-generator-gradle-plugin/pom.xml"
"${root}/modules/openapi-generator-core/pom.xml"
"${root}/modules/openapi-generator-maven-plugin/pom.xml"
"${root}/modules/openapi-generator-online/pom.xml"
"${root}/modules/openapi-generator/pom.xml"
"${root}/modules/openapi-generator-maven-plugin/examples/multi-module/java-client/pom.xml"
"${root}/modules/openapi-generator-maven-plugin/examples/java-client.xml"
"${root}/modules/openapi-generator-maven-plugin/examples/non-java-invalid-spec.xml"
"${root}/modules/openapi-generator-maven-plugin/examples/non-java.xml"
"${root}/samples/meta-codegen/lib/pom.xml"
"${root}/pom.xml"
)

# These files should wrap target version replacement blocks with # RELEASE_VERSION and # /RELEASE_VERSION
declare -a properties_files=(
"${root}/modules/openapi-generator-gradle-plugin/gradle.properties"
"${root}/modules/openapi-generator-gradle-plugin/samples/local-spec/gradle.properties"
)

${cwd}/bump.sh -f ${version} -i ${inc} ${xml_files[@]}
${cwd}/bump.sh -f ${version} -t ${inc} -s '# RELEASE_VERSION' -e '# \/RELEASE_VERSION' ${properties_files[@]}
Loading

0 comments on commit 6b428d6

Please sign in to comment.