Skip to content

Commit 3e7a15e

Browse files
author
Amir Tocker
committed
Add the tools folder and the update_version script
1 parent 4597bd0 commit 3e7a15e

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

tools/update_version

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
#!/usr/bin/env bash
2+
3+
# Update version number and prepare for publishing the new version
4+
5+
set -e
6+
7+
# Empty to run the rest of the line and "echo" for a dry run
8+
CMD_PREFIX=
9+
10+
# Add a quote if this is a dry run
11+
QUOTE=
12+
13+
NEW_VERSION=
14+
15+
function echo_err
16+
{
17+
echo "$@" 1>&2;
18+
}
19+
20+
function usage
21+
{
22+
echo "Usage: $0 [parameters]"
23+
echo " -v | --version <version> set a new version"
24+
echo " -c | --current show current version"
25+
echo " -d | --dry-run print the commands without executing them"
26+
echo " -h | --help print this information and exit"
27+
echo
28+
echo "For example: $0 -v 1.2.3"
29+
}
30+
31+
function process_arguments
32+
{
33+
while [ "$1" != "" ]; do
34+
case $1 in
35+
-v | --version )
36+
shift
37+
NEW_VERSION=${1:-}
38+
if ! [[ "${NEW_VERSION}" =~ [0-9]+\.[0-9]+\.[0-9]+(\-.+)? ]]; then
39+
echo_err "You must supply a new version after -v or --version"
40+
echo_err "For example:"
41+
echo_err " 1.2.3"
42+
echo_err " 1.2.3-rc1"
43+
echo_err ""
44+
usage; return 1
45+
fi
46+
;;
47+
-c | --current )
48+
echo `current_version`
49+
exit
50+
;;
51+
-d | --dry-run )
52+
CMD_PREFIX=echo
53+
echo "Dry Run"
54+
echo ""
55+
;;
56+
-h | --help )
57+
usage; return 0
58+
;;
59+
* )
60+
usage; return 1
61+
esac
62+
shift || true
63+
done
64+
}
65+
66+
# Intentionally make pushd silent
67+
function pushd
68+
{
69+
command pushd "$@" > /dev/null
70+
}
71+
72+
# Intentionally make popd silent
73+
function popd
74+
{
75+
command popd "$@" > /dev/null
76+
}
77+
78+
# Check if one version is less than or equal than other
79+
# Example:
80+
# ver_lte 1.2.3 1.2.3 && echo "yes" || echo "no" # yes
81+
# ver_lte 1.2.3 1.2.4 && echo "yes" || echo "no" # yes
82+
# ver_lte 1.2.4 1.2.3 && echo "yes" || echo "no" # no
83+
function ver_lte
84+
{
85+
[ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
86+
}
87+
88+
# Extract the last entry or entry for a given version
89+
# The function is not currently used in this file.
90+
# Examples:
91+
# changelog_last_entry
92+
# changelog_last_entry 1.10.0
93+
#
94+
function changelog_last_entry
95+
{
96+
sed -e "1,/^${1}/d" -e '/^=/d' -e '/^$/d' -e '/^[0-9]/,$d' CHANGELOG.md
97+
}
98+
99+
function verify_dependencies
100+
{
101+
# Test if the gnu grep is installed
102+
if ! grep --version | grep -q GNU
103+
then
104+
echo_err "GNU grep is required for this script"
105+
echo_err "You can install it using the following command:"
106+
echo_err ""
107+
echo_err "brew install grep --with-default-names"
108+
return 1
109+
fi
110+
111+
if [[ -z "$(type -t git-changelog)" ]]
112+
then
113+
echo_err "git-extras packages is not installed."
114+
echo_err "You can install it using the following command:"
115+
echo_err ""
116+
echo_err "brew install git-extras"
117+
return 1
118+
fi
119+
}
120+
121+
# Replace old string only if it is present in the file, otherwise return 1
122+
function safe_replace
123+
{
124+
local old=$1
125+
local new=$2
126+
local file=$3
127+
128+
grep -q "${old}" "${file}" || { echo_err "${old} was not found in ${file}"; return 1; }
129+
130+
${CMD_PREFIX} sed -E -i '.bak' "${QUOTE}s/${old}/${new}/${QUOTE}" "${file}"
131+
}
132+
133+
function current_version
134+
{
135+
grep -oiP '(?<=version": ")([0-9.]+)(?=")' package.json
136+
}
137+
138+
function update_version
139+
{
140+
if [ -z "${NEW_VERSION}" ]; then
141+
usage; return 1
142+
fi
143+
144+
# Enter git root
145+
pushd $(git rev-parse --show-toplevel)
146+
local current_version=$(current_version)
147+
148+
if [ -z "${current_version}" ]; then
149+
echo_err "Failed getting current version, please check directory structure and/or contact developer"
150+
return 1
151+
fi
152+
153+
# Use literal dot character in regular expression
154+
local current_version_re=${current_version//./\\.}
155+
156+
echo "# Current version is: ${current_version}"
157+
echo "# New version is: ${NEW_VERSION}"
158+
159+
ver_lte "${NEW_VERSION}" "${current_version}" && { echo_err "New version is not greater than current version"; return 1; }
160+
161+
# Add a quote if this is a dry run
162+
QUOTE=${CMD_PREFIX:+"'"}
163+
164+
npm version --no-git-tag-version "${NEW_VERSION}"
165+
166+
${CMD_PREFIX} git changelog -t ${NEW_VERSION} || true
167+
168+
echo ""
169+
echo "# After editing CHANGELOG.md, optionally review changes and issue these commands:"
170+
echo git add package.json CHANGELOG.md
171+
echo git commit -m \"Version ${NEW_VERSION}\"
172+
echo sed -e "'1,/^${NEW_VERSION//./\\.}/d'" \
173+
-e "'/^=/d'" \
174+
-e "'/^$/d'" \
175+
-e "'/^[0-9]/,\$d'" \
176+
CHANGELOG.md \
177+
\| git tag -a "'${NEW_VERSION}'" --file=-
178+
179+
# Don't run those commands on dry run
180+
[ -n "${CMD_PREFIX}" ] && { popd; return 0; }
181+
182+
echo ""
183+
read -p "Run the above commands automatically? (y/N): " confirm && [[ ${confirm} == [yY] || ${confirm} == [yY][eE][sS] ]] || { popd; return 0; }
184+
185+
git add package.json CHANGELOG.md
186+
git commit -m "Version ${NEW_VERSION}"
187+
sed -e "1,/^${NEW_VERSION//./\\.}/d" \
188+
-e "/^=/d" \
189+
-e "/^$/d" \
190+
-e "/^[0-9]/,\$d" \
191+
CHANGELOG.md \
192+
| git tag -a "${NEW_VERSION}" --file=-
193+
194+
popd
195+
}
196+
197+
verify_dependencies
198+
process_arguments $*
199+
update_version

0 commit comments

Comments
 (0)