|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Version Bump Script |
| 4 | +# Usage: ./version-bump.sh <bump_type> [base_ref] |
| 5 | +# bump_type: patch, minor, or major |
| 6 | +# base_ref: base reference for diff (default: origin/main) |
| 7 | + |
| 8 | +set -euo pipefail |
| 9 | + |
| 10 | +usage() { |
| 11 | + echo "Usage: $0 <bump_type> [base_ref]" |
| 12 | + echo " bump_type: patch, minor, or major" |
| 13 | + echo " base_ref: base reference for diff (default: origin/main)" |
| 14 | + echo "" |
| 15 | + echo "Examples:" |
| 16 | + echo " $0 patch # Update versions with patch bump" |
| 17 | + echo " $0 minor # Update versions with minor bump" |
| 18 | + echo " $0 major # Update versions with major bump" |
| 19 | + exit 1 |
| 20 | +} |
| 21 | + |
| 22 | +validate_version() { |
| 23 | + local version="$1" |
| 24 | + if ! [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 25 | + echo "❌ Invalid version format: '$version'. Expected X.Y.Z format." >&2 |
| 26 | + return 1 |
| 27 | + fi |
| 28 | + return 0 |
| 29 | +} |
| 30 | + |
| 31 | +bump_version() { |
| 32 | + local current_version="$1" |
| 33 | + local bump_type="$2" |
| 34 | + |
| 35 | + IFS='.' read -r major minor patch <<< "$current_version" |
| 36 | + |
| 37 | + if ! [[ "$major" =~ ^[0-9]+$ ]] || ! [[ "$minor" =~ ^[0-9]+$ ]] || ! [[ "$patch" =~ ^[0-9]+$ ]]; then |
| 38 | + echo "❌ Version components must be numeric: major='$major' minor='$minor' patch='$patch'" >&2 |
| 39 | + return 1 |
| 40 | + fi |
| 41 | + |
| 42 | + case "$bump_type" in |
| 43 | + "patch") |
| 44 | + echo "$major.$minor.$((patch + 1))" |
| 45 | + ;; |
| 46 | + "minor") |
| 47 | + echo "$major.$((minor + 1)).0" |
| 48 | + ;; |
| 49 | + "major") |
| 50 | + echo "$((major + 1)).0.0" |
| 51 | + ;; |
| 52 | + *) |
| 53 | + echo "❌ Invalid bump type: '$bump_type'. Expected patch, minor, or major." >&2 |
| 54 | + return 1 |
| 55 | + ;; |
| 56 | + esac |
| 57 | +} |
| 58 | + |
| 59 | +update_readme_version() { |
| 60 | + local readme_path="$1" |
| 61 | + local namespace="$2" |
| 62 | + local module_name="$3" |
| 63 | + local new_version="$4" |
| 64 | + |
| 65 | + if [ ! -f "$readme_path" ]; then |
| 66 | + return 1 |
| 67 | + fi |
| 68 | + |
| 69 | + local module_source="registry.coder.com/${namespace}/${module_name}/coder" |
| 70 | + if grep -q "source.*${module_source}" "$readme_path"; then |
| 71 | + echo "Updating version references for $namespace/$module_name in $readme_path" |
| 72 | + awk -v module_source="$module_source" -v new_version="$new_version" ' |
| 73 | + /source.*=.*/ { |
| 74 | + if ($0 ~ module_source) { |
| 75 | + in_target_module = 1 |
| 76 | + } else { |
| 77 | + in_target_module = 0 |
| 78 | + } |
| 79 | + } |
| 80 | + /version.*=.*"/ { |
| 81 | + if (in_target_module) { |
| 82 | + gsub(/version[[:space:]]*=[[:space:]]*"[^"]*"/, "version = \"" new_version "\"") |
| 83 | + in_target_module = 0 |
| 84 | + } |
| 85 | + } |
| 86 | + { print } |
| 87 | + ' "$readme_path" > "${readme_path}.tmp" && mv "${readme_path}.tmp" "$readme_path" |
| 88 | + return 0 |
| 89 | + elif grep -q 'version\s*=\s*"' "$readme_path"; then |
| 90 | + echo "⚠️ Found version references but no module source match for $namespace/$module_name" |
| 91 | + return 1 |
| 92 | + fi |
| 93 | + |
| 94 | + return 1 |
| 95 | +} |
| 96 | + |
| 97 | +main() { |
| 98 | + if [ $# -lt 1 ] || [ $# -gt 2 ]; then |
| 99 | + usage |
| 100 | + fi |
| 101 | + |
| 102 | + local bump_type="$1" |
| 103 | + local base_ref="${2:-origin/main}" |
| 104 | + |
| 105 | + case "$bump_type" in |
| 106 | + "patch" | "minor" | "major") ;; |
| 107 | + |
| 108 | + *) |
| 109 | + echo "❌ Invalid bump type: '$bump_type'. Expected patch, minor, or major." >&2 |
| 110 | + exit 1 |
| 111 | + ;; |
| 112 | + esac |
| 113 | + |
| 114 | + echo "🔍 Detecting modified modules..." |
| 115 | + |
| 116 | + local changed_files |
| 117 | + changed_files=$(git diff --name-only "${base_ref}"...HEAD) |
| 118 | + local modules |
| 119 | + modules=$(echo "$changed_files" | grep -E '^registry/[^/]+/modules/[^/]+/' | cut -d'/' -f1-4 | sort -u) |
| 120 | + |
| 121 | + if [ -z "$modules" ]; then |
| 122 | + echo "❌ No modules detected in changes" |
| 123 | + exit 1 |
| 124 | + fi |
| 125 | + |
| 126 | + echo "Found modules:" |
| 127 | + echo "$modules" |
| 128 | + echo "" |
| 129 | + |
| 130 | + local bumped_modules="" |
| 131 | + local updated_readmes="" |
| 132 | + local untagged_modules="" |
| 133 | + local has_changes=false |
| 134 | + |
| 135 | + while IFS= read -r module_path; do |
| 136 | + if [ -z "$module_path" ]; then continue; fi |
| 137 | + |
| 138 | + local namespace |
| 139 | + namespace=$(echo "$module_path" | cut -d'/' -f2) |
| 140 | + local module_name |
| 141 | + module_name=$(echo "$module_path" | cut -d'/' -f4) |
| 142 | + |
| 143 | + echo "📦 Processing: $namespace/$module_name" |
| 144 | + |
| 145 | + local latest_tag |
| 146 | + latest_tag=$(git tag -l "release/${namespace}/${module_name}/v*" | sort -V | tail -1) |
| 147 | + local readme_path="$module_path/README.md" |
| 148 | + local current_version |
| 149 | + |
| 150 | + if [ -z "$latest_tag" ]; then |
| 151 | + if [ -f "$readme_path" ] && grep -q 'version\s*=\s*"' "$readme_path"; then |
| 152 | + local readme_version |
| 153 | + readme_version=$(grep 'version\s*=\s*"' "$readme_path" | head -1 | sed 's/.*version\s*=\s*"\([^"]*\)".*/\1/') |
| 154 | + echo "No git tag found, but README shows version: $readme_version" |
| 155 | + |
| 156 | + if ! validate_version "$readme_version"; then |
| 157 | + echo "Starting from v1.0.0 instead" |
| 158 | + current_version="1.0.0" |
| 159 | + else |
| 160 | + current_version="$readme_version" |
| 161 | + untagged_modules="$untagged_modules\n- $namespace/$module_name (README: v$readme_version)" |
| 162 | + fi |
| 163 | + else |
| 164 | + echo "No existing tags or version references found for $namespace/$module_name, starting from v1.0.0" |
| 165 | + current_version="1.0.0" |
| 166 | + fi |
| 167 | + else |
| 168 | + current_version=$(echo "$latest_tag" | sed 's/.*\/v//') |
| 169 | + echo "Found git tag: $latest_tag (v$current_version)" |
| 170 | + fi |
| 171 | + |
| 172 | + echo "Current version: $current_version" |
| 173 | + |
| 174 | + if ! validate_version "$current_version"; then |
| 175 | + exit 1 |
| 176 | + fi |
| 177 | + |
| 178 | + local new_version |
| 179 | + new_version=$(bump_version "$current_version" "$bump_type") |
| 180 | + |
| 181 | + echo "New version: $new_version" |
| 182 | + |
| 183 | + if update_readme_version "$readme_path" "$namespace" "$module_name" "$new_version"; then |
| 184 | + updated_readmes="$updated_readmes\n- $namespace/$module_name" |
| 185 | + has_changes=true |
| 186 | + fi |
| 187 | + |
| 188 | + bumped_modules="$bumped_modules\n- $namespace/$module_name: v$current_version → v$new_version" |
| 189 | + echo "" |
| 190 | + |
| 191 | + done <<< "$modules" |
| 192 | + |
| 193 | + echo "📋 Summary:" |
| 194 | + echo "Bump Type: $bump_type" |
| 195 | + echo "" |
| 196 | + echo "Modules Updated:" |
| 197 | + echo -e "$bumped_modules" |
| 198 | + echo "" |
| 199 | + |
| 200 | + if [ -n "$updated_readmes" ]; then |
| 201 | + echo "READMEs Updated:" |
| 202 | + echo -e "$updated_readmes" |
| 203 | + echo "" |
| 204 | + fi |
| 205 | + |
| 206 | + if [ -n "$untagged_modules" ]; then |
| 207 | + echo "⚠️ Modules Without Git Tags:" |
| 208 | + echo -e "$untagged_modules" |
| 209 | + echo "These modules were versioned based on README content. Consider creating proper release tags after merging." |
| 210 | + echo "" |
| 211 | + fi |
| 212 | + |
| 213 | + if [ "$has_changes" = true ]; then |
| 214 | + echo "✅ Version bump completed successfully!" |
| 215 | + echo "📝 README files have been updated with new versions." |
| 216 | + echo "" |
| 217 | + echo "Next steps:" |
| 218 | + echo "1. Review the changes: git diff" |
| 219 | + echo "2. Commit the changes: git add . && git commit -m 'chore: bump module versions ($bump_type)'" |
| 220 | + echo "3. Push the changes: git push" |
| 221 | + exit 0 |
| 222 | + else |
| 223 | + echo "ℹ️ No README files were updated (no version references found matching module sources)." |
| 224 | + echo "Version calculations completed, but no files were modified." |
| 225 | + exit 0 |
| 226 | + fi |
| 227 | +} |
| 228 | + |
| 229 | +main "$@" |
0 commit comments