11#!/usr/bin/env python3
22"""
33bump-version.py - Automatically bump version in .toc and .lua files
4+ Usage: python bump-version.py <new_version>
5+ Example: python bump-version.py 2.9.0
6+
7+ Exit codes:
8+ 0 - Success
9+ 1 - Invalid arguments or version format
10+ 2 - File operation error
411"""
512
613import os
916from pathlib import Path
1017
1118
19+ # ANSI color codes (disabled in CI)
1220class Colors :
1321 RED = "\033 [0;31m"
1422 GREEN = "\033 [0;32m"
@@ -30,18 +38,21 @@ def get_current_version() -> str:
3038 """Get current version from .toc file"""
3139 toc_path = Path ("ConsumableManager.toc" )
3240 if not toc_path .exists ():
33- print (f"{ Colors .RED } Error: { toc_path .name } not found{ Colors .NC } " )
41+ print (f"{ Colors .RED } Error: ConsumableManager.toc not found{ Colors .NC } " )
42+ return "unknown"
43+ try :
44+ content = toc_path .read_text (encoding = "utf-8" )
45+ for line in content .splitlines ():
46+ if line .startswith ("## Version:" ):
47+ return line .split (":" , 1 )[1 ].strip ()
48+ except Exception as e :
49+ print (f"{ Colors .RED } Error reading version: { e } { Colors .NC } " )
3450 return "unknown"
35-
36- content = toc_path .read_text (encoding = "utf-8" )
37- for line in content .splitlines ():
38- if line .startswith ("## Version:" ):
39- return line .split (":" , 1 )[1 ].strip ()
4051 return "unknown"
4152
4253
4354def update_file (filepath : str , old_content : str , new_content : str ) -> bool :
44- """Update file content using pathlib and return success status"""
55+ """Update file content and return success status"""
4556 path = Path (filepath )
4657 if old_content == new_content :
4758 print (f"{ Colors .YELLOW } ℹ{ Colors .NC } No changes needed: { path .name } " )
@@ -56,50 +67,60 @@ def update_file(filepath: str, old_content: str, new_content: str) -> bool:
5667
5768
5869def bump_version (new_version : str ) -> int :
59- """Bump version in .toc and .lua files"""
70+ """
71+ Bump version in .toc and .lua files
72+ Returns: 0 on success, 2 on error
73+ """
74+
6075 print (f"{ Colors .YELLOW } Bumping version to: { Colors .GREEN } { new_version } { Colors .NC } " )
6176
6277 current_version = get_current_version ()
6378 print (f"Current version: { Colors .YELLOW } { current_version } { Colors .NC } " )
64- print (f"New version: { Colors .GREEN } { new_version } { Colors .NC } \n " )
79+ print (f"New version: { Colors .GREEN } { new_version } { Colors .NC } " )
80+ print ()
6581
82+ success = True
6683 toc_path = Path ("ConsumableManager.toc" )
6784 lua_path = Path ("ConsumableManager.lua" )
68- success = True
6985
70- # Update TOC
86+ # Update ConsumableManager.toc
7187 if toc_path .exists ():
72- content = toc_path .read_text (encoding = "utf-8" )
73- new_content = re .sub (
88+ toc_content = toc_path .read_text (encoding = "utf-8" )
89+ new_toc = re .sub (
7490 r"^## Version:.*$" ,
7591 f"## Version: { new_version } " ,
76- content ,
92+ toc_content ,
7793 flags = re .MULTILINE ,
7894 )
79- if update_file (str (toc_path ), content , new_content ):
95+
96+ if update_file (str (toc_path ), toc_content , new_toc ):
8097 print (f"{ Colors .GREEN } ✓{ Colors .NC } Updated: { toc_path .name } " )
8198 else :
8299 success = False
83100 else :
84101 print (f"{ Colors .RED } ✗{ Colors .NC } File not found: { toc_path .name } " )
85102 success = False
86103
87- # Update LUA
104+ # Update ConsumableManager.lua
88105 if lua_path .exists ():
89- content = lua_path .read_text (encoding = "utf-8" )
90- new_content = re .sub (
106+ lua_content = lua_path .read_text (encoding = "utf-8" )
107+
108+ # Update header comment
109+ new_lua = re .sub (
91110 r"^-- Version:.*$" ,
92111 f"-- Version: { new_version } " ,
93- content ,
112+ lua_content ,
94113 flags = re .MULTILINE ,
95114 )
96- new_content = re .sub (
97- r'local version = HexColor\("v[\d.]+' ,
98- f'local version = HexColor("v{ new_version } ' ,
99- new_content ,
115+
116+ # Update status display version (Improved regex to catch quoted versions)
117+ new_lua = re .sub (
118+ r"v\d+\.\d+\.\d+" ,
119+ f"v{ new_version } " ,
120+ new_lua ,
100121 )
101122
102- if update_file (str (lua_path ), content , new_content ):
123+ if update_file (str (lua_path ), lua_content , new_lua ):
103124 print (
104125 f"{ Colors .GREEN } ✓{ Colors .NC } Updated: { lua_path .name } (header + status)"
105126 )
@@ -109,25 +130,32 @@ def bump_version(new_version: str) -> int:
109130 print (f"{ Colors .RED } ✗{ Colors .NC } File not found: { lua_path .name } " )
110131 success = False
111132
133+ # Summary
134+ print ()
112135 if success :
113- print (f"\n { Colors .GREEN } ✓ Version bump complete!{ Colors .NC } " )
136+ print (f"{ Colors .GREEN } ✓ Version bump complete!{ Colors .NC } " )
114137 return 0
115-
116- print (f"\n { Colors .RED } ✗ Version bump failed!{ Colors .NC } " )
117- return 2
138+ else :
139+ print (f"{ Colors .RED } ✗ Version bump failed!{ Colors .NC } " )
140+ return 2
118141
119142
120143def main ():
121144 if len (sys .argv ) != 2 :
122145 print (f"{ Colors .RED } Error: No version specified{ Colors .NC } " )
146+ print (f"Usage: { sys .argv [0 ]} <new_version>" )
147+ print (f"Example: { sys .argv [0 ]} 2.9.0" )
123148 sys .exit (1 )
124149
125150 new_version = sys .argv [1 ]
151+
126152 if not validate_version (new_version ):
127153 print (f"{ Colors .RED } Error: Invalid version format{ Colors .NC } " )
154+ print ("Please use semantic versioning: X.Y.Z (e.g., 2.9.0)" )
128155 sys .exit (1 )
129156
130- sys .exit (bump_version (new_version ))
157+ exit_code = bump_version (new_version )
158+ sys .exit (exit_code )
131159
132160
133161if __name__ == "__main__" :
0 commit comments