Convert version bump script to functions 69/4069/2
authorThanh Ha <thanh.ha@linuxfoundation.org>
Sat, 4 Mar 2017 23:52:16 +0000 (18:52 -0500)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Sat, 4 Mar 2017 23:52:16 +0000 (18:52 -0500)
Convert the version bump script to functions so that it can be sourced
and reused more easily.

Change-Id: I09dde2c7e51b6dc8b70bb5befbe22d1fdb947903
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
shell/version

index f40a4f9..da17ae7 100755 (executable)
@@ -2,7 +2,7 @@
 
 # @License EPL-1.0 <http://spdx.org/licenses/EPL-1.0>
 ##############################################################################
-# Copyright (c) 2014 The Linux Foundation and others.
+# Copyright (c) 2014, 2017 The Linux Foundation and others.
 #
 # All rights reserved. This program and the accompanying materials
 # are made available under the terms of the Eclipse Public License v1.0
@@ -11,6 +11,7 @@
 #
 # Contributors:
 #   Colin Dixon - Initial implementation
+#   Thanh Ha - Convert to functions
 ##############################################################################
 
 # In general, versions should be: <major>.<minor>.<micro>[-<human-readable-tag>]
 #   2.) take all x.y.z-Helium versions to x.y.(z+1)-SNAPSHOT and
 #   3.) take all x.y.z-SNAPSHOT versions to x.(y+1).0-SNAPSHOT
 
-USAGE="USAGE: versions <mode> <release-tag>\n\
-\n\
-mode - bump|release\n\
-tag  - example: Helium-SR1"
+print_version_usage() {
+    echo "Usage:"
+    echo "    version bump <release-tag>       Bump all versions in pom files"
+    echo "    version release <release-tag>    Convert all SNAPSHOTS to <release-tag>"
+    echo "        example-tag: Helium-SR1"
+}
 
-if [ -z "$2" ]
-then
-    echo -e "$USAGE"
-    exit 1
-fi
+version() {
+    subcommand=$1; shift
+    FILENAMES="pom.xml repo-pom.xml features.xml"
 
-MODE=$1
-RELEASE_TAG=$2
-FILENAMES="pom.xml repo-pom.xml features.xml"
-
-
-if [ "$MODE" == "bump" ]
-then
-    echo "Bumping versions..."
-    for name in $FILENAMES
-    do
-        # Notes:
-        #   * bump date-based versions first to avoid date-only versions from being caught as x.y.z,
-        #   * this assumes that a normal x.y.z version can't match YYYY.MM.DD, which is probably true
-        #   * bump -SNAPSHOT versions first so that we don't double bump versions
+    case "$subcommand" in
+        bump )
+            RELEASE_TAG=$1
+            echo "Bumping versions..."
+            version_bump
+            exit 0
+            ;;
+        release )
+            RELEASE_TAG=$1
+            echo "Bumping SNAPSHOTS to $RELEASE_TAG"
+            exit 0
+            ;;
+        * )
+            echo "Invalid command: $subcommand" 1>&2
+            print_version_usage
+            exit 1
+            ;;
+    esac
+}
 
+version_bump() {
+    # Notes:
+    #   * bump date-based versions first to avoid date-only versions from being caught as x.y.z,
+    #   * this assumes that a normal x.y.z version can't match YYYY.MM.DD, which is probably true
+    #   * bump -SNAPSHOT versions first so that we don't double bump versions
+    for name in $FILENAMES; do
         # Changes YYYY.MM.DD.y.z-SNAPSHOT to YYYY.MM.DD.(y+1).0-SNAPSHOT in pom.xml files (if y or z is missing treat as 0)
         find . -type f -name "$name" -exec perl -i -pe "s/(\d\d\d\d\.\d\d\.\d\d)\.(\d+)\.(\d+)-SNAPSHOT/\$1.@{[1+\$2]}.0-SNAPSHOT/g" {} +
         find . -type f -name "$name" -exec perl -i -pe "s/(\d\d\d\d\.\d\d\.\d\d)\.(\d+)-SNAPSHOT/\$1.@{[1+\$2]}.0-SNAPSHOT/g" {} +
@@ -80,15 +92,12 @@ then
         # Changes x.y.z-Helium to x.y.(z+1)-SNAPSHOT in pom.xml files (if z is missing treat as 0)
         find . -type f -name "$name" -exec perl -i -pe "s/([^\d.]\d+)\.(\d+)\.(\d+)-$RELEASE_TAG/\$1.\$2.@{[1+\$3]}-SNAPSHOT/g" {} +
         find . -type f -name "$name" -exec perl -i -pe "s/([^\d.]\d+)\.(\d+)-$RELEASE_TAG/\$1.\$2.1-SNAPSHOT/g" {} +
-
     done
-elif [ "$MODE" == "release" ]
-then
+}
+
+version_release() {
     for name in $FILENAMES
     do
         find . -type f -name "$name" -exec perl -i -pe "s/SNAPSHOT/$RELEASE_TAG/g" {} +
     done
-else
-    echo -e "$USAGE"
-    exit 1
-fi
+}