Skip to content

Commit

Permalink
Write JSON keys/values to __gradle_version__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
object-Object committed Jun 9, 2024
1 parent 8e450e3 commit 2f57a09
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "hatch-gradle-version"
version = "0.9.9"
version = "0.9.10"
authors = [
{ name="object-Object", email="[email protected]" },
]
Expand Down
62 changes: 59 additions & 3 deletions src/hatch_gradle_version/plugins/version_source/json.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import json
from typing import Annotated, Any
from typing import Annotated, Any, overload

from pydantic import Field, field_validator
from pydantic.types import JsonValue

from hatch_gradle_version.common.gradle import GradleVersion
from hatch_gradle_version.common.model import GradlePath
Expand All @@ -17,13 +18,68 @@ class JSONVersionSource(BaseVersionSource):

def get_gradle_version(self) -> GradleVersion:
with self.json_path.open() as f:
data = json.load(f)
data: JsonValue = json.load(f)

raw_version = data
for key in self.key:
if not isinstance(raw_version, dict):
raise ValueError(f"Invalid key: {self.key} (not a dict: {raw_version})")
raw_version = raw_version[key]

return GradleVersion.from_raw(raw_version, {}, self.fmt_raw_gradle_version)
raw_version = self.json_to_string(raw_version)
if raw_version is None:
raise ValueError(f"Invalid key: {self.key} (invalid value: {raw_version})")

extra_versions = dict[str, str]()
self.get_extra_versions(data, extra_versions, [])

return GradleVersion.from_raw(
raw_version,
extra_versions,
self.fmt_raw_gradle_version,
)

def get_extra_versions(
self,
value: JsonValue,
result: dict[str, str],
parents: list[str],
):
match value:
case dict():
for key, child in value.items():
parents.append(key)
self.get_extra_versions(child, result, parents)
parents.pop()
case list():
for i, child in enumerate(value):
parents.append(str(i) if parents else f"_{i}")
self.get_extra_versions(child, result, parents)
parents.pop()
case _:
result[".".join(parents)] = self.json_to_string(value)

@overload
def json_to_string(self, value: str | bool | int | float | None) -> str: ...

@overload
def json_to_string(self, value: list[JsonValue] | dict[str, JsonValue]) -> None: ...

@overload
def json_to_string(self, value: JsonValue) -> str | None: ...

def json_to_string(self, value: JsonValue):
match value:
case dict() | list():
return None
case True:
return "true"
case False:
return "false"
case None:
return "null"
case _:
return str(value)

@field_validator("key", mode="before")
@classmethod
Expand Down
46 changes: 43 additions & 3 deletions src/hatch_gradle_version/plugins/version_source/test_json.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import textwrap
from pathlib import Path
from textwrap import dedent

Expand Down Expand Up @@ -30,17 +31,26 @@ def test_json_version(tmp_path: Path, monkeypatch: MonkeyPatch):
"curseforge": {
"id": "739791"
}
}
},
"foo": [
"bar",
"baz",
{
"qux": "quux"
}
]
}
"""
)
)

(tmp_path / "__version__.py").write_text('PY_VERSION = "1.0"')

hook = JSONVersionSource.from_config(
tmp_path.as_posix(),
{
"source": "json",
"py-path": "",
"py-path": "__version__.py",
"json-path": "mod.json",
"key": "core.version",
},
Expand All @@ -53,5 +63,35 @@ def test_json_version(tmp_path: Path, monkeypatch: MonkeyPatch):
version="0.1.4",
rc=None,
build="1.19.2",
extra_versions={},
extra_versions={
"core.name": "hexbound",
"core.version": "0.1.4+1.19.2",
"core.repository": "https://github.com/Cypher121/hexbound/",
"platforms.modrinth.id": "PHgo4bVw",
"platforms.curseforge.id": "739791",
"foo.0": "bar",
"foo.1": "baz",
"foo.2.qux": "quux",
},
)

hook.get_version_data()

generated_version_file = (tmp_path / "__gradle_version__.py").read_text("utf-8")
assert generated_version_file == textwrap.dedent(
"""\
# This file is auto-generated by hatch-gradle-version. Do not edit.
GRADLE_VERSION = "0.1.4+1.19.2"
FULL_VERSION = "0.1.4.1.0"
CORE_NAME = "hexbound"
CORE_REPOSITORY = "https://github.com/Cypher121/hexbound/"
CORE_VERSION = "0.1.4+1.19.2"
FOO_0 = "bar"
FOO_1 = "baz"
FOO_2_QUX = "quux"
PLATFORMS_CURSEFORGE_ID = "739791"
PLATFORMS_MODRINTH_ID = "PHgo4bVw"
"""
)

0 comments on commit 2f57a09

Please sign in to comment.