Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Java support for pontos-version #772

Merged
merged 4 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 95 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion pontos/version/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@

from typing import Iterable, Tuple, Type

from ._cargo import CargoVersionCommand
from ._cmake import CMakeVersionCommand
from ._command import VersionCommand
from ._go import GoVersionCommand
from ._java import JavaVersionCommand
from ._javascript import JavaScriptVersionCommand
from ._python import PythonVersionCommand
from ._cargo import CargoVersionCommand

__all__ = (
"VersionCommand",
"CMakeVersionCommand",
"GoVersionCommand",
"JavaScriptVersionCommand",
"JavaVersionCommand",
"PythonVersionCommand",
"CargoVersionCommand",
"get_commands",
Expand All @@ -37,6 +39,7 @@
__COMMANDS: Tuple[Type[VersionCommand]] = (
CMakeVersionCommand,
GoVersionCommand,
JavaVersionCommand,
JavaScriptVersionCommand,
PythonVersionCommand,
CargoVersionCommand,
Expand Down
121 changes: 121 additions & 0 deletions pontos/version/commands/_java.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Copyright (C) 2020-2022 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from typing import Literal, Optional, Union

from lxml import etree

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

TEMPLATE = """# pylint: disable=invalid-name

# THIS IS AN AUTOGENERATED FILE. DO NOT TOUCH!

__version__ = "{}"\n"""


# This class is used for Java Version command(s)
class JavaVersionCommand(VersionCommand):
project_file_name = "pom.xml"
_pom_xml: Optional[etree.Element] = None

def _get_version_from_pom_xml(self) -> Version:
"""
Return the version information from the <version> tag of the
pom.xml file. The version may be in non standardized form.
"""

pom_xml: etree.Element = self.pom_xml

version_element = pom_xml.find("{*}version")
if version_element is None:
raise VersionError("Version tag missing in pom.xml")

return PEP440VersioningScheme.parse_version(version_element.text)

def _update_pom_version(
self,
new_version: Version,
) -> None:
"""
Update the version in the pom.xml file
"""
pom_xml: etree.Element = self.pom_xml

version_element = pom_xml.find("{*}version")
if version_element is None:
raise VersionError("Version tag missing in pom.xml")
version_element.text = str(new_version)

etree.ElementTree(pom_xml).write(
self.project_file_path, pretty_print=True, encoding="utf-8"
)

@property
def pom_xml(self) -> etree.Element:
if self._pom_xml is not None:
return self._pom_xml

if not self.project_file_path.exists():
raise VersionError("pom.xml file not found.")

try:
pom_xml: etree.ElementTree = etree.parse(self.project_file_path)
except etree.XMLSyntaxError as e:
raise VersionError(e) from e

self._pom_xml = pom_xml.getroot()

return self._pom_xml

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 self._get_version_from_pom_xml()

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

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

def update_version(
self, new_version: Version, *, force: bool = False
) -> VersionUpdate:
package_version = self.get_current_version()
if not force and new_version == package_version:
return VersionUpdate(previous=package_version, new=new_version)

changed_files = [self.project_file_path]
self._update_pom_version(new_version=new_version)

return VersionUpdate(
previous=package_version,
new=new_version,
changed_files=changed_files,
)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ rich = ">=12.4.4"
typing-extensions = { version = "^4.4.0", python = "<3.8" }
python-dateutil = "^2.8.2"
semver = ">=2.13,<4.0"
lxml = "^4.9.0"

[tool.poetry.dev-dependencies]
autohooks = ">=22.7.0"
Expand Down
Loading