Skip to content

Commit

Permalink
Code Improvements (#23)
Browse files Browse the repository at this point in the history
* Add 'windows_os' function

- DRY ( Don't repeat yourself concept )

* Code cleanup

* Add 'mkdir_if_not_exist' function

- DRY ( Don't repeat yourself concept )

* Add 'git_version' function

- Avoid updating the application version in future releases

* Sort functions

* Rename functions

- 'windos_os()' to 'is_windows()'
- 'mkdir_if_not_exist()' to 'create_hooks_dir()'
-  Replace mkdir hardcode with 'create_hooks_dir()' function

* Update 'create_hooks_dir()' function

- String interpolation within function
  • Loading branch information
joaorobertopb authored and BrainMaestro committed Jun 1, 2018
1 parent 283dd5b commit d3ba750
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cghooks
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use BrainMaestro\GitHooks\Commands\ListCommand;
use BrainMaestro\GitHooks\Commands\HookCommand;
use Symfony\Component\Console\Application;

$application = new Application('Composer Git Hooks', 'v2.2.0');
$application = new Application('Composer Git Hooks', git_version());

$hooks = Hook::getValidHooks($dir);
$application->add(new AddCommand($hooks));
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
"autoload": {
"psr-4": {
"BrainMaestro\\GitHooks\\": "src/"
}
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
7 changes: 2 additions & 5 deletions src/Commands/AddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$gitDir = $input->getOption('git-dir');
$force = $input->getOption('force');
$forceWindows = $input->getOption('force-win');
$hookDir = "{$gitDir}/hooks";

if (! is_dir($hookDir)) {
mkdir($hookDir, 0700, true);
}
create_hooks_dir($gitDir);

foreach ($this->hooks as $hook => $script) {
$filename = "{$gitDir}/hooks/{$hook}";
Expand All @@ -51,7 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (! $force && $fileExists) {
$output->writeln("<comment>{$hook} already exists</comment>");
} else {
if ($forceWindows || strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
if ($forceWindows || is_windows()) {
// On windows we need to add a SHEBANG
// See: https://github.com/BrainMaestro/composer-git-hooks/issues/7
$script = '#!/bin/bash' . PHP_EOL . $script;
Expand Down
5 changes: 3 additions & 2 deletions src/Commands/RemoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
unlink($filename);
$output->writeln("Removed <info>{$hook}</info> hook");
unset($lockFileHooks[$hook]);
} else {
$output->writeln("<error>{$hook} hook does not exist</error>");
continue;
}

$output->writeln("<error>{$hook} hook does not exist</error>");
}

file_put_contents(Hook::LOCK_FILE, json_encode(array_keys($lockFileHooks)));
Expand Down
9 changes: 3 additions & 6 deletions src/Commands/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$gitDir = $input->getOption('git-dir');
$forceWindows = $input->getOption('force-win');
$hookDir = "{$gitDir}/hooks";

if (! is_dir($hookDir)) {
mkdir($hookDir, 0700, true);
}

create_hooks_dir($gitDir);

foreach ($this->hooks as $hook => $script) {
$filename = "{$gitDir}/hooks/{$hook}";

$operation = file_exists($filename) ? 'Updated' : 'Added';

if ($forceWindows || strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
if ($forceWindows || is_windows()) {
// On windows we need to add a SHEBANG
// See: https://github.com/BrainMaestro/composer-git-hooks/issues/7
$script = '#!/bin/bash' . PHP_EOL . $script;
Expand Down
2 changes: 1 addition & 1 deletion src/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function getValidHooks($dir)
$validHooks = [];

foreach ($hooks as $hook => $script) {
if (array_key_exists($hook, self::getHooks())) {
if (self::isValidHook($hook)) {
$validHooks[$hook] = $script;
}
}
Expand Down
48 changes: 48 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use Symfony\Component\Process\Process;

if (! function_exists('create_hooks_dir')) {
/**
* Create hook directory if not exists.
*
* @param string $dir
* @param int $mode
* @param bool $recursive
*
* @return void
*/
function create_hooks_dir($dir, $mode = 0700, $recursive = true)
{
if (! is_dir("{$dir}/hooks")) {
mkdir("{$dir}/hooks", $mode, $recursive);
}
}
}

if (! function_exists('git_version')) {
/**
* Get latest git tag version.
*
* @return string
*/
function git_version()
{
$process = new Process('git describe --tags $(git rev-list --tags --max-count=1)');
$process->run();

return trim($process->getOutput()) ?: 'unreleased';
}
}

if (! function_exists('is_windows')) {
/**
* Determine whether the current environment is Windows based.
*
* @return bool
*/
function is_windows()
{
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}
}
4 changes: 2 additions & 2 deletions tests/AddCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function it_adds_hooks_that_do_not_already_exist()
*/
public function it_adds_shebang_to_hooks_on_windows()
{
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
if (! is_windows()) {
$this->markTestSkipped('This test is only relevant on windows. You\'re running Linux.');
}

Expand Down Expand Up @@ -130,7 +130,7 @@ public function it_ignores_the_hook_lock_file_if_the_ignore_lock_option_is_passe
public function it_uses_a_different_git_path_if_specified()
{
$gitDir = 'test-git-dir';
mkdir("{$gitDir}/hooks", 0700, true);
create_hooks_dir($gitDir);
$this->commandTester->execute(['--git-dir' => $gitDir]);

foreach (array_keys(self::$hooks) as $hook) {
Expand Down
2 changes: 1 addition & 1 deletion tests/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function it_uses_a_different_git_path_if_specified()
{
$gitDir = 'test-git-dir';

mkdir("{$gitDir}/hooks", 0777, true);
create_hooks_dir($gitDir, 0777);

self::createHooks($gitDir);

Expand Down
4 changes: 1 addition & 3 deletions tests/PrepareHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public static function tearDownAfterClass()

public static function createHooks($gitDir = '.git')
{
if (!is_dir("{$gitDir}/hooks")) {
mkdir("{$gitDir}/hooks", 0777, true);
}
create_hooks_dir($gitDir, 0777);

foreach (self::$hooks as $hook => $script) {
file_put_contents("{$gitDir}/hooks/{$hook}", $script);
Expand Down
2 changes: 1 addition & 1 deletion tests/RemoveCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function it_uses_a_different_git_path_if_specified()
{
$gitDir = 'test-git-dir';

mkdir("{$gitDir}/hooks", 0777, true);
create_hooks_dir($gitDir, 0777);

self::createHooks($gitDir);
$this->assertFalse(self::isDirEmpty("{$gitDir}/hooks"));
Expand Down
5 changes: 2 additions & 3 deletions tests/UpdateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function it_updates_hooks_that_already_exist()

public function it_adds_shebang_to_hooks_on_windows()
{
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
if (! is_windows()) {
$this->markTestSkipped('This test is only relevant on windows. You\'re running Linux.');
}

Expand All @@ -71,7 +71,7 @@ public function it_uses_a_different_git_path_if_specified()
{
$gitDir = 'test-git-dir';

mkdir("{$gitDir}/hooks", 0777, true);
create_hooks_dir($gitDir, 0777);

$this->commandTester->execute(['--git-dir' => $gitDir]);

Expand Down Expand Up @@ -104,7 +104,6 @@ public function it_create_git_hooks_path_when_hooks_dir_not_exists()
rmdir($hookDir);
}


/**
* @test
*/
Expand Down

0 comments on commit d3ba750

Please sign in to comment.