Repository navigation
Expand file tree
/
Copy pathShell.php
More file actions
104 lines (89 loc) · 3.19 KB
/
Copy pathShell.php
File metadata and controls
104 lines (89 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
namespace GrumPHP\Task;
use GrumPHP\Collection\FilesCollection;
use GrumPHP\Exception\RuntimeException;
use GrumPHP\Formatter\ProcessFormatterInterface;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Config\ConfigOptionsResolver;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use GrumPHP\Task\Context\RunContext;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @extends AbstractExternalTask<ProcessFormatterInterface>
*/
class Shell extends AbstractExternalTask
{
public static function getConfigurableOptions(): ConfigOptionsResolver
{
$resolver = new OptionsResolver();
$resolver->setDefaults([
'scripts' => [],
'ignore_patterns' => [],
'whitelist_patterns' => [],
'triggered_by' => ['php'],
]);
$resolver->addAllowedTypes('scripts', ['array']);
$resolver->addAllowedTypes('ignore_patterns', ['array']);
$resolver->addAllowedTypes('whitelist_patterns', ['array']);
$resolver->addAllowedTypes('triggered_by', ['array']);
$resolver->setNormalizer('scripts', function (Options $options, array $scripts) {
return array_map(
/**
* @param string|array $script
*/
function ($script) {
return is_string($script) ? (array) $script : $script;
},
$scripts
);
});
return ConfigOptionsResolver::fromOptionsResolver($resolver);
}
/**
* {@inheritdoc}
*/
public function canRunInContext(ContextInterface $context): bool
{
return $context instanceof GitPreCommitContext || $context instanceof RunContext;
}
/**
* {@inheritdoc}
*/
public function run(ContextInterface $context): TaskResultInterface
{
$config = $this->getConfig()->getOptions();
$files = $context->getFiles()
->extensions($config['triggered_by'])
->paths($config['whitelist_patterns'] ?? [])
->notPaths($config['ignore_patterns'] ?? []);
if (0 === \count($files)) {
return TaskResult::createSkipped($this, $context);
}
$exceptions = [];
foreach ($config['scripts'] as $script) {
try {
$this->runShell($script);
} catch (RuntimeException $e) {
$exceptions[] = $e->getMessage();
}
}
if (\count($exceptions)) {
return TaskResult::createFailed($this, $context, implode(PHP_EOL, $exceptions));
}
return TaskResult::createPassed($this, $context);
}
private function runShell(array $scriptArguments): void
{
$arguments = $this->processBuilder->createArgumentsForCommand('sh');
$arguments->addArgumentArray('%s', $scriptArguments);
$process = $this->processBuilder->buildProcess($arguments);
$process->run();
if (!$process->isSuccessful()) {
throw new RuntimeException($this->formatter->format($process));
}
}
}