14
14
namespace CaptainHook \App \Hook \Template ;
15
15
16
16
use CaptainHook \App \CH ;
17
+ use CaptainHook \App \Config ;
17
18
use CaptainHook \App \Hook \Template ;
18
- use CaptainHook \App \Hook \Template \Docker \Config as DockerConfig ;
19
+ use CaptainHook \App \Hook \Template \Docker \RunConfig as DockerConfig ;
19
20
use SebastianFeldmann \Camino \Path \Directory ;
20
21
use SebastianFeldmann \Camino \Path \File ;
21
22
33
34
class Docker implements Template
34
35
{
35
36
/**
36
- * Path to the repository root
37
+ * All path required for template creation
37
38
*
38
- * @var \SebastianFeldmann\Camino\Path\Directory
39
+ * @var \CaptainHook\App\Hook\Template\PathInfo
39
40
*/
40
- private $ repository ;
41
+ private PathInfo $ pathInfo ;
41
42
42
43
/**
43
- * Path to the configuration file
44
+ * CaptainHook configuration
44
45
*
45
- * @var \SebastianFeldmann\Camino\Path\File
46
+ * @var \CaptainHook\App\Config
46
47
*/
47
- private $ config ;
48
-
49
- /**
50
- * Docker configuration with run-command & run-path
51
- *
52
- * @var \CaptainHook\App\Hook\Template\Docker\Config
53
- */
54
- private $ dockerConfig ;
48
+ private Config $ config ;
55
49
56
50
/**
57
51
* Path to the CaptainHook binary script or PHAR
58
52
*
59
53
* @var string
60
54
*/
61
- private $ binaryPath ;
55
+ private string $ binaryPath ;
62
56
63
57
/**
64
58
* Docker constructor
65
59
*
66
- * @param \SebastianFeldmann\Camino\Path\Directory $repo
67
- * @param \SebastianFeldmann\Camino\Path\File $config
68
- * @param \SebastianFeldmann\Camino\Path\File $captain
69
- * @param \CaptainHook\App\Hook\Template\Docker\Config $docker
60
+ * @param \CaptainHook\App\Hook\Template\PathInfo $pathInfo
61
+ * @param \CaptainHook\App\Config $config
70
62
*/
71
- public function __construct (Directory $ repo , File $ config, File $ captain , DockerConfig $ docker )
63
+ public function __construct (PathInfo $ pathInfo , Config $ config )
72
64
{
73
- $ this ->repository = $ repo ;
74
- $ this ->config = $ config ;
75
- $ this ->dockerConfig = $ docker ;
76
- $ this ->binaryPath = $ this ->resolveBinaryPath ($ repo , $ captain );
65
+ $ this ->pathInfo = $ pathInfo ;
66
+ $ this ->config = $ config ;
67
+ $ this ->binaryPath = $ this ->resolveBinaryPath ();
77
68
}
78
69
79
70
/**
@@ -84,55 +75,48 @@ public function __construct(Directory $repo, File $config, File $captain, Docker
84
75
*/
85
76
public function getCode (string $ hook ): string
86
77
{
87
- $ path2Config = $ this ->config -> getRelativePathFrom ( $ this -> repository );
78
+ $ path2Config = $ this ->pathInfo -> getConfigPath ( );
88
79
$ config = $ path2Config !== CH ::CONFIG ? ' --configuration= ' . escapeshellarg ($ path2Config ) : '' ;
80
+ $ bootstrap = !empty ($ this ->config ->getBootstrap ()) ? ' --bootstrap= ' . $ this ->config ->getBootstrap () : '' ;
89
81
90
82
$ lines = [
91
83
'#!/bin/sh ' ,
92
84
'' ,
93
85
'# installed by CaptainHook ' . CH ::VERSION ,
94
86
'' ,
95
- $ this ->dockerConfig ->getDockerCommand () . ' ' . $ this ->binaryPath . ' hook: ' . $ hook . $ config . ' "$@" '
87
+ $ this ->config ->getRunExec () . ' '
88
+ . $ this ->binaryPath . ' hook: ' . $ hook
89
+ . $ config
90
+ . $ bootstrap
91
+ . ' "$@" '
96
92
];
97
93
return implode (PHP_EOL , $ lines ) . PHP_EOL ;
98
94
}
99
95
100
96
/**
101
97
* Resolves the path to the captainhook binary and returns it
102
98
*
103
- * @param \SebastianFeldmann\Camino\Path\Directory $repo Absolute path to the git repository root
104
- * @param \SebastianFeldmann\Camino\Path\File $executable Absolute path to the executable
105
99
* @return string
106
100
*/
107
- private function resolveBinaryPath (Directory $ repo , File $ executable ): string
101
+ private function resolveBinaryPath (): string
108
102
{
109
103
// if a specific executable is configured use just that
110
- if (!empty ($ this ->dockerConfig -> getPathToCaptainHookExecutable ())) {
111
- return $ this ->dockerConfig -> getPathToCaptainHookExecutable ();
104
+ if (!empty ($ this ->config -> getRunPath ())) {
105
+ return $ this ->config -> getRunPath ();
112
106
}
113
107
114
- // check if the captainhook binary is in the repository bin directory
115
- // this is only the case if we work in the captainhook repository
116
- if (file_exists ($ repo ->getPath () . '/bin/captainhook ' )) {
117
- return './bin/captainhook ' ;
118
- }
119
-
120
- // For docker we need to strip down the current working directory.
108
+ // For Docker we need to strip down the current working directory.
121
109
// This is caused because docker will always connect to a specific working directory
122
110
// where the absolute path will not be recognized.
123
111
// E.g.:
124
112
// cwd => /project/
125
113
// path => /project/vendor/bin/captainhook
126
114
// docker => ./vendor/bin/captainhook
127
- $ pathToExecutable = $ executable ->getPath ();
128
-
129
- // if executable is located inside the repository use a relative path
115
+ // if the executable is located inside the repository we can use a relative path
130
116
// by default this should return something like ./vendor/bin/captainhook
131
- if ($ executable ->isChildOf ($ repo )) {
132
- $ pathToExecutable = './ ' . $ executable ->getRelativePathFrom ($ repo );
133
- }
134
-
135
117
// if the executable is not located in your git repository it will return the absolute path
136
- return $ pathToExecutable ;
118
+ // which will most likely not work from within the docker container
119
+ // you have to use the 'run_path' config then
120
+ return $ this ->pathInfo ->getExecutablePath ();
137
121
}
138
122
}
0 commit comments