From 200495442fc7397d929ec8f9e9c1be6a9bbed7a5 Mon Sep 17 00:00:00 2001 From: Maxime Renou Date: Fri, 5 Apr 2024 01:40:22 +0200 Subject: [PATCH] init --- .editorconfig | 15 +++++++++ .gitattributes | 7 +++++ .gitignore | 3 ++ README.md | 32 +++++++++++++++++++ composer.json | 32 +++++++++++++++++++ src/Console/InstallCommand.php | 68 ++++++++++++++++++++++++++++++++++++++++ src/Console/PreCommitCommand.php | 42 +++++++++++++++++++++++++ src/ServiceProvider.php | 47 +++++++++++++++++++++++++++ stubs/pre-commit | 31 ++++++++++++++++++ 9 files changed, 277 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/Console/InstallCommand.php create mode 100644 src/Console/PreCommitCommand.php create mode 100644 src/ServiceProvider.php create mode 100644 stubs/pre-commit diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..6537ca4 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..d83d3e5 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,7 @@ +* text=auto + +*.php diff=php + +.editorconfig export-ignore +.gitattributes export-ignore +.gitignore export-ignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e8f94b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/vendor +composer.lock +/.idea diff --git a/README.md b/README.md new file mode 100644 index 0000000..13879c3 --- /dev/null +++ b/README.md @@ -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 +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..602ed4d --- /dev/null +++ b/composer.json @@ -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 +} diff --git a/src/Console/InstallCommand.php b/src/Console/InstallCommand.php new file mode 100644 index 0000000..3a6e47e --- /dev/null +++ b/src/Console/InstallCommand.php @@ -0,0 +1,68 @@ +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; + } +} diff --git a/src/Console/PreCommitCommand.php b/src/Console/PreCommitCommand.php new file mode 100644 index 0000000..a9ac60d --- /dev/null +++ b/src/Console/PreCommitCommand.php @@ -0,0 +1,42 @@ +line("Pint detected. Running..."); + passthru($pint . ' --dirty'); + } else { + $this->line("Pint not found. Skipping tests."); + } + + return 0; + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php new file mode 100644 index 0000000..4e8da96 --- /dev/null +++ b/src/ServiceProvider.php @@ -0,0 +1,47 @@ +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, + ]; + } +} diff --git a/stubs/pre-commit b/stubs/pre-commit new file mode 100644 index 0000000..8fd70f9 --- /dev/null +++ b/stubs/pre-commit @@ -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