|
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
|
|
|
namespace CaptainHook\App\Console\Command; |
|
13
|
|
|
|
|
14
|
|
|
use CaptainHook\App\Config; |
|
15
|
|
|
use CaptainHook\App\Console\IOUtil; |
|
16
|
|
|
use CaptainHook\App\Hook\Template; |
|
17
|
|
|
use CaptainHook\App\Runner\Installer; |
|
18
|
|
|
use Exception; |
|
19
|
|
|
use RuntimeException; |
|
20
|
|
|
use SebastianFeldmann\Git\Repository; |
|
21
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
22
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
23
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
24
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class Install |
|
28
|
|
|
* |
|
29
|
|
|
* @package CaptainHook |
|
30
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
31
|
|
|
* @link https://github.com/captainhook-git/captainhook |
|
32
|
|
|
* @since Class available since Release 0.9.0 |
|
33
|
|
|
*/ |
|
34
|
|
|
class Install extends RepositoryAware |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Configure the command |
|
38
|
|
|
* |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
16 |
|
protected function configure(): void |
|
42
|
|
|
{ |
|
43
|
16 |
|
parent::configure(); |
|
44
|
16 |
|
$this->setName('install') |
|
45
|
16 |
|
->setDescription('Install hooks to your .git/hooks directory') |
|
46
|
16 |
|
->setHelp('Install git hooks to your .git/hooks directory') |
|
47
|
16 |
|
->addArgument( |
|
48
|
16 |
|
'hook', |
|
49
|
16 |
|
InputArgument::OPTIONAL, |
|
50
|
16 |
|
'Limit the hooks you want to install. ' . |
|
51
|
16 |
|
'You can specify multiple hooks with comma as delimiter. ' . |
|
52
|
16 |
|
'By default all hooks get installed' |
|
53
|
16 |
|
) |
|
54
|
16 |
|
->addOption( |
|
55
|
16 |
|
'only-enabled', |
|
56
|
16 |
|
null, |
|
57
|
16 |
|
InputOption::VALUE_NONE, |
|
58
|
16 |
|
'Limit the hooks you want to install to those enabled in your conf. ' . |
|
59
|
16 |
|
'By default all hooks get installed' |
|
60
|
16 |
|
) |
|
61
|
16 |
|
->addOption( |
|
62
|
16 |
|
'force', |
|
63
|
16 |
|
'f', |
|
64
|
16 |
|
InputOption::VALUE_NONE, |
|
65
|
16 |
|
'Force install without confirmation' |
|
66
|
16 |
|
) |
|
67
|
16 |
|
->addOption( |
|
68
|
16 |
|
'skip-existing', |
|
69
|
16 |
|
's', |
|
70
|
16 |
|
InputOption::VALUE_NONE, |
|
71
|
16 |
|
'Do not overwrite existing hooks' |
|
72
|
16 |
|
) |
|
73
|
16 |
|
->addOption( |
|
74
|
16 |
|
'move-existing-to', |
|
75
|
16 |
|
null, |
|
76
|
16 |
|
InputOption::VALUE_OPTIONAL, |
|
77
|
16 |
|
'Move existing hooks to given directory' |
|
78
|
16 |
|
) |
|
79
|
16 |
|
->addOption( |
|
80
|
16 |
|
'bootstrap', |
|
81
|
16 |
|
'b', |
|
82
|
16 |
|
InputOption::VALUE_OPTIONAL, |
|
83
|
16 |
|
'Path to composers vendor/autoload.php' |
|
84
|
16 |
|
) |
|
85
|
16 |
|
->addOption( |
|
86
|
16 |
|
'run-mode', |
|
87
|
16 |
|
'm', |
|
88
|
16 |
|
InputOption::VALUE_OPTIONAL, |
|
89
|
16 |
|
'Git hook run mode [php|shell|docker]' |
|
90
|
16 |
|
) |
|
91
|
16 |
|
->addOption( |
|
92
|
16 |
|
'run-exec', |
|
93
|
16 |
|
'e', |
|
94
|
16 |
|
InputOption::VALUE_OPTIONAL, |
|
95
|
16 |
|
'The Docker command to start your container e.g. \'docker exec CONTAINER\'' |
|
96
|
16 |
|
) |
|
97
|
16 |
|
->addOption( |
|
98
|
16 |
|
'run-path', |
|
99
|
16 |
|
'p', |
|
100
|
16 |
|
InputOption::VALUE_OPTIONAL, |
|
101
|
16 |
|
'The path to the CaptainHook executable \'/usr/bin/captainhook\'' |
|
102
|
16 |
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Execute the command |
|
107
|
|
|
* |
|
108
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
|
109
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
|
110
|
|
|
* @return int |
|
111
|
|
|
* @throws \Exception |
|
112
|
|
|
*/ |
|
113
|
14 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
114
|
|
|
{ |
|
115
|
|
|
try { |
|
116
|
14 |
|
$args = ['git-directory', 'run-mode', 'run-exec', 'run-path', 'bootstrap']; |
|
117
|
14 |
|
$io = $this->getIO($input, $output); |
|
118
|
14 |
|
$config = $this->createConfig($input, true, $args); |
|
119
|
13 |
|
$repo = $this->createRepository(dirname($config->getGitDirectory())); |
|
120
|
12 |
|
$template = $this->createTemplate($config, $repo); |
|
121
|
|
|
|
|
122
|
11 |
|
$this->determineVerbosity($output, $config); |
|
123
|
|
|
|
|
124
|
11 |
|
$installer = new Installer($io, $config, $repo, $template); |
|
125
|
11 |
|
$installer->setHook(IOUtil::argToString($input->getArgument('hook'))) |
|
126
|
11 |
|
->setForce(IOUtil::argToBool($input->getOption('force'))) |
|
127
|
11 |
|
->setSkipExisting(IOUtil::argToBool($input->getOption('skip-existing'))) |
|
128
|
11 |
|
->setMoveExistingTo(IOUtil::argToString($input->getOption('move-existing-to'))) |
|
129
|
11 |
|
->setOnlyEnabled(IOUtil::argToBool($input->getOption('only-enabled'))) |
|
130
|
11 |
|
->run(); |
|
131
|
|
|
|
|
132
|
8 |
|
return 0; |
|
133
|
6 |
|
} catch (Exception $e) { |
|
134
|
6 |
|
return $this->crash($output, $e); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Create the template to generate the hook source code |
|
140
|
|
|
* |
|
141
|
|
|
* @param \CaptainHook\App\Config $config |
|
142
|
|
|
* @param \SebastianFeldmann\Git\Repository $repo |
|
143
|
|
|
* @return \CaptainHook\App\Hook\Template |
|
144
|
|
|
*/ |
|
145
|
12 |
|
private function createTemplate(Config $config, Repository $repo): Template |
|
146
|
|
|
{ |
|
147
|
|
|
if ( |
|
148
|
12 |
|
$config->getRunConfig()->getMode() === Template::DOCKER |
|
149
|
12 |
|
&& empty($config->getRunConfig()->getDockerCommand()) |
|
150
|
|
|
) { |
|
151
|
1 |
|
throw new RuntimeException('Run "exec" option missing for run-mode docker.'); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
11 |
|
return Template\Builder::build($config, $repo, $this->resolver); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|