-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathrender_templates.py
58 lines (43 loc) · 1.96 KB
/
render_templates.py
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
#
# Copyright (c) 2020-2021 Arm Limited and Contributors. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
"""Render jinja templates required by the project package."""
import datetime
from pathlib import Path
import jinja2
TEMPLATES_DIRECTORY = Path("_internal", "templates")
def render_cmakelists_template(cmakelists_file: Path, program_name: str, os_path: str) -> None:
"""Render CMakeLists.tmpl with the copyright year and program name as the app target name.
Args:
cmakelists_file: The path where CMakeLists.txt will be written.
program_name: The name of the program, will be used as the app target name.
os_path: The directory where the mbed os is stored.
"""
cmakelists_file.write_text(
render_jinja_template(
"CMakeLists.tmpl", {"program_name": program_name, "os_path": os_path,
"year": str(datetime.datetime.now().year)}
)
)
def render_main_cpp_template(main_cpp: Path) -> None:
"""Render a basic main.cpp which prints a hello message and returns.
Args:
main_cpp: Path where the main.cpp file will be written.
"""
main_cpp.write_text(render_jinja_template("main.tmpl", {"year": str(datetime.datetime.now().year)}))
def render_gitignore_template(gitignore: Path) -> None:
"""Write out a basic gitignore file ignoring the build and config directory.
Args:
gitignore: The path where the gitignore file will be written.
"""
gitignore.write_text(render_jinja_template("gitignore.tmpl", {}))
def render_jinja_template(template_name: str, context: dict) -> str:
"""Render a jinja template.
Args:
template_name: The name of the template being rendered.
context: Data to render into the jinja template.
"""
env = jinja2.Environment(loader=jinja2.PackageLoader("mbed_tools.project", str(TEMPLATES_DIRECTORY)))
template = env.get_template(template_name)
return template.render(context)