Skip to content

Commit 0a8de36

Browse files
bjoernricksy0urself
authored andcommitted
Change: Refactor VersionCommands
* Add support for VersioningScheme and therefore different version parsing * Move all VersionCommand classes into a commands package * Mark commands sup modules as "private"
1 parent 3c09b27 commit 0a8de36

File tree

12 files changed

+80
-70
lines changed

12 files changed

+80
-70
lines changed

docs/pontos/version.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
```{toctree}
66
:maxdepth: 1
77
8-
version/cmake
9-
version/go
10-
version/javascript
8+
version/commands
119
version/project
12-
version/python
1310
version/schemes
1411
version/helper
1512
```

docs/pontos/version/cmake.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

docs/pontos/version/commands.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# pontos.version.commands package
2+
3+
```{eval-rst}
4+
.. automodule:: pontos.version.commands
5+
:members:
6+
```

docs/pontos/version/go.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

docs/pontos/version/javascript.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

docs/pontos/version/python.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

pontos/version/commands.py renamed to pontos/version/commands/__init__.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@
1717

1818
from typing import Iterable, Tuple, Type
1919

20-
from .cmake import CMakeVersionCommand
21-
from .go import GoVersionCommand
22-
from .javascript import JavaScriptVersionCommand
23-
from .python import PythonVersionCommand
24-
from .version import VersionCommand
20+
from ._cmake import CMakeVersionCommand
21+
from ._command import VersionCommand
22+
from ._go import GoVersionCommand
23+
from ._javascript import JavaScriptVersionCommand
24+
from ._python import PythonVersionCommand
25+
26+
__all__ = (
27+
"VersionCommand",
28+
"CMakeVersionCommand",
29+
"GoVersionCommand",
30+
"JavaScriptVersionCommand",
31+
"PythonVersionCommand",
32+
"get_commands",
33+
)
2534

2635
__COMMANDS: Tuple[Type[VersionCommand]] = (
2736
CMakeVersionCommand,
@@ -32,4 +41,7 @@
3241

3342

3443
def get_commands() -> Iterable[Type[VersionCommand]]:
44+
"""
45+
Returns the available VersionCommands
46+
"""
3547
return __COMMANDS

pontos/version/cmake.py renamed to pontos/version/commands/_cmake.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
import re
2020
from typing import Generator, Literal, Optional, Tuple, Union
2121

22-
from pontos.version.errors import VersionError
23-
24-
from .version import Version, VersionCommand, VersionUpdate, parse_version
22+
from ..errors import VersionError
23+
from ..schemes import PEP440VersioningScheme
24+
from ..version import Version, VersionUpdate
25+
from ._command import VersionCommand
2526

2627

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

5253
content = self.project_file_path.read_text(encoding="utf-8")
53-
return CMakeVersionParser(content).get_current_version()
54+
current_version = CMakeVersionParser(content).get_current_version()
55+
return self.versioning_scheme.from_version(current_version)
5456

5557
def verify_version(
56-
self, version: Union[Literal["current"], Version]
58+
self, version: Union[Literal["current"], Version, None]
5759
) -> None:
5860
current_version = self.get_current_version()
5961

60-
if version == "current":
62+
if not version or version == "current":
6163
return
6264

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

9597
def get_current_version(self) -> Version:
9698
return (
97-
parse_version(f"{self._current_version}.dev1")
99+
PEP440VersioningScheme.parse_version(
100+
f"{self._current_version}.dev1"
101+
)
98102
if self.is_dev_version()
99-
else parse_version(self._current_version)
103+
else PEP440VersioningScheme.parse_version(self._current_version)
100104
)
101105

102106
def update_version(self, new_version: Version) -> str:
103-
if new_version.is_devrelease:
104-
new_version = parse_version(
107+
if new_version.is_dev_release:
108+
new_version = PEP440VersioningScheme.parse_version(
105109
f"{str(new_version.major)}."
106110
f"{str(new_version.minor)}."
107-
f"{str(new_version.micro)}"
111+
f"{str(new_version.patch)}"
108112
)
109113
develop = True
110114
else:

pontos/version/_command.py renamed to pontos/version/commands/_command.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
from pathlib import Path
2020
from typing import Literal, Union
2121

22-
from .scheme import VersioningScheme
23-
from .version import Version, VersionUpdate
22+
from ..schemes import VersioningScheme
23+
from ..version import Version, VersionUpdate
2424

2525

2626
class VersionCommand(ABC):

pontos/version/go.py renamed to pontos/version/commands/_go.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
from pathlib import Path
2020
from typing import Literal, Union
2121

22-
from .errors import VersionError
23-
from .helper import get_last_release_version
24-
from .version import Version, VersionCommand, VersionUpdate, parse_version
22+
from ..errors import VersionError
23+
from ..helper import get_last_release_version
24+
from ..version import Version, VersionUpdate
25+
from ._command import VersionCommand
2526

2627
VERSION_MATCH = r'var [Vv]ersion = "(.+)"'
2728
TEMPLATE = """package main
@@ -60,7 +61,7 @@ def get_current_version(self) -> Version:
6061
)
6162
match = re.search(VERSION_MATCH, version_file_text)
6263
if match:
63-
return parse_version(match.group(1))
64+
return self.versioning_scheme.parse_version(match.group(1))
6465
else:
6566
raise VersionError(
6667
f"No version found in the {self.version_file_path} file."
@@ -72,12 +73,12 @@ def get_current_version(self) -> Version:
7273
)
7374

7475
def verify_version(
75-
self, version: Union[Literal["current"], Version]
76+
self, version: Union[Literal["current"], Version, None]
7677
) -> None:
7778
"""Verify the current version of this project"""
7879
current_version = self.get_current_version()
7980

80-
if version == "current":
81+
if not version or version == "current":
8182
return
8283

8384
if current_version != version:
@@ -93,7 +94,9 @@ def update_version(
9394
try:
9495
current_version = self.get_current_version()
9596
except VersionError:
96-
current_version = get_last_release_version("v")
97+
current_version = get_last_release_version(
98+
self.versioning_scheme.parse_version, git_tag_prefix="v"
99+
)
97100

98101
if not force and new_version == current_version:
99102
return VersionUpdate(previous=current_version, new=new_version)

0 commit comments

Comments
 (0)