forked from tegonal/github-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-version-README.sh
More file actions
executable file
·91 lines (81 loc) · 4.18 KB
/
update-version-README.sh
File metadata and controls
executable file
·91 lines (81 loc) · 4.18 KB
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
#!/usr/bin/env bash
#
# __ __
# / /____ ___ ____ ___ ___ _/ / This script is provided to you by https://github.com/tegonal/scripts
# / __/ -_) _ `/ _ \/ _ \/ _ `/ / Copyright 2022 Tegonal Genossenschaft <info@tegonal.com>
# \__/\__/\_, /\___/_//_/\_,_/_/ It is licensed under Apache License 2.0
# /___/ Please report bugs and contribute back your improvements
#
# Version: v4.11.0
####### Description #############
#
# Replaces the version used in download badge(s) and in the sneak peek banner
#
####### Usage ###################
#
# #!/usr/bin/env bash
# set -euo pipefail
# shopt -s inherit_errexit || { echo >&2 "please update to bash 5, see errors above" && exit 1; }
# # Assumes tegonal's scripts were fetched with gt - adjust location accordingly
# dir_of_tegonal_scripts="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null && pwd 2>/dev/null)/../lib/tegonal-scripts/src"
# source "$dir_of_tegonal_scripts/setup_tegonal_scripts.sh" "$dir_of_tegonal_scripts"
#
# "$dir_of_tegonal_scripts/releasing/update-version-README.sh" -v 0.1.0
#
# # if you use it in combination with other tegonal-scripts files, then you might want to source it instead
# sourceOnce "$dir_of_tegonal_scripts/releasing/update-version-README.sh"
#
# # and then call the function
# updateVersionReadme -v 0.2.0
#
###################################
set -euo pipefail
shopt -s inherit_errexit || { echo >&2 "please update to bash 5, see errors above" && exit 1; }
unset CDPATH
export TEGONAL_SCRIPTS_VERSION='v4.11.0'
if ! [[ -v dir_of_tegonal_scripts ]]; then
dir_of_tegonal_scripts="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null && pwd 2>/dev/null)/.."
source "$dir_of_tegonal_scripts/setup_tegonal_scripts.sh" "$dir_of_tegonal_scripts"
fi
sourceOnce "$dir_of_tegonal_scripts/utility/parse-args.sh"
function updateVersionReadme() {
source "$dir_of_tegonal_scripts/releasing/common-constants.source.sh" || traceAndDie "could not source common-constants.source.sh"
local version file additionalPattern
# shellcheck disable=SC2034 # is passed by name to parseArguments
local -ra params=(
version "$versionParamPattern" "$versionParamDocu"
file '-f|--file' '(optional) the file where search & replace shall be done -- default: ./README.md'
additionalPattern "$additionalPatternParamPattern" "$additionalPatternParamDocu"
)
local -r examples=$(
# shellcheck disable=SC2312 # cat shouldn't fail for a constant string hence fine to ignore exit code
cat <<-EOM
# update version for ./README.md
update-version-README.sh -v v0.1.0
# update version for ./docs/index.md
update-version-README.sh -v v0.1.0 -f ./docs/index.md
# update version for ./README.md
# also replace occurrences of the defined pattern
update-version-README.sh -v v0.1.0 -p "(VERSION=['\"])[^'\"]+(['\"])"
EOM
)
parseArguments params "$examples" "$TEGONAL_SCRIPTS_VERSION" "$@" || return $?
if ! [[ -v file ]]; then file="./README.md"; fi
if ! [[ -v additionalPattern ]]; then additionalPattern=""; fi
exitIfNotAllArgumentsSet params "$examples" "$TEGONAL_SCRIPTS_VERSION"
echo "set version $version for Download badges and sneak peek banner in $file"
local -r versionWithoutLeadingV="${version:1}"
perl -0777 -i \
-pe "s@(\[!\[Download\]\(https://img.shields.io/badge/Download-).*(-%23[0-9a-f]+\)\]\([^\)]+(?:=|/))v[^\)]+\)@\${1}${version//-//--}\${2}$version\)@g;" \
-pe "s@(\[!\[Download\]\(https://img.shields.io/badge/Download-).*(-%23[0-9a-f]+\)\]\([^\)]+(?:=|/))[0-9][^\)]+\)@\${1}${version//-//--}\${2}$versionWithoutLeadingV\)@g;" \
-pe "s@(\[README of )[^\]]+(\].*/tree/)[^/]+/@\${1}$version\${2}$version/@;" \
"$file" || returnDying "was not able to update the version in download badges and in the sneak peek banner" || return $?
if [[ -n $additionalPattern ]]; then
echo "also going to search for $additionalPattern and replace with \${1}$version\${2}"
perl -0777 -i \
-pe "s/$additionalPattern/\${1}$version\${2}/g;" \
"$file" || returnDying "error during the additional replacement, see above" || return $?
fi
}
${__SOURCED__:+return}
updateVersionReadme "$@"