configure.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import sys
  2. from pathlib import Path
  3. import caseconverter
  4. import click
  5. from jinja2 import Environment, FileSystemLoader
  6. @click.command()
  7. @click.argument("name")
  8. @click.option("--debug/--no-debug", is_flag=True, default=True, help="Enable debug mode.")
  9. def main(name: str, debug: bool):
  10. """
  11. This is a script to configure the project.
  12. It's intended to be called by the `ato create` command.
  13. This script is intended to be run in the same environment
  14. as the ato CLI, so it's expecting to have access to the
  15. same packages and tools; Jinja, etc...
  16. """
  17. # Common variables
  18. extended_globals = {
  19. "name": name,
  20. "caseconverter": caseconverter,
  21. "repo_root": Path(__file__).parent,
  22. }
  23. # Load templates
  24. env = Environment(loader=FileSystemLoader("."))
  25. for template_path in Path(".").glob("**/*.j2"):
  26. # Figure out the target path and variables and what not
  27. target_path = template_path.parent / template_path.name.replace(
  28. ".j2", ""
  29. ).replace("__name__", caseconverter.kebabcase(name))
  30. extended_globals["rel_path"] = target_path
  31. extended_globals["python_path"] = sys.executable
  32. template = env.get_template(str(template_path), globals=extended_globals)
  33. # Make the noise!
  34. with target_path.open("w") as f:
  35. for chunk in template.generate():
  36. f.write(chunk)
  37. # Remove the template
  38. if not debug:
  39. template_path.unlink()
  40. # Remove this script
  41. if not debug:
  42. Path("configure.py").unlink()
  43. if __name__ == "__main__":
  44. main() # pylint: disable=no-value-for-parameter