From c553cb6ea657f1cc2339de4587e468dbcae7523c Mon Sep 17 00:00:00 2001 From: Sam Antonis Date: Tue, 7 Jun 2022 13:56:40 +0200 Subject: [PATCH 1/2] test husky setup --- .huskyrc.json | 15 +++++++++++++++ composer.json | 8 +++++--- 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 .huskyrc.json diff --git a/.huskyrc.json b/.huskyrc.json new file mode 100644 index 0000000..ada5fe3 --- /dev/null +++ b/.huskyrc.json @@ -0,0 +1,15 @@ +{ + "hooks": { + "pre-commit": [ + "echo committing as $(git config user.name)", + "bin/console lint:yaml app/config docker/docker-compose*.yaml --parse-tags --ansi --no-interaction", + "bin/console lint:twig", + "vendor/bin/php-cs-fixer fix --dry-run --diff --using-cache=no --allow-risky=yes --ansi" + ], + "pre-push": [ + "./runTests.sh", + "vendor/bin/codecept run" + ], + "post-merge": "composer install" + } +} diff --git a/composer.json b/composer.json index 71168fc..697c6c1 100644 --- a/composer.json +++ b/composer.json @@ -11,12 +11,14 @@ }, "require-dev": { "phpunit/phpunit": "^9.5", - "symfony/test-pack": "^1.0" + "symfony/test-pack": "^1.0", + "ccinn/composer-husky-plugin": "^0.1.0", + "ccinn/husky-php": "^0.3.0" }, "autoload": { - "psr-4": { + "psr-4": { "Clearfacts\\Bundle\\CodestyleBundle\\": "src/", - "Clearfacts\\Bundle\\CodestyleBundle\\Tests\\": "tests/" + "Clearfacts\\Bundle\\CodestyleBundle\\Tests\\": "tests/" } }, "bin": ["bin/cf-codestyle"], From 1daccaa77381e6223c62c8b42750904ffb2ab98b Mon Sep 17 00:00:00 2001 From: Sam Antonis Date: Fri, 10 Jun 2022 14:46:49 +0200 Subject: [PATCH 2/2] test lint hook --- .huskyrc.json | 15 ------------- composer.json | 7 +++--- templates/hooks/pre-commit-twig | 39 +++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 19 deletions(-) delete mode 100644 .huskyrc.json create mode 100755 templates/hooks/pre-commit-twig diff --git a/.huskyrc.json b/.huskyrc.json deleted file mode 100644 index ada5fe3..0000000 --- a/.huskyrc.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "hooks": { - "pre-commit": [ - "echo committing as $(git config user.name)", - "bin/console lint:yaml app/config docker/docker-compose*.yaml --parse-tags --ansi --no-interaction", - "bin/console lint:twig", - "vendor/bin/php-cs-fixer fix --dry-run --diff --using-cache=no --allow-risky=yes --ansi" - ], - "pre-push": [ - "./runTests.sh", - "vendor/bin/codecept run" - ], - "post-merge": "composer install" - } -} diff --git a/composer.json b/composer.json index 697c6c1..63de74b 100644 --- a/composer.json +++ b/composer.json @@ -11,9 +11,7 @@ }, "require-dev": { "phpunit/phpunit": "^9.5", - "symfony/test-pack": "^1.0", - "ccinn/composer-husky-plugin": "^0.1.0", - "ccinn/husky-php": "^0.3.0" + "symfony/test-pack": "^1.0" }, "autoload": { "psr-4": { @@ -29,7 +27,8 @@ ], "copy-phpcs-config": "bin/cf-codestyle clearfacts:codestyle:copy-cs-config", "lint-phpcs": "bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --dry-run --diff --using-cache=no --allow-risky=yes --ansi src/ tests/", - "lint-phpcs-fix": "bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --diff --using-cache=no --allow-risky=yes --ansi src/ tests/" + "lint-phpcs-fix": "bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --diff --using-cache=no --allow-risky=yes --ansi src/ tests/", + "lint-twig": "bin/console lint:twig" }, "config": { "bin-dir": "bin", diff --git a/templates/hooks/pre-commit-twig b/templates/hooks/pre-commit-twig new file mode 100755 index 0000000..b3a4917 --- /dev/null +++ b/templates/hooks/pre-commit-twig @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +# Grab the staged files +stagedFiles=($(git diff --cached --name-only --diff-filter=AM)) +files="${stagedFiles[@]}" +if [ "$files" = '' ] +then + exit 0 +fi + +# Construct the twig linting command. +command=$(echo "make -s lint:twig ${files}") +echo "$command" + +# Run the command on our docker container and capture the results +make -s lint:twig ${files} >> results.txt + +results=$(cat results.txt) +$(rm results.txt) + +# Block the commit from going through if needed +foundErrors=$(grep -o "begin diff" <<< ${results}) +returnCode=0 + +if [ "$foundErrors" != '' ] +then + echo "${results}" >&2 + + command=$(echo "make -s lint:twig ${files}") + echo "$command" + + make -s lint:twig ${files} + + echo "You had some twig errors, don't forget to add these fixes to git before commiting again!" >&2 + + # Exit so you can stage the files and commit again + returnCode=1 +fi +exit $returnCode