Skip to content

Commit

Permalink
Merge branch 'release/1.6.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
andkirby committed Jan 19, 2015
2 parents b530499 + 4b3a7e8 commit 2616dad
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 37 deletions.
2 changes: 1 addition & 1 deletion LibHooks/config.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config>
<version>1.6.4</version>
<version>1.6.5</version>
<supported_hooks>
<hook>pre-commit</hook>
<hook>commit-msg</hook>
Expand Down
2 changes: 1 addition & 1 deletion LibHooks/lib/PreCommit/Composer/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Application extends BaseApplication
*
* @see LibHooks/config.xml
*/
const VERSION = '1.6.4';
const VERSION = '1.6.5';

/**
* Logo
Expand Down
30 changes: 17 additions & 13 deletions LibHooks/lib/PreCommit/Composer/Command/CommandAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ abstract protected function configureCommand();
*/
protected function configureInput()
{
$this->addOption(
'project-dir', '-d', InputOption::VALUE_REQUIRED,
'Path to project (VCS) root directory.'
);
$this->addOption(
'hook', null, InputOption::VALUE_REQUIRED,
$this->getCustomHookOptionDescription()
Expand Down Expand Up @@ -199,32 +203,32 @@ protected function getHooksDir(OutputInterface $output, $projectDir)
/**
* Ask about GIT project root dir
*
* @param InputInterface $input
* @param OutputInterface $output
* @return array
*/
protected function askProjectDir(OutputInterface $output)
protected function askProjectDir(InputInterface $input, OutputInterface $output)
{
$dir = $this->getCommandDir();
$dir = $input->getOption('project-dir');
if (!$dir) {
$dir = $this->getCommandDir();
}

$validator = function ($dir) {
$dir = rtrim($dir, '\\/');
return is_dir($dir . '/.git');
};

if ($validator($dir)) {
return $dir;
}

do {
$dir = $this->getDialog()->ask(
$output, "Please set your root project directory [$dir]: ", $dir
);
if (!$validator($dir)) {
while (!$dir || !$validator($dir)) {
if ($dir) {
$output->writeln(
'Sorry, selected directory does not contain ".git" directory.'
);
$dir = null;
}
} while (!$dir);
$dir = $this->getDialog()->ask(
$output, "Please set your root project directory [$dir]: ", $dir
);
}

return rtrim($dir, '\\/');
}
Expand Down
55 changes: 39 additions & 16 deletions LibHooks/lib/PreCommit/Composer/Command/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ protected function configureInput()
'overwrite', '-w', InputOption::VALUE_NONE,
'Overwrite exist hook files.'
);
$this->addOption(
'php-binary', '-p', InputOption::VALUE_REQUIRED,
'Path to PHP binary file.'
);
return $this;
}

Expand All @@ -68,13 +72,13 @@ public function execute(InputInterface $input, OutputInterface $output)
{
try {
$hooksDir = $this->getHooksDir(
$output, $this->askProjectDir($output)
$output, $this->askProjectDir($input, $output)
);
$this->createHooks(
$output, $input,
$hooksDir,
$this->getTargetFiles($input, $output),
$this->askPhpPath($output),
$this->askPhpPath($input, $output),
$this->getRunnerFile()
);
} catch (Exception $e) {
Expand Down Expand Up @@ -153,31 +157,50 @@ protected function createHookFile(OutputInterface $output, InputInterface $input
/**
* Ask about PHP executable file
*
* @param InputInterface $input
* @param OutputInterface $output
* @return array
*/
protected function askPhpPath(OutputInterface $output)
protected function askPhpPath(InputInterface $input, OutputInterface $output)
{
$validator = function ($file) {
return is_file($file);
};
$file = $this->getSystemPhpPath();
$validator = $this->getPhpValidator();

if ($validator($file)) {
return $file;
$file = $input->getOption('php-binary');
if (!$file) {
$file = $this->getSystemPhpPath();
}

do {
while (!$file || !$validator($file, $output)) {
if ($file) {
$output->writeln('Given PHP executable file is not valid.');
}
$file = $this->getDialog()->ask(
$output, "Please set your PHP executable file [$file]: ", $file
);
if (!$validator($file)) {
$output->writeln('Given PHP executable file does not exists.');
$file = null;
}
} while (!$file);
}

return rtrim($file, '\\/');
return $file;
}

/**
* Get PHP binary file validator
*
* @return callable
*/
protected function getPhpValidator()
{
return function ($file, OutputInterface $output = null) {
if (is_file($file)) {
$test = `$file -r "echo 'Test passed.';" 2>&1`;
if ($output && $output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
$output->writeln(
'PHP test output: ' . PHP_EOL . $test
);
}
return 0 === strpos($test, 'Test passed.');
}
return false;
};
}

/**
Expand Down
2 changes: 1 addition & 1 deletion LibHooks/lib/PreCommit/Composer/Command/Remove.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function execute(InputInterface $input, OutputInterface $output)
{
try {
$hooksDir = $this->getHooksDir(
$output, $this->askProjectDir($output)
$output, $this->askProjectDir($input, $output)
);
$files = $this->getTargetFiles($input, $output);
$status = $this->removeHookFiles($output, $hooksDir, $files);
Expand Down
26 changes: 21 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $ composer require andkirby/commithook
```
### Set up via command line

This feature is available since v1.6.3.
This feature is available since v1.6.3. _(Simple install command `commithook-install` is available since v1.6.0.)_

#### `commithook install`: Generate files

Expand All @@ -35,25 +35,40 @@ PHP CommitHook files have been created in 'd:/home/my-project/.git/hooks'.
```
If system couldn't find path to your executable PHP file it will ask about it.

Since PHP 5.4 console should not ask your about PHP binary file. Anyway you may set up path to your PHP binary file.
Also you may set path your project/VCS root.
```shell
$ commithook install --php-binary=d:/s/php/php.exe --project-dir=d:/home/my-project
```
Or short version:
```shell
$ commithook install -pd:/s/php/php.exe -dd:/home/my-project
```

NOTE: Tested on Windows. Feel free [to put](../../issues/new "Add a new issue") your faced issues on [the project issues page](../../issues "Issues").

##### Extra Options
`commithook install` has options.
Please take a look them via command `commithook install -h`.
```shell
$ commithook install -h
...
[...]
Options:
--project-dir (-d) Set path to project (VCS) root directory.
--hook Set specific hook file to install.
--commit-msg Set 'commit-msg' hook file to install.
--pre-commit Set 'pre-commit' hook file to install.
--overwrite (-w) Overwrite exist hook files.
...
--php-binary (-p) Set path to PHP binary file.
[...]
```

#### `commithook remove`: Remove Hooks Files
To remove CommitHook files from your project you may use command `commithook remove`.
Options list the same but without `--overwrite`.
To remove CommitHook files from your project you may use command:
```shell
$ commithook remove
```
Options list the same but without `--overwrite` and `--php-binary`.

#### Set up Composer vendor/bin directory
If you using GitBash or Unix system please be sure that your shell can find files in global vendor directory.
Expand Down Expand Up @@ -110,6 +125,7 @@ In such case it will merge all files in the XML node "additional_config". There
The last one can be added into a project and might be used by all developers. PROJECT_DIR - is your project directory where from CommitHOOK has been run.

# Release notes
- v1.6.5 Added new options command `--php-binary|-b` and `--project-dir|-d`. Improved PHP file validator.
- v1.6.4 Pushed tests to use PSR-4 autoload standard and to namespaces usage. Pushed code to use `bin/runner.php` file. `LibHooks/runner.php` is deprecated. Composer package require at least PHP 5.3.x version.
- v1.6.3 Improved installer. Added CommitHook files remover.
- v1.6.2 (alpha) Implemented application console usage.
Expand Down

0 comments on commit 2616dad

Please sign in to comment.