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

STAGED_FILES greedy when there is conditions #215

Closed
CircleCode opened this issue May 5, 2023 · 1 comment
Closed

STAGED_FILES greedy when there is conditions #215

CircleCode opened this issue May 5, 2023 · 1 comment

Comments

@CircleCode
Copy link

imagine the following hook:

{
  "action": "cmd -- {$STAGED_FILES|of-type:php}"
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\ThatIs",
      "args": [
        {
          "ofType": "php",
          "inDirectory": [
            "foo/",
            "bar"
          ]
        }
      ]
    }
  ]
}

and the following staged files:

  • foo/foo.php
  • bar/bar.php
  • baz/baz.php

the hook will call cmd with the 3 files, and there is no way to restrict this (since we would require $STAGED_FILES|of-type:php|in-dir with 2 directories

I imagine several solutions

  1. easy one: allow multivalued in-dir filter (coma separated?), but can be very verbose when there are a lot of dirs
  2. complex one: add a filter like matchFilesCondition that would pick all the files that have been matched by all the conditions applied on files (complicated because it creates a link between condition and command, and also because it should take boolean operator on conditions into account)

Side note: I could also split my hook into one command for each dir, but this increases a lot the time required to run the hook, and this is complicated when there are a lot of dirs involved.

@CircleCode
Copy link
Author

I just found thanks to your tests that this can easily be achieved with multiple uses of the placeholder:

/tests/unit/Runner/Action/Cli/Command/FormatterTest.php#L70-L75

        $formatter = new Formatter($io, $config, $repo);
        $command1  = $formatter->format('cmd1 argument {$STAGED_FILES|in-dir:foo} {$STAGED_FILES|in-dir:baz}');
        $command2  = $formatter->format('cmd2 argument {$STAGED_FILES}');

        $this->assertEquals('cmd1 argument foo/file1.php baz/file3.php', $command1);
        $this->assertEquals('cmd2 argument foo/file1.php bar/file2.php baz/file3.php', $command2);

For my example, this would be:

{
  "action": "cmd -- {$STAGED_FILES|of-type:php|in-dir:foo/} {$STAGED_FILES|of-type:php|in-dir:bar/}"
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\ThatIs",
      "args": [
        {
          "ofType": "php",
          "inDirectory": [
            "foo/",
            "bar/"
          ]
        }
      ]
    }
  ]
}

Thus closing this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant