Skip to content

Commit

Permalink
Warn instead of crashing when hexdoc_minecraft_version is not impleme…
Browse files Browse the repository at this point in the history
…nted
  • Loading branch information
object-Object committed Nov 7, 2023
1 parent 89574aa commit dc82819
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@ nox --no-install # after the first Nox run, use this to skip reinstalling every
# update test snapshots
nox -- --snapshot-update
# run hexdoc commands in an isolated environment to ensure it works on its own
nox -s hexdoc -- export
nox -s hexdoc -- repl
```
8 changes: 8 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import nox

nox.options.sessions = ["tests"]


@nox.session(reuse_venv=True)
def hexdoc(session: nox.Session):
session.install(".")
session.run("hexdoc", *session.posargs)


@nox.session(reuse_venv=True)
def tests(session: nox.Session):
Expand Down
7 changes: 6 additions & 1 deletion src/hexdoc/_cli/utils/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ def load_common_data(props_file: Path, verbosity: int):
pm = PluginManager()

version = load_version(props, pm)
MinecraftVersion.MINECRAFT_VERSION = pm.minecraft_version()
minecraft_version = MinecraftVersion.MINECRAFT_VERSION = pm.minecraft_version()
if minecraft_version is None:
logging.getLogger(__name__).warning(
"No plugins implement hexdoc_minecraft_version, "
"per-version validation will fail"
)

return props, pm, version

Expand Down
7 changes: 0 additions & 7 deletions src/hexdoc/_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
HookReturn,
LoadJinjaTemplatesImpl,
LoadResourceDirsImpl,
MinecraftVersionImpl,
ModVersionImpl,
hookimpl,
)


class HexdocPlugin(
MinecraftVersionImpl,
ModVersionImpl,
LoadResourceDirsImpl,
LoadJinjaTemplatesImpl,
Expand All @@ -25,11 +23,6 @@ class HexdocPlugin(
def hexdoc_mod_version():
return "(TODO: remove)"

@staticmethod
@hookimpl
def hexdoc_minecraft_version() -> str:
return "1.20.1" # TODO: remove

@staticmethod
@hookimpl
def hexdoc_load_resource_dirs() -> HookReturn[Package]:
Expand Down
11 changes: 8 additions & 3 deletions src/hexdoc/core/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,24 @@ def matches(cls, specifier: str | SpecifierSet) -> bool:


class MinecraftVersion(VersionSource):
MINECRAFT_VERSION: ClassVar[str]
MINECRAFT_VERSION: ClassVar[str | None] = None

@override
@classmethod
def get(cls):
def get(cls) -> str:
if cls.MINECRAFT_VERSION is None:
raise RuntimeError(
"Tried to call MinecraftVersion.get() "
"before initializing MinecraftVersion.MINECRAFT_VERSION"
)
return cls.MINECRAFT_VERSION

@override
@classmethod
def matches(cls, specifier: str | SpecifierSet) -> bool:
if isinstance(specifier, str):
specifier = SpecifierSet(specifier)
return cls.MINECRAFT_VERSION in specifier
return cls.get() in specifier


@dataclass(frozen=True)
Expand Down
4 changes: 2 additions & 2 deletions src/hexdoc/plugin/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def __init__(self) -> None:
def mod_version(self, modid: str):
return self._hook_caller(PluginSpec.hexdoc_mod_version, modid)()

def minecraft_version(self) -> str:
def minecraft_version(self) -> str | None:
versions = dict[str, str]()

for modid, caller in self._all_callers(PluginSpec.hexdoc_minecraft_version):
Expand All @@ -113,7 +113,7 @@ def minecraft_version(self) -> str:

match len(set(versions.values())):
case 0:
raise ValueError("No plugins implement hexdoc_minecraft_version")
return None
case 1:
return versions.popitem()[1]
case n:
Expand Down
2 changes: 1 addition & 1 deletion test/_submodules/HexMod

0 comments on commit dc82819

Please sign in to comment.