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\Hook\Template; |
16
|
|
|
use CaptainHook\App\Storage\Util; |
17
|
|
|
use RuntimeException; |
18
|
|
|
use SebastianFeldmann\Git\Repository; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Builder class |
22
|
|
|
* |
23
|
|
|
* Creates git hook Template objects regarding some provided input. |
24
|
|
|
* |
25
|
|
|
* @package CaptainHook |
26
|
|
|
* @author Sebastian Feldmann <[email protected]> |
27
|
|
|
* @link https://github.com/captainhookphp/captainhook |
28
|
|
|
* @since Class available since Release 4.3.0 |
29
|
|
|
*/ |
30
|
|
|
abstract class Builder |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Creates a template that is responsible for the git hook template |
34
|
|
|
* |
35
|
|
|
* @param \CaptainHook\App\Config $config |
36
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
37
|
|
|
* @return \CaptainHook\App\Hook\Template |
38
|
|
|
*/ |
39
|
6 |
|
public static function build(Config $config, Repository $repository): Template |
40
|
|
|
{ |
41
|
6 |
|
$vendorPath = (string) realpath($config->getVendorDirectory()); |
42
|
6 |
|
$repositoryPath = (string) realpath($repository->getRoot()); |
43
|
6 |
|
$configPath = (string) realpath($config->getPath()); |
44
|
|
|
|
45
|
6 |
|
if (empty($vendorPath)) { |
46
|
|
|
throw new RuntimeException('composer vendor directory not found'); |
47
|
|
|
} |
48
|
|
|
|
49
|
6 |
|
if ($config->getRunMode() === Template::DOCKER) { |
50
|
|
|
// For docker we need to strip down the current working directory. |
51
|
|
|
// This is caused because docker will always connect to a specific working directory |
52
|
|
|
// where the absolute path will not be recognized. |
53
|
|
|
// E.g.: |
54
|
|
|
// cwd => /docker |
55
|
|
|
// path => /docker/captainhook-run |
56
|
|
|
// The actual path needs to be /captainhook-run to work |
57
|
2 |
|
$dockerRepoPath = self::getRelativePath($repositoryPath); |
58
|
|
|
|
59
|
2 |
|
return new Docker( |
60
|
2 |
|
$dockerRepoPath, |
61
|
2 |
|
$config->getVendorDirectory(), |
62
|
2 |
|
$config->getRunExec() |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
return new Local( |
67
|
4 |
|
$repositoryPath, |
68
|
|
|
$vendorPath, |
69
|
|
|
$configPath |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Transforms an absolute path to a relative one |
75
|
|
|
* |
76
|
|
|
* @param string $path |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
2 |
|
private static function getRelativePath(string $path): string |
80
|
|
|
{ |
81
|
2 |
|
return Util::getSubPathOf(Util::pathToArray($path), Util::pathToArray((string)getcwd())); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|