utils/dep_freshness/parsers/gradle.py
Krzysztof kuhy Rudnicki a4ea305164 dep_freshness: Gradle ecosystem -- version catalogs, wrapper, Maven registries
The gate had no way to read a Gradle build, which left the two Kotlin
forks (tachiyomisy, syncyomi-src) outside the fleet-wide rule.

- parsers/gradle.py reads gradle/libs.versions.toml ([libraries] and
  [plugins] in every shape: inline string, module + version.ref,
  group/name/version, rich {strictly|require|prefer} tables) into
  `maven:<group>:<artifact>` deps, pointing the fix at the [versions]
  line when a ref is used; BOM-managed entries without a version are
  skipped. gradle-wrapper.properties becomes `toolchain:gradle`.
- registries/maven.py asks Google Maven, Central and the plugin portal
  in that order, reading the full <versions> list -- AndroidX writes
  alphas into <latest>. Plugins resolve through their marker artifact.
- registries/toolchain.gradle_latest reads services.gradle.org.
- http.get_text shares the transport with get_json (XML bodies).
- Templates: the pre-commit `files:` and the CI cache key know the
  catalog. 100% branch on every new file; fixtures only.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Yc4TiPjhbQLfH35QBPfrbP
2026-09-12 19:10:42 +02:00

140 lines
4.9 KiB
Python

"""Gradle: `gradle/libs.versions.toml` -> Maven coordinates, and the wrapper.
The version catalog is the one place a Gradle build should declare versions,
which makes it a manifest the gate can read without evaluating Kotlin DSL.
Three shapes appear in `[libraries]` and `[plugins]`:
foo = "group:artifact:1.2.3" # inline string
foo = { module = "group:artifact", version.ref = "x" }
foo = { group = "g", name = "a", version = "1.2.3" }
plug = { id = "com.example.plugin", version.ref = "x" }
A library with no version is managed by a BOM (`junit-platform-launcher`
in TachiyomiSY) and is skipped: the BOM's own entry is the pin that counts.
A rich version table (`{ strictly = "1.2" }`) is read as the constraint it
spells and lands as unpinned when it is a range, like every other ecosystem.
The line reported is the `[versions]` entry when a `version.ref` is used,
because that is where the fix goes; several libraries sharing one ref each
report the same line, which is one finding per stale artifact, not noise.
`gradle-wrapper.properties` names the Gradle distribution; that is the
toolchain pin, judged against services.gradle.org like Flutter against its
release feed.
"""
from __future__ import annotations
import re
from pathlib import Path
from typing import Any
import tomllib
from dep_freshness._tables import MAVEN, TOOLCHAIN
from dep_freshness.models import Dep
from dep_freshness.parsers._lines import index
from dep_freshness.versions import exact_pin
CATALOG_NAME = "libs.versions.toml"
WRAPPER_NAME = "gradle-wrapper.properties"
_DISTRIBUTION = re.compile(
r"gradle-(?P<version>\d+(?:\.\d+)*(?:-[0-9A-Za-z.-]+?)?)-(?:bin|all)\.zip"
)
def _rich(value: Any) -> str:
"""The version string a catalog value spells, for any of its shapes."""
if isinstance(value, dict):
for key in ("strictly", "require", "prefer"):
if value.get(key):
return str(value[key])
return ""
return str(value or "")
def _coordinates(entry: Any, plugin: bool) -> tuple[str, str] | None:
"""`(name, inline_version)` for a library or plugin entry, or None."""
if isinstance(entry, str):
parts = entry.split(":")
if plugin:
return (parts[0], parts[1] if len(parts) > 1 else "")
if len(parts) < 2:
return None
return (f"{parts[0]}:{parts[1]}", parts[2] if len(parts) > 2 else "")
if not isinstance(entry, dict):
return None
if plugin:
name = str(entry.get("id") or "")
elif entry.get("module"):
name = str(entry["module"])
else:
name = f"{entry.get('group', '')}:{entry.get('name', '')}"
if not name.strip(":"):
return None
version = entry.get("version")
if isinstance(version, dict) and version.get("ref"):
return (name, f"ref:{version['ref']}")
return (name, _rich(version))
def parse_catalog(path: Path) -> list[Dep]:
"""Every versioned library and plugin in a Gradle version catalog."""
try:
data = tomllib.loads(path.read_text(encoding="utf-8"))
except (OSError, tomllib.TOMLDecodeError):
return []
versions = {k: _rich(v) for k, v in (data.get("versions") or {}).items()}
lines = index(path)
deps: list[Dep] = []
for section, plugin in (("libraries", False), ("plugins", True)):
for alias, entry in (data.get(section) or {}).items():
coords = _coordinates(entry, plugin)
if coords is None:
continue
name, version = coords
line = lines.get(str(alias), 0)
if version.startswith("ref:"):
ref = version[4:]
version = versions.get(ref, "")
line = lines.get(ref, line)
if not version:
continue # BOM-managed, or a dangling ref the build itself rejects
if plugin:
name = f"{name}:{name}.gradle.plugin"
deps.append(
Dep(
ecosystem=MAVEN,
name=name,
constraint=version,
path=path,
line=line,
pinned=exact_pin(version),
)
)
return deps
def parse_wrapper(path: Path) -> list[Dep]:
"""The Gradle distribution the wrapper downloads, as a toolchain pin."""
try:
text = path.read_text(encoding="utf-8")
except OSError:
return []
for number, line in enumerate(text.splitlines(), start=1):
if not line.startswith("distributionUrl="):
continue
match = _DISTRIBUTION.search(line)
version = match.group("version") if match else ""
return [
Dep(
ecosystem=TOOLCHAIN,
name="gradle",
constraint=version,
path=path,
line=number,
pinned=exact_pin(version),
)
]
return []