Skip to content

Commit fed00cb

Browse files
authored
Merge pull request #1167 from evs-xsarus/feature/1157-triggered_by_filenames
Ticket #1157: support specific filenames as trigger for Shell.
2 parents 85facb6 + d8bb0e6 commit fed00cb

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

doc/tasks/shell.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ grumphp:
99
tasks:
1010
shell:
1111
scripts: []
12+
ignore_patterns: [],
13+
whitelist_patterns: [],
1214
triggered_by: [php]
1315
```
1416
@@ -35,11 +37,30 @@ grumphp:
3537

3638
*Note:* When using the `-c` option, the next argument should contain the full executable with all parameters. Be careful: quotes will be escaped!
3739

40+
**ignore_patterns**
41+
42+
*Default: []*
43+
44+
This is a list of file patterns that will be ignored by the shell tasks.
45+
Leave this option blank to run the task for all files defined in the whitelist_patterns and or triggered_by extensions.
46+
47+
**whitelist_patterns**
48+
49+
*Default: []*
50+
51+
This is a list of regex patterns that will filter files to validate. With this option you can skip files like tests.
52+
This option is used in relation with the parameter `triggered_by`.
53+
For example: whitelist files in `src/FolderA/` and `src/FolderB/` you can use
54+
```yaml
55+
whitelist_patterns:
56+
- /^src\/FolderA\/(.*)/
57+
- /^src\/FolderB\/(.*)/
58+
```
3859

3960
**triggered_by**
4061

4162
*Default: [php]*
4263

4364
This option will specify which file extensions will trigger the shell tasks.
44-
By default, Shell will be triggered by altering a PHP file.
65+
By default, Shell will be triggered by altering a PHP file.
4566
You can overwrite this option to whatever file you want to use!

src/Task/Shell.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace GrumPHP\Task;
66

7+
use GrumPHP\Collection\FilesCollection;
78
use GrumPHP\Exception\RuntimeException;
89
use GrumPHP\Formatter\ProcessFormatterInterface;
910
use GrumPHP\Runner\TaskResult;
@@ -25,10 +26,14 @@ public static function getConfigurableOptions(): ConfigOptionsResolver
2526
$resolver = new OptionsResolver();
2627
$resolver->setDefaults([
2728
'scripts' => [],
29+
'ignore_patterns' => [],
30+
'whitelist_patterns' => [],
2831
'triggered_by' => ['php'],
2932
]);
3033

3134
$resolver->addAllowedTypes('scripts', ['array']);
35+
$resolver->addAllowedTypes('ignore_patterns', ['array']);
36+
$resolver->addAllowedTypes('whitelist_patterns', ['array']);
3237
$resolver->addAllowedTypes('triggered_by', ['array']);
3338
$resolver->setNormalizer('scripts', function (Options $options, array $scripts) {
3439
return array_map(
@@ -59,7 +64,11 @@ public function canRunInContext(ContextInterface $context): bool
5964
public function run(ContextInterface $context): TaskResultInterface
6065
{
6166
$config = $this->getConfig()->getOptions();
62-
$files = $context->getFiles()->extensions($config['triggered_by']);
67+
68+
$files = $context->getFiles()
69+
->extensions($config['triggered_by'])
70+
->paths($config['whitelist_patterns'] ?? [])
71+
->notPaths($config['ignore_patterns'] ?? []);
6372
if (0 === \count($files)) {
6473
return TaskResult::createSkipped($this, $context);
6574
}

test/Unit/Task/ShellTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public function provideConfigurableOptions(): iterable
2626
[],
2727
[
2828
'scripts' => [],
29+
'ignore_patterns' => [],
30+
'whitelist_patterns' => [],
2931
'triggered_by' => ['php'],
3032
]
3133
];
@@ -37,6 +39,8 @@ public function provideConfigurableOptions(): iterable
3739
'scripts' => [
3840
['phpunit']
3941
],
42+
'ignore_patterns' => [],
43+
'whitelist_patterns' => [],
4044
'triggered_by' => ['php'],
4145
]
4246
];
@@ -50,6 +54,8 @@ public function provideConfigurableOptions(): iterable
5054
'scripts' => [
5155
['phpunit', 'tests']
5256
],
57+
'ignore_patterns' => [],
58+
'whitelist_patterns' => [],
5359
'triggered_by' => ['php'],
5460
]
5561
];
@@ -129,6 +135,22 @@ public function provideSkipsOnStuff(): iterable
129135
$this->mockContext(RunContext::class),
130136
function () {}
131137
];
138+
yield 'no-files-after-ignore-patterns' => [
139+
[
140+
'ignore_patterns' => ['test/'],
141+
],
142+
$this->mockContext(RunContext::class, ['test/file.php']),
143+
function() {
144+
}
145+
];
146+
yield 'no-files-after-whitelist-patterns' => [
147+
[
148+
'whitelist_patterns' => ['src/'],
149+
],
150+
$this->mockContext(RunContext::class, ['config/file.php']),
151+
function() {
152+
}
153+
];
132154
yield 'no-files-after-triggered-by' => [
133155
[],
134156
$this->mockContext(RunContext::class, ['notatwigfile.txt']),

0 commit comments

Comments
 (0)