Skip to content

Commit

Permalink
Change: Refactor VersionCommands
Browse files Browse the repository at this point in the history
* Add support for VersioningScheme and therefore different version
  parsing
* Move all VersionCommand classes into a commands package
* Mark commands sup modules as "private"
  • Loading branch information
bjoernricks authored and y0urself committed Mar 14, 2023
1 parent 3c09b27 commit 0a8de36
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 70 deletions.
5 changes: 1 addition & 4 deletions docs/pontos/version.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
```{toctree}
:maxdepth: 1
version/cmake
version/go
version/javascript
version/commands
version/project
version/python
version/schemes
version/helper
```
Expand Down
6 changes: 0 additions & 6 deletions docs/pontos/version/cmake.md

This file was deleted.

6 changes: 6 additions & 0 deletions docs/pontos/version/commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# pontos.version.commands package

```{eval-rst}
.. automodule:: pontos.version.commands
:members:
```
6 changes: 0 additions & 6 deletions docs/pontos/version/go.md

This file was deleted.

6 changes: 0 additions & 6 deletions docs/pontos/version/javascript.md

This file was deleted.

6 changes: 0 additions & 6 deletions docs/pontos/version/python.md

This file was deleted.

22 changes: 17 additions & 5 deletions pontos/version/commands.py → pontos/version/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@

from typing import Iterable, Tuple, Type

from .cmake import CMakeVersionCommand
from .go import GoVersionCommand
from .javascript import JavaScriptVersionCommand
from .python import PythonVersionCommand
from .version import VersionCommand
from ._cmake import CMakeVersionCommand
from ._command import VersionCommand
from ._go import GoVersionCommand
from ._javascript import JavaScriptVersionCommand
from ._python import PythonVersionCommand

__all__ = (
"VersionCommand",
"CMakeVersionCommand",
"GoVersionCommand",
"JavaScriptVersionCommand",
"PythonVersionCommand",
"get_commands",
)

