Skip to content

Commit

Permalink
chore: update release scripts (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
hds authored and hawkw committed Sep 29, 2023
1 parent 94a5a51 commit 4220023
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 34 deletions.
137 changes: 107 additions & 30 deletions bin/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ USAGE:
$(basename "$0") [FLAGS] <CRATE> <VERSION>
FLAGS:
-h, --help Show this help text and exit.
-v, --verbose Enable verbose output.
-d, --dry-run Do not change any files or commit a git tag."
-h, --help Show this help text and exit.
-v, --verbose Enable verbose output.
-d, --dry-run Do not change any files or commit a git tag.
--publish-dry-run Perform a dry run on the publish step only."

set -euo pipefail

Expand Down Expand Up @@ -71,6 +72,17 @@ update_version() {
# check the current version of the crate
local curr_version
curr_version=$(cargo pkgid -p "$crate" | sed -n 's/.*#\(.*\)/\1/p')

if ! cargo --list | grep -q "set-version"; then
err "missing cargo-set-version executable (from cargo-edit)"
if confirm " install it?"; then
cargo install cargo-edit
else
echo "okay, exiting"
exit 1
fi
fi

if [[ "$curr_version" == "$version" ]]; then
err "crate $crate is already at version $version!"
if ! confirm " are you sure you want to release $version?"; then
Expand All @@ -79,9 +91,49 @@ update_version() {
fi
else
status "Updating" "$crate from $curr_version to $version"
sed -i \
"/\[package\]/,/\[.*dependencies\]/{s/^version = \"$curr_version\"/version = \"$version\"/}" \
"$cargo_toml"
cargo set-version -p $crate $version
fi

# If we're releasing console-api, we need to update its version in
# the other crates in the workspace too.
if [[ "$crate" == "console-api" ]]; then
local cargo_upgrade=(cargo upgrade --offline -p console-api@$version)
if [[ "$verbose" ]]; then
cargo_upgrade+=("$verbose")
fi

"${cargo_upgrade[@]}"
files+=($(ls Cargo.lock */Cargo.toml))
fi
}

commit() {
if ! [[ -z "${1+1}" ]] && [[ "$1" == "--amend" ]]; then
status "Amending" "release commit"
amend="$1"
else
status "Creating" "release commit"
amend=""
fi

# Prepare a commit message including the changelog from just this release.
tmp_dir=$(mktemp -d 2>/dev/null || mktemp -d -t 'tokio-console-release')
tmp_changelog_path="$tmp_dir/tmp-changelog"
tmp_commit_msg_path="$tmp_dir/tmp-commit-msg"
"$bindir/update-changelog.sh" --unreleased --changelog-path "$tmp_changelog_path" "$crate" "$crate-v$version"
(echo -e "chore($slug): prepare to release $crate $version\n" && cat $tmp_changelog_path | grep -v "generated by git-cliff") > $tmp_commit_msg_path

git_commit=(git commit -sS -F $tmp_commit_msg_path)

if [[ "$amend" ]]; then
git_commit+=($amend)
fi

if [[ "$dry_run" ]]; then
echo ""
echo "# " "${git_commit[@]}"
else
"${git_commit[@]}"
fi
}

Expand All @@ -98,11 +150,19 @@ publish() {

if [[ "$dry_run" ]]; then
cargo_publish+=("$dry_run")
elif [[ "$publish_dry_run" ]]; then
cargo_publish+=("$publish_dry_run")
fi

"${cargo_package[@]}"
"${cargo_publish[@]}"

cd "$rootdir"

status "Adding" "Cargo.lock after publish"
git add "Cargo.lock"
commit --amend

status "Tagging" "$tag"
local git_tag=(git tag "$tag")
local git_push_tags=(git push --tags)
Expand All @@ -111,7 +171,20 @@ publish() {
echo "# " "${git_push_tags[@]}"
else
"${git_tag[@]}"
"${git_push_tags[@]}"
fi
}

push() {
status "Pushing" "release commit and tag $tag"
local git_push=(git push -u origin)
local git_push_tag=(git push origin "$tag")

if [[ "$dry_run" ]]; then
echo "# " "${git_push[@]}"
echo "# " "${git_push_tag[@]}"
else
"${git_push[@]}"
"${git_push_tag[@]}"
fi
}

Expand All @@ -129,6 +202,7 @@ update_changelog() {

verbose=''
dry_run=''
publish_dry_run=''

for arg in "$@"
do
Expand All @@ -143,6 +217,9 @@ do
-d|--dry-run)
dry_run="--dry-run"
;;
--publish-dry-run)
publish_dry_run="--dry-run"
;;
-*)
err "unknown flag $arg"
echo "$usage"
Expand Down Expand Up @@ -178,6 +255,21 @@ else
errexit=1
fi

case "$crate" in
console-subscriber)
slug="subscriber"
;;
console-api)
slug="api"
;;
tokio-console)
slug="console"
;;
*)
slug="$crate"
;;
esac

