Files
version-increment/shared.sh

115 lines
4.2 KiB
Bash
Raw Permalink Normal View History

2021-10-20 11:18:23 +11:00
#!/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
2021-10-21 23:38:24 +11:00
##==----------------------------------------------------------------------------
## SemVer regexes
## see: https://semver.org/spec/v2.0.0.html#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
2021-10-20 11:18:23 +11:00
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}"
2021-10-20 11:18:23 +11:00
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
2024-08-31 15:51:39 +10:00
escaped_prefix=$(printf '%s\n' "$tag_prefix" | sed 's/[][\/.^$*]/\\&/g') # special chars matched by sed: ] [ / . ^ $ *
2024-08-31 15:51:39 +10:00
# shellcheck disable=SC2143
2024-08-31 14:16:34 +10:00
if [[ -z "$(echo "${tag}" | grep "^${tag_prefix}")" ]] ; then
echo ""
return
fi
2024-08-31 15:51:39 +10:00
# 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:'
2021-10-21 23:38:24 +11:00
##==----------------------------------------------------------------------------
## 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
2021-10-21 23:38:24 +11:00
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:-}"
2021-10-20 11:18:23 +11:00
##==----------------------------------------------------------------------------
2024-04-24 10:58:22 +10:00
## MacOS compatibility
2021-10-20 11:18:23 +11:00
2024-04-24 10:58:22 +10:00
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
2021-10-20 11:18:23 +11:00
echo "🛑 GNU grep not installed, try brew install coreutils" 1>&2
exit 9
2024-04-24 10:58:22 +10:00
else
export use_gnugrep="true"
2021-10-20 11:18:23 +11:00
fi
fi
2022-10-19 08:51:07 +11:00
2024-04-24 10:58:22 +10:00
function grep_p() {
if [[ "${use_perl}" == 'true' ]] ; then
perl -ne "print if /${1}/"
2024-04-24 10:58:22 +10:00
elif [[ "${use_gnugrep}" == 'true' ]] ; then
# shellcheck disable=SC2086
ggrep -P "${1}"
2024-04-24 10:58:22 +10:00
else
# shellcheck disable=SC2086
command grep -P "${1}"
2024-04-24 10:58:22 +10:00
fi
}
2022-10-19 08:51:07 +11:00
##==----------------------------------------------------------------------------
## Non GitHub compatibility - for testing both locally and in BATS
if [[ -z "${GITHUB_OUTPUT:-}" || -n "${BATS_VERSION:-}" ]] ; then
export GITHUB_OUTPUT="/dev/stdout"
fi