|
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 as ActionRunner; |
|
18
|
|
|
use CaptainHook\App\Runner\Action\Cli\Command\Formatter; |
|
19
|
|
|
use SebastianFeldmann\Cli\Processor\Symfony as Processor; |
|
20
|
|
|
use SebastianFeldmann\Git\Repository; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class Cli |
|
24
|
|
|
* |
|
25
|
|
|
* @package CaptainHook |
|
26
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
27
|
|
|
* @link https://github.com/captainhook-git/captainhook |
|
28
|
|
|
* @since Class available since Release 0.9.0 |
|
29
|
|
|
* @internal |
|
30
|
|
|
*/ |
|
31
|
|
|
class Cli implements ActionRunner |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Execute the configured action |
|
35
|
|
|
* |
|
36
|
|
|
* @param \CaptainHook\App\Config $config |
|
37
|
|
|
* @param \CaptainHook\App\Console\IO $io |
|
38
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
|
39
|
|
|
* @param \CaptainHook\App\Config\Action $action |
|
40
|
|
|
* @return void |
|
41
|
|
|
* @throws \CaptainHook\App\Exception\ActionFailed |
|
42
|
|
|
*/ |
|
43
|
20 |
|
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void |
|
44
|
|
|
{ |
|
45
|
20 |
|
$processor = new Processor(); |
|
46
|
20 |
|
$cmdOriginal = $action->getAction(); |
|
47
|
20 |
|
$cmdFormatted = $this->formatCommand($io, $config, $repository, $cmdOriginal); |
|
48
|
|
|
|
|
49
|
20 |
|
$io->write( |
|
50
|
20 |
|
' <comment>cmd:</comment> ' . $cmdFormatted, |
|
51
|
20 |
|
true, |
|
52
|
20 |
|
IO::VERBOSE |
|
53
|
20 |
|
); |
|
54
|
|
|
|
|
55
|
20 |
|
$result = $processor->run($cmdFormatted); |
|
56
|
20 |
|
$output = ''; |
|
57
|
|
|
|
|
58
|
20 |
|
if (!empty($result->getStdOut())) { |
|
59
|
12 |
|
$output .= PHP_EOL . $result->getStdOut(); |
|
60
|
|
|
} |
|
61
|
20 |
|
if (!empty($result->getStdErr())) { |
|
62
|
7 |
|
$output .= PHP_EOL . $result->getStdErr(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
20 |
|
if (!empty($output)) { |
|
66
|
15 |
|
$io->write( |
|
67
|
15 |
|
[' <comment>command output:</comment>', trim($output)], |
|
68
|
15 |
|
true, |
|
69
|
15 |
|
!$result->isSuccessful() ? IO::NORMAL : IO::VERBOSE |
|
70
|
15 |
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
20 |
|
if (!$result->isSuccessful()) { |
|
74
|
7 |
|
throw new Exception\ActionFailed( |
|
75
|
7 |
|
'failed to execute: ' . $cmdFormatted |
|
76
|
7 |
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Replace argument placeholder with their original values |
|
82
|
|
|
* |
|
83
|
|
|
* This replaces the hook argument and other placeholders |
|
84
|
|
|
* - prepare-commit-msg => FILE, MODE, HASH |
|
85
|
|
|
* - commit-msg => FILE |
|
86
|
|
|
* - pre-push => TARGET, URL |
|
87
|
|
|
* - pre-commit => - |
|
88
|
|
|
* - post-checkout => PREVIOUSHEAD, NEWHEAD, MODE |
|
89
|
|
|
* - post-merge => SQUASH |
|
90
|
|
|
* |
|
91
|
|
|
* @param \CaptainHook\App\Console\IO $io |
|
92
|
|
|
* @param \CaptainHook\App\Config $config |
|
93
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
|
94
|
|
|
* @param string $command |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
20 |
|
protected function formatCommand(IO $io, Config $config, Repository $repository, string $command): string |
|
98
|
|
|
{ |
|
99
|
20 |
|
$formatter = new Formatter($io, $config, $repository); |
|
100
|
20 |
|
return $formatter->format($command); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|