Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions util/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env bash
#
# Usage: util/release.sh [version]
#
# where version (optional) is the new version of rules_rust.
set -xeuo pipefail

# Normalize working directory to root of repository.
cd "$(dirname "${BASH_SOURCE[0]}")"/..

# Read the old version.
readonly OLD="$(cat version.bzl | grep VERSION | awk '{print $3}' | tr -d '"')"

function new_from_old() {
local major=$(awk -F. '{print $1}' <<<"$OLD")
local minor=$(awk -F. '{print $2}' <<<"$OLD")
echo "$major.$((minor + 1)).0"
}

readonly NEW="${1:-$(new_from_old)}"

# Update matching VERSION constants in version.bzl files.
function version_pattern() {
local version_quoted="\"$1\""
echo "VERSION = $version_quoted"
}

grep -rl \
--include='version.bzl' \
"$(version_pattern $OLD)" \
| xargs sed -i "s/^$(version_pattern $OLD)$/$(version_pattern $NEW)/g"

# Update matching bazel_dep(name = "rules_rust", version = ...) declarations.
function bazel_dep_pattern() {
local version_quoted="\"$1\""
echo "bazel_dep(name = \"rules_rust\", version = $version_quoted)"
}

grep -rl \
--include='MODULE.bazel' --include='*.bzl' --include='*.md' \
"$(bazel_dep_pattern $OLD)" \
| xargs sed -i "s/^$(bazel_dep_pattern $OLD)$/$(bazel_dep_pattern $NEW)/"

# Update module() declarations:
# module(
# name = "rules_rust",
# version = "...",
# )
function update_module_version() {
local file=$1

local out="$(awk -v old=$OLD -v new=$NEW '
BEGIN {
VERSION_PATTERN = "version = \"" old "\"";
NAME_PATTERN = "name = \"rules_rust.*\"";
CLOSING_PAREN_PATTERN = "^)$";

inside_module = 0;
name_matches = 0;
}
/^module\($/ {
inside_module = 1;
}
inside_module {
if ($0 ~ NAME_PATTERN) {
name_matches = 1;
} else if ($0 ~ VERSION_PATTERN && name_matches) {
gsub(old, new);
} else if ($0 ~ CLOSING_PAREN_PATTERN) {
inside_module = 0;
name_matches = 0;
}
}
{ print }
' "$file")"
echo "$out" > "$file"
}

find . -name MODULE.bazel -print0 | while IFS= read -r -d $'\0' file; do
update_module_version "$file"
done