forked from Limych/ha-average
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease
executable file
·127 lines (106 loc) · 3.58 KB
/
release
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
#!/usr/bin/env bash
# Stop on errors
set -e
ROOT="$( cd "$( dirname "$(readlink -f "$0")" )/.." >/dev/null 2>&1 && pwd )"
cd "${ROOT}"
# Load common functions
source ./bin/_common
NAT='0|[1-9][0-9]*'
ALPHANUM='[0-9]*[A-Za-z-][0-9A-Za-z-]*'
IDENT="$NAT|$ALPHANUM"
FIELD='[0-9A-Za-z-]+'
SEMVER_REGEX="\
^($NAT)\\.($NAT)\\.($NAT)\
(\\-(${IDENT})(\\.(${IDENT}))*)?\
(\\+${FIELD}(\\.${FIELD})*)?$"
function validate_version {
local version=$1
if [[ "$version" =~ $SEMVER_REGEX ]]; then
# if a second argument is passed, store the result in var named by $2
if test "$#" -eq "2"; then
local major=${BASH_REMATCH[1]}
local minor=${BASH_REMATCH[2]}
local patch=${BASH_REMATCH[3]}
local prere=${BASH_REMATCH[4]}
local build=${BASH_REMATCH[8]}
eval "$2=(\"$major\" \"$minor\" \"$patch\" \"$prere\" \"$build\")"
else
echo "$version"
fi
else
die "Version $version does not match the SemVer scheme 'X.Y.Z(-PRERELEASE)(+BUILD)'"
fi
}
PREFIX_ALPHANUM='[.0-9A-Za-z-]*[.A-Za-z-]'
DIGITS='[0-9][0-9]*'
EXTRACT_REGEX="^(${PREFIX_ALPHANUM})*(${DIGITS})$"
function extract_prerel {
local prefix; local numeric;
if [[ "$1" =~ $EXTRACT_REGEX ]]; then
# found prefix and trailing numeric parts
prefix="${BASH_REMATCH[1]}"
numeric="${BASH_REMATCH[2]}"
else
# no numeric part
prefix="${1}"
numeric=
fi
eval "$2=(\"$prefix\" \"$numeric\")"
}
function bump_prerel {
validate_version "${1}" parts
# shellcheck disable=SC2154
local major="${parts[0]}"
local minor="${parts[1]}"
local patch="${parts[2]}"
extract_prerel "${parts[3]#-}" prerel_parts # extract parts of previous pre-release
# shellcheck disable=SC2154
prev_prefix="${prerel_parts[0]}"
prev_numeric="${prerel_parts[1]}"
if test -n "$prev_numeric"; then
# previous pre-release is already numbered, bump it
: $(( ++prev_numeric ))
echo "$(validate_version "${major}.${minor}.${patch}-${prev_prefix}${prev_numeric}")"
else
# append starting number
echo "$(validate_version "${major}.${minor}.${patch}-${prev_prefix}2")"
fi
}
if output=$(git branch --show-current) \
&& [[ "$output" != "master" && "$output" != "main" && "$output" != "dev" ]]; then
log.fatal "Please, change HEAD to 'master', 'main' or 'dev' branch."
log.info "At now HEAD at '${output}' branch."
exit 1
fi
if output=$(git status --porcelain) && [[ -n "$output" ]]; then
log.fatal "Please, make working tree clean first."
log.info ${output}
exit 2
fi
if ! pytest; then
exit 3
fi
const_path=$(find custom_components/ -name const.py)
version=$(grep "^VERSION: Final =" ${const_path} | sed -E "s/^[^\"]+\"([^\"]*).*$/\\1/")
if [[ -z $1 ]]; then
log.fatal "Please, describe new version number as first argument."
log.info "Current version number is ${version}"
exit 3
fi
new=$(validate_version "${1}")
log.info "Patch files to version '${new}'..."
sed -i -E "s/(^VERSION: Final = \")[^\"]*/\\1${new}/" ${const_path}
./bin/update_manifest
if output=$(git status --porcelain) && [[ -n "$output" ]]; then
git commit -a --no-verify -m "Bump version to ${new}"
fi
git tag -a "$new" -m "v$new"
log.info "Commit tagged as v$new"
if echo "${new}" | grep -q "-"; then
dev=$(bump_prerel "${new}")
else
dev="$(echo "${new}" | awk -F. '/[0-9]+\./{$NF++;print}' OFS=.)-alpha"
fi
log.info "Patch files to version '${dev}'..."
sed -i -E "s/(^VERSION: Final = \")[^\"]*/\\1${dev}/" ${const_path}
./bin/update_manifest