1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BernardoSilva\GitHooksInstallerPlugin\Composer; |
4
|
|
|
|
5
|
|
|
use Composer\Installer\LibraryInstaller; |
6
|
|
|
use Composer\Package\PackageInterface; |
7
|
|
|
use Composer\Repository\InstalledRepositoryInterface; |
8
|
|
|
|
9
|
|
|
class Installer extends LibraryInstaller |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* List of supported package types for this plugin. |
13
|
|
|
* View all available types: https://github.com/composer/composer/blob/master/doc/04-schema.md#type |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
public static $supportedTypes = [ |
18
|
|
|
'git-hook', |
19
|
|
|
'library' |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
private $supportedHooks = [ |
23
|
|
|
'applypatch-msg', |
24
|
|
|
'pre-applypatch', |
25
|
|
|
'post-applypatch', |
26
|
|
|
'pre-commit', |
27
|
|
|
'prepare-commit-msg', |
28
|
|
|
'commit-msg', |
29
|
|
|
'post-commit', |
30
|
|
|
'pre-rebase', |
31
|
|
|
'post-checkout', |
32
|
|
|
'post-merge', |
33
|
|
|
'pre-push', |
34
|
|
|
'pre-receive', |
35
|
|
|
'update', |
36
|
|
|
'post-receive', |
37
|
|
|
'post-update', |
38
|
|
|
'push-to-checkout', |
39
|
|
|
'pre-auto-gc', |
40
|
|
|
'post-rewrite', |
41
|
|
|
'sendemail-validate' |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
5 |
|
public function supports($packageType) |
48
|
|
|
{ |
49
|
5 |
|
return in_array($packageType, self::$supportedTypes); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Determines the install path for git hooks, |
54
|
|
|
* |
55
|
|
|
* The installation path is the standard git hooks directory documented here: |
56
|
|
|
* https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks |
57
|
|
|
* |
58
|
|
|
* @param PackageInterface $package |
59
|
|
|
* |
60
|
|
|
* @return string a path relative to the root of the composer.json that is being installed. |
61
|
|
|
*/ |
62
|
3 |
|
public function getInstallPath(PackageInterface $package) |
63
|
|
|
{ |
64
|
3 |
|
if (!$this->supports($package->getType())) { |
65
|
1 |
|
throw new \InvalidArgumentException( |
66
|
|
|
'Unable to install package, git-hook packages only ' |
67
|
1 |
|
. 'support "git-hook", "library" type packages.' |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Allow to LibraryInstaller to resolve the installPath for other packages. |
72
|
2 |
|
if ($package->getType() !== 'git-hook') { |
73
|
1 |
|
return parent::getInstallPath($package); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return $this->getHooksInstallationPath(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Install the plugin to the git hooks path |
81
|
|
|
* |
82
|
|
|
* Note: This method installs the plugin into a temporary directory and then only copy the git-hook |
83
|
|
|
* related files into the git hooks directory to avoid colision with other plugins. |
84
|
|
|
*/ |
85
|
|
|
public function install(InstalledRepositoryInterface $repo, PackageInterface $package) |
86
|
|
|
{ |
87
|
|
|
parent::install($repo, $package); |
88
|
|
|
foreach ($this->getSupportedHooks() as $gitHookName) { |
89
|
|
|
$installedHookFilePath = $this->getInstallPath($package) . DIRECTORY_SEPARATOR . $gitHookName; |
90
|
|
|
|
91
|
|
|
if (file_exists($installedHookFilePath)) { |
92
|
|
|
$hookDestinationPath = $this->getGitHooksPath() . DIRECTORY_SEPARATOR . $gitHookName; |
93
|
|
|
copy($installedHookFilePath, $hookDestinationPath); |
94
|
|
|
chmod($hookDestinationPath, 0755); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// clean up temporary data |
99
|
|
|
exec('rm -rf ' . $this->getHooksInstallationPath()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return the git hooks path. |
104
|
|
|
*/ |
105
|
1 |
|
protected function getGitHooksPath() |
106
|
|
|
{ |
107
|
1 |
|
$gitDir = exec('git rev-parse --git-dir'); |
108
|
1 |
|
if ($gitDir != '.git') { |
109
|
|
|
$gitDir .= DIRECTORY_SEPARATOR . '.git'; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
return $gitDir . DIRECTORY_SEPARATOR . 'hooks'; |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
protected function getHooksInstallationPath() |
116
|
|
|
{ |
117
|
1 |
|
return $this->getGitHooksPath() . DIRECTORY_SEPARATOR . '.GitHooksInstallerPlugin'; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
private function getSupportedHooks() |
124
|
|
|
{ |
125
|
|
|
return $this->supportedHooks; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|