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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace CaptainHook\App\Hook\Template\Local; |
15
|
|
|
|
16
|
|
|
use CaptainHook\App\CH; |
17
|
|
|
use CaptainHook\App\Hook\Template; |
18
|
|
|
use CaptainHook\App\Hooks; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Shell class |
22
|
|
|
* |
23
|
|
|
* Generates the sourcecode for the php hook scripts in .git/hooks/*. |
24
|
|
|
* |
25
|
|
|
* @package CaptainHook |
26
|
|
|
* @author Sebastian Feldmann <[email protected]> |
27
|
|
|
* @link https://github.com/captainhook-git/captainhook |
28
|
|
|
* @since Class available since Release 5.0.0 |
29
|
|
|
*/ |
30
|
|
|
class Shell extends Template\Local |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Returns lines of code for the local src installation |
34
|
|
|
* |
35
|
|
|
* @param string $hook |
36
|
|
|
* @return array<string> |
37
|
|
|
*/ |
38
|
24 |
|
protected function getHookLines(string $hook): array |
39
|
|
|
{ |
40
|
24 |
|
return [ |
41
|
24 |
|
'#!/bin/sh', |
42
|
24 |
|
'', |
43
|
24 |
|
'# installed by CaptainHook ' . CH::VERSION, |
44
|
24 |
|
'', |
45
|
24 |
|
'INTERACTIVE="--no-interaction"', |
46
|
24 |
|
'', |
47
|
24 |
|
'# if necessary read original hook stdIn to pass it in as --input option', |
48
|
24 |
|
Hooks::receivesStdIn($hook) ? 'input=$(cat)' : 'input=""', |
49
|
24 |
|
'', |
50
|
24 |
|
'if [ -t 1 ]; then', |
51
|
24 |
|
' # If we\'re in a terminal, redirect stdout and stderr to /dev/tty and', |
52
|
24 |
|
' # read stdin from /dev/tty. Allow interactive mode for CaptainHook.', |
53
|
24 |
|
' exec >/dev/tty 2>/dev/tty </dev/tty', |
54
|
24 |
|
' INTERACTIVE=""', |
55
|
24 |
|
'fi', |
56
|
24 |
|
'', |
57
|
24 |
|
$this->getExecutable() |
58
|
24 |
|
. ' $INTERACTIVE' |
59
|
24 |
|
. ' --configuration=' . $this->pathInfo->getConfigPath() |
60
|
24 |
|
. $this->getBootstrapCmdOption() |
61
|
24 |
|
. ' --input="$input"' |
62
|
24 |
|
. ' hook:' . $hook . ' "$@"' |
63
|
24 |
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the path to the executable including a configured php executable |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
24 |
|
protected function getExecutable(): string |
72
|
|
|
{ |
73
|
24 |
|
$executable = !empty($this->config->getPhpPath()) ? $this->config->getPhpPath() . ' ' : ''; |
74
|
|
|
|
75
|
24 |
|
if (!empty($this->config->getRunConfig()->getCaptainsPath())) { |
76
|
2 |
|
return $executable . $this->config->getRunConfig()->getCaptainsPath(); |
77
|
|
|
} |
78
|
|
|
|
79
|
22 |
|
return $executable . $this->pathInfo->getExecutablePath(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|