Skip to content
This repository was archived by the owner on Dec 8, 2017. It is now read-only.

Commit 4aac5f9

Browse files
author
Samuel Parkinson
committed
Improvments to the hook install command.
1 parent d141094 commit 4aac5f9

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

hooks/php-pre-commit.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use StaticReview\Review\General\NoCommitTagReview;
2525
use StaticReview\Review\PHP\PhpLeadingLineReview;
2626
use StaticReview\Review\PHP\PhpLintReview;
27+
use StaticReview\Review\Config\ComposerConfigReview;
2728

2829
$reporter = new Reporter();
2930

src/Command/HookInstallCommand.php

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,39 +27,63 @@ protected function configure()
2727

2828
$this->setDescription('Symlink a hook to the given target.');
2929

30-
$this->addArgument('hook', InputArgument::REQUIRED, 'The hook to link, either a path to a file or the filename of a hook in the hooks folder.')
31-
->addArgument('target', InputArgument::REQUIRED, 'The target location, including the filename (e.g. .git/hooks/pre-commit).');
30+
$this->addArgument('target', InputArgument::REQUIRED, 'The hook to link, either a path to a file or the filename of a hook in the hooks folder.')
31+
->addArgument('link', InputArgument::REQUIRED, 'The target location, including the filename (e.g. .git/hooks/pre-commit).');
3232

3333
$this->addOption('force', 'f', InputOption::VALUE_NONE, 'Overrite any existing files at the symlink target.');
3434
}
3535

3636
protected function execute(InputInterface $input, OutputInterface $output)
3737
{
38-
$source = realpath(__DIR__ . '/../../hooks/') . '/' . $input->getArgument('hook') . '.php';
38+
$force = $input->getOption('force');
39+
$target = $this->getTargetPath($input);
40+
$link = $input->getArgument('link');
3941

40-
if (! file_exists($source) && file_exists($input->getArgument('hook'))) {
41-
$source = $input->getArgument('hook');
42-
} else {
43-
$error = sprintf('<error>The hook %s does not exist!</error>', $input->getArgument('hook'));
44-
$output->writeln($error);
45-
exit(1);
42+
if ($output->isVeryVerbose()) {
43+
$message = sprintf('<info>Using %s for the hook target.</info>', $target);
44+
$output->writeln($message);
45+
46+
$message = sprintf('<info>Using %s for the hook path.</info>', $link);
47+
$output->writeln($message);
4648
}
4749

48-
$target = $input->getArgument('target');
49-
$force = $input->getOption('force');
50+
if (file_exists($link) && $force) {
51+
unlink($link);
5052

51-
if ($force && file_exists($target)) {
52-
unlink($target);
53-
$output->writeln('Removed existing file.');
53+
$message = sprintf('<info>Removed existing file at %s.</info>', $link);
54+
$output->writeln($message);
5455
}
5556

56-
if (file_exists($source)
57-
&& (! file_exists($target) || $force)) {
58-
symlink($source, $target);
59-
chmod($target, 0755);
57+
if (! file_exists($link) || $force) {
58+
symlink($target, $link);
59+
chmod($link, 0755);
6060
$output->writeln('Symlink created.');
6161
} else {
62-
$output->writeln('<error>A file at the target already exists.</error>');
62+
$message = sprintf('<error>A file at %s already exists.</error>', $link);
63+
$output->writeln($message);
64+
exit(1);
6365
}
6466
}
67+
68+
/**
69+
* @param $input InputInterface
70+
* @return string
71+
*/
72+
protected function getTargetPath(InputInterface $input)
73+
{
74+
if (file_exists($input->getArgument('target'))) {
75+
$target = realpath($input->getArgument('target'));
76+
} else {
77+
$path = '%s/%s.php';
78+
$target = sprintf($path, realpath(__DIR__ . '/../../hooks/'), $input->getArgument('target'));
79+
}
80+
81+
if (! file_exists($target)) {
82+
$error = sprintf('<error>The hook %s does not exist!</error>', $target);
83+
$output->writeln($error);
84+
exit(1);
85+
}
86+
87+
return $target;
88+
}
6589
}

0 commit comments

Comments
 (0)