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