Skip to content

Commit cd4595a

Browse files
Merge pull request #221 from feature/use-run-path-as-is
Feature/use run path as is
2 parents c5b2d63 + 1a029ea commit cd4595a

File tree

10 files changed

+330
-311
lines changed

10 files changed

+330
-311
lines changed

src/Hook/Template/Builder.php

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -46,57 +46,31 @@ abstract class Builder
4646
*/
4747
public static function build(Config $config, Repository $repository, Resolver $resolver): Template
4848
{
49-
$repositoryPath = self::toAbsolutePath($repository->getRoot());
50-
$configPath = self::toAbsolutePath($config->getPath());
51-
$bootstrapPath = dirname($configPath) . '/' . $config->getBootstrap();
52-
$captainPath = self::toAbsolutePath($resolver->getExecutable());
49+
$pathInfo = new PathInfo($repository->getRoot(), $config->getPath(), $resolver->getExecutable());
50+
$bootstrapPath = dirname($config->getPath()) . '/' . $config->getBootstrap();
5351

5452
if (!file_exists($bootstrapPath)) {
5553
throw new RuntimeException('bootstrap file not found: \'' . $bootstrapPath . '\'');
5654
}
5755

58-
$phpPath = $config->getPhpPath();
59-
6056
switch ($config->getRunMode()) {
6157
case Template::DOCKER:
6258
return new Docker(
63-
new Directory($repositoryPath),
64-
new File($configPath),
65-
new File($captainPath),
66-
new Docker\Config($config->getRunExec(), $config->getRunPath())
59+
$pathInfo,
60+
$config
6761
);
6862
case Template::PHP:
6963
return new PHP(
70-
new Directory($repositoryPath),
71-
new File($configPath),
72-
new File($captainPath),
73-
$config->getBootstrap(),
74-
$resolver->isPharRelease(),
75-
$phpPath
64+
$pathInfo,
65+
$config,
66+
$resolver->isPharRelease()
7667
);
7768
default:
7869
return new Shell(
79-
new Directory($repositoryPath),
80-
new File($configPath),
81-
new File($captainPath),
82-
$config->getBootstrap(),
83-
$resolver->isPharRelease(),
84-
$phpPath
70+
$pathInfo,
71+
$config,
72+
$resolver->isPharRelease()
8573
);
8674
}
8775
}
88-
89-
/**
90-
* Make sure the given path is absolute
91-
*
92-
* @param string $path
93-
* @return string
94-
*/
95-
private static function toAbsolutePath(string $path): string
96-
{
97-
if (Check::isAbsolutePath($path)) {
98-
return $path;
99-
}
100-
return (string) realpath($path);
101-
}
10276
}

src/Hook/Template/Docker.php

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
namespace CaptainHook\App\Hook\Template;
1515

1616
use CaptainHook\App\CH;
17+
use CaptainHook\App\Config;
1718
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;
1920
use SebastianFeldmann\Camino\Path\Directory;
2021
use SebastianFeldmann\Camino\Path\File;
2122

@@ -33,47 +34,37 @@
3334
class Docker implements Template
3435
{
3536
/**
36-
* Path to the repository root
37+
* All path required for template creation
3738
*
38-
* @var \SebastianFeldmann\Camino\Path\Directory
39+
* @var \CaptainHook\App\Hook\Template\PathInfo
3940
*/
40-
private $repository;
41+
private PathInfo $pathInfo;
4142

4243
/**
43-
* Path to the configuration file
44+
* CaptainHook configuration
4445
*
45-
* @var \SebastianFeldmann\Camino\Path\File
46+
* @var \CaptainHook\App\Config
4647
*/
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;
5549

5650
/**
5751
* Path to the CaptainHook binary script or PHAR
5852
*
5953
* @var string
6054
*/
61-
private $binaryPath;
55+
private string $binaryPath;
6256

6357
/**
6458
* Docker constructor
6559
*
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
7062
*/
71-
public function __construct(Directory $repo, File $config, File $captain, DockerConfig $docker)
63+
public function __construct(PathInfo $pathInfo, Config $config)
7264
{
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();
7768
}
7869

7970
/**
@@ -84,55 +75,48 @@ public function __construct(Directory $repo, File $config, File $captain, Docker
8475
*/
8576
public function getCode(string $hook): string
8677
{
87-
$path2Config = $this->config->getRelativePathFrom($this->repository);
78+
$path2Config = $this->pathInfo->getConfigPath();
8879
$config = $path2Config !== CH::CONFIG ? ' --configuration=' . escapeshellarg($path2Config) : '';
80+
$bootstrap = !empty($this->config->getBootstrap()) ? ' --bootstrap=' . $this->config->getBootstrap() : '';
8981

9082
$lines = [
9183
'#!/bin/sh',
9284
'',
9385
'# installed by CaptainHook ' . CH::VERSION,
9486
'',
95-
$this->dockerConfig->getDockerCommand() . ' ' . $this->binaryPath . ' hook:' . $hook . $config . ' "$@"'
87+
$this->config->getRunExec() . ' '
88+
. $this->binaryPath . ' hook:' . $hook
89+
. $config
90+
. $bootstrap
91+
. ' "$@"'
9692
];
9793
return implode(PHP_EOL, $lines) . PHP_EOL;
9894
}
9995

10096
/**
10197
* Resolves the path to the captainhook binary and returns it
10298
*
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
10599
* @return string
106100
*/
107-
private function resolveBinaryPath(Directory $repo, File $executable): string
101+
private function resolveBinaryPath(): string
108102
{
109103
// 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();
112106
}
113107

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.
121109
// This is caused because docker will always connect to a specific working directory
122110
// where the absolute path will not be recognized.
123111
// E.g.:
124112
// cwd => /project/
125113
// path => /project/vendor/bin/captainhook
126114
// 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
130116
// by default this should return something like ./vendor/bin/captainhook
131-
if ($executable->isChildOf($repo)) {
132-
$pathToExecutable = './' . $executable->getRelativePathFrom($repo);
133-
}
134-
135117
// 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();
137121
}
138122
}

