|
| 1 | +"""Post-generate hook for cookiecutter.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from pathlib import Path |
| 5 | +from shutil import rmtree, copy, copytree |
| 6 | + |
| 7 | + |
| 8 | +def change_line_endings_crlf_to_lf(): |
| 9 | + for path in Path(".").glob("**/*"): |
| 10 | + if path.is_file(): |
| 11 | + data = path.read_bytes() |
| 12 | + lf_data = data.replace(b"\r\n", b"\n") |
| 13 | + path.write_bytes(lf_data) |
| 14 | + |
| 15 | + |
| 16 | +def remove_unwanted_templates(): |
| 17 | + |
| 18 | + if not {{ cookiecutter.add_issue_templates }}: |
| 19 | + LOG.info("Skipping issue templates files generation ...") |
| 20 | + path = Path("./ISSUE_TEMPLATE") |
| 21 | + rmtree(path) |
| 22 | + |
| 23 | + if not {{ cookiecutter.add_PR_template }}: |
| 24 | + LOG.info("Skipping PR templates file generation ...") |
| 25 | + path = Path("./pull_request_template.md") |
| 26 | + path.unlink() |
| 27 | + |
| 28 | + |
| 29 | +def add_ci_action_unit_tests_runner(): |
| 30 | + """Adds a CI action for running unit tests, if applicable""" |
| 31 | + |
| 32 | + target_ci_tests_action = "{{cookiecutter.add_ci_action_unit_tests_runner}}" |
| 33 | + |
| 34 | + if target_ci_tests_action.lower() != "none": |
| 35 | + |
| 36 | + LOG.info("Adding action for running unit tests (workflows folder) with default config...") |
| 37 | + |
| 38 | + destination = Path("workflows") |
| 39 | + ci_test_workflow_files_path = Path("_", "workflows", target_ci_tests_action) |
| 40 | + copytree(ci_test_workflow_files_path, destination) |
| 41 | + |
| 42 | + else: |
| 43 | + LOG.info("Skipping CI action for running unit tests file generation (%s selected) ...", target_ci_tests_action) |
| 44 | + |
| 45 | + |
| 46 | +def clean(): |
| 47 | + """Remove files and folders only needed as input for generation.""" |
| 48 | + LOG.info("Removing input data folder ...") |
| 49 | + rmtree("_") |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + |
| 54 | + logging.basicConfig(level=logging.DEBUG, format="%(message)s") |
| 55 | + LOG = logging.getLogger("post_gen_project") |
| 56 | + remove_unwanted_templates() |
| 57 | + add_ci_action_unit_tests_runner() |
| 58 | + clean() |
| 59 | + change_line_endings_crlf_to_lf() |
0 commit comments