This commit is contained in:
2024-04-05 01:40:22 +02:00
commit 200495442f
9 changed files with 277 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
<?php
namespace Bluesquare\Hooks\Console;
use Illuminate\Console\Command;
#[AsCommand(name: 'hooks:install')]
class InstallCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'hooks:install';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Install Git hooks';
/**
* Execute the console command.
*
* @return int|null
*/
public function handle()
{
$git_directory = base_path('.git');
$git_hooks_directory = "{$git_directory}/hooks";
$this->line("Checking Git hooks directory...");
if (! is_dir($git_directory)) {
$this->error("Could not install hooks: $git_directory is not a Git repository");
return 1;
}
if (! is_dir($git_directory)) {
if (! mkdir($git_hooks_directory, 0755, true)) {
$this->error("Could not install hooks: could not create Git hooks directory $git_hooks_directory");
return 1;
}
$this->line("Created Git hooks directory: $git_hooks_directory");
}
$files = ['pre-commit'];
foreach ($files as $file) {
$git_hook_file = "{$git_hooks_directory}/{$file}";
if (file_exists($git_hook_file)) {
$this->line("Git hook file already exists: $git_hook_file. Skipping.");
continue;
}
if (file_put_contents($git_hook_file, file_get_contents(__DIR__.'/../../stubs/'.$file)) === false) {
$this->error("Could not install hooks: could not create Git hook file $git_hook_file");
return 1;
}
}
return 0;
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace Bluesquare\Hooks\Console;
use Illuminate\Console\Command;
#[AsCommand(name: 'hooks:pre-commit')]
class PreCommitCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'hooks:pre-commit';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Execute pre-commit hooks';
/**
* Execute the console command.
*
* @return int|null
*/
public function handle()
{
$pint = base_path('vendor/bin/pint');
if (file_exists($pint)) {
$this->line("Pint detected. Running...");
passthru($pint . ' --dirty');
} else {
$this->line("Pint not found. Skipping tests.");
}
return 0;
}
}

47
src/ServiceProvider.php Normal file
View File

@@ -0,0 +1,47 @@
<?php
namespace Bluesquare\Hooks;
use Illuminate\Contracts\Support\DeferrableProvider;
class ServiceProvider extends \Illuminate\Support\ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
if (! $this->app->runningInConsole()) {
return;
}
$this->commands([
Console\InstallCommand::class,
Console\PreCommitCommand::class,
]);
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
Console\InstallCommand::class,
];
}
}