Change old version to the new one in every file given. Note that this version is not the tag formatted one. So for example, your tag could look like `v1.0.0` while your version in the package like `1.0.0`. Returns the list of updated files.
(
current_version: str,
new_version: str,
version_files: Iterable[str],
*,
check_consistency: bool,
encoding: str,
)
| 17 | |
| 18 | |
| 19 | def update_version_in_files( |
| 20 | current_version: str, |
| 21 | new_version: str, |
| 22 | version_files: Iterable[str], |
| 23 | *, |
| 24 | check_consistency: bool, |
| 25 | encoding: str, |
| 26 | ) -> list[str]: |
| 27 | """Change old version to the new one in every file given. |
| 28 | |
| 29 | Note that this version is not the tag formatted one. |
| 30 | So for example, your tag could look like `v1.0.0` while your version in |
| 31 | the package like `1.0.0`. |
| 32 | |
| 33 | Returns the list of updated files. |
| 34 | """ |
| 35 | updated_files = [] |
| 36 | |
| 37 | for path, pattern in _resolve_files_and_regexes(version_files, current_version): |
| 38 | current_version_found = False |
| 39 | bumped_lines = [] |
| 40 | |
| 41 | with open(path, encoding=encoding) as version_file: |
| 42 | for line in version_file: |
| 43 | bumped_line = ( |
| 44 | line.replace(current_version, new_version) |
| 45 | if pattern.search(line) |
| 46 | else line |
| 47 | ) |
| 48 | |
| 49 | current_version_found = current_version_found or bumped_line != line |
| 50 | bumped_lines.append(bumped_line) |
| 51 | |
| 52 | if check_consistency and not current_version_found: |
| 53 | raise CurrentVersionNotFoundError( |
| 54 | f"Current version {current_version} is not found in {path}.\n" |
| 55 | "The version defined in commitizen configuration and the ones in " |
| 56 | "version_files are possibly inconsistent." |
| 57 | ) |
| 58 | |
| 59 | bumped_version_file_content = "".join(bumped_lines) |
| 60 | |
| 61 | # Write the file out again |
| 62 | with smart_open(path, "w", encoding=encoding) as file: |
| 63 | file.write(bumped_version_file_content) |
| 64 | updated_files.append(path) |
| 65 | |
| 66 | return updated_files |
| 67 | |
| 68 | |
| 69 | def _resolve_files_and_regexes( |
nothing calls this directly
no test coverage detected