aboutsummaryrefslogtreecommitdiff
path: root/scripts/version-bump.sh
blob: 287580befed3d458a815935cb439285342962fa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
#   Project configuration variables
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
VERSION_FILE="include/tinyalsa/version.h"

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
#   Scripts internal variables
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
LF="\n"
PARAMS=""

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
#   Helper functions
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
die()
{
  echo "Error: $@" 1>&2
  exit 1
}

print_usage()
{
  echo "Usage: $0 [OPTIONS] ACTION"
  echo
  echo "Available options:"
  echo "  -s,--script  Format output in \"script\" mode (no trailing newline)."
  echo
  echo "Available actions:"
  echo "  print   [minor|major|patch]  Print the current version."
  echo "  release [minor|major|patch]  Bump the specified version part"
  echo
  echo "Please run this script from the project root folder."
}


# Gets a part of the version from the project version file (version.h).
# Takes one argument: the matching version identifier in the version file.
get_version_part()
{
  local V=$(grep -m 1 "^#define\([ \t]*\)$1" ${VERSION_FILE} | sed 's/[^0-9]*//g')

  [ ! -z ${V} ] || die "Could not get $1 from ${VERSION_FILE}"

  echo ${V}
}


# Gets the complete version from the version file.
get_version()
{
  [ -f ${VERSION_FILE} ] || die "No ${VERSION_FILE} found! Is this the project root?";

  VERSION_MAJOR=$(get_version_part "TINYALSA_VERSION_MAJOR")
  VERSION_MINOR=$(get_version_part "TINYALSA_VERSION_MINOR")
  VERSION_PATCH=$(get_version_part "TINYALSA_VERSION_PATCH")
}

# Commits the new version part to the version file.
# Takes two arguments: the version part identifier in the version file and the
#   new version number.
commit_version_part()
{
  if [ -z $1 ] || [ -z $2 ]; then
    return 0
  fi

  sed -i "s/\(^#define[ \t]*$1\)[ \t]*\([0-9]*\)/\1 $2/g" ${VERSION_FILE} \
    || die "Could not commit version";

  return 0
}

# Commits the new version to the version file.
# Takes three arguments, the new version numbers for major, minor and patch
commit_version()
{
  commit_version_part "TINYALSA_VERSION_PATCH" $1
  commit_version_part "TINYALSA_VERSION_MINOR" $2
  commit_version_part "TINYALSA_VERSION_MAJOR" $3

  return 0
}

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
#   Actions implementations / functions
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
print_version()
{
  get_version

  if [ -z $1 ]; then
    printf "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}${LF}"
  else
    case "$1" in
      major)
        printf "${VERSION_MAJOR}${LF}"
        ;;
      minor)
        printf "${VERSION_MINOR}${LF}"
        ;;
      patch)
        printf "${VERSION_PATCH}${LF}"
        ;;
      *)
        die "Unknown part \"$1\" (must be one of minor, major and patch)."
        ;;
    esac
  fi

  return 0
}

bump_version()
{
  get_version

  local PART="patch"

  if [ ! -z $1 ]; then
    PART="$1"
  fi

  case "$PART" in
    major)
      VERSION_MAJOR=$((VERSION_MAJOR+1))
      VERSION_MINOR=0
      VERSION_PATCH=0
    ;;
    minor)
      VERSION_MINOR=$((VERSION_MINOR+1))
      VERSION_PATCH=0
    ;;
    patch)
      VERSION_PATCH=$((VERSION_PATCH+1))
    ;;
    *)
      die "Unknown part \"$1\" (must be one of minor, major and patch)."
    ;;
  esac

  commit_version ${VERSION_PATCH} ${VERSION_MINOR} ${VERSION_MAJOR}
  print_version

  return 0
}

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
#   Command Line parsing
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
parse_command()
{
  if [ "$#" -eq "0" ]; then
    print_usage
    exit 1
  fi

  case "$1" in
    print)
      print_version "$2"
      exit $?
      ;;
    release)
      bump_version "$2"
      exit $?
      ;;
    *)
      die "Unsupported action \"$1\"."
      ;;
  esac
}

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
#   Main
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

set -e
trap "set +e" 0

# Checking parameters
if [ "$#" -eq "0" ]; then
  print_usage
  exit 0
fi

while [ "$#" -ne "0" ]; do
  case "$1" in
    -s|--script)
      unset LF
      shift
      ;;
    --)
      shift
      break
      ;;
    -*|--*=)
      die "Unsupported flag \"$1\"."
      ;;
    *)
      PARAMS="$PARAMS ${1}"
      shift
      ;;
  esac
done

# set positional arguments in their proper place
set -- "${PARAMS}"

parse_command ${PARAMS}

# The script should never reach this place.
die "Internal error. Please report this."