-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
211 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"""Top-level interface for project forge.""" | ||
|
||
from project_forge.cli import cli | ||
|
||
cli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
"""The command-line interface.""" | ||
|
||
from pathlib import Path | ||
from typing import Any, Optional | ||
|
||
import rich_click as click | ||
from click.core import Context | ||
|
||
from project_forge import __version__ | ||
from project_forge.core.io import parse_file | ||
|
||
|
||
@click.group( | ||
context_settings={ | ||
"help_option_names": ["-h", "--help"], | ||
}, | ||
add_help_option=True, | ||
) | ||
@click.version_option(version=__version__) | ||
@click.pass_context | ||
def cli(ctx: Context) -> None: | ||
"""Version bump your Python project.""" | ||
pass | ||
|
||
|
||
@cli.command() | ||
@click.argument( | ||
"composition", | ||
type=click.Path(exists=True, dir_okay=False, file_okay=True, resolve_path=True, path_type=Path), | ||
) | ||
@click.option( | ||
"--use-defaults", | ||
is_flag=True, | ||
help="Do not prompt for input and use the defaults specified in the composition.", | ||
) | ||
@click.option( | ||
"--output-dir", | ||
"-o", | ||
required=False, | ||
default=lambda: Path.cwd(), # NOQA: PLW0108 | ||
type=click.Path(exists=True, dir_okay=True, file_okay=False, resolve_path=True, path_type=Path), | ||
help="The directory to render the composition to. Defaults to the current working directory.", | ||
) | ||
@click.option( | ||
"--data-file", | ||
"-f", | ||
required=False, | ||
type=click.Path(exists=True, dir_okay=False, file_okay=True, resolve_path=True, path_type=Path), | ||
help=( | ||
"The path to a JSON, YAML, or TOML file whose contents are added to the initial context. " | ||
"Great for answering some or all the answers for a composition." | ||
), | ||
) | ||
@click.option( | ||
"--data", | ||
"-d", | ||
nargs=2, | ||
type=str, | ||
metavar="KEY VALUE", | ||
required=False, | ||
multiple=True, | ||
help="The key-value pairs added to the initial context. Great for providing answers to composition questions.", | ||
) | ||
def build( | ||
composition: Path, | ||
use_defaults: bool, | ||
output_dir: Path, | ||
data_file: Optional[Path] = None, | ||
data: Optional[tuple[tuple[str, str], ...]] = None, | ||
): | ||
"""Build a project from a composition and render it to a directory.""" | ||
from project_forge.commands.build import build_project | ||
|
||
initial_context: dict[str, Any] = {} | ||
if data_file: | ||
initial_context |= parse_file(data_file) | ||
|
||
if data: | ||
initial_context |= dict(data) | ||
print(type(output_dir)) | ||
build_project(composition, output_dir=output_dir, use_defaults=use_defaults, initial_context=initial_context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Command implementation.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Starting point to render a project.""" | ||
|
||
import logging | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
from project_forge.configurations.composition import read_composition_file | ||
from project_forge.context_builder.context import build_context | ||
from project_forge.rendering.environment import load_environment | ||
from project_forge.rendering.render import render_env | ||
from project_forge.rendering.templates import catalog_inheritance | ||
from project_forge.tui import ask_question | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def build_project( | ||
composition_file: Path, output_dir: Path, use_defaults: bool = False, initial_context: Optional[dict] = None | ||
) -> None: | ||
"""Render a project to a directory.""" | ||
initial_context = initial_context or {} | ||
composition = read_composition_file(composition_file) | ||
|
||
if use_defaults: | ||
for overlay in composition.overlays: | ||
overlay.ask_questions = False | ||
context = build_context(composition, ask_question, initial_context) | ||
|
||
template_paths = [overlay.pattern.template_location.resolve() for overlay in composition.overlays] # type: ignore[union-attr] | ||
inheritance = catalog_inheritance(template_paths) | ||
env = load_environment(inheritance) | ||
render_env(env, inheritance, context, output_dir) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters