@@ -75,16 +75,22 @@ def update_version_in_files(
7575
7676 Returns the list of updated files.
7777 """
78- # TODO: separate check step and write step
79- updated = []
80- for path , regex in _files_and_regexes (files , current_version ):
81- current_version_found , version_file = _bump_with_regex (
82- path ,
83- current_version ,
84- new_version ,
85- regex ,
86- encoding = encoding ,
87- )
78+ updated_files = []
79+
80+ for path , pattern in _resolve_files_and_regexes (files , current_version ):
81+ current_version_found = False
82+ bumped_lines = []
83+
84+ with open (path , encoding = encoding ) as version_file :
85+ for line in version_file :
86+ bumped_line = (
87+ line .replace (current_version , new_version )
88+ if pattern .search (line )
89+ else line
90+ )
91+
92+ current_version_found = current_version_found or bumped_line != line
93+ bumped_lines .append (bumped_line )
8894
8995 if check_consistency and not current_version_found :
9096 raise CurrentVersionNotFoundError (
@@ -93,14 +99,19 @@ def update_version_in_files(
9399 "version_files are possibly inconsistent."
94100 )
95101
102+ bumped_version_file_content = "" .join (bumped_lines )
103+
96104 # Write the file out again
97105 with smart_open (path , "w" , encoding = encoding ) as file :
98- file .write (version_file )
99- updated .append (path )
100- return updated
106+ file .write (bumped_version_file_content )
107+ updated_files .append (path )
108+
109+ return updated_files
101110
102111
103- def _files_and_regexes (patterns : Iterable [str ], version : str ) -> list [tuple [str , str ]]:
112+ def _resolve_files_and_regexes (
113+ patterns : Iterable [str ], version : str
114+ ) -> list [tuple [str , re .Pattern ]]:
104115 """
105116 Resolve all distinct files with their regexp from a list of glob patterns with optional regexp
106117 """
@@ -115,31 +126,7 @@ def _files_and_regexes(patterns: Iterable[str], version: str) -> list[tuple[str,
115126 for file in iglob (filepath ):
116127 out .add ((file , regex ))
117128
118- return sorted (out )
119-
120-
121- def _bump_with_regex (
122- version_filepath : str ,
123- current_version : str ,
124- new_version : str ,
125- regex : str ,
126- encoding : str = ENCODING ,
127- ) -> tuple [bool , str ]:
128- current_version_found = False
129- lines = []
130- pattern = re .compile (regex )
131- with open (version_filepath , encoding = encoding ) as f :
132- for line in f :
133- if not pattern .search (line ):
134- lines .append (line )
135- continue
136-
137- bumped_line = line .replace (current_version , new_version )
138- if bumped_line != line :
139- current_version_found = True
140- lines .append (bumped_line )
141-
142- return current_version_found , "" .join (lines )
129+ return [(file , re .compile (regex )) for file , regex in sorted (out )]
143130
144131
145132def create_commit_message (
0 commit comments