1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of CaptainHook. |
4
|
|
|
* |
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace CaptainHook\App\Runner\Action; |
11
|
|
|
|
12
|
|
|
use CaptainHook\App\Config; |
13
|
|
|
use CaptainHook\App\Console\IO; |
14
|
|
|
use CaptainHook\App\Exception; |
15
|
|
|
use SebastianFeldmann\Cli\Processor\ProcOpen as Processor; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Cli |
19
|
|
|
* |
20
|
|
|
* @package CaptainHook |
21
|
|
|
* @author Sebastian Feldmann <[email protected]> |
22
|
|
|
* @link https://github.com/sebastianfeldmann/captainhook |
23
|
|
|
* @since Class available since Release 0.9.0 |
24
|
|
|
*/ |
25
|
|
|
class Cli |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Execute the configured action. |
29
|
|
|
* |
30
|
|
|
* @param \CaptainHook\App\Console\IO $io |
31
|
|
|
* @param \CaptainHook\App\Config\Action $action |
32
|
|
|
* @param \CaptainHook\App\Config\Options $arguments |
33
|
|
|
* @throws \CaptainHook\App\Exception\ActionFailed |
34
|
|
|
*/ |
35
|
5 |
|
public function execute(IO $io, Config\Action $action, Config\Options $arguments) |
36
|
|
|
{ |
37
|
5 |
|
$processor = new Processor(); |
38
|
5 |
|
$result = $processor->run($this->formatCommand($action->getAction(), $arguments->getAll())); |
39
|
|
|
|
40
|
5 |
|
if (!$result->isSuccessful()) { |
41
|
1 |
|
throw Exception\ActionFailed::withMessage($result->getStdOut() . PHP_EOL . $result->getStdErr()); |
42
|
|
|
} |
43
|
|
|
|
44
|
4 |
|
$io->write($result->getStdOut()); |
45
|
4 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Replace argument placeholder with their original values |
49
|
|
|
* |
50
|
|
|
* This replaces the hook argument placeholder. |
51
|
|
|
* - prepare-commit-msg => FILE, MODE, HASH |
52
|
|
|
* - commit-msg => FILE |
53
|
|
|
* - pre-push => NAME, URL |
54
|
|
|
* - pre-commit => - |
55
|
|
|
* |
56
|
|
|
* @param string $command |
57
|
|
|
* @param array $args |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
5 |
|
protected function formatCommand(string $command, array $args) |
61
|
|
|
{ |
62
|
5 |
|
foreach ($args as $key => $value) { |
63
|
2 |
|
$command = str_replace('{' . strtoupper($key) . '}', $value, $command); |
64
|
|
|
} |
65
|
5 |
|
return $command; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|