Skip to content

Code Improvements #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 1, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}/hooks");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last nitpick: I would prefer create_hooks_dir($gitDir); and then the string interpolation will happen in the function. that removes some redundancy. what do you think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, much better! I did not pay attention to this before 😅, I'll make the change.


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}/hooks");

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)) {
mkdir($dir, $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)');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now requires symfony/process as a project dependancy, right?

$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}/hooks");
$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}/hooks", 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}/hooks", 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}/hooks", 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}/hooks", 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