task.mk/generate.py

54 lines
1.1 KiB
Python
Raw Normal View History

2022-09-05 12:23:17 -05:00
#!/usr/bin/env python3
import sys
from pathlib import Path
import jinja2
2022-09-24 16:20:23 -05:00
py_script_names = [
"help",
2022-10-03 16:02:30 -05:00
"print",
2022-09-24 16:20:23 -05:00
"print-ansi",
"vars",
"confirm",
"utils",
"phonify",
"parsers",
2022-09-24 16:20:23 -05:00
]
2022-09-05 12:23:17 -05:00
def get_jinja_env():
templateLoader = jinja2.FileSystemLoader(searchpath=Path(__file__).parent / "src")
return jinja2.Environment(
loader=templateLoader,
block_start_string="#%",
block_end_string="%#",
variable_start_string="##-",
variable_end_string="-##",
2022-09-16 12:47:40 -05:00
comment_start_string="###-",
comment_end_string="-###",
2022-09-05 12:23:17 -05:00
)
def render(env, template, **kwargs):
template = env.get_template(template)
return template.render(**kwargs)
2022-09-16 12:47:40 -05:00
def dropnewlines(text):
return "\n".join([line for line in text.splitlines() if line])
2022-09-05 12:23:17 -05:00
def main():
if len(sys.argv) == 2:
version = sys.argv[1]
else:
version = "dev"
env = get_jinja_env()
2022-09-16 12:47:40 -05:00
py_scripts = [dropnewlines(render(env, f"{name}.py")) for name in py_script_names]
2022-09-05 12:23:17 -05:00
print(render(env, "task.mk", py_scripts=py_scripts, version=version))
if __name__ == "__main__":
main()