cookiecutter / cookiecutter

A cross-platform command-line utility that creates projects from cookiecutters (project templates), e.g. Python package projects, C projects.
https://pypi.org/project/cookiecutter/
BSD 3-Clause "New" or "Revised" License
22.35k stars 1.99k forks source link

Run sorted list of hooks #1928

Open SHEscher opened 1 year ago

SHEscher commented 1 year ago

Hi,

first of all, thanks for the great tool!

I have a use-case, in which I run two post_gen_project.* hooks. One is a python script, the other is a bash script.

I would like to determine the order in which they run. I noticed that the order changes currently, depending on the OS in use (I tested Mac and Linux).

Ordering could be archived by, e.g., naming the hooks post_gen_project_0.py & post_gen_project_1.sh, such that a simple sorted() would return them in order, see your code:

https://github.com/cookiecutter/cookiecutter/blob/1b8520e7075175db4a3deae85e71081730ca7ad1/cookiecutter/hooks.py#L130C19-L130C26

 for script in sorted(scripts):
        run_script_with_context(script, project_dir, context)

This would require an additional small change in valid_hook().

Is this something you would consider for the package?

Best

david-abn commented 11 months ago

As of now, cookiecutter wont allow hook names that deviate from "post_gen_project" or "pre_gen_project" or "pre_prompt".

Created new PR to allow for multiple scripts, as long as they contain substring(s) mentioned above. i.e. "pre_gen_project_2" is valid. Also added sorting as requested :)