From 234c8e49a6e57b90fe01ace5d9612f8ed2deb46f Mon Sep 17 00:00:00 2001 From: Pablo Lopez Date: Mon, 29 Apr 2024 16:18:56 +0200 Subject: [PATCH 1/6] Create PhpCsCommand.php --- core-dev/src/Command/PhpCsCommand.php | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 core-dev/src/Command/PhpCsCommand.php diff --git a/core-dev/src/Command/PhpCsCommand.php b/core-dev/src/Command/PhpCsCommand.php new file mode 100644 index 0000000..380792b --- /dev/null +++ b/core-dev/src/Command/PhpCsCommand.php @@ -0,0 +1,36 @@ +setName('phpcs') + ->setDescription('Run PHPCS code analysis against core.'); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output): int { + $command = "composer phpcs -- --report-full"; + $phpcs = Process::fromShellCommandline($command); + $output->writeln($command); + $phpcs->setTimeout(0); + $phpcs->run(function ($type, $data) use ($output) { + $output->write($data); + }); + if ($phpcs->getExitCode()) { + return 1; + } + return 0; + } +} From 6d012e700adc50edb7a5b40da0ef412154eb6b01 Mon Sep 17 00:00:00 2001 From: Pablo Lopez Date: Mon, 29 Apr 2024 16:19:55 +0200 Subject: [PATCH 2/6] Update install.yaml --- install.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/install.yaml b/install.yaml index 5dfe99f..4c9b744 100644 --- a/install.yaml +++ b/install.yaml @@ -18,6 +18,7 @@ project_files: - core-dev/src/Command/TestCommand.php - core-dev/src/Command/TestBrowserCommand.php - core-dev/src/Command/UninstallCommand.php + - core-dev/src/Command/PhpCsCommand.php post_install_actions: - cp core-dev/phpunit-chrome.xml ../core/phpunit.xml From 40eb35ba60eda78236b9d4b33b966e7a2ffed0ed Mon Sep 17 00:00:00 2001 From: Pablo Lopez Date: Mon, 29 Apr 2024 16:24:21 +0200 Subject: [PATCH 3/6] Update drupal --- commands/web/drupal | 2 ++ 1 file changed, 2 insertions(+) diff --git a/commands/web/drupal b/commands/web/drupal index cfa76f7..e6a31e7 100755 --- a/commands/web/drupal +++ b/commands/web/drupal @@ -21,6 +21,7 @@ use DrupalCoreDev\Command\AdminLoginCommand; use DrupalCoreDev\Command\TestCommand; use DrupalCoreDev\Command\TestBrowserCommand; use DrupalCoreDev\Command\UninstallCommand; +use DrupalCoreDev\Command\PhpCsCommand; if (PHP_SAPI !== 'cli') { return; @@ -39,5 +40,6 @@ $application->add(new AdminLoginCommand($loader)); $application->add(new TestCommand()); $application->add(new TestBrowserCommand()); $application->add(new UninstallCommand()); +$application->add(new PhpCsCommand()); $application->run(); From 30a5d305aaf8c8c4107b7f3feffdea3ffbd3f158 Mon Sep 17 00:00:00 2001 From: Pablo Lopez Date: Mon, 29 Apr 2024 16:25:13 +0200 Subject: [PATCH 4/6] Update PhpCsCommand.php --- core-dev/src/Command/PhpCsCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-dev/src/Command/PhpCsCommand.php b/core-dev/src/Command/PhpCsCommand.php index 380792b..29e726a 100644 --- a/core-dev/src/Command/PhpCsCommand.php +++ b/core-dev/src/Command/PhpCsCommand.php @@ -21,7 +21,7 @@ protected function configure(): void { * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output): int { - $command = "composer phpcs -- --report-full"; + $command = "composer phpcs -- --report-full --report-summary"; $phpcs = Process::fromShellCommandline($command); $output->writeln($command); $phpcs->setTimeout(0); From 6cea0359632a713ac068c3545f90747e26236fa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20L=C3=B3pez?= Date: Fri, 3 May 2024 10:54:49 +0200 Subject: [PATCH 5/6] Add more linting commands --- commands/web/drupal | 20 ++++++-- core-dev/src/Command/LintCommand.php | 51 +++++++++++++++++++ core-dev/src/Command/LintCspellCommand.php | 36 +++++++++++++ core-dev/src/Command/LintCssCommand.php | 47 +++++++++++++++++ core-dev/src/Command/LintJsCommand.php | 47 +++++++++++++++++ ...{PhpCsCommand.php => LintPhpCsCommand.php} | 6 +-- core-dev/src/Command/LintPhpStanCommand.php | 36 +++++++++++++ install.yaml | 7 ++- 8 files changed, 241 insertions(+), 9 deletions(-) create mode 100644 core-dev/src/Command/LintCommand.php create mode 100644 core-dev/src/Command/LintCspellCommand.php create mode 100644 core-dev/src/Command/LintCssCommand.php create mode 100644 core-dev/src/Command/LintJsCommand.php rename core-dev/src/Command/{PhpCsCommand.php => LintPhpCsCommand.php} (84%) create mode 100644 core-dev/src/Command/LintPhpStanCommand.php diff --git a/commands/web/drupal b/commands/web/drupal index 4595772..ac34e2f 100755 --- a/commands/web/drupal +++ b/commands/web/drupal @@ -15,13 +15,18 @@ use Drupal\Core\Command\GenerateTheme; use Drupal\Core\Command\InstallCommand; -use Symfony\Component\Console\Application; -use DrupalCoreDev\Command\CacheCommand; use DrupalCoreDev\Command\AdminLoginCommand; -use DrupalCoreDev\Command\TestCommand; +use DrupalCoreDev\Command\CacheCommand; +use DrupalCoreDev\Command\LintCommand; +use DrupalCoreDev\Command\LintCspellCommand; +use DrupalCoreDev\Command\LintCssCommand; +use DrupalCoreDev\Command\LintJsCommand; +use DrupalCoreDev\Command\LintPhpCsCommand; +use DrupalCoreDev\Command\LintPhpStanCommand; use DrupalCoreDev\Command\TestBrowserCommand; +use DrupalCoreDev\Command\TestCommand; use DrupalCoreDev\Command\UninstallCommand; -use DrupalCoreDev\Command\PhpCsCommand; +use Symfony\Component\Console\Application; if (PHP_SAPI !== 'cli') { return; @@ -40,6 +45,11 @@ $application->add(new AdminLoginCommand($loader)); $application->add(new TestCommand()); $application->add(new TestBrowserCommand()); $application->add(new UninstallCommand()); -$application->add(new PhpCsCommand()); +$application->add(new LintPhpCsCommand()); +$application->add(new LintPhpStanCommand()); +$application->add(new LintCssCommand()); +$application->add(new LintJsCommand()); +$application->add(new LintCspellCommand()); +$application->add(new LintCommand()); $application->run(); diff --git a/core-dev/src/Command/LintCommand.php b/core-dev/src/Command/LintCommand.php new file mode 100644 index 0000000..872429b --- /dev/null +++ b/core-dev/src/Command/LintCommand.php @@ -0,0 +1,51 @@ +setName('lint') + ->setDescription('Run lint tests.') + ->addOption('stop-on-failure', null, InputOption::VALUE_NONE, 'Tail the completion debug log'); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output): int { + $return = 0; + $stop_on_failure = $input->getOption('stop-on-failure'); + + $commands = [ + new LintPhpCsCommand(), + new LintPhpStanCommand(), + new LintCssCommand(), + new LintJsCommand(), + new LintCspellCommand(), + ]; + + foreach ($commands as $command) { + $return_command = $command->execute($input, $output); + if (!$return_command) { + continue; + } + $return = $return_command; + + if ($stop_on_failure) { + return $return; + } + } + + return $return; + } +} diff --git a/core-dev/src/Command/LintCspellCommand.php b/core-dev/src/Command/LintCspellCommand.php new file mode 100644 index 0000000..2ad2a47 --- /dev/null +++ b/core-dev/src/Command/LintCspellCommand.php @@ -0,0 +1,36 @@ +setName('lint:cspell') + ->setDescription('Run CSpell analysis against modified files.'); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output): int { + $command = "cd core && git diff --name-only | sed \"s_^_../_\" | yarn run spellcheck:core --no-must-find-files --file-list stdin"; + $phpcs = Process::fromShellCommandline($command); + $output->writeln($command); + $phpcs->setTimeout(0); + $phpcs->run(function ($type, $data) use ($output) { + $output->write($data); + }); + if ($phpcs->getExitCode()) { + return 1; + } + return 0; + } +} diff --git a/core-dev/src/Command/LintCssCommand.php b/core-dev/src/Command/LintCssCommand.php new file mode 100644 index 0000000..1b111e7 --- /dev/null +++ b/core-dev/src/Command/LintCssCommand.php @@ -0,0 +1,47 @@ +setName('lint:css') + ->setDescription('Run CSS coding standard analysis against core.'); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output): int { + $io = new SymfonyStyle($input, $output); + $yarn = getcwd() . '/core/.yarn'; + $node_modules = getcwd() . '/core/node_modules'; + + // Check if dependencies folder exists before to start. + if(!file_exists($yarn) || !file_exists($node_modules)) { + $io->error('Missing Yarn dependencies. Ensure that you run yarn install before executing this command.'); + return 1; + } + + $command = "cd core && yarn run lint:css --color --custom-formatter=node_modules/stylelint-formatter-gitlab"; + $phpcs = Process::fromShellCommandline($command); + $output->writeln($command); + $phpcs->setTimeout(0); + $phpcs->run(function ($type, $data) use ($output) { + $output->write($data); + }); + if ($phpcs->getExitCode()) { + return 1; + } + return 0; + } +} diff --git a/core-dev/src/Command/LintJsCommand.php b/core-dev/src/Command/LintJsCommand.php new file mode 100644 index 0000000..c394efb --- /dev/null +++ b/core-dev/src/Command/LintJsCommand.php @@ -0,0 +1,47 @@ +setName('lint:js') + ->setDescription('Run JS coding standard analysis against core.'); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output): int { + $io = new SymfonyStyle($input, $output); + $yarn = getcwd() . '/core/.yarn'; + $node_modules = getcwd() . '/core/node_modules'; + + // Check if dependencies folder exists before to start. + if(!file_exists($yarn) || !file_exists($node_modules)) { + $io->error('Missing Yarn dependencies. Ensure that you run yarn install before executing this command.'); + return 1; + } + + $command = "cd core && yarn run lint:core-js-passing"; + $phpcs = Process::fromShellCommandline($command); + $output->writeln($command); + $phpcs->setTimeout(0); + $phpcs->run(function ($type, $data) use ($output) { + $output->write($data); + }); + if ($phpcs->getExitCode()) { + return 1; + } + return 0; + } +} diff --git a/core-dev/src/Command/PhpCsCommand.php b/core-dev/src/Command/LintPhpCsCommand.php similarity index 84% rename from core-dev/src/Command/PhpCsCommand.php rename to core-dev/src/Command/LintPhpCsCommand.php index 29e726a..dc5987a 100644 --- a/core-dev/src/Command/PhpCsCommand.php +++ b/core-dev/src/Command/LintPhpCsCommand.php @@ -8,13 +8,13 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Process\Process; -class PhpCsCommand extends Command { +class LintPhpCsCommand extends Command { /** * {@inheritdoc} */ protected function configure(): void { - $this->setName('phpcs') - ->setDescription('Run PHPCS code analysis against core.'); + $this->setName('lint:phpcs') + ->setDescription('Run PHPCS coding standard analysis against core.'); } /** diff --git a/core-dev/src/Command/LintPhpStanCommand.php b/core-dev/src/Command/LintPhpStanCommand.php new file mode 100644 index 0000000..26b290c --- /dev/null +++ b/core-dev/src/Command/LintPhpStanCommand.php @@ -0,0 +1,36 @@ +setName('lint:phpstan') + ->setDescription('Run PHPStan code quality analysis against core.'); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output): int { + $command = "php vendor/bin/phpstan analyze --configuration=./core/phpstan.neon.dist --error-format=table"; + $phpcs = Process::fromShellCommandline($command); + $output->writeln($command); + $phpcs->setTimeout(0); + $phpcs->run(function ($type, $data) use ($output) { + $output->write($data); + }); + if ($phpcs->getExitCode()) { + return 1; + } + return 0; + } +} diff --git a/install.yaml b/install.yaml index f0b97a5..dcd7431 100644 --- a/install.yaml +++ b/install.yaml @@ -18,7 +18,12 @@ project_files: - core-dev/src/Command/TestCommand.php - core-dev/src/Command/TestBrowserCommand.php - core-dev/src/Command/UninstallCommand.php - - core-dev/src/Command/PhpCsCommand.php + - core-dev/src/Command/LintPhpCsCommand.php + - core-dev/src/Command/LintPhpStanCommand.php + - core-dev/src/Command/LintCssCommand.php + - core-dev/src/Command/LintJsCommand.php + - core-dev/src/Command/LintCspellCommand.php + - core-dev/src/Command/LintCommand.php post_install_actions: - cp core-dev/phpunit-chrome.xml ../core/phpunit.xml From aa497f78c78688e82fce94e6282d87b7f60c3ca3 Mon Sep 17 00:00:00 2001 From: Sally Young Date: Mon, 7 Oct 2024 11:10:49 +0100 Subject: [PATCH 6/6] Fixes to lint commands and add documentation --- README.md | 17 +++++++++++++++++ core-dev/phpunit-chrome.xml | 3 ++- core-dev/phpunit-firefox.xml | 3 ++- core-dev/src/Command/LintCommand.php | 2 +- core-dev/src/Command/LintCspellCommand.php | 10 ++++++++-- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 18e37d5..9783747 100644 --- a/README.md +++ b/README.md @@ -72,3 +72,20 @@ a11y test for a custom admin theme ``` ddev nightwatch --tag a11y:admin --adminTheme seven ``` + +## Core Linting + +This will run static tests against core standards. + +``` +ddev drupal lint:phpstan +ddev drupal lint:phpcs +ddev drupal lint:js +ddev drupal lint:css +ddev drupal lint:cspell +# CSpell against only modified files +ddev drupal lint:cspell --modified-only +``` + +You can run all linting with `ddev drupal lint`, or with fail-fast turned on: +`ddev drupal lint --stop-on-failure` \ No newline at end of file diff --git a/core-dev/phpunit-chrome.xml b/core-dev/phpunit-chrome.xml index fc02f8e..b0e3abf 100644 --- a/core-dev/phpunit-chrome.xml +++ b/core-dev/phpunit-chrome.xml @@ -1,5 +1,6 @@ @@ -35,7 +36,7 @@ @@ -35,7 +36,7 @@