Skip to content

Commit

Permalink
feat: add codegen cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Dec 6, 2023
1 parent e0c2f11 commit 4e7db00
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
18 changes: 17 additions & 1 deletion bolt/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,31 @@
from typing import Iterable, Optional, Tuple

import click
from beet import Project
from beet import ErrorMessage, Project
from beet.core.utils import format_directory
from beet.toolchain.cli import beet, message_fence
from mecha import AstCacheBackend

from bolt import AstMemo, MemoRegistry, MemoStorage

pass_project = click.make_pass_decorator(Project) # type: ignore


@beet.command()
@pass_project
@click.argument("filename", type=click.Path(exists=True, dir_okay=False))
def codegen(project: Project, filename: str):
"""Inspect cached bolt codegen."""
source_path = Path(filename).resolve()
ast_path = project.cache["mecha"].get_path(f"{source_path}-ast")

try:
with ast_path.open("rb") as f:
click.echo(AstCacheBackend().load_data(f)["codegen"], nl=False)
except Exception as exc:
raise ErrorMessage(f'No cached codegen for "{filename}".') from exc


@beet.command()
@pass_project
@click.option(
Expand Down
4 changes: 4 additions & 0 deletions bolt/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class CompiledModule:

ast: AstRoot
code: Optional[CodeType]
python: Optional[str]
refs: List[Any]
dependencies: Set[str]
prelude_imports: List[AstPrelude]
Expand Down Expand Up @@ -268,6 +269,7 @@ def __getitem__(self, current: Union[TextFileBase[Any], str]) -> CompiledModule:
module = CompiledModule(
ast=compilation_unit.ast,
code=code,
python=result.source,
refs=result.refs,
dependencies=result.dependencies,
prelude_imports=result.prelude_imports,
Expand Down Expand Up @@ -470,6 +472,7 @@ def load(self, f: BufferedReader) -> AstRoot:
self.modules.registry[self.modules.database.current] = CompiledModule(
ast=data["ast"],
code=marshal.load(f),
python=data["codegen"],
refs=data["refs"],
dependencies=data["dependencies"],
prelude_imports=data["prelude_imports"],
Expand All @@ -491,6 +494,7 @@ def dump(self, node: AstRoot, f: BufferedWriter):
self.dump_data(
{
"ast": module.ast,
"codegen": module.python,
"refs": module.refs,
"dependencies": module.dependencies,
"prelude_imports": module.prelude_imports,
Expand Down

0 comments on commit 4e7db00

Please sign in to comment.