Skip to content

Overdue code cleanup #44

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 1 commit into from
Sep 2, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ RUN composer install

COPY . /app/

RUN composer fix-style && composer test && ./cghooks add
RUN composer check-style
RUN composer test
RUN ./cghooks add
116 changes: 61 additions & 55 deletions src/Commands/AddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
namespace BrainMaestro\GitHooks\Commands;

use BrainMaestro\GitHooks\Hook;
use Symfony\Component\Console\Command\Command;
use BrainMaestro\GitHooks\Commands\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class AddCommand extends Command
{
private $hooks;

public function __construct($hooks)
{
$this->hooks = $hooks;
parent::__construct();
}
private $force;
private $windows;
private $noLock;
private $ignoreLock;
private $addedHooks = [];

protected function configure()
{
Expand All @@ -32,75 +30,83 @@ protected function configure()
;
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function init($input)
{
$addedHooks = [];
$gitDir = $input->getOption('git-dir');
$force = $input->getOption('force');
$forceWindows = $input->getOption('force-win');

create_hooks_dir($gitDir);

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

$script = is_array($script) ? implode(PHP_EOL, $script) : $script;

if (! $force && $fileExists) {
$output->writeln("<comment>{$hook} already exists</comment>");
} else {
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;
}

file_put_contents($filename, $script);
chmod($filename, 0755);
$output->writeln(
$fileExists
? "Overwrote <info>{$hook}</info> hook"
: "Added <info>{$hook}</info> hook"
);
$addedHooks[] = $hook;
}
}
$this->force = $input->getOption('force');
$this->windows = $input->getOption('force-win') || is_windows();
$this->noLock = $input->getOption('no-lock');
$this->ignoreLock = $input->getOption('ignore-lock');
}

if (! count($addedHooks)) {
$output->writeln('<error>No hooks were added. Try updating</error>');
return;
protected function command()
{
create_hooks_dir($this->gitDir);

foreach ($this->hooks as $hook => $contents) {
$this->addHook($hook, $contents);
}

if ($input->getOption('no-lock')) {
$output->writeln('<comment>Skipped creating a '. Hook::LOCK_FILE . ' file</comment>');
if (! count($this->addedHooks)) {
$this->error('No hooks were added. Try updating');
return;
}

$this->addLockFile($addedHooks, $output);
$this->addLockFile();
$this->ignoreLockFile();
}

private function addHook($hook, $contents)
{
$filename = "{$this->gitDir}/hooks/{$hook}";
$exists = file_exists($filename);

// On windows, the shebang needs to point to bash
// See: https://github.com/BrainMaestro/composer-git-hooks/issues/7
$shebang = ($this->windows ? '#!/bin/bash' : '#!/bin/sh') . PHP_EOL . PHP_EOL;
$contents = is_array($contents) ? implode(PHP_EOL, $contents) : $contents;

if (! $input->getOption('ignore-lock')) {
$output->writeln('<comment>Skipped adding '. Hook::LOCK_FILE . ' to .gitignore</comment>');
if (! $this->force && $exists) {
$this->comment("{$hook} already exists");
return;
}

$this->ignoreLockFile($output);
file_put_contents($filename, $shebang . $contents);
chmod($filename, 0755);

$operation = $exists ? 'Overwrote' : 'Added';
$this->log("{$operation} <info>{$hook}</info> hook");

$this->addedHooks[] = $hook;
}

private function addLockFile($hooks, $output)
private function addLockFile()
{
file_put_contents(Hook::LOCK_FILE, json_encode($hooks));
$output->writeln('<comment>Created ' . Hook::LOCK_FILE . ' file</comment>');
if ($this->noLock) {
$this->comment('Skipped creating a '. Hook::LOCK_FILE . ' file');
return;
}

file_put_contents(Hook::LOCK_FILE, json_encode($this->addedHooks));
$this->comment('Created ' . Hook::LOCK_FILE . ' file');
}

private function ignoreLockFile($output)
private function ignoreLockFile()
{
if ($this->noLock) {
return;
}

if (! $this->ignoreLock) {
$this->comment('Skipped adding '. Hook::LOCK_FILE . ' to .gitignore');
return;
}

$contents = file_get_contents('.gitignore');
$return = strpos($contents, Hook::LOCK_FILE);

if ($return === false) {
file_put_contents('.gitignore', Hook::LOCK_FILE . PHP_EOL, FILE_APPEND);
$output->writeln('<comment>Added ' . Hook::LOCK_FILE . ' to .gitignore</comment>');
$this->comment('Added ' . Hook::LOCK_FILE . ' to .gitignore');
}
}
}
48 changes: 48 additions & 0 deletions src/Commands/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace BrainMaestro\GitHooks\Commands;

use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

abstract class Command extends SymfonyCommand
{
private $output;

protected $hooks;
protected $gitDir;

public function __construct($hooks)
{
$this->hooks = $hooks;
parent::__construct();
}

abstract protected function init($input);
abstract protected function command();

final protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->gitDir = $input->getOption('git-dir');
$this->init($input);
$this->command();
}

protected function log($log)
{
$this->output->writeln($log);
}

protected function comment($comment)
{
$this->output->writeln("<comment>{$comment}</comment>");
}

protected function error($error)
{
$this->output->writeln("<error>{$error}</error>");
}
}
14 changes: 7 additions & 7 deletions src/Commands/HookCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
namespace BrainMaestro\GitHooks\Commands;

use BrainMaestro\GitHooks\Hook;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class HookCommand extends Command
class HookCommand extends SymfonyCommand
{
private $hook;
private $script;
private $contents;

public function __construct($hook, $script)
public function __construct($hook, $contents)
{
$this->hook = $hook;
$this->script = $script;
$this->contents = $contents;
parent::__construct();
}

Expand All @@ -31,7 +31,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$script = is_array($this->script) ? implode(PHP_EOL, $this->script) : $this->script;
$output->writeln(shell_exec($script));
$contents = is_array($this->contents) ? implode(PHP_EOL, $this->contents) : $this->contents;
$output->writeln(shell_exec($contents));
}
}
20 changes: 7 additions & 13 deletions src/Commands/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@
namespace BrainMaestro\GitHooks\Commands;

