From 2afdf43cd2e5c9801921b994b63d8bbdb2e92efa Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Mon, 24 Apr 2023 22:54:34 +0100 Subject: [PATCH] feat: add and enable pre-push Git hooks --- src/Command/GenerateCommand.php | 12 ++++++++++ src/DataTransferObject/Config.php | 1 + templates/git-hooks/pre-push.twig | 5 +++++ templates/justfile.twig | 37 ++++++++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100755 templates/git-hooks/pre-push.twig diff --git a/src/Command/GenerateCommand.php b/src/Command/GenerateCommand.php index 8a568c2..1ca9bfa 100644 --- a/src/Command/GenerateCommand.php +++ b/src/Command/GenerateCommand.php @@ -142,6 +142,10 @@ class GenerateCommand extends Command if ($filesystem->exists("{$outputDir}/tools/docker/images/php/root/usr/local/bin/docker-entrypoint-php")) { $filesystem->chmod("{$outputDir}/tools/docker/images/php/root/usr/local/bin/docker-entrypoint-php", 0755); } + + if ($filesystem->exists("{$outputDir}/.githooks/pre-push")) { + $filesystem->chmod("{$outputDir}/.githooks/pre-push", 0755); + } } function getFiles(array $configurationData): Collection @@ -219,6 +223,14 @@ class GenerateCommand extends Command ); } + if (Arr::get($configurationData, 'experimental.runGitHooksBeforePush', false) === true) { + $filesToGenerate[] = new TemplateFile( + data: 'git-hooks/pre-push', + name: 'pre-push', + path: '.githooks', + ); + } + return $filesToGenerate; } diff --git a/src/DataTransferObject/Config.php b/src/DataTransferObject/Config.php index 28b0241..e7e1e01 100644 --- a/src/DataTransferObject/Config.php +++ b/src/DataTransferObject/Config.php @@ -40,6 +40,7 @@ final class Config allowMissingFields: true, fields: [ 'createGitHubActionsConfiguration' => new Assert\Type('boolean'), + 'runGitHooksBeforePush' => new Assert\Type('boolean'), 'useNewDatabaseCredentials' => new Assert\Type('boolean'), ] )] diff --git a/templates/git-hooks/pre-push.twig b/templates/git-hooks/pre-push.twig new file mode 100755 index 0000000..f08851f --- /dev/null +++ b/templates/git-hooks/pre-push.twig @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +set -euo pipefail + +just test-commit diff --git a/templates/justfile.twig b/templates/justfile.twig index 1930d02..8aacbc8 100644 --- a/templates/justfile.twig +++ b/templates/justfile.twig @@ -19,7 +19,7 @@ composer *args: alias phpunit := test test *args: - {{ "just _run php phpunit {{ args }}" | raw }} + {{ "just _run php phpunit --colors=always {{ args }}" | raw }} {% endif %} {% if "drupal-project" is same as type %} @@ -58,6 +58,39 @@ artisan *args: {{ "just _run artisan {{ args }}" | raw }} {% endif %} +{% if experimental.runGitHooksBeforePush %} +# Enable or disable Git hooks +git-hooks command: + #!/usr/bin/env bash + set -euo pipefail + + case "{{ '{{ command }}'|raw }}" in + "on") + echo "Enabling Git hooks..." + git config core.hooksPath .githooks + ;; + "off") + echo "Disabling Git hooks..." + git config --unset core.hooksPath + ;; + *) + echo "Error: Invalid argument. Must be either 'on' or 'off'" + ;; + esac + +test-commit: + {% if "phpcs" in php|keys -%} + just _run php phpcs + {%- endif %} + + {% if "phpstan" in php|keys -%} + just _run php phpstan analyze --no-progress --memory-limit=512M + {%- endif %} + + just test --testdox --testsuite unit + just test --testdox --testsuite kernel +{% endif %} + _exec +args: {{ "docker compose exec {{ args }}" | raw }} @@ -68,3 +101,5 @@ _run service command *args: --rm \ -T \ {{ "{{ service }} {{ args }}"|raw }} + +# vim: ft=just