Skip to content

Commit

Permalink
[ Task ] Extract Hook and Platform classes (#22)
Browse files Browse the repository at this point in the history
* add methods to Platform

* add notes for future feature

* use new Platform methods in Install command

* move cwd and normalization to Platform class

* Whisky::bin -> Whisky::bin_path

* implement Hook->uninstall method

* add convenience methods for readability

* refactor static constructors for flexibility

* start testing Hook class

* use Hook class in Uninstall command

* use Hook class in Update command
  • Loading branch information
ProjektGopher authored Aug 10, 2023
1 parent fa93dd7 commit 23b44eb
Show file tree
Hide file tree
Showing 9 changed files with 321 additions and 210 deletions.
2 changes: 1 addition & 1 deletion app/Commands/GetRunCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function handle(): int
return Command::SUCCESS;
}

$bin = Whisky::bin();
$bin = Whisky::bin_path();
$this->line(Whisky::base_path("bin/run-hook {$this->argument('hook')} {$bin}"));

return Command::SUCCESS;
Expand Down
94 changes: 35 additions & 59 deletions app/Commands/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace ProjektGopher\Whisky\Commands;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use LaravelZero\Framework\Commands\Command;
use ProjektGopher\Whisky\Hook;
use ProjektGopher\Whisky\Platform;
use ProjektGopher\Whisky\Whisky;

class Install extends Command
Expand All @@ -14,21 +14,27 @@ class Install extends Command

protected $description = 'Install git hooks';

public function __construct(
public Platform $platform,
) {
parent::__construct();
}