use BrainMaestro\GitHooks\Hook;
use Symfony\Component\Console\Command\Command;
use BrainMaestro\GitHooks\Commands\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ListCommand extends Command
{
private $hooks;

public function __construct($hooks)
{
$this->hooks = $hooks;
parent::__construct();
}

protected function configure()
{
$this
Expand All @@ -28,15 +20,17 @@ protected function configure()
;
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function init($input)
{
$gitDir = $input->getOption('git-dir');
}

protected function command()
{
foreach (array_keys($this->hooks) as $hook) {
$filename = "{$gitDir}/hooks/{$hook}";
$filename = "{$this->gitDir}/hooks/{$hook}";

if (is_file($filename)) {
$output->writeln("<info>{$hook}</info>");
$this->log("<info>{$hook}</info>");
}
}
}
Expand Down
44 changes: 22 additions & 22 deletions src/Commands/RemoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@
namespace BrainMaestro\GitHooks\Commands;

use BrainMaestro\GitHooks\Hook;
use Symfony\Component\Console\Command\Command;
use BrainMaestro\GitHooks\Commands\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class RemoveCommand extends Command
{
private $hooks;

public function __construct($hooks)
{
$this->hooks = $hooks;
parent::__construct();
}
private $force;
private $lockFileHooks;
private $hooksToRemove;

protected function configure()
{
Expand All @@ -41,31 +37,35 @@ protected function configure()
;
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function init($input)
{
$lockFileHooks = file_exists(Hook::LOCK_FILE)
? array_flip(json_decode(file_get_contents(Hook::LOCK_FILE)))
: [];
$gitDir = $input->getOption('git-dir');
$this->force = $input->getOption('force');
$this->lockFileHooks = file_exists(Hook::LOCK_FILE)
? array_flip(json_decode(file_get_contents(Hook::LOCK_FILE)))
: [];
$this->hooksToRemove = $input->getArgument('hooks');
}

foreach ($input->getArgument('hooks') as $hook) {
$filename = "{$gitDir}/hooks/{$hook}";
protected function command()
{
foreach ($this->hooksToRemove as $hook) {
$filename = "{$this->gitDir}/hooks/{$hook}";

if (! array_key_exists($hook, $lockFileHooks) && ! $input->getOption('force')) {
$output->writeln("<comment>Skipped {$hook} hook - not present in lock file</comment>");
if (! array_key_exists($hook, $this->lockFileHooks) && ! $this->force) {
$this->comment("Skipped {$hook} hook - not present in lock file");
continue;
}

if (array_key_exists($hook, $this->hooks) && is_file($filename)) {
unlink($filename);
$output->writeln("Removed <info>{$hook}</info> hook");
unset($lockFileHooks[$hook]);
$this->log("Removed <info>{$hook}</info> hook");
unset($this->lockFileHooks[$hook]);
continue;
}
$output->writeln("<error>{$hook} hook does not exist</error>");

$this->error("{$hook} hook does not exist");
}

file_put_contents(Hook::LOCK_FILE, json_encode(array_keys($lockFileHooks)));
file_put_contents(Hook::LOCK_FILE, json_encode(array_keys($this->lockFileHooks)));
}
}
Loading