Skip to content

Commit

Permalink
feat: add ctx.cache.generated
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Jun 10, 2021
1 parent ecea21b commit 6b9d6b5
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 7 deletions.
30 changes: 28 additions & 2 deletions beet/toolchain/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"Pipeline",
"Plugin",
"PluginSpec",
"ProjectCache",
"Context",
"ContextContainer",
]
Expand All @@ -16,7 +17,7 @@

from beet.core.cache import MultiCache
from beet.core.container import Container
from beet.core.utils import JsonDict, TextComponent, extra_field
from beet.core.utils import FileSystemPath, JsonDict, TextComponent, extra_field
from beet.library.data_pack import DataPack
from beet.library.resource_pack import ResourcePack

Expand Down Expand Up @@ -49,6 +50,31 @@ def missing(self, key: Callable[["Context"], Any]) -> Any:
return key(self.ctx)


class ProjectCache(MultiCache):
"""The project cache.
The `generated` attribute is a MultiCache instance that's
meant to be tracked by version control, unlike the main project
cache that usually lives in the ignored `.beet_cache` directory.
"""

generated: MultiCache

def __init__(
self,
directory: FileSystemPath,
generated_directory: FileSystemPath,
default_cache: str = "default",
gitignore: bool = True,
):
super().__init__(directory, default_cache, gitignore)
self.generated = MultiCache(generated_directory, default_cache, gitignore=False)

def flush(self):
super().flush()
self.generated.flush()


@dataclass
class Context:
"""The build context."""
Expand All @@ -61,7 +87,7 @@ class Context:
directory: Path
output_directory: Optional[Path]
meta: JsonDict
cache: MultiCache
cache: ProjectCache
worker: WorkerPoolHandle
template: TemplateManager

Expand Down
11 changes: 6 additions & 5 deletions beet/toolchain/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
from typing import ClassVar, Iterable, Iterator, List, Optional, Sequence

from beet.contrib.render import render
from beet.core.cache import MultiCache
from beet.core.utils import FileSystemPath, intersperse, normalize_string
from beet.core.watch import DirectoryWatcher, FileChanges

from .config import PackConfig, ProjectConfig, load_config, locate_config
from .context import Context
from .context import Context, ProjectCache
from .pipeline import FormattedPipelineException
from .template import TemplateManager
from .utils import locate_minecraft
Expand All @@ -41,7 +40,7 @@ class Project:
config_path: Optional[FileSystemPath] = None
config_detect: Iterable[str] = ("beet.json", "beet.toml", "beet.yml", "beet.yaml")

resolved_cache: Optional[MultiCache] = None
resolved_cache: Optional[ProjectCache] = None
cache_name: str = ".beet_cache"

resolved_worker_pool: Optional[WorkerPool] = None
Expand Down Expand Up @@ -84,10 +83,12 @@ def template_directories(self) -> List[FileSystemPath]:
]

@property
def cache(self) -> MultiCache:
def cache(self) -> ProjectCache:
if self.resolved_cache is not None:
return self.resolved_cache
self.resolved_cache = MultiCache(self.directory / self.cache_name)
self.resolved_cache = ProjectCache(
self.directory / self.cache_name, self.directory / "generated"
)
return self.resolved_cache

@property
Expand Down
3 changes: 3 additions & 0 deletions examples/code_generated/beet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"pipeline": ["demo"]
}
10 changes: 10 additions & 0 deletions examples/code_generated/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from beet import Context, Function


def beet_default(ctx: Context):
function_path = ctx.cache.generated.directory / "foo.mcfunction"

if not function_path.is_file():
function_path.write_text("say hello\n")

ctx.data["demo:foo"] = Function(source_path=function_path)
1 change: 1 addition & 0 deletions examples/code_generated/generated/default/foo.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say hello
5 changes: 5 additions & 0 deletions examples/code_generated/generated/default/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"timestamp": "2021-06-10T05:36:51.690885",
"expire": null,
"json": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 6,
"description": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 6,
"description": ""
}
}

0 comments on commit 6b9d6b5

Please sign in to comment.