@@ -150,26 +150,25 @@ def update_version_in_files(
150
150
"""
151
151
# TODO: separate check step and write step
152
152
for location in files :
153
- filepath , * regexes = location .split (":" , maxsplit = 1 )
153
+ filepath , * regexes = location .split (":" )
154
154
regex = regexes [0 ] if regexes else None
155
-
156
- # Read in the file
157
- file_content = []
158
155
current_version_found = False
159
- with open (filepath , "r" ) as version_file :
160
- for line in version_file :
161
- if regex :
162
- match = re .search (regex , line )
163
- if not match :
164
- file_content .append (line )
165
- continue
166
-
167
- # Replace the target string
168
- if current_version in line :
169
- current_version_found = True
170
- file_content .append (line .replace (current_version , new_version ))
171
- else :
172
- file_content .append (line )
156
+
157
+ version_file = open (filepath , "r" ).read ()
158
+ match = regex and re .search (regex , version_file , re .MULTILINE )
159
+ if match :
160
+ left = version_file [: match .end ()]
161
+ right = version_file [match .end () :]
162
+ line_break = _get_line_break_position (right )
163
+ middle = right [:line_break ]
164
+ current_version_found = current_version in middle
165
+ right = right [line_break :]
166
+ version_file = left + middle .replace (current_version , new_version ) + right
167
+
168
+ if not regex :
169
+ current_version_regex = _version_to_regex (current_version )
170
+ current_version_found = current_version_regex .search (version_file ) and True
171
+ version_file = current_version_regex .sub (new_version , version_file )
173
172
174
173
if check_consistency and not current_version_found :
175
174
raise CurrentVersionNotFoundError (
@@ -180,7 +179,17 @@ def update_version_in_files(
180
179
181
180
# Write the file out again
182
181
with open (filepath , "w" ) as file :
183
- file .write ("" .join (file_content ))
182
+ file .write ("" .join (version_file ))
183
+
184
+
185
+ def _get_line_break_position (text : str ) -> int :
186
+ position = text .find ("\n " )
187
+ return max (position , 0 )
188
+
189
+
190
+ def _version_to_regex (version : str ):
191
+ clean_regex = version .replace ("." , r"\." ).replace ("+" , r"\+" )
192
+ return re .compile (f"\\ b{ clean_regex } \\ b" )
184
193
185
194
186
195
def create_tag (version : Union [Version , str ], tag_format : Optional [str ] = None ):
0 commit comments