Skip to content

Commit

Permalink
Added ability to pass initial contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
coordt committed Feb 13, 2023
1 parent f2725ef commit 9e3cdf3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
5 changes: 4 additions & 1 deletion cookie_composer/commands/add.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""The implementation of the add command."""
from typing import Optional
from typing import Any, Dict, Optional

import logging
from pathlib import Path
Expand Down Expand Up @@ -31,6 +31,7 @@ def add_cmd(
overwrite_if_exists: bool = False,
skip_if_file_exists: bool = False,
default_config: bool = False,
initial_context: Optional[Dict[str, Any]] = None,
):
"""
Add a template or configuration to an existing project.
Expand All @@ -44,6 +45,7 @@ def add_cmd(
overwrite_if_exists: Overwrite the contents of the output directory if it already exists
skip_if_file_exists: Skip the files in the corresponding directories if they already exist
default_config: Do not load a config file. Use the defaults instead
initial_context: The initial context for the composition layer
Raises:
GitError: If the destination_dir is not a git repository
Expand All @@ -69,6 +71,7 @@ def add_cmd(
output_dir,
overwrite_if_exists,
skip_if_file_exists,
initial_context=initial_context or {},
)

# Get the merged context for all layers
Expand Down
7 changes: 5 additions & 2 deletions cookie_composer/commands/create.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Methods for generating projects."""

from typing import Optional
from typing import Any, Dict, Optional

import logging
from pathlib import Path
Expand All @@ -24,6 +24,7 @@ def create_cmd(
overwrite_if_exists: bool = False,
skip_if_file_exists: bool = False,
default_config: bool = False,
initial_context: Optional[Dict[str, Any]] = None,
) -> Path:
"""
Generate a new project from a composition file, local template or remote template.
Expand All @@ -37,6 +38,7 @@ def create_cmd(
overwrite_if_exists: Overwrite the contents of the output directory if it already exists
skip_if_file_exists: Skip the files in the corresponding directories if they already exist
default_config: Do not load a config file. Use the defaults instead
initial_context: The initial context for the composition
Returns:
The path to the generated project.
Expand All @@ -51,8 +53,9 @@ def create_cmd(
output_dir,
overwrite_if_exists,
skip_if_file_exists,
initial_context or {},
)
rendered_layers = render_layers(composition.layers, output_dir, no_input=no_input)
rendered_layers = render_layers(composition.layers, output_dir, no_input=no_input, accept_hooks=False)
rendered_composition = RenderedComposition(
layers=rendered_layers,
render_dir=output_dir,
Expand Down
5 changes: 4 additions & 1 deletion cookie_composer/commands/link.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""The implementation of the link command."""
from typing import Optional
from typing import Any, Dict, Optional

from pathlib import Path

Expand All @@ -16,6 +16,7 @@ def link_cmd(
overwrite_if_exists: bool = False,
skip_if_file_exists: bool = False,
default_config: bool = False,
initial_context: Optional[Dict[str, Any]] = None,
):
"""
Link a template or configuration to an existing project.
Expand All @@ -29,6 +30,7 @@ def link_cmd(
overwrite_if_exists: Overwrite the contents of the output directory if it already exists
skip_if_file_exists: Skip the files in the corresponding directories if they already exist
default_config: Do not load a config file. Use the defaults instead
initial_context: The initial context for the composition
Raises:
GitError: If the destination_dir is not a git repository
Expand Down Expand Up @@ -60,6 +62,7 @@ def link_cmd(
overwrite_if_exists,
skip_if_file_exists,
default_config,
initial_context=initial_context or {},
)

changed_files = [item.a_path for item in repo.index.diff(None)]
Expand Down
5 changes: 4 additions & 1 deletion cookie_composer/composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def layer_names(self) -> List[str]:

def is_composition_file(path_or_url: Union[str, Path]) -> bool:
"""
Is the filename a composition file?
Return ``True`` if the filename a composition file.
Args:
path_or_url: The path or URL to check
Expand Down Expand Up @@ -329,6 +329,7 @@ def get_composition_from_path_or_url(
output_dir: Optional[Path] = None,
overwrite_if_exists: bool = False,
skip_if_file_exists: bool = False,
initial_context: Optional[Dict[str, Any]] = None,
) -> Composition:
"""
Generate a :class:`Composition` from a path or URL.
Expand All @@ -342,6 +343,7 @@ def get_composition_from_path_or_url(
output_dir: Where to generate the project
overwrite_if_exists: Overwrite the contents of the output directory if it already exists
skip_if_file_exists: Skip the files in the corresponding directories if they already exist
initial_context: The initial context for the composition
Returns:
The composition object.
Expand All @@ -358,6 +360,7 @@ def get_composition_from_path_or_url(
no_input=no_input or default_config,
skip_if_file_exists=skip_if_file_exists,
overwrite=overwrite_rules,
context=initial_context or {},
)
composition = Composition(layers=[tmpl])
logger.info(f"Rendering template {path_or_url} to {output_dir}.")
Expand Down

0 comments on commit 9e3cdf3

Please sign in to comment.