__COMMANDS: Tuple[Type[VersionCommand]] = (
CMakeVersionCommand,
Expand All @@ -32,4 +41,7 @@


def get_commands() -> Iterable[Type[VersionCommand]]:
"""
Returns the available VersionCommands
"""
return __COMMANDS
26 changes: 15 additions & 11 deletions pontos/version/cmake.py → pontos/version/commands/_cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
import re
from typing import Generator, Literal, Optional, Tuple, Union

from pontos.version.errors import VersionError

from .version import Version, VersionCommand, VersionUpdate, parse_version
from ..errors import VersionError
from ..schemes import PEP440VersioningScheme
from ..version import Version, VersionUpdate
from ._command import VersionCommand


class CMakeVersionCommand(VersionCommand):
Expand Down Expand Up @@ -50,14 +51,15 @@ def get_current_version(self) -> Version:
raise VersionError(f"{self.project_file_path} not found.")

content = self.project_file_path.read_text(encoding="utf-8")
return CMakeVersionParser(content).get_current_version()
current_version = CMakeVersionParser(content).get_current_version()
return self.versioning_scheme.from_version(current_version)

def verify_version(
self, version: Union[Literal["current"], Version]
self, version: Union[Literal["current"], Version, None]
) -> None:
current_version = self.get_current_version()

if version == "current":
if not version or version == "current":
return

if current_version != version:
Expand Down Expand Up @@ -94,17 +96,19 @@ def __init__(self, cmake_content_lines: str):

def get_current_version(self) -> Version:
return (
parse_version(f"{self._current_version}.dev1")
PEP440VersioningScheme.parse_version(
f"{self._current_version}.dev1"
)
if self.is_dev_version()
else parse_version(self._current_version)
else PEP440VersioningScheme.parse_version(self._current_version)
)

def update_version(self, new_version: Version) -> str:
if new_version.is_devrelease:
new_version = parse_version(
if new_version.is_dev_release:
new_version = PEP440VersioningScheme.parse_version(
f"{str(new_version.major)}."
f"{str(new_version.minor)}."
f"{str(new_version.micro)}"
f"{str(new_version.patch)}"
)
develop = True
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
from pathlib import Path
from typing import Literal, Union

from .scheme import VersioningScheme
from .version import Version, VersionUpdate
from ..schemes import VersioningScheme
from ..version import Version, VersionUpdate


class VersionCommand(ABC):
Expand Down
17 changes: 10 additions & 7 deletions pontos/version/go.py → pontos/version/commands/_go.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
from pathlib import Path
from typing import Literal, Union

from .errors import VersionError
from .helper import get_last_release_version
from .version import Version, VersionCommand, VersionUpdate, parse_version
from ..errors import VersionError
from ..helper import get_last_release_version
from ..version import Version, VersionUpdate
from ._command import VersionCommand

VERSION_MATCH = r'var [Vv]ersion = "(.+)"'
TEMPLATE = """package main
Expand Down Expand Up @@ -60,7 +61,7 @@ def get_current_version(self) -> Version:
)
match = re.search(VERSION_MATCH, version_file_text)
if match:
return parse_version(match.group(1))
return self.versioning_scheme.parse_version(match.group(1))
else:
raise VersionError(
f"No version found in the {self.version_file_path} file."
Expand All @@ -72,12 +73,12 @@ def get_current_version(self) -> Version:
)

def verify_version(
self, version: Union[Literal["current"], Version]
self, version: Union[Literal["current"], Version, None]
) -> None:
"""Verify the current version of this project"""
current_version = self.get_current_version()

if version == "current":
if not version or version == "current":
return

if current_version != version:
Expand All @@ -93,7 +94,9 @@ def update_version(
try:
current_version = self.get_current_version()
except VersionError:
current_version = get_last_release_version("v")
current_version = get_last_release_version(
self.versioning_scheme.parse_version, git_tag_prefix="v"
)

if not force and new_version == current_version:
return VersionUpdate(previous=current_version, new=new_version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
from pathlib import Path
from typing import Any, Dict, Literal, Optional, Union

from .errors import VersionError
from .version import Version, VersionCommand, VersionUpdate, parse_version
from ..errors import VersionError
from ..version import Version, VersionUpdate
from ._command import VersionCommand


# This class is used for JavaScript Version command(s)
Expand Down Expand Up @@ -70,21 +71,21 @@ def _get_current_file_version(
if not match:
raise VersionError(f"VERSION variable not found in {version_file}")

return Version(match.group("version"))
return self.versioning_scheme.parse_version(match.group("version"))

def get_current_version(self) -> Version:
"""Get the current version of this project
In go the version is only defined within the repository
tags, thus we need to check git, what tag is the latest"""
return parse_version(self.package["version"])
return self.versioning_scheme.parse_version(self.package["version"])

def verify_version(
self, version: Union[Literal["current"], Version]
self, version: Union[Literal["current"], Version, None]
) -> None:
"""Verify the current version of this project"""
current_version = self.get_current_version()

if version == "current":
if not version or version == "current":
for version_file in self.version_file_paths:
file_version = self._get_current_file_version(version_file)
if file_version and file_version != current_version:
Expand Down
33 changes: 22 additions & 11 deletions pontos/version/python.py → pontos/version/commands/_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

import tomlkit

from .errors import VersionError
from .version import Version, VersionCommand, VersionUpdate, parse_version
from ..errors import VersionError
from ..schemes import PEP440VersioningScheme
from ..version import Version, VersionUpdate
from ._command import VersionCommand

TEMPLATE = """# pylint: disable=invalid-name
Expand All @@ -48,7 +50,7 @@ def _get_version_from_pyproject_toml(self) -> Version:
and "poetry" in self.pyproject_toml["tool"] # type: ignore
and "version" in self.pyproject_toml["tool"]["poetry"] # type: ignore # pylint: disable=line-too-long
):
return parse_version(self.pyproject_toml["tool"]["poetry"]["version"]) # type: ignore # pylint: disable=line-too-long
return self.versioning_scheme.parse_version(self.pyproject_toml["tool"]["poetry"]["version"]) # type: ignore # pylint: disable=line-too-long

raise VersionError(
"Version information not found in "
Expand Down Expand Up @@ -145,7 +147,9 @@ def get_current_version(self) -> Version:
)
version_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(version_module)
return parse_version(version_module.__version__)
return PEP440VersioningScheme.parse_version(
version_module.__version__
)
except FileNotFoundError:
raise VersionError(
f"Could not load version from '{module_name}'. "
Expand All @@ -157,7 +161,7 @@ def get_current_version(self) -> Version:
) from None

def verify_version(
self, version: Union[Literal["current"], Version]
self, version: Union[Literal["current"], Version, None]
) -> None:
current_version = self.get_current_version()
pyproject_version = self._get_version_from_pyproject_toml()
Expand All @@ -169,7 +173,7 @@ def verify_version(
f"version {current_version}."
)

if version != "current":
if version and version != "current":
if version != current_version:
raise VersionError(
f"Provided version {version} does not match the "
Expand All @@ -186,15 +190,22 @@ def update_version(
# pyproject.toml
current_version = self._get_version_from_pyproject_toml()

if not force and new_version == current_version:
return VersionUpdate(previous=current_version, new=new_version)
new_pep440_version = PEP440VersioningScheme.from_version(new_version)
current_converted_version = self.versioning_scheme.from_version(
current_version
)

if not force and new_pep440_version == current_version:
return VersionUpdate(
previous=current_converted_version, new=new_version
)

self._update_pyproject_version(new_version=new_version)
self._update_pyproject_version(new_version=new_pep440_version)

self._update_version_file(new_version=new_version)
self._update_version_file(new_version=new_pep440_version)

return VersionUpdate(
previous=current_version,
previous=current_converted_version,
new=new_version,
changed_files=[self.version_file_path, self.project_file_path],
)

0 comments on commit 0a8de36

Please sign in to comment.