if [[ "${errexit+errexit}" ]]; then
echo "$usage"
exit 1
Expand Down Expand Up @@ -226,41 +318,26 @@ fi

echo ""

if confirm "commit and push?"; then
git_commit=(git commit -sS -m "chore($crate): prepare to release $crate $version")

if [[ "$dry_run" ]]; then

echo ""
echo "# " "${git_commit[@]}"
echo "# " "${git_push[@]}"
else
"${git_commit[@]}"
fi
if confirm "commit?"; then
echo ""
commit
else
echo "okay, exiting"
exit 1
fi

if confirm "publish the crate?"; then

echo ""
publish
else
echo "okay, exiting"
exit 1
fi

cd "$rootdir"
git add "Cargo.lock"
git_push=(git push -u origin --force-with-lease)
git_amend=(git commit --amend --reuse-message HEAD)
if [[ "$dry_run" ]]; then
if confirm "push release commit and tag?"; then
echo ""
echo "# git add Cargo.lock"
echo "# " "${git_amend[@]}"
echo "# " "${git_push[@]}"
push
else
"${git_amend[@]}"
"${git_push[@]}"
fi
echo "okay, exiting"
exit 1
fi
27 changes: 23 additions & 4 deletions bin/update-changelog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ USAGE:
$(basename "$0") [FLAGS] <CRATE_PATH> <TAG>
FLAGS:
-h, --help Show this help text and exit.
-v, --verbose Enable verbose output."
-h, --help Show this help text and exit.
-v, --verbose Enable verbose output.
-u, --unreleased Only add unreleased changes to changelog
--changelog-path <FILE_PATH> Write the changelog to this path.
default: <CRATE_PATH>/CHANGELOG.md"

set -euo pipefail

Expand All @@ -19,9 +22,13 @@ rootdir=$( cd "$bindir"/.. && pwd )
cd "$rootdir"

verbose=''
unreleased=''
changelog_path=''

for arg in "$@"
while [[ $# -gt 0 ]]
do
arg=$1
shift
case "$arg" in
-h|--help)
echo "$usage"
Expand All @@ -30,6 +37,13 @@ do
-v|--verbose)
verbose="--verbose"
;;
-u|--unreleased)
unreleased="--unreleased"
;;
--changelog-path)
changelog_path="$1"
shift
;;
-*)
err "unknown flag $arg"
echo "$usage"
Expand Down Expand Up @@ -74,7 +88,9 @@ if ! [[ -x "$(command -v git-cliff)" ]]; then
fi
fi

changelog_path="${path}/CHANGELOG.md"
if [[ -z "$changelog_path" ]]; then
changelog_path="${path}/CHANGELOG.md"
fi

status "Updating" "$changelog_path for tag $tag"

Expand All @@ -88,6 +104,9 @@ git_cliff=(
if [[ "$verbose" ]]; then
git_cliff+=("$verbose")
fi
if [[ "$unreleased" ]]; then
git_cliff+=("$unreleased")
fi

export GIT_CLIFF__GIT__TAG_PATTERN="${path}-v[0-9]*"
"${git_cliff[@]}"
1 change: 1 addition & 0 deletions cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ filter_unconventional = true
split_commits = false
# regex for preprocessing the commit messages
commit_preprocessors = [
{ pattern = '^(feat|fix|doc|perf)\((api|subscriber|console)\) ', replace = '${1}(${2}): '}, # fix missing colon after commit type
{ pattern = '\((\w+\s)?#([0-9]+)\)', replace = "([#${2}](https://github.com/tokio-rs/console/issues/${2}))"}, # replace issue numbers
]
# regex for parsing and grouping commits
Expand Down

0 comments on commit 4220023

Please sign in to comment.