From e204caeb33e295fe5a8693f0c3ebd10b1e4b2dad Mon Sep 17 00:00:00 2001 From: Valentin Berlier Date: Tue, 31 Oct 2023 02:47:04 +0100 Subject: [PATCH] feat: add compile together --- mecha/api.py | 43 +++++++++++++++++++++++++++++++++++-------- mecha/plugin.py | 2 +- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/mecha/api.py b/mecha/api.py index 663b48b9..2d07d835 100644 --- a/mecha/api.py +++ b/mecha/api.py @@ -19,6 +19,7 @@ from typing import ( Any, Dict, + Iterable, Iterator, List, Literal, @@ -395,6 +396,20 @@ def compile( ) -> PackType: ... + @overload + def compile( + self, + *, + together: Iterable[Union[ResourcePack, DataPack]], + match: Optional[List[str]] = None, + multiline: Optional[bool] = None, + formatting: Optional[JsonDict] = None, + readonly: Optional[bool] = None, + initial_step: int = 0, + report: Optional[DiagnosticCollection] = None, + ) -> None: + ... + @overload def compile( self, @@ -429,10 +444,17 @@ def compile( def compile( self, - source: Union[ - Union[ResourcePack, DataPack], TextFileBase[Any], List[str], str, AstRoot - ], + source: Optional[ + Union[ + Union[ResourcePack, DataPack], + TextFileBase[Any], + List[str], + str, + AstRoot, + ] + ] = None, *, + together: Optional[Iterable[Union[ResourcePack, DataPack]]] = None, match: Optional[List[str]] = None, filename: Optional[FileSystemPath] = None, resource_location: Optional[str] = None, @@ -442,7 +464,7 @@ def compile( readonly: Optional[bool] = None, initial_step: int = 0, report: Optional[DiagnosticCollection] = None, - ) -> Union[Union[ResourcePack, DataPack], TextFileBase[Any]]: + ) -> Optional[Union[Union[ResourcePack, DataPack], TextFileBase[Any]]]: """Apply all compilation steps.""" self.database.setup_compilation() @@ -451,16 +473,21 @@ def compile( if readonly is None: readonly = self.readonly + result = None + if isinstance(source, (ResourcePack, DataPack)): result = source + together = [source] + if together is not None: if match is None: match = self.match - for provider in self.providers: - for file_instance, compilation_unit in provider(source, match): - self.database[file_instance] = compilation_unit - self.database.enqueue(file_instance) + for pack in together: + for provider in self.providers: + for file_instance, compilation_unit in provider(pack, match): + self.database[file_instance] = compilation_unit + self.database.enqueue(file_instance) else: if isinstance(source, (list, str)): source = Function(source) diff --git a/mecha/plugin.py b/mecha/plugin.py index 72e17c4f..a1f2cbe5 100644 --- a/mecha/plugin.py +++ b/mecha/plugin.py @@ -11,4 +11,4 @@ def beet_default(ctx: Context): mc = ctx.inject(Mecha) - mc.compile(ctx.data, report=mc.diagnostics) + mc.compile(together=ctx.packs, report=mc.diagnostics)