alirezanet / Husky.Net

Git hooks made easy with Husky.Net internal task runner! 🐶 It brings the dev-dependency concept to the .NET world!
https://alirezanet.github.io/Husky.Net/
MIT License
684 stars 31 forks source link

Usage of variables in "includes" glob pattern #114

Open alirezanet opened 4 months ago

alirezanet commented 4 months ago

Discussed in https://github.com/alirezanet/Husky.Net/discussions/113

Originally posted by **ThijmenDam** May 21, 2024 Hi @alirezanet and contributors, first of all I'd like to thank you for this very useful package. So, our team would greatly benefit from the possibility of using variables in the "include" glob pattern. For example: ```json { "tasks": [ { "name": "dotnet-format-staged-files", "group": "pre-commit-operations", "command": "dotnet", "args": [ "format", "${args}", "--verbosity", "diagnostic", "--no-restore", "--include", "${staged}" ], "include": [ "${args}/**/*.cs" <---- this is what I refer to ] } ] } ``` The reason this would be very useful for our team is performance. To elaborate, some of our repositories contain multiple solutions, and without variables in the "include" glob pattern, we are not able to execute `dotnet format` **only** for the solutions that contain staged files. This means that `dotnet format` is executed for every solution, even if there are no staged files from that solution (which slows down the pre-commit hook by a lot). To illustrate, consider the following pre-commit hook: ```bash echo "[PRE-COMMIT] Formatting staged .NET files..." repository_root=$(dirname "$(dirname "$(dirname "$0")")") # Loop through all subdirectories of the root folder of the repository for dir in "$repository_root"/*; do # Ignore the directory if it is not a folder [ ! -d "$dir" ] && continue # Ignore directories that are not a .NET solution folder [ -z "$(find "$dir" -type f -name '*.sln')" ] && continue # Here we have confirmed that the directory contains a solution file solution_dir="$dir" # Get the path to the solution file in the .NET solution folder solution_file=$(find "$dir" -type f -name '*.sln') echo "[PRE-COMMIT] Formatting $solution_file" dotnet husky run --name dotnet-format-staged-files --args "$solution_dir" done ``` We pass the solution directory as argument, because `dotnet format` requires the workspace to be provided as command line argument whenever `dotnet format` is executed from a directory that does not contain a solution file (i.e. the root of our repository). To conclude, if we could use the `${args}` variable in the "include" glob pattern, `dotnet format` would only be executed for solutions that contain staged files. Which saves a lot of time for repositories with multiple solutions! Thanks for reading, and I'd love to hear what you think of this. Best, Thijmen