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

15
.editorconfig Normal file
View File

@ -0,0 +1,15 @@
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false
[*.{yml,yaml}]
indent_size = 2

7
.gitattributes vendored Normal file
View File

@ -0,0 +1,7 @@
* text=auto
*.php diff=php
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/vendor
composer.lock
/.idea

32
README.md Normal file
View File

@ -0,0 +1,32 @@
# Bluesquare Hooks
This repository contains a collection of git hooks that can be used to enforce code quality and best practices in your Laravel projects.
It automatically installs git hooks.
## Installation
Edit your `composer.json` file to include the following script and repository:
```
"require-dev": {
"bluesquare/hooks": "dev-main"
},
"scripts": {
"post-autoload-dump": [
"[ $COMPOSER_DEV_MODE -eq 0 ] || @php artisan hooks:install"
],
},
"repositories": [
{
"type": "vcs",
"url": "https://git.bluesquare.io/bluesquare/hooks.git"
}
]
```
Then run:
```bash
composer update bluesquare/hooks --dev
```

32
composer.json Normal file
View File

@ -0,0 +1,32 @@
{
"name": "bluesquare/laravel-hooks",
"description": "Add Git hooks to your Laravel project.",
"keywords": ["laravel", "git", "hooks"],
"license": "MIT",
"authors": [
{
"name": "Maxime Renou",
"email": "maxime@bluesquare.io"
}
],
"require": {
"php": "^8.1.0"
},
"autoload": {
"psr-4": {
"Bluesquare\\Hooks\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Bluesquare\\Hooks\\ServiceProvider"
]
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}

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,
];
}
}

31
stubs/pre-commit Normal file
View File

@ -0,0 +1,31 @@
#!/bin/bash
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".php\{0,1\}$")
# Disable if it's a merge
git merge HEAD &> /dev/null
IS_MERGE_PROCESS=$?
if [ $IS_MERGE_PROCESS -ne 0 ]
then
echo -e "\e[92m Skipping pre-commit hook. \e[0m"
exit $?
fi
# Skip if no staged files
if [[ "$STAGED_FILES" = "" ]]; then
echo -e "\e[92m No staged files. Skipping pre-commit hook. \e[0m"
exit 0
fi
echo -e "\e[92m Running pre-commit hooks... \e[0m"
./artisan hooks:pre-commit
if [[ "$?" == 0 ]]; then
echo -e "\e[102m Pre-commit hooks passed \e[0m"
else
echo -e "\e[102m Pre-commit hooks failed \e[0m"
exit 1
fi
exit 0