Skip to content

Commit

Permalink
feat: add compile together
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Oct 31, 2023
1 parent 09a3d7d commit e204cae
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
43 changes: 35 additions & 8 deletions mecha/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from typing import (
Any,
Dict,
Iterable,
Iterator,
List,
Literal,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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()

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion mecha/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit e204cae

Please sign in to comment.