public function handle(): int
{
if (! $this->gitIsInitialized()) {
if ($this->platform->gitIsNotInitialized()) {
$this->error('Git has not been initialized in this project, aborting...');

return Command::FAILURE;
}

if (
! File::exists(Whisky::cwd('whisky.json')) ||
! File::exists(Platform::cwd('whisky.json')) ||
$this->confirm('overwrite existing whisky.json?', false)
) {
$this->info('Creating whisky.json in project root...');
File::put(
Whisky::cwd('whisky.json'),
Platform::cwd('whisky.json'),
File::get(Whisky::base_path('stubs/whisky.json')),
);
}
Expand All @@ -37,72 +43,42 @@ public function handle(): int
$this->info('Installing git hooks...');
}

$this->getHooks()->each(function (string $hook) {
$this->installHook($hook);
});

if (! Whisky::isWindows()) {
if ($this->option('verbose')) {
$this->info('Verifying hooks are executable...');
/**
* Do the hook installation, and talk about it.
*/
Hook::each(function (Hook $hook): void {
if ($hook->isNotEnabled()) {
if ($this->option('verbose')) {
$this->line("{$hook->name} file does not exist yet, creating...");
}
$hook->enable();
}
exec('chmod +x '.Whisky::cwd('.git/hooks').'/*');
exec('chmod +x '.Whisky::base_path('bin/run-hook'));
}

$this->info('Git hooks installed successfully.');

return Command::SUCCESS;
}

private function gitIsInitialized(): bool
{
return File::exists(Whisky::cwd('.git'));
}
if ($hook->isInstalled()) {
if ($this->option('verbose')) {
$this->warn("{$hook->name} git hook already installed, skipping...");
}

private function getHooks(): Collection
{
return collect(array_keys(Whisky::readConfig('hooks')));
}
return;
}

private function hookIsInstalled(string $hook): bool
{
$bin = Whisky::bin();

return Str::contains(
File::get(Whisky::cwd(".git/hooks/{$hook}")),
[
"eval \"$({$bin} get-run-cmd {$hook})\"",
// TODO: legacy - handle upgrade somehow
"eval \"$(./vendor/bin/whisky get-run-cmd {$hook})\"",
],
);
}
$hook->install();

private function installHook(string $hook): void
{
if (! File::exists(Whisky::cwd(".git/hooks/{$hook}"))) {
if ($this->option('verbose')) {
$this->line("{$hook} file does not exist yet, creating...");
$this->info("{$hook->name} git hook installed successfully.");
}
File::put(Whisky::cwd(".git/hooks/{$hook}"), '#!/bin/sh'.PHP_EOL);
}
});

if ($this->hookIsInstalled($hook)) {
if ($this->platform->isNotWindows()) {
if ($this->option('verbose')) {
$this->warn("{$hook} git hook already installed, skipping...");
$this->info('Verifying hooks are executable...');
}

return;
exec('chmod +x '.Platform::cwd('.git/hooks').'/*');
exec('chmod +x '.Whisky::base_path('bin/run-hook'));
}

$bin = Whisky::bin();
File::append(
Whisky::cwd(".git/hooks/{$hook}"),
"eval \"$({$bin} get-run-cmd {$hook})\"".PHP_EOL,
);
$this->info('Git hooks installed successfully.');

if ($this->option('verbose')) {
$this->info("{$hook} git hook installed successfully.");
}
return Command::SUCCESS;
}
}
13 changes: 4 additions & 9 deletions app/Commands/Scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace ProjektGopher\Whisky\Commands;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use LaravelZero\Framework\Commands\Command;
use ProjektGopher\Whisky\Whisky;
use ProjektGopher\Whisky\Hook;
use ProjektGopher\Whisky\Platform;

class Scripts extends Command
{
Expand All @@ -15,22 +15,17 @@ class Scripts extends Command

public function handle(): int
{
if (File::missing(Whisky::cwd('whisky.json'))) {
if (File::missing(Platform::cwd('whisky.json'))) {
$this->error('Whisky has not been initialized in this project, aborting...');
$this->line('Run `./vendor/bin/whisky install` to initialize Whisky in this project.');

return Command::FAILURE;
}

$this->getScripts($this->argument('hook'))->each(function (string $script): void {
Hook::make($this->argument('hook'))->getScripts()->each(function (string $script): void {
$this->line($script);
});

return Command::SUCCESS;
}

private function getScripts(string $hook): Collection
{
return collect(Whisky::readConfig("hooks.{$hook}"));
}
}
34 changes: 6 additions & 28 deletions app/Commands/Uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace ProjektGopher\Whisky\Commands;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use LaravelZero\Framework\Commands\Command;
use ProjektGopher\Whisky\Whisky;
use SplFileInfo;
use ProjektGopher\Whisky\Hook;
use ProjektGopher\Whisky\Platform;

class Uninstall extends Command
{
Expand All @@ -16,39 +15,18 @@ class Uninstall extends Command

public function handle(): int
{
collect(
File::files(Whisky::cwd('.git/hooks'))
)->filter(
fn (SplFileInfo $file) => ! str_ends_with($file->getFilename(), 'sample')
)->each(function (SplFileInfo $file): void {
$bin = Whisky::bin();
$path = $file->getPathname();
$hook = $file->getFilename();
$contents = File::get($path);
$commands = [
"eval \"$({$bin} get-run-cmd {$hook})\"".PHP_EOL,
// TODO: legacy - handle upgrade somehow
"eval \"$(./vendor/bin/whisky get-run-cmd {$hook})\"".PHP_EOL,
];

if (! Str::contains($contents, $commands)) {
Hook::all(Hook::FROM_GIT)->each(function (Hook $hook): void {
if (! $hook->uninstall()) {
return;
}

$contents = str_replace(
$commands,
'',
File::get($path),
);
File::put($path, $contents);
$this->info("Removed Whisky from {$hook} hook.");
$this->info("Removed Whisky from {$hook->name} hook.");
});

if (
$this->option('json') ||
$this->confirm('Would you also like to remove whisky.json?')
) {
File::delete(Whisky::cwd('whisky.json'));
File::delete(Platform::cwd('whisky.json'));
$this->info('whisky.json removed.');
}

Expand Down
96 changes: 19 additions & 77 deletions app/Commands/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@

namespace ProjektGopher\Whisky\Commands;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use LaravelZero\Framework\Commands\Command;
use ProjektGopher\Whisky\Whisky;
use SplFileInfo;
use ProjektGopher\Whisky\Hook;

/**
* TODO: This command has a lot of duplication.
* Most of this can live on the Whisky class,
* and some should be in a new Hook class.
* TODO: This has a lot of duplication wrt Commands\Uninstall and Commands\Install.
*/
class Update extends Command
{
Expand All @@ -22,85 +16,33 @@ class Update extends Command

public function handle(): int
{

$this->uninstall();

$this->getHooks()->each(function (string $hook) {
$this->installHook($hook);
});

$this->line('done.');

return Command::SUCCESS;
}

private function uninstall(): void
{
collect(
File::files(Whisky::cwd('.git/hooks'))
)->filter(
fn (SplFileInfo $file) => ! str_ends_with($file->getFilename(), 'sample')
)->each(function (SplFileInfo $file): void {
$path = $file->getPathname();
$hook = $file->getFilename();
$contents = File::get($path);
$bin = Whisky::bin();
$command = "eval \"$({$bin} get-run-cmd {$hook})\"".PHP_EOL;

if (! str_contains($contents, $command)) {
Hook::all(Hook::FROM_GIT)->each(function (Hook $hook): void {
if (! $hook->uninstall()) {
return;
}

$contents = str_replace(
$command,
'',
File::get($path),
);
File::put($path, $contents);
$this->info("Removed Whisky from {$hook} hook.");
if ($this->option('verbose')) {
$this->info("Removed Whisky from {$hook->name} hook.");
}
});
}

private function getHooks(): Collection
{
return collect(array_keys(Whisky::readConfig('hooks')));
}

private function hookIsInstalled(string $hook): bool
{
$bin = Whisky::bin();

return Str::contains(
File::get(Whisky::cwd(".git/hooks/{$hook}")),
"eval \"$({$bin} get-run-cmd {$hook})\"",
);
}

private function installHook(string $hook): void
{
if (! File::exists(Whisky::cwd(".git/hooks/{$hook}"))) {
if ($this->option('verbose')) {
$this->line("{$hook} file does not exist yet, creating...");
Hook::all(Hook::FROM_CONFIG)->each(function (Hook $hook) {
if ($hook->fileIsMissing()) {
if ($this->option('verbose')) {
$this->line("{$hook->name} file does not exist yet, creating...");
}
$hook->enable();
}
File::put(Whisky::cwd(".git/hooks/{$hook}"), '#!/bin/sh'.PHP_EOL);
}

if ($this->hookIsInstalled($hook)) {
$hook->install();

if ($this->option('verbose')) {
$this->warn("{$hook} git hook already installed, skipping...");
$this->info("{$hook->name} git hook installed successfully.");
}
});

return;
}

$bin = Whisky::bin();
File::append(
Whisky::cwd(".git/hooks/{$hook}"),
"eval \"$({$bin} get-run-cmd {$hook})\"".PHP_EOL,
);
$this->line('done.');

if ($this->option('verbose')) {
$this->info("{$hook} git hook installed successfully.");
}
return Command::SUCCESS;
}
}
Loading

0 comments on commit 23b44eb

Please sign in to comment.