-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extract autosave and linking strategy into their own plugins
- Loading branch information
Showing
6 changed files
with
178 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Service for saving the data pack and resource pack at the end of the build.""" | ||
|
||
|
||
__all__ = [ | ||
"Autosave", | ||
"AutosaveOptions", | ||
] | ||
|
||
|
||
from dataclasses import dataclass, field | ||
from typing import List | ||
|
||
from pydantic import BaseModel | ||
|
||
from beet import Context, PluginSpec | ||
|
||
|
||
class AutosaveOptions(BaseModel): | ||
link: bool = False | ||
output_handlers: List[str] = [] | ||
link_handlers: List[str] = [] | ||
|
||
|
||
@dataclass | ||
class Autosave: | ||
"""Service for saving the data pack and resource pack at the end of the build.""" | ||
|
||
ctx: Context | ||
link: bool = False | ||
output_handlers: List[PluginSpec] = field(default_factory=list) | ||
link_handlers: List[PluginSpec] = field(default_factory=list) | ||
|
||
def __post_init__(self): | ||
opts = self.ctx.validate("autosave", AutosaveOptions) | ||
self.link = opts.link | ||
self.output_handlers = list(opts.output_handlers) | ||
self.link_handlers = list(opts.link_handlers) | ||
self.ctx.require(self.finalize) | ||
|
||
def add_output(self, *specs: PluginSpec): | ||
"""Register output handler.""" | ||
self.output_handlers.extend(specs) | ||
|
||
def add_link(self, *specs: PluginSpec): | ||
"""Register link handler.""" | ||
self.link_handlers.extend(specs) | ||
|
||
def finalize(self, ctx: Context): | ||
yield | ||
ctx.require(*self.output_handlers) | ||
if self.link: | ||
ctx.require(*self.link_handlers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""Plugin for linking the generated resource pack and data pack to Minecraft.""" | ||
|
||
|
||
__all__ = [ | ||
"LinkOptions", | ||
"link", | ||
] | ||
|
||
|
||
import logging | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
from beet import Context, configurable | ||
from beet.core.utils import FileSystemPath, log_time, remove_path | ||
|
||
logger = logging.getLogger("link") | ||
|
||
|
||
class LinkOptions(BaseModel): | ||
resource_pack: Optional[FileSystemPath] = None | ||
data_pack: Optional[FileSystemPath] = None | ||
|
||
|
||
def beet_default(ctx: Context): | ||
ctx.require(link) | ||
|
||
|
||
@configurable(validator=LinkOptions) | ||
def link(ctx: Context, opts: LinkOptions): | ||
"""Plugin for linking the generated resource pack and data pack to Minecraft.""" | ||
dirty = ctx.cache["link"].json.setdefault("dirty", []) | ||
remove_path(*dirty) | ||
dirty.clear() | ||
|
||
to_link = [ | ||
(ctx.directory / directory, pack) | ||
for directory, pack in zip([opts.resource_pack, opts.data_pack], ctx.packs) | ||
if directory and pack | ||
] | ||
|
||
if to_link: | ||
with log_time("Link project."): | ||
for directory, pack in to_link: | ||
try: | ||
dirty.append(str(pack.save(directory))) | ||
except FileExistsError as exc: | ||
logger.warning(str(exc)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Plugin that outputs the data pack and the resource pack in a local directory.""" | ||
|
||
|
||
__all__ = [ | ||
"OutputOptions", | ||
"output", | ||
] | ||
|
||
|
||
from typing import List, Optional, Union | ||
|
||
from pydantic import BaseModel | ||
|
||
from beet import Context, configurable | ||
from beet.core.utils import FileSystemPath, log_time | ||
|
||
|
||
class OutputOptions(BaseModel): | ||
directory: Optional[Union[FileSystemPath, List[FileSystemPath]]] = None | ||
|
||
|
||
def beet_default(ctx: Context): | ||
ctx.require(output) | ||
|
||
|
||
@configurable(validator=OutputOptions) | ||
def output(ctx: Context, opts: OutputOptions): | ||
"""Plugin that outputs the data pack and the resource pack in a local directory.""" | ||
if not opts.directory: | ||
return | ||
|
||
paths = opts.directory if isinstance(opts.directory, list) else [opts.directory] | ||
paths = [ctx.directory / path for path in paths] | ||
|
||
packs = list(filter(None, ctx.packs)) | ||
|
||
if paths and packs: | ||
with log_time("Output files."): | ||
for pack in packs: | ||
for path in paths: | ||
pack.save(path, overwrite=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters