22
33# Version Bump Script
44# Extracts version bump logic from GitHub Actions workflow
5- # Usage: ./version-bump.sh <bump_type> [base_ref]
6- # bump_type: patch, minor, or major
5+ # Usage: ./version-bump.sh <bump_type> [base_ref] [--check]
6+ # bump_type: patch, minor, major, or check
77# base_ref: base reference for diff (default: origin/main)
8+ # --check: only check if versions need updating, don't modify files
89
910set -euo pipefail
1011
1112# Function to print usage
1213usage () {
13- echo " Usage: $0 <bump_type> [base_ref]"
14- echo " bump_type: patch, minor, or major "
14+ echo " Usage: $0 <bump_type> [base_ref] [--check] "
15+ echo " bump_type: patch, minor, major, or check "
1516 echo " base_ref: base reference for diff (default: origin/main)"
17+ echo " --check: only check if versions need updating, don't modify files"
18+ echo " "
19+ echo " Examples:"
20+ echo " $0 patch # Update versions with patch bump"
21+ echo " $0 check # Check if versions need updating"
22+ echo " $0 patch origin/main --check # Check what patch bump would do"
1623 exit 1
1724}
1825
@@ -62,6 +69,7 @@ update_readme_version() {
6269 local namespace=" $2 "
6370 local module_name=" $3 "
6471 local new_version=" $4 "
72+ local check_mode=" $5 "
6573
6674 if [ ! -f " $readme_path " ]; then
6775 return 1
@@ -70,24 +78,28 @@ update_readme_version() {
7078 # Check if README contains version references for this specific module
7179 local module_source=" registry.coder.com/${namespace} /${module_name} /coder"
7280 if grep -q " source.*${module_source} " " $readme_path " ; then
73- echo " Updating version references for $namespace /$module_name in $readme_path "
74- # Use awk to only update versions that follow the specific module source
75- awk -v module_source=" $module_source " -v new_version=" $new_version " '
76- /source.*=.*/ {
77- if ($0 ~ module_source) {
78- in_target_module = 1
79- } else {
80- in_target_module = 0
81- }
82- }
83- /version.*=.*"/ {
84- if (in_target_module) {
85- gsub(/version[[:space:]]*=[[:space:]]*"[^"]*"/, "version = \"" new_version "\"")
86- in_target_module = 0
87- }
88- }
89- { print }
90- ' " $readme_path " > " ${readme_path} .tmp" && mv " ${readme_path} .tmp" " $readme_path "
81+ if [ " $check_mode " = " true" ]; then
82+ echo " Would update version references for $namespace /$module_name in $readme_path "
83+ else
84+ echo " Updating version references for $namespace /$module_name in $readme_path "
85+ # Use awk to only update versions that follow the specific module source
86+ awk -v module_source=" $module_source " -v new_version=" $new_version " '
87+ /source.*=.*/ {
88+ if ($0 ~ module_source) {
89+ in_target_module = 1
90+ } else {
91+ in_target_module = 0
92+ }
93+ }
94+ /version.*=.*"/ {
95+ if (in_target_module) {
96+ gsub(/version[[:space:]]*=[[:space:]]*"[^"]*"/, "version = \"" new_version "\"")
97+ in_target_module = 0
98+ }
99+ }
100+ { print }
101+ ' " $readme_path " > " ${readme_path} .tmp" && mv " ${readme_path} .tmp" " $readme_path "
102+ fi
91103 return 0
92104 elif grep -q ' version\s*=\s*"' " $readme_path " ; then
93105 echo " ⚠️ Found version references but no module source match for $namespace /$module_name "
@@ -100,19 +112,40 @@ update_readme_version() {
100112# Main function
101113main () {
102114 # Parse arguments
103- if [ $# -lt 1 ] || [ $# -gt 2 ]; then
115+ if [ $# -lt 1 ] || [ $# -gt 3 ]; then
104116 usage
105117 fi
106118
107119 local bump_type=" $1 "
108- local base_ref=" ${2:- origin/ main} "
120+ local base_ref=" origin/main"
121+ local check_mode=false
122+
123+ # Parse remaining arguments
124+ shift
125+ while [ $# -gt 0 ]; do
126+ case " $1 " in
127+ --check)
128+ check_mode=true
129+ ;;
130+ * )
131+ base_ref=" $1 "
132+ ;;
133+ esac
134+ shift
135+ done
136+
137+ # Handle "check" as bump type
138+ if [ " $bump_type " = " check" ]; then
139+ check_mode=true
140+ bump_type=" patch" # Use patch as default for check mode
141+ fi
109142
110143 # Validate bump type
111144 case " $bump_type " in
112145 " patch" |" minor" |" major" )
113146 ;;
114147 * )
115- echo " ❌ Invalid bump type: '$bump_type '. Expected patch, minor, or major ." >&2
148+ echo " ❌ Invalid bump type: '$bump_type '. Expected patch, minor, major, or check ." >&2
116149 exit 1
117150 ;;
118151 esac
@@ -196,7 +229,7 @@ main() {
196229 echo " New version: $new_version "
197230
198231 # Update README if applicable
199- if update_readme_version " $readme_path " " $namespace " " $module_name " " $new_version " ; then
232+ if update_readme_version " $readme_path " " $namespace " " $module_name " " $new_version " " $check_mode " ; then
200233 updated_readmes=" $updated_readmes \n- $namespace /$module_name "
201234 has_changes=true
202235 fi
@@ -228,19 +261,39 @@ main() {
228261 fi
229262
230263 # Check for changes and provide guidance
231- if [ " $has_changes " = true ]; then
232- echo " ✅ Version bump completed successfully!"
233- echo " 📝 README files have been updated with new versions."
234- echo " "
235- echo " Next steps:"
236- echo " 1. Review the changes: git diff"
237- echo " 2. Commit the changes: git add . && git commit -m 'chore: bump module versions ($bump_type )'"
238- echo " 3. Push the changes: git push"
239- exit 0
264+ if [ " $check_mode " = " true" ]; then
265+ if [ " $has_changes " = true ]; then
266+ echo " ❌ Module versions need to be updated!"
267+ echo " 📝 The following modules would have their README files updated:"
268+ echo -e " $updated_readmes "
269+ echo " "
270+ echo " To fix this, run one of:"
271+ echo " ./.github/scripts/version-bump.sh patch"
272+ echo " ./.github/scripts/version-bump.sh minor"
273+ echo " ./.github/scripts/version-bump.sh major"
274+ echo " "
275+ echo " Or add a version label to your PR: version:patch, version:minor, or version:major"
276+ exit 1
277+ else
278+ echo " ✅ Module versions are up to date!"
279+ echo " ℹ️ No version updates needed."
280+ exit 0
281+ fi
240282 else
241- echo " ℹ️ No README files were updated (no version references found matching module sources)."
242- echo " Version calculations completed, but no files were modified."
243- exit 0
283+ if [ " $has_changes " = true ]; then
284+ echo " ✅ Version bump completed successfully!"
285+ echo " 📝 README files have been updated with new versions."
286+ echo " "
287+ echo " Next steps:"
288+ echo " 1. Review the changes: git diff"
289+ echo " 2. Commit the changes: git add . && git commit -m 'chore: bump module versions ($bump_type )'"
290+ echo " 3. Push the changes: git push"
291+ exit 0
292+ else
293+ echo " ℹ️ No README files were updated (no version references found matching module sources)."
294+ echo " Version calculations completed, but no files were modified."
295+ exit 0
296+ fi
244297 fi
245298}
246299
0 commit comments