|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of CaptainHook |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CaptainHook\App\Runner\Action; |
|
13
|
|
|
|
|
14
|
|
|
use CaptainHook\App\Config; |
|
15
|
|
|
use CaptainHook\App\Console\IO; |
|
16
|
|
|
use CaptainHook\App\Exception; |
|
17
|
|
|
use CaptainHook\App\Runner\Action\Cli\Command\Formatter; |
|
18
|
|
|
use SebastianFeldmann\Cli\Processor\Symfony as Processor; |
|
19
|
|
|
use SebastianFeldmann\Git\Repository; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class Cli |
|
23
|
|
|
* |
|
24
|
|
|
* @package CaptainHook |
|
25
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
26
|
|
|
* @link https://github.com/captainhookphp/captainhook |
|
27
|
|
|
* @since Class available since Release 0.9.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
class Cli |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Execute the configured action |
|
33
|
|
|
* |
|
34
|
|
|
* @param \CaptainHook\App\Console\IO $io |
|
35
|
10 |
|
* @param \SebastianFeldmann\Git\Repository $repository |
|
36
|
|
|
* @param \CaptainHook\App\Config\Action $action |
|
37
|
10 |
|
* @return void |
|
38
|
10 |
|
* @throws \CaptainHook\App\Exception\ActionFailed |
|
39
|
|
|
*/ |
|
40
|
10 |
|
public function execute(IO $io, Repository $repository, Config\Action $action): void |
|
41
|
1 |
|
{ |
|
42
|
|
|
$processor = new Processor(); |
|
43
|
9 |
|
$cmdOriginal = $action->getAction(); |
|
44
|
9 |
|
$cmdFormatted = $this->formatCommand($repository, $cmdOriginal, $io->getArguments()); |
|
45
|
|
|
|
|
46
|
|
|
// if any placeholders got replaced output the finally executed command |
|
47
|
|
|
if ($cmdFormatted !== $cmdOriginal) { |
|
48
|
|
|
$io->write('Execute: <comment>' . $cmdFormatted . '</comment>', true, IO::VERBOSE); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$result = $processor->run($cmdFormatted); |
|
52
|
|
|
if (!$result->isSuccessful()) { |
|
53
|
|
|
throw new Exception\ActionFailed($result->getStdOut() . PHP_EOL . $result->getStdErr()); |
|
54
|
|
|
} |
|
55
|
|
|
$io->write(empty($result->getStdOut()) ? '<info>OK</info>' : $result->getStdOut()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Replace argument placeholder with their original values |
|
60
|
|
|
* |
|
61
|
10 |
|
* This replaces the hook argument and other placeholders |
|
62
|
|
|
* - prepare-commit-msg => FILE, MODE, HASH |
|
63
|
10 |
|
* - commit-msg => FILE |
|
64
|
1 |
|
* - pre-push => TARGET, URL |
|
65
|
|
|
* - pre-commit => - |
|
66
|
10 |
|
* - post-checkout => PREVIOUSHEAD, NEWHEAD, MODE |
|
67
|
|
|
* - post-merge => SQUASH |
|
68
|
|
|
* |
|
69
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
|
70
|
|
|
* @param string $command |
|
71
|
|
|
* @param array<string> $args |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function formatCommand(Repository $repository, string $command, array $args): string |
|
75
|
|
|
{ |
|
76
|
|
|
$formatter = new Formatter($repository, $args); |
|
77
|
|
|
return $formatter->format($command); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|