1 | <?php |
||
13 | class GitHooksInstaller extends LibraryInstaller |
||
14 | { |
||
15 | /** |
||
16 | * @inheritdoc |
||
17 | */ |
||
18 | 2 | public function supports($packageType) |
|
22 | |||
23 | /** |
||
24 | * {@inheritDoc} |
||
25 | */ |
||
26 | public function install(InstalledRepositoryInterface $repo, PackageInterface $package) |
||
27 | { |
||
28 | parent::install($repo, $package); |
||
29 | |||
30 | $gitRootPath = realpath($this->getGitRootPath()); |
||
31 | if (!is_dir($gitRootPath)) { |
||
32 | $this->io->writeError(sprintf(' Skipped installation of %s: Not a git repository found on "git-root" path %s', $package->getName(), $gitRootPath)); |
||
33 | return; |
||
34 | } |
||
35 | |||
36 | $originPath = $this->getInstallPath($package); |
||
37 | $targetPath = $this->getGitHooksInstallPath(); |
||
38 | |||
39 | if ($this->io->isVerbose()) { |
||
40 | $this->io->write(sprintf(' Installing git hooks from %s into %s', $originPath, $targetPath)); |
||
41 | } |
||
42 | |||
43 | // throws exception if one of both paths doesn't exists or couldn't be created |
||
44 | $this->filesystem->ensureDirectoryExists($originPath); |
||
45 | $this->filesystem->ensureDirectoryExists($targetPath); |
||
46 | |||
47 | $this->copyGitHooks($originPath, $targetPath); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritDoc} |
||
52 | */ |
||
53 | public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) |
||
54 | { |
||
55 | parent::update($repo, $initial, $target); |
||
56 | |||
57 | $gitRootPath = realpath($this->getGitRootPath()); |
||
58 | if (!is_dir($gitRootPath)) { |
||
59 | $this->io->writeError(sprintf(' Skipped update of %s: Not a git repository found on "git-root" path %s', $target->getName(), $gitRootPath)); |
||
60 | return; |
||
61 | } |
||
62 | |||
63 | $originPath = $this->getInstallPath($initial); |
||
64 | $targetPath = $this->getGitHooksInstallPath(); |
||
65 | |||
66 | if ($this->io->isVerbose()) { |
||
67 | $this->io->write(sprintf(' Updating git hooks from %s into %s', $originPath, $targetPath)); |
||
68 | } |
||
69 | |||
70 | // throws exception if one of both paths doesn't exists or couldn't be created |
||
71 | $this->filesystem->ensureDirectoryExists($originPath); |
||
72 | $this->filesystem->ensureDirectoryExists($targetPath); |
||
73 | |||
74 | $this->copyGitHooks($originPath, $targetPath, true); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * {@inheritDoc} |
||
79 | */ |
||
80 | public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) |
||
81 | { |
||
82 | parent::uninstall($repo, $package); |
||
83 | |||
84 | $gitRootPath = realpath($this->getGitRootPath()); |
||
85 | if (!is_dir($gitRootPath)) { |
||
86 | $this->io->writeError(sprintf(' Skipped update of %s: Not a git repository found on "git-root" path %s', $package->getName(), $gitRootPath)); |
||
87 | return; |
||
88 | } |
||
89 | |||
90 | $originPath = $this->getInstallPath($package); |
||
91 | $targetPath = $this->getGitHooksInstallPath(); |
||
92 | |||
93 | if ($this->io->isVerbose()) { |
||
94 | $this->io->write(sprintf(' Uninstalling git hooks from %s into %s', $originPath, $targetPath)); |
||
95 | } |
||
96 | |||
97 | $this->removeGitHooks($originPath, $targetPath); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param string $sourcePath |
||
102 | * @param string $targetPath |
||
103 | * @param bool $isUpdate |
||
104 | */ |
||
105 | private function copyGitHooks($sourcePath, $targetPath, $isUpdate = false) |
||
138 | |||
139 | /** |
||
140 | * @param string $sourcePath |
||
141 | * @param string $targetPath |
||
142 | */ |
||
143 | private function removeGitHooks($sourcePath, $targetPath) |
||
160 | |||
161 | /** |
||
162 | * Returns the installation path of the Git hooks |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | private function getGitHooksInstallPath() |
||
170 | |||
171 | /** |
||
172 | * Returns the git root path |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | private function getGitRootPath() |
||
183 | } |
||
184 |