From 42200234a61e1bc6473b76e9b793c2e5a58253af Mon Sep 17 00:00:00 2001 From: Hayden Stainsby Date: Sat, 23 Sep 2023 20:33:41 +0200 Subject: [PATCH] chore: update release scripts (#466) --- bin/release.sh | 137 +++++++++++++++++++++++++++++++--------- bin/update-changelog.sh | 27 ++++++-- cliff.toml | 1 + 3 files changed, 131 insertions(+), 34 deletions(-) diff --git a/bin/release.sh b/bin/release.sh index 73a3721df..72c774e98 100755 --- a/bin/release.sh +++ b/bin/release.sh @@ -5,9 +5,10 @@ USAGE: $(basename "$0") [FLAGS] 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 @@ -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 @@ -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 } @@ -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) @@ -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 } @@ -129,6 +202,7 @@ update_changelog() { verbose='' dry_run='' +publish_dry_run='' for arg in "$@" do @@ -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" @@ -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 @@ -226,24 +318,15 @@ 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 @@ -251,16 +334,10 @@ else 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 \ No newline at end of file diff --git a/bin/update-changelog.sh b/bin/update-changelog.sh index 7cbef4699..f05637a72 100755 --- a/bin/update-changelog.sh +++ b/bin/update-changelog.sh @@ -5,8 +5,11 @@ USAGE: $(basename "$0") [FLAGS] 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 Write the changelog to this path. + default: /CHANGELOG.md" set -euo pipefail @@ -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" @@ -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" @@ -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" @@ -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[@]}" \ No newline at end of file diff --git a/cliff.toml b/cliff.toml index 57b7af176..16f2c3f17 100644 --- a/cliff.toml +++ b/cliff.toml @@ -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