-
Notifications
You must be signed in to change notification settings - Fork 15
/
shared.sh
114 lines (95 loc) · 4.23 KB
/
shared.sh
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
#!/bin/bash
# shellcheck disable=SC2034
set -euo pipefail
# Force UTF-8 for all commands, for Git-Bash on Windows compatibility
export LC_ALL=C.UTF-8
##==----------------------------------------------------------------------------
## SemVer regexes
## see: https://semver.org/spec/v2.0.0.html#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
pcre_semver='^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$'
pcre_master_ver='^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)$'
pcre_allow_vprefix="^v{0,1}${pcre_master_ver:1}"
pcre_old_calver='^(?P<major>0|[1-9]\d*)-0{0,1}(?P<minor>0|[0-9]\d*)-R(?P<patch>0|[1-9]\d*)$'
##==----------------------------------------------------------------------------
## Utility function for removing tag_prefix if present
remove_prefix() {
local tag="$1"
if [[ -n "${tag_prefix:-}" ]]; then
# Escape special characters in tag_prefix
local escaped_prefix
escaped_prefix=$(printf '%s\n' "$tag_prefix" | sed 's/[][\/.^$*]/\\&/g') # special chars matched by sed: ] [ / . ^ $ *
# shellcheck disable=SC2143
if [[ -z "$(echo "${tag}" | grep "^${tag_prefix}")" ]] ; then
echo ""
return
fi
# shellcheck disable=SC2001
echo "${tag}" | sed "s|^${escaped_prefix}||" # Use | as the delimiter to avoid conflicts with /
else
echo "${tag}"
fi
}
##==----------------------------------------------------------------------------
## Conventional commit regexes
## see: https://www.conventionalcommits.org/en/v1.0.0/
pcre_conventional_commit_patch='^(build|chore|ci|docs|fix|perf|refactor|revert|style|test)(\([a-zA-Z0-9-]+\))?:\s.*'
pcre_conventional_commit_minor='^(feat)(\([a-zA-Z0-9-]+\))?:\s.*'
pcre_conventional_commit_breaking='^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-zA-Z0-9-]+\))?!:.*|BREAKING CHANGE:'
##==----------------------------------------------------------------------------
## Input validation
input_errors='false'
scheme="${scheme:-semver}"
if [[ "${scheme}" != 'semver' && "${scheme}" != 'calver' && "${scheme}" != 'conventional_commits' ]] ; then
echo "🛑 Value of 'scheme' is not valid, choose from 'semver', 'calver' or 'conventional_commits" 1>&2
input_errors='true'
fi
pep440="${pep440:-false}"
if [[ "${pep440}" != 'false' && "${pep440}" != 'true' ]] ; then
echo "🛑 Value of 'pep440' is not valid, choose from 'false' or 'true'" 1>&2
input_errors='true'
fi
use_api="${use_api:-false}"
if [[ "${use_api}" != 'false' && "${use_api}" != 'true' ]] ; then
echo "🛑 Value of 'use_api' is not valid, choose from 'false' or 'true'" 1>&2
input_errors='true'
fi
if [[ "${use_api}" == 'true' ]] ; then
if [[ -z "${github_token:-}" ]] ; then
echo "🛑 'use_api' is true, but environment variable 'github_token' is not set" 1>&2
input_errors='true'
fi
fi
# Check if the tag_prefix is set, and if not, set it to an empty string
tag_prefix="${tag_prefix:-}"
##==----------------------------------------------------------------------------
## MacOS compatibility
export use_perl="false"
export use_gnugrep="false"
if [[ "${GITHUB_ACTIONS:-}" == 'true' && "$(uname)" == 'Darwin' ]] ; then
export use_perl="true"
elif [[ "$(uname)" == 'Darwin' ]] ; then
if perl --version 1>/dev/null ; then
export use_perl="true"
elif ! ggrep --version 1>/dev/null ; then
echo "🛑 GNU grep not installed, try brew install coreutils" 1>&2
exit 9
else
export use_gnugrep="true"
fi
fi
function grep_p() {
if [[ "${use_perl}" == 'true' ]] ; then
perl -ne "print if /${1}/"
elif [[ "${use_gnugrep}" == 'true' ]] ; then
# shellcheck disable=SC2086
ggrep -P "${1}"
else
# shellcheck disable=SC2086
command grep -P "${1}"
fi
}
##==----------------------------------------------------------------------------
## Non GitHub compatibility - for testing both locally and in BATS
if [[ -z "${GITHUB_OUTPUT:-}" || -n "${BATS_VERSION:-}" ]] ; then
export GITHUB_OUTPUT="/dev/stdout"
fi