|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Usage: util/release.sh [version] |
| 4 | +# |
| 5 | +# where version (optional) is the new version of rules_rust. |
| 6 | +set -xeuo pipefail |
| 7 | + |
| 8 | +# Normalize working directory to root of repository. |
| 9 | +cd "$(dirname "${BASH_SOURCE[0]}")"/.. |
| 10 | + |
| 11 | +# Read the old version. |
| 12 | +readonly OLD="$(cat version.bzl | grep VERSION | awk '{print $3}' | tr -d '"')" |
| 13 | + |
| 14 | +function new_from_old() { |
| 15 | + local major=$(awk -F. '{print $1}' <<<"$OLD") |
| 16 | + local minor=$(awk -F. '{print $2}' <<<"$OLD") |
| 17 | + echo "$major.$((minor + 1)).0" |
| 18 | +} |
| 19 | + |
| 20 | +readonly NEW="${1:-$(new_from_old)}" |
| 21 | + |
| 22 | +# Update matching VERSION constants in version.bzl files. |
| 23 | +function version_pattern() { |
| 24 | + local version_quoted="\"$1\"" |
| 25 | + echo "VERSION = $version_quoted" |
| 26 | +} |
| 27 | + |
| 28 | +grep -rl \ |
| 29 | + --include='version.bzl' \ |
| 30 | + "$(version_pattern $OLD)" \ |
| 31 | + | xargs sed -i "s/^$(version_pattern $OLD)$/$(version_pattern $NEW)/g" |
| 32 | + |
| 33 | +# Update matching bazel_dep(name = "rules_rust", version = ...) declarations. |
| 34 | +function bazel_dep_pattern() { |
| 35 | + local version_quoted="\"$1\"" |
| 36 | + echo "bazel_dep(name = \"rules_rust\", version = $version_quoted)" |
| 37 | +} |
| 38 | + |
| 39 | +grep -rl \ |
| 40 | + --include='MODULE.bazel' --include='*.bzl' --include='*.md' \ |
| 41 | + "$(bazel_dep_pattern $OLD)" \ |
| 42 | + | xargs sed -i "s/^$(bazel_dep_pattern $OLD)$/$(bazel_dep_pattern $NEW)/" |
| 43 | + |
| 44 | +# Update module() declarations: |
| 45 | +# module( |
| 46 | +# name = "rules_rust", |
| 47 | +# version = "...", |
| 48 | +# ) |
| 49 | +function update_module_version() { |
| 50 | + local file=$1 |
| 51 | + |
| 52 | + local out="$(awk -v old=$OLD -v new=$NEW ' |
| 53 | + BEGIN { |
| 54 | + VERSION_PATTERN = "version = \"" old "\""; |
| 55 | + NAME_PATTERN = "name = \"rules_rust.*\""; |
| 56 | + CLOSING_PAREN_PATTERN = "^)$"; |
| 57 | +
|
| 58 | + inside_module = 0; |
| 59 | + name_matches = 0; |
| 60 | + } |
| 61 | + /^module\($/ { |
| 62 | + inside_module = 1; |
| 63 | + } |
| 64 | + inside_module { |
| 65 | + if ($0 ~ NAME_PATTERN) { |
| 66 | + name_matches = 1; |
| 67 | + } else if ($0 ~ VERSION_PATTERN && name_matches) { |
| 68 | + gsub(old, new); |
| 69 | + } else if ($0 ~ CLOSING_PAREN_PATTERN) { |
| 70 | + inside_module = 0; |
| 71 | + name_matches = 0; |
| 72 | + } |
| 73 | + } |
| 74 | + { print } |
| 75 | + ' "$file")" |
| 76 | + echo "$out" > "$file" |
| 77 | +} |
| 78 | + |
| 79 | +find . -name MODULE.bazel -print0 | while IFS= read -r -d $'\0' file; do |
| 80 | + update_module_version "$file" |
| 81 | +done |
0 commit comments