blob: 8876c40a55924d78bb21e921081a75b333bcea11 [file] [log] [blame]
Alexandru N. Onea86525572019-04-10 00:30:22 +03001#!/usr/bin/env bash
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +03002
3# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
4#
5# Project configuration variables
6#
7# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8VERSION_FILE="include/tinyalsa/version.h"
Alexandru N. Onea52a79572019-04-09 21:51:27 +03009CHANGELOG_FILE="debian/changelog"
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030010
11# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
12#
13# Scripts internal variables
14#
15# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
16LF="\n"
17PARAMS=""
Alexandru N. Oneacbc69372019-04-10 00:45:38 +030018DRYRUN=0
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030019
20# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21#
22# Helper functions
23#
24# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
25die()
26{
27 echo "Error: $@" 1>&2
28 exit 1
29}
30
31print_usage()
32{
Alexandru N. Onea86525572019-04-10 00:30:22 +030033 echo
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030034 echo "Usage: $0 [OPTIONS] ACTION"
35 echo
36 echo "Available options:"
Alexandru N. Oneacbc69372019-04-10 00:45:38 +030037 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. Onea3a6abfa2019-04-09 01:23:10 +030039 echo
40 echo "Available actions:"
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030041 echo " print [minor|major|patch] Print the current version."
42 echo " release [minor|major|patch] Bump the specified version part"
Alexandru N. Onea86525572019-04-10 00:30:22 +030043 echo " check Check the changelog latest released"
44 echo " version against the version file."
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030045 echo
46 echo "Please run this script from the project root folder."
Alexandru N. Onea86525572019-04-10 00:30:22 +030047 echo
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030048}
49
Alexandru N. Onea86525572019-04-10 00:30:22 +030050check_files()
51{
52 [ -f ${VERSION_FILE} ] || die "No ${VERSION_FILE} found!";
53 [ -f ${CHANGELOG_FILE} ] || die "No ${CHANGELOG_FILE} found!"
54}
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030055
56# Gets a part of the version from the project version file (version.h).
Alexandru N. Onea86525572019-04-10 00:30:22 +030057# Takes one argument: the matching version identifier in the version file, e.g.
58# TINYALSA_VERSION_MAJOR
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030059get_version_part()
60{
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030061 local V=$(grep -m 1 "^#define\([ \t]*\)$1" ${VERSION_FILE} | sed 's/[^0-9]*//g')
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030062
Alexandru N. Onea86525572019-04-10 00:30:22 +030063 if [ -z ${V} ]; then
64 die "Could not get $1 from ${VERSION_FILE}"
65 fi
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030066
67 echo ${V}
68}
69
70
71# Gets the complete version from the version file.
Alexandru N. Oneacbc69372019-04-10 00:45:38 +030072# Sets VERSION_MAJOR, VERSION_MINOR and VERSION_PATCH globals
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030073get_version()
74{
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030075 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. Onea891c04d2019-04-09 21:16:34 +030080# 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. Onea86525572019-04-10 00:30:22 +030082# new version number. If no arguments, do nothing.
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030083commit_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. Onea86525572019-04-10 00:30:22 +030090 || die "Could not commit version for $1";
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030091
Alexandru N. Onea86525572019-04-10 00:30:22 +030092 [ $(get_version_part $1) = "$2" ] || die "Version check after commit failed for $1"
93
94 return 0;
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030095}
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
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300108# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
109#
110# Actions implementations / functions
111#
112# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
113print_version()
114{
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300115 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. Onea891c04d2019-04-09 21:16:34 +0300137bump_version()
138{
Alexandru N. Onea891c04d2019-04-09 21:16:34 +0300139 local PART="patch"
140
141 if [ ! -z $1 ]; then
142 PART="$1"
143 fi
144
145 case "$PART" in
146 major)
147 VERSION_MAJOR=$((VERSION_MAJOR+1))
148 VERSION_MINOR=0
149 VERSION_PATCH=0
150 ;;
151 minor)
152 VERSION_MINOR=$((VERSION_MINOR+1))
153 VERSION_PATCH=0
154 ;;
155 patch)
156 VERSION_PATCH=$((VERSION_PATCH+1))
157 ;;
158 *)
159 die "Unknown part \"$1\" (must be one of minor, major and patch)."
160 ;;
161 esac
162
Alexandru N. Oneacbc69372019-04-10 00:45:38 +0300163 if [ ${DRYRUN} -ne 1 ]; then
164 commit_version ${VERSION_PATCH} ${VERSION_MINOR} ${VERSION_MAJOR}
165 fi
Alexandru N. Onea891c04d2019-04-09 21:16:34 +0300166
Alexandru N. Oneacbc69372019-04-10 00:45:38 +0300167 print_version
Alexandru N. Onea891c04d2019-04-09 21:16:34 +0300168 return 0
169}
170
Alexandru N. Onea52a79572019-04-09 21:51:27 +0300171check_version()
172{
Alexandru N. Onea86525572019-04-10 00:30:22 +0300173 local LOG_VERSION=$(grep -m 1 "^tinyalsa (" ${CHANGELOG_FILE}| sed "s/[^0-9.]*//g")
174 local REF_VERSION="${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}"
Alexandru N. Onea52a79572019-04-09 21:51:27 +0300175
176 if [ "${LOG_VERSION}" != "${REF_VERSION}" ]; then
177 die "Changelog version (${LOG_VERSION}) does not match package version (${REF_VERSION})."
178 fi
179
180 printf "Changelog version (${LOG_VERSION}) OK!${LF}"
181 return 0
182}
183
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300184# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
185#
186# Command Line parsing
187#
188# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
189parse_command()
190{
191 if [ "$#" -eq "0" ]; then
192 print_usage
193 exit 1
194 fi
195
196 case "$1" in
197 print)
Alexandru N. Oneacbc69372019-04-10 00:45:38 +0300198 get_version
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300199 print_version "$2"
200 exit $?
201 ;;
Alexandru N. Onea891c04d2019-04-09 21:16:34 +0300202 release)
Alexandru N. Oneacbc69372019-04-10 00:45:38 +0300203 get_version
Alexandru N. Onea891c04d2019-04-09 21:16:34 +0300204 bump_version "$2"
205 exit $?
206 ;;
Alexandru N. Onea52a79572019-04-09 21:51:27 +0300207 check)
Alexandru N. Oneacbc69372019-04-10 00:45:38 +0300208 get_version
Alexandru N. Onea52a79572019-04-09 21:51:27 +0300209 check_version
210 exit $?
211 ;;
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300212 *)
213 die "Unsupported action \"$1\"."
214 ;;
215 esac
216}
217
218# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
219#
220# Main
221#
222# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
223
224set -e
225trap "set +e" 0
226
227# Checking parameters
228if [ "$#" -eq "0" ]; then
229 print_usage
230 exit 0
231fi
232
233while [ "$#" -ne "0" ]; do
234 case "$1" in
235 -s|--script)
236 unset LF
237 shift
238 ;;
Alexandru N. Oneacbc69372019-04-10 00:45:38 +0300239 -d|--dry-run)
240 DRYRUN=1
241 shift
242 ;;
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300243 --)
244 shift
245 break
246 ;;
247 -*|--*=)
248 die "Unsupported flag \"$1\"."
249 ;;
250 *)
251 PARAMS="$PARAMS ${1}"
252 shift
253 ;;
254 esac
255done
256
257# set positional arguments in their proper place
258set -- "${PARAMS}"
259
Alexandru N. Onea86525572019-04-10 00:30:22 +0300260check_files
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300261parse_command ${PARAMS}
262
263# The script should never reach this place.
264die "Internal error. Please report this."