src/Hook/Template/Docker/Config.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/Hook/Template/Local.php

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,71 +13,44 @@
1313

1414
namespace CaptainHook\App\Hook\Template;
1515

16+
use CaptainHook\App\Config;
1617
use CaptainHook\App\Hook\Template;
17-
use SebastianFeldmann\Camino\Path;
18-
use SebastianFeldmann\Camino\Path\Directory;
19-
use SebastianFeldmann\Camino\Path\File;
2018

2119
abstract class Local implements Template
2220
{
2321
/**
24-
* Path to the captainhook configuration
22+
* All template related path information
2523
*
26-
* @var string
24+
* @var \CaptainHook\App\Hook\Template\PathInfo
2725
*/
28-
protected $configPath;
26+
protected PathInfo $pathInfo;
2927

3028
/**
31-
* Original bootstrap option
29+
* CaptainHook configuration
3230
*
33-
* @var string
31+
* @var \CaptainHook\App\Config
3432
*/
35-
protected $bootstrap;
36-
37-
/**
38-
* Path to the captainhook executable
39-
*
40-
* @var string
41-
*/
42-
protected $executablePath;
33+
protected Config $config;
4334

4435
/**
4536
* Is the executable a phar file
4637
*
4738
* @var bool
4839
*/
49-
protected $isPhar;
50-
51-
/**
52-
* Path to the php binary
53-
*
54-
* @var string
55-
*/
56-
protected $phpPath;
40+
protected bool $isPhar;
5741

5842
/**
5943
* Local constructor
6044
*
61-
* @param \SebastianFeldmann\Camino\Path\Directory $repo
62-
* @param \SebastianFeldmann\Camino\Path\File $config
63-
* @param \SebastianFeldmann\Camino\Path\File $captainHook
64-
* @param string $bootstrap
65-
* @param bool $isPhar
66-
* @param string $phpPath
45+
* @param \CaptainHook\App\Hook\Template\PathInfo $pathInfo
46+
* @param \CaptainHook\App\Config $config
47+
* @param bool $isPhar
6748
*/
68-
public function __construct(
69-
Directory $repo,
70-
File $config,
71-
File $captainHook,
72-
string $bootstrap,
73-
bool $isPhar,
74-
string $phpPath
75-
) {
76-
$this->bootstrap = $bootstrap;
77-
$this->configPath = $this->getPathForHookTo($repo, $config);
78-
$this->executablePath = $this->getPathForHookTo($repo, $captainHook);
79-
$this->isPhar = $isPhar;
80-
$this->phpPath = $phpPath;
49+
public function __construct(PathInfo $pathInfo, Config $config, bool $isPhar)
50+
{
51+
$this->pathInfo = $pathInfo;
52+
$this->config = $config;
53+
$this->isPhar = $isPhar;
8154
}
8255

8356
/**
@@ -91,15 +64,6 @@ public function getCode(string $hook): string
9164
return implode(PHP_EOL, $this->getHookLines($hook)) . PHP_EOL;
9265
}
9366

94-
/**
95-
* Return the path to the target path from inside the .git/hooks directory f.e. __DIR__ ../../vendor
96-
*
97-
* @param \SebastianFeldmann\Camino\Path\Directory $repo
98-
* @param \SebastianFeldmann\Camino\Path $target
99-
* @return string
100-
*/
101-
abstract protected function getPathForHookTo(Directory $repo, Path $target): string;
102-
10367
/**
10468
* Return the code for the git hook scripts
10569
*

0 commit comments

Comments
 (0)