2
2
import glob
3
3
import logging
4
4
from difflib import context_diff
5
- from typing import List , MutableMapping
5
+ from typing import List , MutableMapping , Optional
6
6
7
7
from bumpversion .config import FileConfig
8
8
from bumpversion .exceptions import VersionNotFoundError
14
14
class ConfiguredFile :
15
15
"""A file to modify in a configured way."""
16
16
17
- def __init__ (self , file_cfg : FileConfig , version_config : VersionConfig ) -> None :
17
+ def __init__ (
18
+ self ,
19
+ file_cfg : FileConfig ,
20
+ version_config : VersionConfig ,
21
+ search : Optional [str ] = None ,
22
+ replace : Optional [str ] = None ,
23
+ ) -> None :
18
24
self .path = file_cfg .filename
19
25
self .parse = file_cfg .parse or version_config .parse_regex .pattern
20
26
self .serialize = file_cfg .serialize or version_config .serialize_formats
21
- self .search = file_cfg .search or version_config .search
22
- self .replace = file_cfg .replace or version_config .replace
27
+ self .search = search or file_cfg .search or version_config .search
28
+ self .replace = replace or file_cfg .replace or version_config .replace
23
29
self .version_config = VersionConfig (
24
30
self .parse , self .serialize , self .search , self .replace , version_config .part_configs
25
31
)
@@ -97,8 +103,10 @@ def replace_version(
97
103
file_content_before = f .read ()
98
104
file_new_lines = f .newlines [0 ] if isinstance (f .newlines , tuple ) else f .newlines
99
105
100
- context ["current_version" ] = self .version_config .serialize (current_version , context )
101
- context ["new_version" ] = self .version_config .serialize (new_version , context )
106
+ if current_version :
107
+ context ["current_version" ] = self .version_config .serialize (current_version , context )
108
+ if new_version :
109
+ context ["new_version" ] = self .version_config .serialize (new_version , context )
102
110
103
111
search_for = self .version_config .search .format (** context )
104
112
replace_with = self .version_config .replace .format (** context )
@@ -138,13 +146,17 @@ def __repr__(self) -> str:
138
146
return f"<bumpversion.ConfiguredFile:{ self .path } >"
139
147
140
148
141
- def resolve_file_config (files : List [FileConfig ], version_config : VersionConfig ) -> List [ConfiguredFile ]:
149
+ def resolve_file_config (
150
+ files : List [FileConfig ], version_config : VersionConfig , search : Optional [str ] = None , replace : Optional [str ] = None
151
+ ) -> List [ConfiguredFile ]:
142
152
"""
143
153
Resolve the files, searching and replacing values according to the FileConfig.
144
154
145
155
Args:
146
156
files: A list of file configurations
147
157
version_config: How the version should be changed
158
+ search: The search pattern to use instead of any configured search pattern
159
+ replace: The replace pattern to use instead of any configured replace pattern
148
160
149
161
Returns:
150
162
A list of ConfiguredFiles
@@ -154,7 +166,7 @@ def resolve_file_config(files: List[FileConfig], version_config: VersionConfig)
154
166
if file_cfg .glob :
155
167
configured_files .extend (get_glob_files (file_cfg , version_config ))
156
168
else :
157
- configured_files .append (ConfiguredFile (file_cfg , version_config ))
169
+ configured_files .append (ConfiguredFile (file_cfg , version_config , search , replace ))
158
170
159
171
return configured_files
160
172
@@ -181,13 +193,17 @@ def modify_files(
181
193
f .replace_version (current_version , new_version , context , dry_run )
182
194
183
195
184
- def get_glob_files (file_cfg : FileConfig , version_config : VersionConfig ) -> List [ConfiguredFile ]:
196
+ def get_glob_files (
197
+ file_cfg : FileConfig , version_config : VersionConfig , search : Optional [str ] = None , replace : Optional [str ] = None
198
+ ) -> List [ConfiguredFile ]:
185
199
"""
186
200
Return a list of files that match the glob pattern.
187
201
188
202
Args:
189
203
file_cfg: The file configuration containing the glob pattern
190
204
version_config: The version configuration
205
+ search: The search pattern to use instead of any configured search pattern
206
+ replace: The replace pattern to use instead of any configured replace pattern
191
207
192
208
Returns:
193
209
A list of resolved files according to the pattern.
@@ -196,7 +212,7 @@ def get_glob_files(file_cfg: FileConfig, version_config: VersionConfig) -> List[
196
212
for filename_glob in glob .glob (file_cfg .glob , recursive = True ):
197
213
new_file_cfg = file_cfg .copy ()
198
214
new_file_cfg .filename = filename_glob
199
- files .append (ConfiguredFile (new_file_cfg , version_config ))
215
+ files .append (ConfiguredFile (new_file_cfg , version_config , search , replace ))
200
216
return files
201
217
202
218
0 commit comments