From f863b9df32e5aad7c2965e28acc9bbf64f993018 Mon Sep 17 00:00:00 2001 From: Andrew Roslik Date: Mon, 19 Jan 2015 03:39:24 +0200 Subject: [PATCH 1/4] - Improved PHP validator. - Added console option to set PHP binary file. --- .../PreCommit/Composer/Command/Install.php | 53 +++++++++++++------ 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/LibHooks/lib/PreCommit/Composer/Command/Install.php b/LibHooks/lib/PreCommit/Composer/Command/Install.php index 42d3351..2f065e1 100644 --- a/LibHooks/lib/PreCommit/Composer/Command/Install.php +++ b/LibHooks/lib/PreCommit/Composer/Command/Install.php @@ -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; } @@ -74,7 +78,7 @@ public function execute(InputInterface $input, OutputInterface $output) $output, $input, $hooksDir, $this->getTargetFiles($input, $output), - $this->askPhpPath($output), + $this->askPhpPath($input, $output), $this->getRunnerFile() ); } catch (Exception $e) { @@ -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; + }; } /** From dcba45ab5d4175e15c47e34951e7f14d1f24df72 Mon Sep 17 00:00:00 2001 From: Andrew Roslik Date: Mon, 19 Jan 2015 03:47:48 +0200 Subject: [PATCH 2/4] - Added option project-dir to set VCS directory from console. --- .../Composer/Command/CommandAbstract.php | 30 +++++++++++-------- .../PreCommit/Composer/Command/Install.php | 2 +- .../lib/PreCommit/Composer/Command/Remove.php | 2 +- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/LibHooks/lib/PreCommit/Composer/Command/CommandAbstract.php b/LibHooks/lib/PreCommit/Composer/Command/CommandAbstract.php index a472205..ed0d5fd 100644 --- a/LibHooks/lib/PreCommit/Composer/Command/CommandAbstract.php +++ b/LibHooks/lib/PreCommit/Composer/Command/CommandAbstract.php @@ -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() @@ -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, '\\/'); } diff --git a/LibHooks/lib/PreCommit/Composer/Command/Install.php b/LibHooks/lib/PreCommit/Composer/Command/Install.php index 2f065e1..d33915d 100644 --- a/LibHooks/lib/PreCommit/Composer/Command/Install.php +++ b/LibHooks/lib/PreCommit/Composer/Command/Install.php @@ -72,7 +72,7 @@ public function execute(InputInterface $input, OutputInterface $output) { try { $hooksDir = $this->getHooksDir( - $output, $this->askProjectDir($output) + $output, $this->askProjectDir($input, $output) ); $this->createHooks( $output, $input, diff --git a/LibHooks/lib/PreCommit/Composer/Command/Remove.php b/LibHooks/lib/PreCommit/Composer/Command/Remove.php index aa6f3a2..6a26670 100644 --- a/LibHooks/lib/PreCommit/Composer/Command/Remove.php +++ b/LibHooks/lib/PreCommit/Composer/Command/Remove.php @@ -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); From 78e2e59d81ce8bc7382467220beda5945537fb9d Mon Sep 17 00:00:00 2001 From: Andrew Roslik Date: Mon, 19 Jan 2015 04:03:45 +0200 Subject: [PATCH 3/4] - Updated README.md. --- README.md | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 457d9c9..2c6f6e2 100644 --- a/README.md +++ b/README.md @@ -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 @@ -35,6 +35,16 @@ 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 @@ -42,18 +52,23 @@ NOTE: Tested on Windows. Feel free [to put](../../issues/new "Add a new issue") 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. @@ -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. From 4b3a7e8b1fdb189e2b85fb75b7a057c7f95f3f79 Mon Sep 17 00:00:00 2001 From: Andrew Roslik Date: Mon, 19 Jan 2015 04:04:35 +0200 Subject: [PATCH 4/4] - Updated version. --- LibHooks/config.xml | 2 +- LibHooks/lib/PreCommit/Composer/Application.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/LibHooks/config.xml b/LibHooks/config.xml index 7d8059c..9bc21b6 100644 --- a/LibHooks/config.xml +++ b/LibHooks/config.xml @@ -1,6 +1,6 @@ - 1.6.4 + 1.6.5 pre-commit commit-msg diff --git a/LibHooks/lib/PreCommit/Composer/Application.php b/LibHooks/lib/PreCommit/Composer/Application.php index 6f25bbb..39e09f0 100644 --- a/LibHooks/lib/PreCommit/Composer/Application.php +++ b/LibHooks/lib/PreCommit/Composer/Application.php @@ -17,7 +17,7 @@ class Application extends BaseApplication * * @see LibHooks/config.xml */ - const VERSION = '1.6.4'; + const VERSION = '1.6.5'; /** * Logo