Hook types
Init
-
Init hooks are executed before anything else.
-
The variables
crate_type/authors/username/os-archandis_initare available. -
The variable
project-namemay be available.And only if
cargo-generatewas called with the--initflag, in which case it is the raw user input. -
The variable
project-namemay be set - avoiding a user prompt!The variable will still be subject for case changes to fit with the rust/cargo expectations.
The
--nameparameter still decides the final destination dir (together with the the--initflag), in order not to confuse the user.
Pre
-
Pre hooks are run after all placeholders mentioned in cargo-generate.toml has been resolved.
-
The hooks are free to add additional variables, but its too late to influence the conditional system.
This is a side effect of conditionals influencing the hooks - so placeholders need to be evaluated before the hooks are known.
Post
- Post hooks are run after template expansion, but before final output is moved to the final destination.
Why not later? Security, and the fact that a failing script still causes no errors in the users destination.
Named hooks
Available since version 0.26.0
Most entries in [hooks] are paths to .rhai files. One is not: fmt names the built-in
formatting step rather than a script.
[hooks]
post = ["fmt"]
- The identifier only has meaning in
post. Ininitorpreit is ignored with a warning. - Unlike a script hook, it runs on the final destination rather than the template’s working directory.
- Listing it re-enables formatting for a template that set
fmt = false. - The name is reserved, so a hook script cannot use it. Any other name,
fmt.rhaiincluded, behaves normally.