Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1126 bin console task sf #1138

Merged
merged 11 commits into from
Jun 14, 2024
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ feel free to do this, but remember to follow this few simple rules:

## Branching strategy

- __Always__ base your changes on the `master` branch (all new development happens here),
- When you create Pull Request, always select `master` branch as target, otherwise it
- __Always__ base your changes on the latest version `v<version>.x` branch (all new development happens here),
- When you create Pull Request, always select `v<version>.x` branch as target, otherwise it
will be closed (this is selected by default).

## Coverage
Expand Down
2 changes: 2 additions & 0 deletions doc/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ grumphp:
securitychecker_symfony: ~
shell: ~
stylelint: ~
symfony_console: ~
tester: ~
twigcs: ~
twigcsfixer: ~
Expand Down Expand Up @@ -128,6 +129,7 @@ Every task has its own default configuration. It is possible to overwrite the pa
- [Symfony](tasks/securitychecker/symfony.md)
- [Shell](tasks/shell.md)
- [Stylelint](tasks/stylelint.md)
- [Symfony Console](tasks/symfony_console.md)
- [Tester](tasks/tester.md)
- [TwigCs](tasks/twigcs.md)
- [Twig-CS-Fixer](tasks/twigcsfixer.md)
Expand Down
92 changes: 92 additions & 0 deletions doc/tasks/symfony_console.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Symfony Console

Run a symfony console command.

## Requirements

This task requires a Symfony application with console component.

## Config

The task lives under the `symfony_console` namespace and has following configurable parameters:

```yaml
# grumphp.yml
grumphp:
tasks:
symfony_console:
veewee marked this conversation as resolved.
Show resolved Hide resolved
bin: "./bin/console"
command: [ "lint:container", "-vvv" ]
ignore_patterns: [],
whitelist_patterns: [],
triggered_by: ['php', 'yml', 'xml'],
run_always: false
```

### Parameters

**bin**

*Default: `./bin/console`*

Specify the path to the Symfony Console script.

**command**

veewee marked this conversation as resolved.
Show resolved Hide resolved
*Default: `[]`*

Specify the symfony command with defined options and arguments.
Verify the installed console component version for available commands `./bin/console list`

**ignore_patterns**

*Default: []*

This is a list of file patterns that will be ignored by the Symfony console task.
Leave this option blank to run the task for all files defined in the whitelist_patterns and or triggered_by extensions.

**whitelist_patterns**

*Default: []*

This is a list of regex patterns that will filter files to validate. With this option you can skip files like tests.
This option is used in relation with the parameter `triggered_by`.
For example: whitelist files in `src/FolderA/` and `src/FolderB/` you can use
```yaml
whitelist_patterns:
- /^src\/FolderA\/(.*)/
- /^src\/FolderB\/(.*)/
```

**triggered_by**

*Default: [php, yml, xml]*

This option will specify which file extensions will trigger the Symfony console task.
By default, altering a `php`, `yml`, `xml` file will trigger the task.
You can overwrite this option to whatever filetype you want to validate!

**run_always**

*Default: false*

If this is set to `true` the Symfony console task will be executed on every commit, regardless of any modified files.

## Multiple Console command tasks

[Run the same task twice with different configuration](../tasks.md#run-the-same-task-twice-with-different-configuration)

Specific running multiple symfony console commands:

```yaml
# grumphp.yml
grumphp:
lint-container:
command: [ "lint:container", "-vvv"]
metadata:
task: symfony_console
lint-yaml:
command: [ "lint:yaml", "path/to/yaml"]
metadata:
task: symfony_console
```
7 changes: 7 additions & 0 deletions resources/config/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,13 @@ services:
tags:
- {name: grumphp.task, task: stylelint}

GrumPHP\Task\SymfonyConsole:
arguments:
- '@process_builder'
- '@formatter.raw_process'
tags:
- {name: grumphp.task, task: symfony_console}

GrumPHP\Task\Tester:
class:
arguments:
Expand Down
80 changes: 80 additions & 0 deletions src/Task/SymfonyConsole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace GrumPHP\Task;

use GrumPHP\Formatter\ProcessFormatterInterface;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Config\ConfigOptionsResolver;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use GrumPHP\Task\Context\RunContext;
use Symfony\Component\OptionsResolver\OptionsResolver;

/** @extends AbstractExternalTask<ProcessFormatterInterface> */
class SymfonyConsole extends AbstractExternalTask
{
public static function getConfigurableOptions(): ConfigOptionsResolver
{
return ConfigOptionsResolver::fromOptionsResolver(
(new OptionsResolver())
->setDefaults([
'bin' => './bin/console',
'command' => [],
'ignore_patterns' => [],
'whitelist_patterns' => [],
'triggered_by' => ['php', 'yml', 'xml'],
'run_always' => false,
])
->addAllowedTypes('bin', ['string'])
->addAllowedTypes('command', ['string[]'])
->addAllowedTypes('ignore_patterns', ['array'])
->addAllowedTypes('whitelist_patterns', ['array'])
->addAllowedTypes('triggered_by', ['array'])
->addAllowedTypes('run_always', ['bool'])
->setAllowedValues(
'bin',
static fn(string $bin): bool => '' !== $bin
)
->setAllowedValues(
'command',
static fn(array $command): bool => '' !== \implode('', $command)
veewee marked this conversation as resolved.
Show resolved Hide resolved
)
->setAllowedValues('bin', static fn(string $bin): bool => '' !== $bin)
->setRequired('command')
);
}

public function canRunInContext(ContextInterface $context): bool
{
return ($context instanceof GitPreCommitContext || $context instanceof RunContext);
}

public function run(ContextInterface $context): TaskResultInterface
{
$config = $this->getConfig()->getOptions();
veewee marked this conversation as resolved.
Show resolved Hide resolved

$files = $context->getFiles()
->extensions($config['triggered_by'])
->paths($config['whitelist_patterns'] ?? [])
->notPaths($config['ignore_patterns'] ?? []);
if (!$config['run_always'] && 0 === \count($files)) {
return TaskResult::createSkipped($this, $context);
}

$arguments = $this->processBuilder->createArgumentsForCommand('php');
$arguments->add($config['bin']);
$arguments->addArgumentArray('%s', $config['command']);

$process = $this->processBuilder->buildProcess($arguments);
$process->run();

if (!$process->isSuccessful()) {
return TaskResult::createFailed($this, $context, $this->formatter->format($process));
}

return TaskResult::createPassed($this, $context);
}
}
3 changes: 1 addition & 2 deletions src/Test/Task/AbstractTaskTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use GrumPHP\Collection\FilesCollection;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Config\EmptyTaskConfig;
use GrumPHP\Task\Config\Metadata;
use GrumPHP\Task\Config\TaskConfig;
Expand Down Expand Up @@ -47,7 +46,7 @@ protected function setUp(): void
public function it_contains_configurable_options(array $input, ?array $output): void
{
if (!$output) {
self::expectException(ExceptionInterface::class);
$this->expectException(ExceptionInterface::class);
}

$resolver = $this->task::getConfigurableOptions();
Expand Down
Loading