Skip to content

Commit

Permalink
Change: Use Version class when version information is passed
Browse files Browse the repository at this point in the history
Exchange version information as a Version class instance instead of
string.
  • Loading branch information
bjoernricks committed Mar 1, 2023
1 parent a011aa6 commit 0209a1b
Show file tree
Hide file tree
Showing 15 changed files with 382 additions and 387 deletions.
4 changes: 4 additions & 0 deletions pontos/release/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from typing import Callable, Tuple, Type

from pontos.release.helper import ReleaseType
from pontos.version.version import parse_version

from .release import release
from .sign import sign
Expand Down Expand Up @@ -92,6 +93,7 @@ def parse_args(args) -> Tuple[str, str, Namespace]:
"Will release changelog as version. Must be PEP 440 compliant. "
"default: lookup version in project definition."
),
type=parse_version,
action=ReleaseVersionAction,
)

Expand All @@ -101,6 +103,7 @@ def parse_args(args) -> Tuple[str, str, Namespace]:
"Sets the next PEP 440 compliant version in project definition "
"after the release. default: set to next dev version"
),
type=parse_version,
)

release_parser.add_argument(
Expand Down Expand Up @@ -149,6 +152,7 @@ def parse_args(args) -> Tuple[str, str, Namespace]:
sign_parser.add_argument(
"--release-version",
help="Will release changelog as version. Must be PEP 440 compliant.",
type=parse_version,
)
sign_parser.add_argument(
"--git-tag-prefix",
Expand Down
30 changes: 17 additions & 13 deletions pontos/release/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from pontos.version.commands import gather_project
from pontos.version.errors import VersionError
from pontos.version.helper import get_last_release_version
from pontos.version.version import VersionCommand
from pontos.version.version import Version, VersionCommand

from .helper import ReleaseType, find_signing_key, get_git_repository_name

Expand All @@ -57,8 +57,8 @@ def _get_release_version(
self,
command: VersionCommand,
release_type: ReleaseType,
release_version: Optional[str],
) -> str:
release_version: Optional[Version],
) -> Version:
current_version = command.get_current_version()
calculator = command.get_version_calculator()
if release_type == ReleaseType.CALENDAR:
Expand Down Expand Up @@ -93,14 +93,9 @@ def _has_tag(self, git_version: str) -> bool:
git_tags = self.git.list_tags()
return git_version in git_tags

def _create_changelog(self, release_version: str, cc_config: Path) -> str:
last_release_version = get_last_release_version(self.git_tag_prefix)

self.terminal.info(
f"Creating changelog for {release_version} since "
f"{last_release_version}"
)

def _create_changelog(
self, release_version: str, last_release_version: str, cc_config: Path
) -> str:
changelog_builder = ChangelogBuilder(
space=self.space,
project=self.project,
Expand Down Expand Up @@ -135,7 +130,7 @@ async def run(
space: str,
project: Optional[str],
release_type: ReleaseType,
release_version: Optional[str],
release_version: Optional[Version],
next_version: Optional[str],
git_signing_key: str,
git_remote_name: Optional[str],
Expand Down Expand Up @@ -218,7 +213,16 @@ async def run(
)
return ReleaseReturnValue.UPDATE_VERSION_ERROR

release_text = self._create_changelog(release_version, cc_config)
last_release_version = get_last_release_version(self.git_tag_prefix)

self.terminal.info(
f"Creating changelog for {release_version} since "
f"{last_release_version}"
)

release_text = self._create_changelog(
release_version, last_release_version, cc_config
)

self.terminal.info("Committing changes")

Expand Down
3 changes: 2 additions & 1 deletion pontos/release/sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from pontos.terminal import Terminal
from pontos.terminal.rich import RichTerminal
from pontos.version.helper import get_last_release_version
from pontos.version.version import Version

from .helper import get_git_repository_name

Expand Down Expand Up @@ -173,7 +174,7 @@ async def run(
project: Optional[str],
space: str,
git_tag_prefix: Optional[str],
release_version: Optional[str],
release_version: Optional[Version],
signing_key: str,
passphrase: str,
) -> SignReturnValue:
Expand Down
65 changes: 27 additions & 38 deletions pontos/version/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,46 @@


import re
from typing import Generator, Optional, Tuple
from typing import Generator, Literal, Optional, Tuple, Union

from packaging.version import Version
from pontos.version.errors import VersionError

from .errors import VersionError
from .helper import (
check_develop,
is_version_pep440_compliant,
safe_version,
versions_equal,
)
from .version import VersionCommand, VersionUpdate
from .version import Version, VersionCommand, VersionUpdate, parse_version


class CMakeVersionCommand(VersionCommand):
project_file_name = "CMakeLists.txt"

def update_version(
self, new_version: str, *, force: bool = False
self, new_version: Version, *, force: bool = False
) -> VersionUpdate:
content = self.project_file_path.read_text(encoding="utf-8")
cmvp = CMakeVersionParser(content)
cmake_version_parser = CMakeVersionParser(content)
previous_version = self.get_current_version()

if not force and versions_equal(new_version, previous_version):
if not force and new_version == previous_version:
return VersionUpdate(previous=previous_version, new=new_version)

new_content = cmvp.update_version(new_version)
new_content = cmake_version_parser.update_version(new_version)
self.project_file_path.write_text(new_content, encoding="utf-8")
return VersionUpdate(
previous=previous_version,
new=new_version,
changed_files=[self.project_file_path],
)

def get_current_version(self) -> str:
def get_current_version(self) -> Version:
content = self.project_file_path.read_text(encoding="utf-8")
return CMakeVersionParser(content).get_current_version()

def verify_version(self, version: str) -> None:
def verify_version(
self, version: Union[Literal["current"], Version]
) -> None:
current_version = self.get_current_version()
if not is_version_pep440_compliant(current_version):
if current_version != version:
raise VersionError(
f"The version {current_version} is not PEP 440 compliant."
f"Provided version {version} does not match the "
f"current version {current_version}."
)


Expand Down Expand Up @@ -89,33 +85,26 @@ def __init__(self, cmake_content_lines: str):
]
)

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

def update_version(self, new_version: str) -> str:
if not is_version_pep440_compliant(new_version):
raise VersionError(
f"version {new_version} is not pep 440 compliant."
def update_version(self, new_version: Version) -> str:
if new_version.is_devrelease:
new_version = parse_version(
f"{str(new_version.major)}."
f"{str(new_version.minor)}."
f"{str(new_version.micro)}"
)

new_version = safe_version(new_version)
if check_develop(new_version):
vers = Version(new_version)
if vers.dev is not None:
new_version = str(
Version(
f"{str(vers.major)}.{str(vers.minor)}"
f".{str(vers.micro)}"
)
)
develop = True
else:
develop = False

to_update = self._cmake_content_lines[self._version_line_number]
updated = to_update.replace(self._current_version, new_version)
updated = to_update.replace(self._current_version, str(new_version))
self._cmake_content_lines[self._version_line_number] = updated
self._current_version = new_version
if self._project_dev_version_line_number:
Expand Down
34 changes: 14 additions & 20 deletions pontos/version/go.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@

import re
from pathlib import Path

from pontos.git import Git, TagSort
from typing import Literal, Union

from .errors import VersionError
from .helper import is_version_pep440_compliant, safe_version, versions_equal
from .version import VersionCommand, VersionUpdate
from .helper import get_last_release_version
from .version import Version, VersionCommand, VersionUpdate, parse_version

TEMPLATE = """package main
Expand All @@ -37,15 +36,15 @@ class GoVersionCommand(VersionCommand):
project_file_name = "go.mod"
version_file_path = Path("version.go")

def _update_version_file(self, new_version: str) -> None:
def _update_version_file(self, new_version: Version) -> None:
"""
Update the version file with the new version
"""
self.version_file_path.write_text(
TEMPLATE.format(new_version), encoding="utf-8"
TEMPLATE.format(str(new_version)), encoding="utf-8"
)

def get_current_version(self) -> str:
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"""
Expand All @@ -57,7 +56,7 @@ def get_current_version(self) -> str:
r'var version = "([deprv0-9.]+)"', version_file_text
)
if match:
return match.group(1)
return parse_version(match.group(1))
else:
raise VersionError(
f"No version found in the {self.version_file_path} file."
Expand All @@ -68,32 +67,27 @@ def get_current_version(self) -> str:
"This file is required for pontos"
)

def verify_version(self, version: str) -> None:
def verify_version(
self, version: Union[Literal["current"], Version]
) -> None:
"""Verify the current version of this project"""
current_version = self.get_current_version()
if not is_version_pep440_compliant(current_version):
raise VersionError(
f"The version {current_version} is not PEP 440 compliant."
)

if not versions_equal(current_version, version):
if current_version != version:
raise VersionError(
f"Provided version {version} does not match the "
f"current version {current_version}."
)

def update_version(
self, new_version: str, *, force: bool = False
self, new_version: Version, *, force: bool = False
) -> VersionUpdate:
"""Update the current version of this project"""
new_version = safe_version(new_version)

try:
current_version = self.get_current_version()
except VersionError:
current_version = Git().list_tags(sort=TagSort.VERSION)[-1]
current_version = get_last_release_version("v")

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

self._update_version_file(new_version=new_version)
Expand Down
Loading

0 comments on commit 0209a1b

Please sign in to comment.