11#! /bin/bash
22
33# Version Bump Script
4- # Extracts version bump logic from GitHub Actions workflow
54# Usage: ./version-bump.sh <bump_type> [base_ref]
65# bump_type: patch, minor, or major
76# base_ref: base reference for diff (default: origin/main)
87
98set -euo pipefail
109
11- # Function to print usage
1210usage () {
1311 echo " Usage: $0 <bump_type> [base_ref]"
1412 echo " bump_type: patch, minor, or major"
@@ -21,7 +19,6 @@ usage() {
2119 exit 1
2220}
2321
24- # Function to validate version format
2522validate_version () {
2623 local version=" $1 "
2724 if ! [[ " $version " =~ ^[0-9]+\. [0-9]+\. [0-9]+$ ]]; then
@@ -31,14 +28,12 @@ validate_version() {
3128 return 0
3229}
3330
34- # Function to bump version
3531bump_version () {
3632 local current_version=" $1 "
3733 local bump_type=" $2 "
3834
3935 IFS=' .' read -r major minor patch <<< " $current_version"
4036
41- # Validate that components are numeric
4237 if ! [[ " $major " =~ ^[0-9]+$ ]] || ! [[ " $minor " =~ ^[0-9]+$ ]] || ! [[ " $patch " =~ ^[0-9]+$ ]]; then
4338 echo " ❌ Version components must be numeric: major='$major ' minor='$minor ' patch='$patch '" >&2
4439 return 1
@@ -61,7 +56,6 @@ bump_version() {
6156 esac
6257}
6358
64- # Function to update README version
6559update_readme_version () {
6660 local readme_path=" $1 "
6761 local namespace=" $2 "
@@ -72,11 +66,9 @@ update_readme_version() {
7266 return 1
7367 fi
7468
75- # Check if README contains version references for this specific module
7669 local module_source=" registry.coder.com/${namespace} /${module_name} /coder"
7770 if grep -q " source.*${module_source} " " $readme_path " ; then
7871 echo " Updating version references for $namespace /$module_name in $readme_path "
79- # Use awk to only update versions that follow the specific module source
8072 awk -v module_source=" $module_source " -v new_version=" $new_version " '
8173 /source.*=.*/ {
8274 if ($0 ~ module_source) {
@@ -102,17 +94,14 @@ update_readme_version() {
10294 return 1
10395}
10496
105- # Main function
10697main () {
107- # Parse arguments
10898 if [ $# -lt 1 ] || [ $# -gt 2 ]; then
10999 usage
110100 fi
111101
112102 local bump_type=" $1 "
113103 local base_ref=" ${2:- origin/ main} "
114104
115- # Validate bump type
116105 case " $bump_type " in
117106 " patch" |" minor" |" major" )
118107 ;;
@@ -124,7 +113,6 @@ main() {
124113
125114 echo " 🔍 Detecting modified modules..."
126115
127- # Detect modified modules
128116 local changed_files
129117 changed_files=$( git diff --name-only " ${base_ref} " ...HEAD)
130118 local modules
@@ -139,13 +127,11 @@ main() {
139127 echo " $modules "
140128 echo " "
141129
142- # Initialize tracking variables
143130 local bumped_modules=" "
144131 local updated_readmes=" "
145132 local untagged_modules=" "
146133 local has_changes=false
147134
148- # Process each module
149135 while IFS= read -r module_path; do
150136 if [ -z " $module_path " ]; then continue ; fi
151137
@@ -156,21 +142,17 @@ main() {
156142
157143 echo " 📦 Processing: $namespace /$module_name "
158144
159- # Find latest tag
160145 local latest_tag
161146 latest_tag=$( git tag -l " release/${namespace} /${module_name} /v*" | sort -V | tail -1)
162147 local readme_path=" $module_path /README.md"
163148 local current_version
164149
165150 if [ -z " $latest_tag " ]; then
166- # No tag found, check if README has version references
167151 if [ -f " $readme_path " ] && grep -q ' version\s*=\s*"' " $readme_path " ; then
168- # Extract version from README
169152 local readme_version
170153 readme_version=$( grep ' version\s*=\s*"' " $readme_path " | head -1 | sed ' s/.*version\s*=\s*"\([^"]*\)".*/\1/' )
171154 echo " No git tag found, but README shows version: $readme_version "
172155
173- # Validate extracted version format
174156 if ! validate_version " $readme_version " ; then
175157 echo " Starting from v1.0.0 instead"
176158 current_version=" 1.0.0"
@@ -189,18 +171,15 @@ main() {
189171
190172 echo " Current version: $current_version "
191173
192- # Validate current version format
193174 if ! validate_version " $current_version " ; then
194175 exit 1
195176 fi
196177
197- # Calculate new version
198178 local new_version
199179 new_version=$( bump_version " $current_version " " $bump_type " )
200180
201181 echo " New version: $new_version "
202182
203- # Update README if applicable
204183 if update_readme_version " $readme_path " " $namespace " " $module_name " " $new_version " ; then
205184 updated_readmes=" $updated_readmes \n- $namespace /$module_name "
206185 has_changes=true
@@ -211,7 +190,6 @@ main() {
211190
212191 done <<< " $modules"
213192
214- # Output results
215193 echo " 📋 Summary:"
216194 echo " Bump Type: $bump_type "
217195 echo " "
@@ -232,7 +210,6 @@ main() {
232210 echo " "
233211 fi
234212
235- # Provide guidance
236213 if [ " $has_changes " = true ]; then
237214 echo " ✅ Version bump completed successfully!"
238215 echo " 📝 README files have been updated with new versions."
@@ -249,5 +226,4 @@ main() {
249226 fi
250227}
251228
252- # Run main function with all arguments
253229main " $@ "
0 commit comments