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
|
|
|
namespace CaptainHook\App\Console\Command; |
11
|
|
|
|
12
|
|
|
use CaptainHook\App\CH; |
13
|
|
|
use CaptainHook\App\Console\IOUtil; |
14
|
|
|
use CaptainHook\App\Hook\Template; |
15
|
|
|
use CaptainHook\App\Runner\Installer; |
16
|
|
|
use RuntimeException; |
17
|
|
|
use SebastianFeldmann\Git\Repository; |
18
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Input\InputOption; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Install |
25
|
|
|
* |
26
|
|
|
* @package CaptainHook |
27
|
|
|
* @author Sebastian Feldmann <[email protected]> |
28
|
|
|
* @link https://github.com/captainhookphp/captainhook |
29
|
|
|
* @since Class available since Release 0.9.0 |
30
|
|
|
*/ |
31
|
|
|
class Install extends Base |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Configure the command |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
7 |
|
protected function configure() : void |
39
|
|
|
{ |
40
|
7 |
|
$this->setName('install') |
41
|
7 |
|
->setDescription('Install git hooks') |
42
|
7 |
|
->setHelp('This command will install the git hooks to your .git directory') |
43
|
7 |
|
->addArgument('hook', InputArgument::OPTIONAL, 'Hook you want to install') |
44
|
7 |
|
->addOption( |
45
|
7 |
|
'configuration', |
46
|
7 |
|
'c', |
47
|
7 |
|
InputOption::VALUE_OPTIONAL, |
48
|
7 |
|
'Path to your json configuration', |
49
|
7 |
|
getcwd() . DIRECTORY_SEPARATOR . CH::CONFIG |
50
|
|
|
) |
51
|
7 |
|
->addOption( |
52
|
7 |
|
'force', |
53
|
7 |
|
'f', |
54
|
7 |
|
InputOption::VALUE_NONE, |
55
|
7 |
|
'Force to overwrite existing hooks' |
56
|
|
|
) |
57
|
7 |
|
->addOption( |
58
|
7 |
|
'git-directory', |
59
|
7 |
|
'g', |
60
|
7 |
|
InputOption::VALUE_OPTIONAL, |
61
|
7 |
|
'Path to your .git directory' |
62
|
|
|
) |
63
|
7 |
|
->addOption( |
64
|
7 |
|
'run-mode', |
65
|
7 |
|
'm', |
66
|
7 |
|
InputOption::VALUE_OPTIONAL, |
67
|
7 |
|
'Git hook run mode [local|docker]' |
68
|
|
|
) |
69
|
7 |
|
->addOption( |
70
|
7 |
|
'run-exec', |
71
|
7 |
|
'e', |
72
|
7 |
|
InputOption::VALUE_OPTIONAL, |
73
|
7 |
|
'The Docker command to start your container e.g. \'docker exec CONTAINER\'' |
74
|
|
|
); |
75
|
7 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Execute the command |
79
|
|
|
* |
80
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
81
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
82
|
|
|
* @return int|null |
83
|
|
|
* @throws \CaptainHook\App\Exception\InvalidHookName |
84
|
|
|
*/ |
85
|
6 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
86
|
|
|
{ |
87
|
6 |
|
$io = $this->getIO($input, $output); |
88
|
6 |
|
$config = $this->getConfig(IOUtil::argToString($input->getOption('configuration')), true); |
89
|
|
|
|
90
|
|
|
// figure out where the git repository is located, setting needs to include the '.git' directory |
91
|
|
|
// the cli option supersedes all other settings |
92
|
|
|
// 1. command option --git-directory |
93
|
|
|
// 2. captainhook.json config, git-directory value |
94
|
|
|
// 3. default current working directory |
95
|
|
|
// same applies for run-mode and run-command OPTION > CONFIG > DEFAULT |
96
|
5 |
|
$gitDir = $this->getOpt(IOUtil::argToString($input->getOption('git-directory')), $config->getGitDirectory()); |
97
|
5 |
|
$runMode = $this->getOpt(IOUtil::argToString($input->getOption('run-mode')), $config->getRunMode()); |
98
|
5 |
|
$runCmd = $this->getOpt(IOUtil::argToString($input->getOption('run-exec')), $config->getRunExec()); |
99
|
5 |
|
$repo = new Repository(dirname($gitDir)); |
100
|
|
|
|
101
|
4 |
|
if ($runMode === Template::DOCKER && empty($runCmd)) { |
102
|
1 |
|
throw new RuntimeException( |
103
|
1 |
|
'Option "run-exec" missing for run-mode docker.' |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
3 |
|
$installer = new Installer($io, $config, $repo); |
108
|
3 |
|
$installer->setForce(IOUtil::argToBool($input->getOption('force'))) |
109
|
3 |
|
->setHook(IOUtil::argToString($input->getArgument('hook'))) |
110
|
3 |
|
->setTemplate(Template\Builder::build($input, $config, $repo)) |
111
|
3 |
|
->run(); |
112
|
|
|
|
113
|
3 |
|
return 0; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Choose option value over config value |
118
|
|
|
* |
119
|
|
|
* @param string $optionValue |
120
|
|
|
* @param string $configValue |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
5 |
|
protected function getOpt(string $optionValue, string $configValue) : string |
124
|
|
|
{ |
125
|
5 |
|
return !empty($optionValue) ? $optionValue : $configValue; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|