Skip to content

Commit

Permalink
Fixed rendering layers from previous commits.
Browse files Browse the repository at this point in the history
  • Loading branch information
coordt committed Oct 15, 2023
1 parent 60a2393 commit 0fefdbc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
18 changes: 12 additions & 6 deletions cookie_composer/commands/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def update_cmd(project_dir: Optional[Path] = None, no_input: bool = False) -> No
latest_template_sha = rendered_layer.layer.template.repo.latest_sha
if latest_template_sha is None or latest_template_sha != rendered_layer.rendered_commit:
requires_updating = True
new_layer = rendered_layer.layer.model_copy(deep=True, update={"commit": latest_template_sha})
new_layer = rendered_layer.layer.model_copy(deep=True, update={"_commit": latest_template_sha})
update_layers.append(new_layer)

if not requires_updating:
Expand All @@ -57,10 +57,14 @@ def update_cmd(project_dir: Optional[Path] = None, no_input: bool = False) -> No
with TemporaryDirectory() as tempdir:
current_state_dir = Path(tempdir) / "current_state"
current_state_dir.mkdir(exist_ok=True)
updated_state_dir = Path(tempdir) / "update_state"
updated_state_dir.mkdir(exist_ok=True)

current_layers = [layer.layer for layer in proj_composition.layers]
current_layers = []
for layer in proj_composition.layers:
layer_config = layer.layer
layer_config._commit = layer.rendered_commit
layer_config.initial_context = layer.rendered_context
current_layers.append(layer_config)

current_rendered_layers = render_layers(
current_layers,
current_state_dir,
Expand All @@ -74,6 +78,8 @@ def update_cmd(project_dir: Optional[Path] = None, no_input: bool = False) -> No
deleted_paths = get_deleted_files(current_state_dir, project_dir.parent)
deleted_paths.add(Path(".git")) # don't want the .git dir, if it exists

updated_state_dir = Path(tempdir) / "update_state"
updated_state_dir.mkdir(exist_ok=True)
updated_rendered_layers = render_layers(
update_layers,
updated_state_dir,
Expand Down Expand Up @@ -112,14 +118,14 @@ def update_rendered_composition_layers(
"""
Update ``base.layers`` with ``updated_layers`` where layer names match.
If for some reason a layer exists in ``updated_layers`` but not in ``base``, it is discarded.
If, for some reason, a layer exists in ``updated_layers`` but not in ``base``, it is discarded.
Args:
base: The base composition whose layers are to be updated
updated_layers: The new rendered layers
Raises:
RuntimeError: If a layer's location ``render_dir`` properties don't match
RuntimeError: If a layer's location ``render_dir`` properties don't match
RuntimeError: If the compositions' ``rendered_name`` properties don't match
Returns:
Expand Down
2 changes: 1 addition & 1 deletion cookie_composer/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

def serialize_layer(layer: LayerConfig) -> dict:
"""Serialize the layer configuration for outputting in a rendered layer."""
layer_info = layer.model_dump(exclude={"template"})
layer_info = layer.model_dump(exclude={"template", "_commit"})
layer_info.update(
{
"template": layer.template.repo.source,
Expand Down
32 changes: 23 additions & 9 deletions cookie_composer/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class LayerConfig(BaseModel):
merge_strategies: Dict[str, str] = Field(default_factory=lambda: {"*": DO_NOT_MERGE})
"""The method to merge specific paths or glob patterns."""

_commit: Optional[str] = None
"""Used when updating layers."""

@property
def layer_name(self) -> str:
"""The name of the template layer."""
Expand Down Expand Up @@ -228,7 +231,11 @@ def get_template_rendered_name(template: Template, context: MutableMapping) -> s


def render_layer(
layer_config: LayerConfig, render_dir: Path, full_context: Optional[Context] = None, accept_hooks: str = "yes"
layer_config: LayerConfig,
render_dir: Path,
full_context: Optional[Context] = None,
commit: Optional[str] = None,
accept_hooks: str = "yes",
) -> RenderedLayer:
"""
Process one layer of the template composition.
Expand All @@ -239,6 +246,7 @@ def render_layer(
layer_config: The configuration of the layer to render
render_dir: Where to render the template
full_context: The extra context from all layers in the composition
commit: The commit to checkout if the template is a git repo
accept_hooks: Accept pre- and post-hooks if set to ``True``
Returns:
Expand Down Expand Up @@ -270,18 +278,22 @@ def render_layer(
rendered_name = get_template_rendered_name(layer_config.template, cookiecutter_context)

# call cookiecutter's generate files function
generate_files(
repo_dir=repo_dir,
context=cookiecutter_context,
overwrite_if_exists=False,
output_dir=str(render_dir),
accept_hooks=_accept_hooks,
)
with layer_config.template.repo.render_source(commit=commit) as repo_dir:
if layer_config.template.directory:
repo_dir = repo_dir / layer_config.template.directory # NOQA: PLW2901
generate_files(
repo_dir=repo_dir,
context=cookiecutter_context,
overwrite_if_exists=False,
output_dir=str(render_dir),
accept_hooks=_accept_hooks,
)

rendered_layer = RenderedLayer(
layer=layer_config,
location=render_dir,
rendered_context=copy.deepcopy(layer_context),
rendered_commit=commit,
rendered_name=rendered_name,
)

Expand Down Expand Up @@ -356,7 +368,9 @@ def render_layers(
for layer_config, accept_hook in zip(layers, accept_hooks_layers):
layer_config.no_input = True if no_input else layer_config.no_input
with tempfile.TemporaryDirectory() as render_dir:
rendered_layer = render_layer(layer_config, Path(render_dir), full_context, accept_hook)
rendered_layer = render_layer(
layer_config, Path(render_dir), full_context, layer_config._commit, accept_hook
)
merge_layers(destination, rendered_layer)
full_context.update(rendered_layer.rendered_context)
rendered_layer.location = destination
Expand Down

0 comments on commit 0fefdbc

Please sign in to comment.