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
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace CaptainHook\App\Hook\Template; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Config; |
15
|
|
|
use CaptainHook\App\Console\IOUtil; |
16
|
|
|
use CaptainHook\App\Hook\Template; |
17
|
|
|
use CaptainHook\App\Storage\Util; |
18
|
|
|
use SebastianFeldmann\Git\Repository; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Builder class |
23
|
|
|
* |
24
|
|
|
* Creates git hook Template objects regarding some provided input. |
25
|
|
|
* |
26
|
|
|
* @package CaptainHook |
27
|
|
|
* @author Sebastian Feldmann <[email protected]> |
28
|
|
|
* @link https://github.com/captainhookphp/captainhook |
29
|
|
|
* @since Class available since Release 4.3.0 |
30
|
|
|
*/ |
31
|
|
|
abstract class Builder |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Creates a template that is responsible for the git hook template |
35
|
|
|
* |
36
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
37
|
|
|
* @param \CaptainHook\App\Config $config |
38
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
39
|
|
|
* @param string $runMode |
40
|
|
|
* |
41
|
|
|
* @return \CaptainHook\App\Hook\Template |
42
|
|
|
*/ |
43
|
6 |
|
public static function build( |
44
|
|
|
InputInterface $input, |
45
|
|
|
Config $config, |
46
|
|
|
Repository $repository |
47
|
|
|
): Template |
48
|
|
|
{ |
49
|
6 |
|
$runMode = self::getOpt( |
50
|
6 |
|
IOUtil::argToString($input->getOption(Config::SETTING_RUN_MODE)), |
51
|
6 |
|
$config->getRunMode() |
52
|
|
|
); |
53
|
|
|
|
54
|
6 |
|
if ($runMode === Template::DOCKER) { |
55
|
|
|
// For docker we need to strip down the current working directory. |
56
|
|
|
// This is caused because docker will always connect to a specific working directory |
57
|
|
|
// where the absolute path will not be recognized. |
58
|
|
|
// E.g.: |
59
|
|
|
// cwd => /docker |
60
|
|
|
// path => /docker/captainhook-run |
61
|
|
|
// The actual path needs to be /captainhook-run to work |
62
|
2 |
|
$repoPath = self::getRelativePath((string) realpath($repository->getRoot())); |
63
|
|
|
|
64
|
2 |
|
$runExec = self::getOpt( |
65
|
2 |
|
IOUtil::argToString($input->getOption(Config::SETTING_RUN_EXEC)), |
66
|
2 |
|
$config->getRunExec() |
67
|
|
|
); |
68
|
|
|
|
69
|
2 |
|
return new Docker( |
70
|
2 |
|
$repoPath, |
71
|
2 |
|
'vendor', |
72
|
2 |
|
$runExec |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
return new Local( |
77
|
4 |
|
(string) realpath($repository->getRoot()), |
78
|
4 |
|
getcwd() . '/vendor', |
79
|
4 |
|
(string) realpath($config->getPath()) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Transforms an absolute path to a relative one |
85
|
|
|
* |
86
|
|
|
* @param string $path |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
2 |
|
private static function getRelativePath(string $path) |
90
|
|
|
{ |
91
|
2 |
|
return Util::getSubPathOf(Util::pathToArray($path), Util::pathToArray(getcwd())); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Choose option value over config value |
96
|
|
|
* |
97
|
|
|
* @param string $optionValue |
98
|
|
|
* @param string $configValue |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
6 |
|
private static function getOpt(string $optionValue, string $configValue) : string |
102
|
|
|
{ |
103
|
6 |
|
return !empty($optionValue) ? $optionValue : $configValue; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|