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