xref: /aosp_15_r20/external/tinyalsa_new/scripts/version.sh (revision 02e95f1a335b55495d41ca67eaf42361f13704fa)
1#!/bin/sh
2
3# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
4#
5#   Project configuration variables
6#
7# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8VERSION_FILE="include/tinyalsa/version.h"
9CHANGELOG_FILE="debian/changelog"
10
11# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
12#
13#   Scripts internal variables
14#
15# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
16LF="\n"
17PARAMS=""
18DRYRUN=0
19
20# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21#
22#   Helper functions
23#
24# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
25die()
26{
27  echo "Error: $@" 1>&2
28  exit 1
29}
30
31print_usage()
32{
33  echo
34  echo "Usage: $0 [OPTIONS] ACTION"
35  echo
36  echo "Available options:"
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."
39  echo
40  echo "Available actions:"
41  echo "  print   [minor|major|patch]  Print the current version."
42  echo "  release [minor|major|patch]  Bump the specified version part"
43  echo "  check                        Check the changelog latest released"
44  echo "                               version against the version file."
45  echo
46  echo "Please run this script from the project root folder."
47  echo
48}
49
50check_files()
51{
52  [ -f ${VERSION_FILE}   ] || die "No ${VERSION_FILE} found!";
53  [ -f ${CHANGELOG_FILE} ] || die "No ${CHANGELOG_FILE} found!"
54}
55
56# Gets a part of the version from the project version file (version.h).
57# Takes one argument: the matching version identifier in the version file, e.g.
58#   TINYALSA_VERSION_MAJOR
59get_version_part()
60{
61  set -- "$1" "$(grep -m 1 "^#define\([ \t]*\)$1" ${VERSION_FILE} | sed 's/[^0-9]*//g')"
62
63  if [ -z "$2" ]; then
64    die "Could not get $1 from ${VERSION_FILE}"
65  fi
66
67  echo "$2"
68}
69
70
71# Gets the complete version from the version file.
72# Sets VERSION_MAJOR, VERSION_MINOR and VERSION_PATCH globals
73get_version()
74{
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
80# Commits the new version part to the version file.
81# Takes two arguments: the version part identifier in the version file and the
82#   new version number. If no arguments, do nothing.
83commit_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} \
90    || die "Could not commit version for $1";
91
92  [ $(get_version_part $1) = "$2" ] || die "Version check after commit failed for $1"
93
94  return 0;
95}
96
97# Commits the new version to the version file.
98# Takes three arguments, the new version numbers for major, minor and patch
99commit_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
108# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
109#
110#   Actions implementations / functions
111#
112# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
113print_version()
114{
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
137bump_version()
138{
139  case "${1:-patch}" in
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
157  if [ ${DRYRUN} -ne 1 ]; then
158    commit_version ${VERSION_PATCH} ${VERSION_MINOR} ${VERSION_MAJOR}
159  fi
160
161  print_version
162  return 0
163}
164
165check_version()
166{
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}"
171
172  if [ "$1" != "$2" ]; then
173    die "Changelog version ($1) does not match package version ($2)."
174  fi
175
176  printf "Changelog version ($1) OK!${LF}"
177  return 0
178}
179
180# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
181#
182#   Command Line parsing
183#
184# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
185parse_command()
186{
187  if [ "$#" -eq "0" ]; then
188    print_usage
189    exit 1
190  fi
191
192  case "$1" in
193    print)
194      get_version
195      print_version "$2"
196      exit $?
197      ;;
198    release)
199      get_version
200      bump_version "$2"
201      exit $?
202      ;;
203    check)
204      get_version
205      check_version
206      exit $?
207      ;;
208    *)
209      die "Unsupported action \"$1\"."
210      ;;
211  esac
212}
213
214# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
215#
216#   Main
217#
218# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
219
220set -e
221trap "set +e" 0
222
223# Checking parameters
224if [ "$#" -eq "0" ]; then
225  print_usage
226  exit 0
227fi
228
229while [ "$#" -ne "0" ]; do
230  case "$1" in
231    -s|--script)
232      unset LF
233      shift
234      ;;
235    -d|--dry-run)
236      DRYRUN=1
237      shift
238      ;;
239    --)
240      shift
241      break
242      ;;
243    -*|--*=)
244      die "Unsupported flag \"$1\"."
245      ;;
246    *)
247      PARAMS="$PARAMS ${1}"
248      shift
249      ;;
250  esac
251done
252
253# set positional arguments in their proper place
254set -- "${PARAMS}"
255
256check_files
257parse_command ${PARAMS}
258
259# The script should never reach this place.
260die "Internal error. Please report this."
261