From 3a9263867cb6c2b2e48645ad42ce51251ebd561b Mon Sep 17 00:00:00 2001 From: Vlad Emelianov Date: Sat, 9 Nov 2024 05:44:25 +0300 Subject: [PATCH] Do not change version if not needed --- newversion/main.py | 2 +- newversion/package_version.py | 45 ++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/newversion/main.py b/newversion/main.py index 30caad5..255fefb 100644 --- a/newversion/main.py +++ b/newversion/main.py @@ -51,7 +51,7 @@ def main_api(config: CLINamespace) -> str: version=config.version, path=config.path, ) - if config.package: + if config.package and config.version == Version.zero(): executor.version = executor.command_get_version() try: result = _run_main_api(executor, config) diff --git a/newversion/package_version.py b/newversion/package_version.py index b6f456a..5ff079c 100644 --- a/newversion/package_version.py +++ b/newversion/package_version.py @@ -37,15 +37,17 @@ def _setup_cfg_path(self) -> Path: return self._path / "setup.cfg" @property - def _pyproject_path(self) -> Path: + def _pyproject_toml_path(self) -> Path: return self._path / "pyproject.toml" def _get_from_pyproject(self) -> Optional[Version]: - if not self._pyproject_path.exists(): + if not self._pyproject_toml_path.exists(): return None - self._logger.debug(f"Found {print_path(self._pyproject_path)}, extracting version from it") - text = self._pyproject_path.read_text() + self._logger.debug( + f"Found {print_path(self._pyproject_toml_path)}, extracting version from it" + ) + text = self._pyproject_toml_path.read_text() for line in text.splitlines(): if not line.startswith("version"): continue @@ -61,10 +63,10 @@ def _get_from_pyproject(self) -> Optional[Version]: return None def _set_in_pyproject(self, version: Version) -> None: - if not self._pyproject_path.exists(): + if not self._pyproject_toml_path.exists(): return - text = self._pyproject_path.read_text() + text = self._pyproject_toml_path.read_text() line_ending = EOLFixer.get_line_ending(text) lines: list[str] = [] changed = False @@ -79,17 +81,24 @@ def _set_in_pyproject(self, version: Version) -> None: continue old_version = match.group(1) + new_version = version.dumps() + if old_version == new_version: + self._logger.info( + f"Version in {print_path(self._pyproject_toml_path)}" + f" is already set to {new_version}, no change needed" + ) + return self._logger.info( - f"Changing version in {print_path(self._pyproject_path)}" - f" from {old_version} to {version.dumps()}" + f"Changing version in {print_path(self._pyproject_toml_path)}" + f" from {old_version} to {new_version}" ) - new_line = line.replace(old_version, version.dumps()) + new_line = line.replace(old_version, new_version) lines.append(new_line) changed = True if changed: text = EOLFixer.add_newline(line_ending.join(lines)) - self._pyproject_path.write_text(text) + self._pyproject_toml_path.write_text(text) def _get_from_setup_cfg(self) -> Optional[Version]: if not self._setup_cfg_path.exists(): @@ -143,9 +152,16 @@ def _set_in_setup_cfg(self, version: Version) -> None: continue old_version = match.group(1) + new_version = version.dumps() + if old_version == new_version: + self._logger.info( + f"Version in {print_path(self._setup_cfg_path)}" + f" is already set to {new_version}, no change needed" + ) + return self._logger.info( f"Changing version in {print_path(self._setup_cfg_path)}" - f" from {old_version} to {version.dumps()}" + f" from {old_version} to {new_version}" ) new_line = line.replace(old_version, version.dumps()) lines.append(new_line) @@ -194,6 +210,13 @@ def _set_in_setup_py(self, version: Version) -> None: continue old_version = match.group(1) + new_version = version.dumps() + if old_version == new_version: + self._logger.info( + f"Version in {print_path(self._setup_py_path)}" + f" is already set to {new_version}, no change needed" + ) + return self._logger.info( f"Changing version in {print_path(self._setup_py_path)}" f" from {old_version} to {version.dumps()}"