|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +usage() { |
| 5 | + cat <<EOF |
| 6 | +Usage: $0 [OPTIONS] [<MODULE> <VERSION>] |
| 7 | +
|
| 8 | +Create annotated git tags for module releases. |
| 9 | +
|
| 10 | +This script is used by maintainers to create annotated tags for module |
| 11 | +releases. When a tag is pushed, it triggers a GitHub workflow that |
| 12 | +updates README versions. |
| 13 | +
|
| 14 | +Options: |
| 15 | + -l, --list List all modules with their versions |
| 16 | + -n, --dry-run Show what would be done without making changes |
| 17 | + -p, --push Push the created tag to the remote repository |
| 18 | + -h, --help Show this help message |
| 19 | +
|
| 20 | +Examples: |
| 21 | + $0 --list |
| 22 | + $0 nodejs 1.2.3 |
| 23 | + $0 nodejs 1.2.3 --push |
| 24 | + $0 --dry-run nodejs 1.2.3 |
| 25 | +EOF |
| 26 | + exit "${1:-0}" |
| 27 | +} |
| 28 | + |
| 29 | +check_getopt() { |
| 30 | + # Check if we have GNU or BSD getopt. |
| 31 | + if getopt --test >/dev/null 2>&1; then |
| 32 | + # Exit status 4 means GNU getopt is available. |
| 33 | + if [[ $? -ne 4 ]]; then |
| 34 | + echo "Error: GNU getopt is not available." >&2 |
| 35 | + echo "On macOS, you can install GNU getopt and add it to your PATH:" >&2 |
| 36 | + echo |
| 37 | + echo $'\tbrew install gnu-getopt' >&2 |
| 38 | + echo $'\texport PATH="$(brew --prefix gnu-getopt)/bin:$PATH"' >&2 |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | + fi |
| 42 | +} |
| 43 | + |
| 44 | +maybe_dry_run() { |
| 45 | + if [[ $dry_run == true ]]; then |
| 46 | + echo "[DRY RUN] $*" |
| 47 | + return 0 |
| 48 | + fi |
| 49 | + "$@" |
| 50 | +} |
| 51 | + |
| 52 | +get_readme_version() { |
| 53 | + grep -o 'version *= *"[0-9]\+\.[0-9]\+\.[0-9]\+"' "$1" | |
| 54 | + head -1 | |
| 55 | + grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+' || |
| 56 | + echo "0.0.0" |
| 57 | +} |
| 58 | + |
| 59 | +list_modules() { |
| 60 | + printf "\nListing all modules and their latest versions:\n" |
| 61 | + printf "%s\n" "--------------------------------------------------------------" |
| 62 | + printf "%-30s %-15s %-15s\n" "MODULE" "README VERSION" "LATEST TAG" |
| 63 | + printf "%s\n" "--------------------------------------------------------------" |
| 64 | + |
| 65 | + # Process each module directory. |
| 66 | + for dir in */; do |
| 67 | + # Skip non-module directories. |
| 68 | + [[ ! -d $dir || ! -f ${dir}README.md || $dir == ".git/" ]] && continue |
| 69 | + |
| 70 | + module="${dir%/}" |
| 71 | + readme_version=$(get_readme_version "${dir}README.md") |
| 72 | + latest_tag=$(git tag -l "release/${module}/v*" | sort -V | tail -n 1) |
| 73 | + tag_version="none" |
| 74 | + if [[ -n $latest_tag ]]; then |
| 75 | + tag_version="${latest_tag#"release/${module}/v"}" |
| 76 | + fi |
| 77 | + |
| 78 | + printf "%-30s %-15s %-15s\n" "$module" "$readme_version" "$tag_version" |
| 79 | + done |
| 80 | + |
| 81 | + printf "%s\n" "--------------------------------------------------------------" |
| 82 | +} |
| 83 | + |
| 84 | +is_valid_version() { |
| 85 | + if ! [[ $1 =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 86 | + echo "Error: Version must be in format X.Y.Z (e.g., 1.2.3)" >&2 |
| 87 | + return 1 |
| 88 | + fi |
| 89 | +} |
| 90 | + |
| 91 | +get_tag_name() { |
| 92 | + local module="$1" |
| 93 | + local version="$2" |
| 94 | + local tag_name="release/$module/v$version" |
| 95 | + local readme_path="$module/README.md" |
| 96 | + |
| 97 | + if [[ ! -d $module || ! -f $readme_path ]]; then |
| 98 | + echo "Error: Module '$module' not found or missing README.md" >&2 |
| 99 | + return 1 |
| 100 | + fi |
| 101 | + |
| 102 | + local readme_version |
| 103 | + readme_version=$(get_readme_version "$readme_path") |
| 104 | + |
| 105 | + { |
| 106 | + echo "Module: $module" |
| 107 | + echo "Current README version: $readme_version" |
| 108 | + echo "New tag version: $version" |
| 109 | + echo "Tag name: $tag_name" |
| 110 | + } >&2 |
| 111 | + |
| 112 | + echo "$tag_name" |
| 113 | +} |
| 114 | + |
| 115 | +# Ensure getopt is available. |
| 116 | +check_getopt |
| 117 | + |
| 118 | +# Set defaults. |
| 119 | +list=false |
| 120 | +dry_run=false |
| 121 | +push=false |
| 122 | +module= |
| 123 | +version= |
| 124 | + |
| 125 | +# Parse command-line options. |
| 126 | +if ! temp=$(getopt -o ldph --long list,dry-run,push,help -n "$0" -- "$@"); then |
| 127 | + echo "Error: Failed to parse arguments" >&2 |
| 128 | + usage 1 |
| 129 | +fi |
| 130 | +eval set -- "$temp" |
| 131 | + |
| 132 | +while true; do |
| 133 | + case "$1" in |
| 134 | + -l | --list) |
| 135 | + list=true |
| 136 | + shift |
| 137 | + ;; |
| 138 | + -d | --dry-run) |
| 139 | + dry_run=true |
| 140 | + shift |
| 141 | + ;; |
| 142 | + -p | --push) |
| 143 | + push=true |
| 144 | + shift |
| 145 | + ;; |
| 146 | + -h | --help) |
| 147 | + usage |
| 148 | + ;; |
| 149 | + --) |
| 150 | + shift |
| 151 | + break |
| 152 | + ;; |
| 153 | + *) |
| 154 | + echo "Error: Internal error!" >&2 |
| 155 | + exit 1 |
| 156 | + ;; |
| 157 | + esac |
| 158 | +done |
| 159 | + |
| 160 | +if [[ $list == true ]]; then |
| 161 | + list_modules |
| 162 | + exit 0 |
| 163 | +fi |
| 164 | + |
| 165 | +if [[ $# -ne 2 ]]; then |
| 166 | + echo "Error: MODULE and VERSION are required when not using --list" >&2 |
| 167 | + usage 1 |
| 168 | +fi |
| 169 | + |
| 170 | +module="$1" |
| 171 | +version="$2" |
| 172 | + |
| 173 | +if ! is_valid_version "$version"; then |
| 174 | + exit 1 |
| 175 | +fi |
| 176 | + |
| 177 | +if ! tag_name=$(get_tag_name "$module" "$version"); then |
| 178 | + exit 1 |
| 179 | +fi |
| 180 | + |
| 181 | +if git rev-parse -q --verify "refs/tags/$tag_name" >/dev/null 2>&1; then |
| 182 | + echo "Notice: Tag '$tag_name' already exists" >&2 |
| 183 | +else |
| 184 | + maybe_dry_run git tag -a "$tag_name" -m "Release $module v$version" |
| 185 | + if [[ $push == true ]]; then |
| 186 | + maybe_dry_run echo "Tag '$tag_name' created." |
| 187 | + else |
| 188 | + maybe_dry_run echo "Tag '$tag_name' created locally. Use --push to push it to remote." |
| 189 | + maybe_dry_run "ℹ️ Note: Remember to push the tag when ready." |
| 190 | + fi |
| 191 | +fi |
| 192 | + |
| 193 | +if [[ $push == true ]]; then |
| 194 | + maybe_dry_run git push origin "$tag_name" |
| 195 | + maybe_dry_run echo "Success! Tag '$tag_name' pushed to remote." |
| 196 | +fi |
0 commit comments