Skip to content

Commit

Permalink
update bump-version.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Apr 28, 2024
1 parent c787edd commit 0b1ddc6
Showing 1 changed file with 41 additions and 40 deletions.
81 changes: 41 additions & 40 deletions update/bump-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,40 @@
set -e

invalid_args() {
echo "Usage: $(basename $0) <old_version> <new_version>|major|minor|patch"
echo "Usage: $(basename $0) <new_version>|major|minor|patch"
exit 1
}

match_line() {
grep --quiet --fixed-strings "$@"
}

version_pattern='([0-9]+)\.([0-9]+)\.([0-9]+)'
get_current_version() {
local file="$1"
local keyword="$2"
local line="$(grep --fixed-strings "set ${keyword}=" "${file}")"
local pattern='set '"${keyword}"'=([0-9]+)'
if [[ "${line}" =~ $pattern ]]; then
echo ${BASH_REMATCH[1]}
else
echo 0
fi
}

old_version=$1
new_version=$2
echo "old_version='$old_version'"
echo "new_version='$new_version'"
old_major=$(get_current_version build.bat VERSION_MAJOR)
old_minor=$(get_current_version build.bat VERSION_MINOR)
old_patch=$(get_current_version build.bat VERSION_PATCH)

if [[ $old_version =~ $version_pattern ]]; then
old_major=${BASH_REMATCH[1]}
old_minor=${BASH_REMATCH[2]}
old_patch=${BASH_REMATCH[3]}
else
invalid_args
fi
old_version="${old_major}.${old_minor}.${old_patch}"

new_version=$1

new_major=$old_major
new_minor=$old_minor
new_patch=$old_patch

version_pattern='([0-9]+)\.([0-9]+)\.([0-9]+)'

if [[ $new_version =~ $version_pattern ]]; then
new_major=${BASH_REMATCH[1]}
new_minor=${BASH_REMATCH[2]}
Expand All @@ -47,18 +54,24 @@ else
invalid_args
fi

# verify old_version is current version.
match_line "WEASEL_VERSION_STR \"${old_version}\"" include/WeaselVersion.h &&
match_line "WEASEL_VERSION=${old_version}" build.bat &&
match_line "version: ${old_version}" appveyor.yml || (
echo >&2 "${old_version} doesn't match current version."
exit 1
)

old_version="${old_major}.${old_minor}.${old_patch}"
new_version="${new_major}.${new_minor}.${new_patch}"
echo "updating ${old_version} => ${new_version}"

update_version_number() {
local file="$1"
local variable="$2"
local old_value="$3"
local new_value="$4"
if [[ "${old_value}" == "${new_value}" ]]; then
return
fi
sed -i'~' "s/set ${variable}=[0-9]*/set ${variable}=${new_value}/" "${file}"
rm "${file}~"
}

update_version_number build.bat VERSION_MAJOR $old_major $new_major
update_version_number build.bat VERSION_MINOR $old_minor $new_minor
update_version_number build.bat VERSION_PATCH $old_patch $new_patch

if [[ $OSTYPE =~ darwin ]]; then
L_BOUND='[[:<:]]'
Expand All @@ -73,15 +86,7 @@ SEP='\([,.]\)'
version_pattern="${L_BOUND}${old_major}${SEP}${old_minor}${SEP}${old_patch}${R_BOUND}"
replacement=${new_major}'\1'${new_minor}'\2'${new_patch}

edit_rc_file() {
local file="$1"
iconv -f 'utf-16le' -t 'utf-8' "${file}" | \
sed "s/${version_pattern}/${replacement}/g" | \
iconv -f 'utf-8' -t 'utf-16le' > "${file}".new
mv "${file}".new "${file}"
}

edit_source_file() {
update_version_string() {
local file="$1"
sed -i'~' "s/${version_pattern}/${replacement}/g" "${file}"
rm "${file}~"
Expand All @@ -94,6 +99,8 @@ update_pub_date() {
rm "${file}~"
}

# prerequisite:
# cargo install clog-cli
update_changelog() {
local version="$1"
clog --from-latest-tag \
Expand All @@ -102,19 +109,13 @@ update_changelog() {
--setversion "${version}"
}

edit_rc_file WeaselServer/WeaselServer.rc

edit_source_file include/WeaselVersion.h
edit_source_file build.bat
edit_source_file appveyor.yml

edit_source_file update/testing-appcast.xml
edit_source_file update/appcast.xml
update_version_string update/testing-appcast.xml
update_version_string update/appcast.xml
update_pub_date update/testing-appcast.xml
update_pub_date update/appcast.xml

update_changelog "${new_version}"
${VISUAL:-${EDITOR:-vim}} CHANGELOG.md
${VISUAL:-${EDITOR:-nano}} CHANGELOG.md
match_line "## ${new_version} " CHANGELOG.md || (
echo >&2 "CHANGELOG.md has no changes for version ${new_version}."
exit 1
Expand Down

0 comments on commit 0b1ddc6

Please sign in to comment.