summaryrefslogtreecommitdiff
path: root/dots_manager/template.py
blob: 74d48ae96179a304a7f5c8738397a5d2cd283790 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from pathlib import Path
from typing import Optional
import shutil
import jinja2
from dots_manager.config import Config
from dots_manager.parallel import parallelize
from dots_manager.utils import is_some
from dots_manager.env import Environment


def is_template(path: Path) -> bool:
    return path.suffix == Config.template_extension


def find_templates_under(destination: Path) -> list[Path]:
    return [
        t
        for t in destination.glob("**/*" + Config.template_extension)
        if t.is_file() and is_template(t)
    ]


def render_template_to(
    template: Path, destination: Path, env: Environment
) -> Optional[Path]:
    env.logger.debug(f"rendering template {template} to {destination} ✧*ฺ")
    if not is_template(template):
        env.logger.error(f"{template} is not a valid template D:")
        return None

    jinja_env = jinja2.Environment(
        loader=jinja2.BaseLoader,
        undefined=jinja2.StrictUndefined,
        trim_blocks=True,
        lstrip_blocks=True,
    )
    rendered = render(template, jinja_env, env)
    if not rendered:
        env.logger.error(f"failed to render template {template}")
        return None

    destination.write_text(rendered)
    return destination


def render(
    source: Path, jinja_env: jinja2.Environment, env: Environment
) -> Optional[str]:
    try:
        env.logger.debug(f"rendering template {source} ✿.。.:・")
        return jinja_env.from_string(source.read_text()).render(**env.context)
    except Exception as e:
        env.logger.error(f"couldn’t render {source}: {e}")
        return None


def compile_dotfiles(source: Path, output: Path, env: Environment) -> bool:
    env.logger.info(f"compiling {source} to {output}")
    output.mkdir(exist_ok=True, parents=True)
    shutil.copytree(source, output, dirs_exist_ok=True)

    tasks = parallelize(
        lambda template: is_some(
            render_template_to,
            template,
            template.with_suffix(""),
            env,
        )[0]
        and (template.unlink() is None),
        find_templates_under(output),
        env,
    )
    return all(tasks)