Alexandru N. Onea | 3a6abfa | 2019-04-09 01:23:10 +0300 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 4 | # |
| 5 | # Project configuration variables |
| 6 | # |
| 7 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 8 | VERSION_FILE="include/tinyalsa/version.h" |
| 9 | |
| 10 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 11 | # |
| 12 | # Scripts internal variables |
| 13 | # |
| 14 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 15 | LF="\n" |
| 16 | PARAMS="" |
| 17 | |
| 18 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 19 | # |
| 20 | # Helper functions |
| 21 | # |
| 22 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 23 | die() |
| 24 | { |
| 25 | echo "Error: $@" 1>&2 |
| 26 | exit 1 |
| 27 | } |
| 28 | |
| 29 | print_usage() |
| 30 | { |
| 31 | echo "Usage: $0 [OPTIONS] ACTION" |
| 32 | echo |
| 33 | echo "Available options:" |
Alexandru N. Onea | 891c04d | 2019-04-09 21:16:34 +0300 | [diff] [blame^] | 34 | echo " -s,--script Format output in \"script\" mode (no trailing newline)." |
Alexandru N. Onea | 3a6abfa | 2019-04-09 01:23:10 +0300 | [diff] [blame] | 35 | echo |
| 36 | echo "Available actions:" |
Alexandru N. Onea | 891c04d | 2019-04-09 21:16:34 +0300 | [diff] [blame^] | 37 | echo " print [minor|major|patch] Print the current version." |
| 38 | echo " release [minor|major|patch] Bump the specified version part" |
Alexandru N. Onea | 3a6abfa | 2019-04-09 01:23:10 +0300 | [diff] [blame] | 39 | echo |
| 40 | echo "Please run this script from the project root folder." |
| 41 | } |
| 42 | |
| 43 | |
| 44 | # Gets a part of the version from the project version file (version.h). |
| 45 | # Takes one argument: the matching version identifier in the version file. |
| 46 | get_version_part() |
| 47 | { |
Alexandru N. Onea | 891c04d | 2019-04-09 21:16:34 +0300 | [diff] [blame^] | 48 | local V=$(grep -m 1 "^#define\([ \t]*\)$1" ${VERSION_FILE} | sed 's/[^0-9]*//g') |
Alexandru N. Onea | 3a6abfa | 2019-04-09 01:23:10 +0300 | [diff] [blame] | 49 | |
Alexandru N. Onea | 891c04d | 2019-04-09 21:16:34 +0300 | [diff] [blame^] | 50 | [ ! -z ${V} ] || die "Could not get $1 from ${VERSION_FILE}" |
Alexandru N. Onea | 3a6abfa | 2019-04-09 01:23:10 +0300 | [diff] [blame] | 51 | |
| 52 | echo ${V} |
| 53 | } |
| 54 | |
| 55 | |
| 56 | # Gets the complete version from the version file. |
| 57 | get_version() |
| 58 | { |
| 59 | [ -f ${VERSION_FILE} ] || die "No ${VERSION_FILE} found! Is this the project root?"; |
| 60 | |
| 61 | VERSION_MAJOR=$(get_version_part "TINYALSA_VERSION_MAJOR") |
| 62 | VERSION_MINOR=$(get_version_part "TINYALSA_VERSION_MINOR") |
| 63 | VERSION_PATCH=$(get_version_part "TINYALSA_VERSION_PATCH") |
| 64 | } |
| 65 | |
Alexandru N. Onea | 891c04d | 2019-04-09 21:16:34 +0300 | [diff] [blame^] | 66 | # Commits the new version part to the version file. |
| 67 | # Takes two arguments: the version part identifier in the version file and the |
| 68 | # new version number. |
| 69 | commit_version_part() |
| 70 | { |
| 71 | if [ -z $1 ] || [ -z $2 ]; then |
| 72 | return 0 |
| 73 | fi |
| 74 | |
| 75 | sed -i "s/\(^#define[ \t]*$1\)[ \t]*\([0-9]*\)/\1 $2/g" ${VERSION_FILE} \ |
| 76 | || die "Could not commit version"; |
| 77 | |
| 78 | return 0 |
| 79 | } |
| 80 | |
| 81 | # Commits the new version to the version file. |
| 82 | # Takes three arguments, the new version numbers for major, minor and patch |
| 83 | commit_version() |
| 84 | { |
| 85 | commit_version_part "TINYALSA_VERSION_PATCH" $1 |
| 86 | commit_version_part "TINYALSA_VERSION_MINOR" $2 |
| 87 | commit_version_part "TINYALSA_VERSION_MAJOR" $3 |
| 88 | |
| 89 | return 0 |
| 90 | } |
| 91 | |
Alexandru N. Onea | 3a6abfa | 2019-04-09 01:23:10 +0300 | [diff] [blame] | 92 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 93 | # |
| 94 | # Actions implementations / functions |
| 95 | # |
| 96 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 97 | print_version() |
| 98 | { |
| 99 | get_version |
| 100 | |
| 101 | if [ -z $1 ]; then |
| 102 | printf "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}${LF}" |
| 103 | else |
| 104 | case "$1" in |
| 105 | major) |
| 106 | printf "${VERSION_MAJOR}${LF}" |
| 107 | ;; |
| 108 | minor) |
| 109 | printf "${VERSION_MINOR}${LF}" |
| 110 | ;; |
| 111 | patch) |
| 112 | printf "${VERSION_PATCH}${LF}" |
| 113 | ;; |
| 114 | *) |
| 115 | die "Unknown part \"$1\" (must be one of minor, major and patch)." |
| 116 | ;; |
| 117 | esac |
| 118 | fi |
| 119 | |
| 120 | return 0 |
| 121 | } |
| 122 | |
Alexandru N. Onea | 891c04d | 2019-04-09 21:16:34 +0300 | [diff] [blame^] | 123 | bump_version() |
| 124 | { |
| 125 | get_version |
| 126 | |
| 127 | local PART="patch" |
| 128 | |
| 129 | if [ ! -z $1 ]; then |
| 130 | PART="$1" |
| 131 | fi |
| 132 | |
| 133 | case "$PART" in |
| 134 | major) |
| 135 | VERSION_MAJOR=$((VERSION_MAJOR+1)) |
| 136 | VERSION_MINOR=0 |
| 137 | VERSION_PATCH=0 |
| 138 | ;; |
| 139 | minor) |
| 140 | VERSION_MINOR=$((VERSION_MINOR+1)) |
| 141 | VERSION_PATCH=0 |
| 142 | ;; |
| 143 | patch) |
| 144 | VERSION_PATCH=$((VERSION_PATCH+1)) |
| 145 | ;; |
| 146 | *) |
| 147 | die "Unknown part \"$1\" (must be one of minor, major and patch)." |
| 148 | ;; |
| 149 | esac |
| 150 | |
| 151 | commit_version ${VERSION_PATCH} ${VERSION_MINOR} ${VERSION_MAJOR} |
| 152 | print_version |
| 153 | |
| 154 | return 0 |
| 155 | } |
| 156 | |
Alexandru N. Onea | 3a6abfa | 2019-04-09 01:23:10 +0300 | [diff] [blame] | 157 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 158 | # |
| 159 | # Command Line parsing |
| 160 | # |
| 161 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 162 | parse_command() |
| 163 | { |
| 164 | if [ "$#" -eq "0" ]; then |
| 165 | print_usage |
| 166 | exit 1 |
| 167 | fi |
| 168 | |
| 169 | case "$1" in |
| 170 | print) |
| 171 | print_version "$2" |
| 172 | exit $? |
| 173 | ;; |
Alexandru N. Onea | 891c04d | 2019-04-09 21:16:34 +0300 | [diff] [blame^] | 174 | release) |
| 175 | bump_version "$2" |
| 176 | exit $? |
| 177 | ;; |
Alexandru N. Onea | 3a6abfa | 2019-04-09 01:23:10 +0300 | [diff] [blame] | 178 | *) |
| 179 | die "Unsupported action \"$1\"." |
| 180 | ;; |
| 181 | esac |
| 182 | } |
| 183 | |
| 184 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 185 | # |
| 186 | # Main |
| 187 | # |
| 188 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 189 | |
| 190 | set -e |
| 191 | trap "set +e" 0 |
| 192 | |
| 193 | # Checking parameters |
| 194 | if [ "$#" -eq "0" ]; then |
| 195 | print_usage |
| 196 | exit 0 |
| 197 | fi |
| 198 | |
| 199 | while [ "$#" -ne "0" ]; do |
| 200 | case "$1" in |
| 201 | -s|--script) |
| 202 | unset LF |
| 203 | shift |
| 204 | ;; |
| 205 | --) |
| 206 | shift |
| 207 | break |
| 208 | ;; |
| 209 | -*|--*=) |
| 210 | die "Unsupported flag \"$1\"." |
| 211 | ;; |
| 212 | *) |
| 213 | PARAMS="$PARAMS ${1}" |
| 214 | shift |
| 215 | ;; |
| 216 | esac |
| 217 | done |
| 218 | |
| 219 | # set positional arguments in their proper place |
| 220 | set -- "${PARAMS}" |
| 221 | |
| 222 | parse_command ${PARAMS} |
| 223 | |
| 224 | # The script should never reach this place. |
| 225 | die "Internal error. Please report this." |