Skip to content

Commit

Permalink
refactor(cli): use jinja over copier (#181)
Browse files Browse the repository at this point in the history
* change template location

* use jinja template

* remove git dependency

* only test_NAME.md for mdx tests

* fix cli init test

* add python deps

* updated poetry lock
  • Loading branch information
rnbguy committed Nov 18, 2022
1 parent 908a5bf commit 087e3bc
Show file tree
Hide file tree
Showing 21 changed files with 37 additions and 406 deletions.
34 changes: 16 additions & 18 deletions atomkraft/cli/__init__.py
@@ -1,22 +1,17 @@
import os
import sys
from pathlib import Path
from typing import Callable, Dict, Optional, Type

import git
import modelator
import pytest
import typer
from atomkraft import __version__
from atomkraft.utils.project import NoProjectError, project_root
from copier import run_auto
from atomkraft.utils.project import NoProjectError, init_project, project_root
from rich import print

from .. import chain, test
from ..reactor.reactor import generate_reactor

GH_TEMPLATE = "gh:informalsystems/atomkraft"
GH_REVISION = "dev"


class ErrorHandlingTyper(typer.Typer):
def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -88,21 +83,24 @@ def version():
no_args_is_help=True,
)
def init(
name: Path = typer.Argument(..., help="Name of new directory", show_default=False),
atomkraft_rev: Optional[str] = typer.Option(
None, help="Atomkraft Github revision for project template"
),
name: str = typer.Argument(..., help="Name of new directory", show_default=False),
):
"""
Initialize new Atomkraft project in the given directory
"""
try:
git.Repo(os.getcwd(), search_parent_directories=True)
except git.InvalidGitRepositoryError:
git.Repo.init(name)
if atomkraft_rev is None:
atomkraft_rev = GH_REVISION
run_auto(GH_TEMPLATE, name, vcs_ref=atomkraft_rev, data={"project_name": name})
dir_path = Path.cwd() / name

if (dir_path).exists():
print(
f"[red bold]{'Error'.rjust(12)}[/red bold] A directory `{name}` already exists!"
)
raise typer.Exit(1)

init_project(name, Path.cwd() / name)

print(
f"[green bold]{'Created'.rjust(12)}[/green bold] Atomkraft project `{name}`."
)


app.add_typer(
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions atomkraft/utils/project.py
Expand Up @@ -2,6 +2,8 @@
from pathlib import Path
from typing import Union

import jinja2

ATOMKRAFT_INTERNAL_DIR = ".atomkraft"

ATOMKRAFT_VAL_DIR_PREFIX = "val_"
Expand Down Expand Up @@ -37,3 +39,14 @@ def get_relative_project_path(path: Path) -> Path:
return path.relative_to(project_root())
else:
return path.absolute().relative_to(project_root())


def init_project(name: str, dir_path: Path):
loader = jinja2.PackageLoader("atomkraft", "templates/project")
env = jinja2.Environment(loader=loader)
env.globals["project_name"] = name
for tmpl_path in env.list_templates():
tmpl = env.get_template(tmpl_path)
final_path = dir_path / tmpl_path
final_path.parent.mkdir(parents=True, exist_ok=True)
tmpl.stream().dump(str(final_path))
7 changes: 0 additions & 7 deletions copier.yml

This file was deleted.

0 comments on commit 087e3bc

Please sign in to comment.