Skip to content

Commit 5b2feb3

Browse files
authored
Merge pull request #44 from BrainMaestro/code-cleanup
Overdue code cleanup
2 parents 0d7d48e + 10ba892 commit 5b2feb3

11 files changed

+171
-128
lines changed

Diff for: Dockerfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ RUN composer install
1111

1212
COPY . /app/
1313

14-
RUN composer fix-style && composer test && ./cghooks add
14+
RUN composer check-style
15+
RUN composer test
16+
RUN ./cghooks add

Diff for: src/Commands/AddCommand.php

+61-55
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
namespace BrainMaestro\GitHooks\Commands;
44

55
use BrainMaestro\GitHooks\Hook;
6-
use Symfony\Component\Console\Command\Command;
6+
use BrainMaestro\GitHooks\Commands\Command;
77
use Symfony\Component\Console\Input\InputInterface;
88
use Symfony\Component\Console\Input\InputOption;
99
use Symfony\Component\Console\Output\OutputInterface;
1010

1111
class AddCommand extends Command
1212
{
13-
private $hooks;
14-
15-
public function __construct($hooks)
16-
{
17-
$this->hooks = $hooks;
18-
parent::__construct();
19-
}
13+
private $force;
14+
private $windows;
15+
private $noLock;
16+
private $ignoreLock;
17+
private $addedHooks = [];
2018

2119
protected function configure()
2220
{
@@ -32,75 +30,83 @@ protected function configure()
3230
;
3331
}
3432

35-
protected function execute(InputInterface $input, OutputInterface $output)
33+
protected function init($input)
3634
{
37-
$addedHooks = [];
38-
$gitDir = $input->getOption('git-dir');
39-
$force = $input->getOption('force');
40-
$forceWindows = $input->getOption('force-win');
41-
42-
create_hooks_dir($gitDir);
43-
44-
foreach ($this->hooks as $hook => $script) {
45-
$filename = "{$gitDir}/hooks/{$hook}";
46-
$fileExists = file_exists($filename);
47-
48-
$script = is_array($script) ? implode(PHP_EOL, $script) : $script;
49-
50-
if (! $force && $fileExists) {
51-
$output->writeln("<comment>{$hook} already exists</comment>");
52-
} else {
53-
if ($forceWindows || is_windows()) {
54-
// On windows we need to add a SHEBANG
55-
// See: https://github.com/BrainMaestro/composer-git-hooks/issues/7
56-
$script = '#!/bin/bash' . PHP_EOL . $script;
57-
}
58-
59-
file_put_contents($filename, $script);
60-
chmod($filename, 0755);
61-
$output->writeln(
62-
$fileExists
63-
? "Overwrote <info>{$hook}</info> hook"
64-
: "Added <info>{$hook}</info> hook"
65-
);
66-
$addedHooks[] = $hook;
67-
}
68-
}
35+
$this->force = $input->getOption('force');
36+
$this->windows = $input->getOption('force-win') || is_windows();
37+
$this->noLock = $input->getOption('no-lock');
38+
$this->ignoreLock = $input->getOption('ignore-lock');
39+
}
6940

70-
if (! count($addedHooks)) {
71-
$output->writeln('<error>No hooks were added. Try updating</error>');
72-
return;
41+
protected function command()
42+
{
43+
create_hooks_dir($this->gitDir);
44+
45+
foreach ($this->hooks as $hook => $contents) {
46+
$this->addHook($hook, $contents);
7347
}
7448

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

80-
$this->addLockFile($addedHooks, $output);
54+
$this->addLockFile();
55+
$this->ignoreLockFile();
56+
}
57+
58+
private function addHook($hook, $contents)
59+
{
60+
$filename = "{$this->gitDir}/hooks/{$hook}";
61+
$exists = file_exists($filename);
62+
63+
// On windows, the shebang needs to point to bash
64+
// See: https://github.com/BrainMaestro/composer-git-hooks/issues/7
65+
$shebang = ($this->windows ? '#!/bin/bash' : '#!/bin/sh') . PHP_EOL . PHP_EOL;
66+
$contents = is_array($contents) ? implode(PHP_EOL, $contents) : $contents;
8167

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

87-
$this->ignoreLockFile($output);
73+
file_put_contents($filename, $shebang . $contents);
74+
chmod($filename, 0755);
75+
76+
$operation = $exists ? 'Overwrote' : 'Added';
77+
$this->log("{$operation} <info>{$hook}</info> hook");
78+
79+
$this->addedHooks[] = $hook;
8880
}
8981

90-
private function addLockFile($hooks, $output)
82+
private function addLockFile()
9183
{
92-
file_put_contents(Hook::LOCK_FILE, json_encode($hooks));
93-
$output->writeln('<comment>Created ' . Hook::LOCK_FILE . ' file</comment>');
84+
if ($this->noLock) {
85+
$this->comment('Skipped creating a '. Hook::LOCK_FILE . ' file');
86+
return;
87+
}
88+
89+
file_put_contents(Hook::LOCK_FILE, json_encode($this->addedHooks));
90+
$this->comment('Created ' . Hook::LOCK_FILE . ' file');
9491
}
9592

96-
private function ignoreLockFile($output)
93+
private function ignoreLockFile()
9794
{
95+
if ($this->noLock) {
96+
return;
97+
}
98+
99+
if (! $this->ignoreLock) {
100+
$this->comment('Skipped adding '. Hook::LOCK_FILE . ' to .gitignore');
101+
return;
102+
}
103+
98104
$contents = file_get_contents('.gitignore');
99105
$return = strpos($contents, Hook::LOCK_FILE);
100106

101107
if ($return === false) {
102108
file_put_contents('.gitignore', Hook::LOCK_FILE . PHP_EOL, FILE_APPEND);
103-
$output->writeln('<comment>Added ' . Hook::LOCK_FILE . ' to .gitignore</comment>');
109+
$this->comment('Added ' . Hook::LOCK_FILE . ' to .gitignore');
104110
}
105111
}
106112
}

Diff for: src/Commands/Command.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace BrainMaestro\GitHooks\Commands;
4+
5+
use Symfony\Component\Console\Command\Command as SymfonyCommand;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Input\InputOption;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
abstract class Command extends SymfonyCommand
11+
{
12+
private $output;
13+
14+
protected $hooks;
15+
protected $gitDir;
16+
17+
public function __construct($hooks)
18+
{
19+
$this->hooks = $hooks;
20+
parent::__construct();
21+
}
22+
23+
abstract protected function init($input);
24+
abstract protected function command();
25+
26+
final protected function execute(InputInterface $input, OutputInterface $output)
27+
{
28+
$this->output = $output;
29+
$this->gitDir = $input->getOption('git-dir');
30+
$this->init($input);
31+
$this->command();
32+
}
33+
34+
protected function log($log)
35+
{
36+
$this->output->writeln($log);
37+
}
38+
39+
protected function comment($comment)
40+
{
41+
$this->output->writeln("<comment>{$comment}</comment>");
42+
}
43+
44+
protected function error($error)
45+
{
46+
$this->output->writeln("<error>{$error}</error>");
47+
}
48+
}

Diff for: src/Commands/HookCommand.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
namespace BrainMaestro\GitHooks\Commands;
44

55
use BrainMaestro\GitHooks\Hook;
6-
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Command\Command as SymfonyCommand;
77
use Symfony\Component\Console\Input\InputInterface;
88
use Symfony\Component\Console\Input\InputOption;
99
use Symfony\Component\Console\Output\OutputInterface;
1010

11-
class HookCommand extends Command
11+
class HookCommand extends SymfonyCommand
1212
{
1313
private $hook;
14-
private $script;
14+
private $contents;
1515

16-
public function __construct($hook, $script)
16+
public function __construct($hook, $contents)
1717
{
1818
$this->hook = $hook;
19-
$this->script = $script;
19+
$this->contents = $contents;
2020
parent::__construct();
2121
}
2222

@@ -31,7 +31,7 @@ protected function configure()
3131

3232
protected function execute(InputInterface $input, OutputInterface $output)
3333
{
34-
$script = is_array($this->script) ? implode(PHP_EOL, $this->script) : $this->script;
35-
$output->writeln(shell_exec($script));
34+
$contents = is_array($this->contents) ? implode(PHP_EOL, $this->contents) : $this->contents;
35+
$output->writeln(shell_exec($contents));
3636
}
3737
}

Diff for: src/Commands/ListCommand.php

+7-13
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,13 @@
33
namespace BrainMaestro\GitHooks\Commands;
44

55
use BrainMaestro\GitHooks\Hook;
6-
use Symfony\Component\Console\Command\Command;
6+
use BrainMaestro\GitHooks\Commands\Command;
77
use Symfony\Component\Console\Input\InputInterface;
88
use Symfony\Component\Console\Input\InputOption;
99
use Symfony\Component\Console\Output\OutputInterface;
1010

1111
class ListCommand extends Command
1212
{
13-
private $hooks;
14-
15-
public function __construct($hooks)
16-
{
17-
$this->hooks = $hooks;
18-
parent::__construct();
19-
}
20-
2113
protected function configure()
2214
{
2315
$this
@@ -28,15 +20,17 @@ protected function configure()
2820
;
2921
}
3022

31-
protected function execute(InputInterface $input, OutputInterface $output)
23+
protected function init($input)
3224
{
33-
$gitDir = $input->getOption('git-dir');
25+
}
3426

27+
protected function command()
28+
{
3529
foreach (array_keys($this->hooks) as $hook) {
36-
$filename = "{$gitDir}/hooks/{$hook}";
30+
$filename = "{$this->gitDir}/hooks/{$hook}";
3731

3832
if (is_file($filename)) {
39-
$output->writeln("<info>{$hook}</info>");
33+
$this->log("<info>{$hook}</info>");
4034
}
4135
}
4236
}

Diff for: src/Commands/RemoveCommand.php

+22-22
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@
33
namespace BrainMaestro\GitHooks\Commands;
44

55
use BrainMaestro\GitHooks\Hook;
6-
use Symfony\Component\Console\Command\Command;
6+
use BrainMaestro\GitHooks\Commands\Command;
77
use Symfony\Component\Console\Input\InputInterface;
88
use Symfony\Component\Console\Input\InputArgument;
99
use Symfony\Component\Console\Input\InputOption;
1010
use Symfony\Component\Console\Output\OutputInterface;
1111

1212
class RemoveCommand extends Command
1313
{
14-
private $hooks;
15-
16-
public function __construct($hooks)
17-
{
18-
$this->hooks = $hooks;
19-
parent::__construct();
20-
}
14+
private $force;
15+
private $lockFileHooks;
16+
private $hooksToRemove;
2117

2218
protected function configure()
2319
{
@@ -41,31 +37,35 @@ protected function configure()
4137
;
4238
}
4339

44-
protected function execute(InputInterface $input, OutputInterface $output)
40+
protected function init($input)
4541
{
46-
$lockFileHooks = file_exists(Hook::LOCK_FILE)
47-
? array_flip(json_decode(file_get_contents(Hook::LOCK_FILE)))
48-
: [];
49-
$gitDir = $input->getOption('git-dir');
42+
$this->force = $input->getOption('force');
43+
$this->lockFileHooks = file_exists(Hook::LOCK_FILE)
44+
? array_flip(json_decode(file_get_contents(Hook::LOCK_FILE)))
45+
: [];
46+
$this->hooksToRemove = $input->getArgument('hooks');
47+
}
5048

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

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

5959
if (array_key_exists($hook, $this->hooks) && is_file($filename)) {
6060
unlink($filename);
61-
$output->writeln("Removed <info>{$hook}</info> hook");
62-
unset($lockFileHooks[$hook]);
61+
$this->log("Removed <info>{$hook}</info> hook");
62+
unset($this->lockFileHooks[$hook]);
6363
continue;
6464
}
65-
66-
$output->writeln("<error>{$hook} hook does not exist</error>");
65+
66+
$this->error("{$hook} hook does not exist");
6767
}
6868

69-
file_put_contents(Hook::LOCK_FILE, json_encode(array_keys($lockFileHooks)));
69+
file_put_contents(Hook::LOCK_FILE, json_encode(array_keys($this->lockFileHooks)));
7070
}
7171
}

0 commit comments

Comments
 (0)