Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of variables in "includes" glob pattern #114

Open
alirezanet opened this issue May 22, 2024 Discussed in #113 · 0 comments
Open

Usage of variables in "includes" glob pattern #114

alirezanet opened this issue May 22, 2024 Discussed in #113 · 0 comments
Labels
feature request Request for a feature

Comments

@alirezanet
Copy link
Owner

Discussed in #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:

{
  "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:

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

@alirezanet alirezanet added the feature request Request for a feature label May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Request for a feature
Projects
None yet
Development

No branches or pull requests

1 participant