Skip to content

Commit

Permalink
Added subset of cookiecutter options to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
coordt committed Sep 6, 2022
1 parent a4c9652 commit a66f17a
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 9 deletions.
104 changes: 99 additions & 5 deletions cookie_composer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,120 @@ def cli():

@cli.command()
@click_log.simple_verbosity_option(logger)
@click.option(
"--no-input",
is_flag=True,
help="Do not prompt for parameters and only use cookiecutter.json file content. "
"Defaults to deleting any cached resources and redownloading them.",
)
@click.option(
"-c",
"--checkout",
help="branch, tag or commit to checkout after git clone",
)
@click.option(
"--directory",
help="Directory within repo that holds cookiecutter.json file "
"for advanced repositories with multi templates in it",
)
@click.option(
"-f",
"--overwrite-if-exists",
is_flag=True,
help="Overwrite the contents of the output directory if it already exists",
)
@click.option(
"-s",
"--skip-if-file-exists",
is_flag=True,
help="Skip the files in the corresponding directories if they already exist",
default=False,
)
@click.option(
"--default-config",
is_flag=True,
help="Do not load a config file. Use the defaults instead",
)
@click.argument("path_or_url", type=str, required=True)
@click.argument(
"output_dir",
required=False,
default=lambda: Path.cwd(),
type=click.Path(exists=True, dir_okay=True, file_okay=False, resolve_path=True),
)
def create(path_or_url: str, output_dir: Optional[Path]):
def create(
no_input: bool,
checkout: str,
directory: str,
overwrite_if_exists: bool,
skip_if_file_exists: bool,
default_config: bool,
path_or_url: str,
output_dir: Optional[Path],
):
"""Create a project from a template or configuration."""
create_cmd(path_or_url, output_dir)
create_cmd(
path_or_url,
output_dir,
no_input,
checkout,
directory,
overwrite_if_exists,
skip_if_file_exists,
default_config,
)


@cli.command()
@click_log.simple_verbosity_option(logger)
@click.option("--no-input", is_flag=True)
@click.argument("path_or_url", type=str, required=True)
@click.option(
"--no-input",
is_flag=True,
help="Do not prompt for parameters and only use cookiecutter.json file content. "
"Defaults to deleting any cached resources and redownloading them.",
)
@click.option(
"-c",
"--checkout",
help="branch, tag or commit to checkout after git clone",
)
@click.option(
"--directory",
help="Directory within repo that holds cookiecutter.json file "
"for advanced repositories with multi templates in it",
)
@click.option(
"-f",
"--overwrite-if-exists",
is_flag=True,
help="Overwrite the contents of the output directory if it already exists",
)
@click.option(
"-s",
"--skip-if-file-exists",
is_flag=True,
help="Skip the files in the corresponding directories if they already exist",
default=False,
)
@click.option(
"--default-config",
is_flag=True,
help="Do not load a config file. Use the defaults instead",
)
@click.argument("path_or_url", type=str, required=True, help="The template or composition path or URL")
@click.argument(
"destination", required=False, type=click.Path(exists=True, file_okay=False, writable=True, path_type=Path)
)
def add(no_input: bool, path_or_url: str, destination: Optional[Path]):
def add(
no_input: bool,
checkout: str,
directory: str,
overwrite_if_exists: bool,
skip_if_file_exists: bool,
default_config: bool,
path_or_url: str,
destination: Optional[Path],
):
"""Add a template or configuration to an existing project."""
destination = destination or Path(".")
try:
Expand Down
20 changes: 19 additions & 1 deletion cookie_composer/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def add_cmd(
path_or_url: str,
destination_dir: Optional[Path] = None,
no_input: bool = False,
checkout: Optional[str] = None,
directory: Optional[str] = None,
overwrite_if_exists: bool = False,
skip_if_file_exists: bool = False,
default_config: bool = False,
):
"""
Add a template or configuration to an existing project.
Expand All @@ -37,6 +42,11 @@ def add_cmd(
path_or_url: A URL or string to add the template or configuration
destination_dir: The project directory to add the layer to
no_input: If ``True`` force each layer's ``no_input`` attribute to ``True``
checkout: The branch, tag or commit to check out after git clone
directory: Directory within repo that holds cookiecutter.json file
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
Raises:
GitError: If the destination_dir is not a git repository
Expand All @@ -57,7 +67,15 @@ def add_cmd(
addl_composition = read_composition(path_or_url)
logger.info(f"Adding composition {path_or_url} to {output_dir}.")
else:
tmpl = LayerConfig(template=path_or_url)
overwrite_rules = ["*"] if overwrite_if_exists else []
tmpl = LayerConfig(
template=path_or_url,
directory=directory,
checkout=checkout,
no_input=no_input or default_config,
skip_if_file_exists=skip_if_file_exists,
overwrite=overwrite_rules,
)
addl_composition = Composition(layers=[tmpl])
logger.info(f"Adding template {path_or_url} to {output_dir}.")

Expand Down
22 changes: 20 additions & 2 deletions cookie_composer/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def create_cmd(
path_or_url: str,
output_dir: Optional[Path] = None,
no_input: bool = False,
checkout: Optional[str] = None,
directory: Optional[str] = None,
overwrite_if_exists: bool = False,
skip_if_file_exists: bool = False,
default_config: bool = False,
) -> Path:
"""
Generate a new project from a composition file, local template or remote template.
Expand All @@ -30,6 +35,11 @@ def create_cmd(
path_or_url: The path or url to the composition file or template
output_dir: Where to generate the project
no_input: If ``True`` force each layer's ``no_input`` attribute to ``True``
checkout: The branch, tag or commit to check out after git clone
directory: Directory within repo that holds cookiecutter.json file
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
Returns:
The path to the generated project.
Expand All @@ -39,8 +49,16 @@ def create_cmd(
composition = read_composition(path_or_url)
logger.info(f"Rendering composition {path_or_url} to {output_dir}.")
else:
tmpl = LayerConfig(template=path_or_url)
composition = Composition(layers=[tmpl], destination=output_dir)
overwrite_rules = ["*"] if overwrite_if_exists else []
tmpl = LayerConfig(
template=path_or_url,
directory=directory,
checkout=checkout,
no_input=no_input or default_config,
skip_if_file_exists=skip_if_file_exists,
overwrite=overwrite_rules,
)
composition = Composition(layers=[tmpl])
logger.info(f"Rendering template {path_or_url} to {output_dir}.")
rendered_layers = render_layers(composition.layers, output_dir, no_input=no_input)
rendered_composition = RenderedComposition(
Expand Down
2 changes: 1 addition & 1 deletion cookie_composer/composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def write_composition(layers: List[LayerConfig], destination: Union[str, Path]):

def write_rendered_composition(composition: RenderedComposition):
"""
Write the composition file using the rendered layers to the appropriate.
Write the composition file using the rendered layers to the appropriate place.
Args:
composition: The rendered composition object to export
Expand Down

0 comments on commit a66f17a

Please sign in to comment.