-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1131 from antoniovj1/v2.x
feat: Add task for Twig-CS-Fixer
- Loading branch information
Showing
6 changed files
with
428 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Twig-CS-Fixer | ||
|
||
Check and fix Twig coding standard using [VincentLanglet/Twig-CS-Fixer](https://github.com/VincentLanglet/Twig-CS-Fixer). | ||
|
||
***Composer*** | ||
|
||
``` | ||
composer require --dev "vincentlanglet/twig-cs-fixer:>=2" | ||
``` | ||
|
||
***Config*** | ||
|
||
The task lives under the `twigcsfixer` namespace and has following configurable parameters: | ||
|
||
```yaml | ||
# grumphp.yml | ||
grumphp: | ||
tasks: | ||
twigcsfixer: | ||
paths: [] | ||
level: ~ | ||
config: ~ | ||
report: 'text' | ||
no-cache: false | ||
verbose: false | ||
triggered_by: ['twig'] | ||
``` | ||
**paths** | ||
*Default: []* | ||
By default, current folder will be used. | ||
On precommit only changed files that live in the paths will be passed as arguments. | ||
**level** | ||
*Default: 'notice'* | ||
The level of the messages to display (possibles values are : 'notice', 'warning', 'error'). | ||
**config** | ||
*Default: null* | ||
Path to a `.twig-cs-fixer.php` config file. If not set, the default config will be used. | ||
|
||
You can check config file [here](https://github.com/VincentLanglet/Twig-CS-Fixer/blob/main/docs/configuration.md). | ||
|
||
**report** | ||
|
||
*Default: 'text'* | ||
|
||
The `--report` option allows to choose the output format for the linter report. | ||
|
||
Supported formats are: | ||
- `text` selected by default. | ||
- `checkstyle` following the common checkstyle XML schema. | ||
- `github` if you want annotations on GitHub actions. | ||
- `junit` following JUnit schema XML from Jenkins. | ||
- `null` if you don't want any reporting. | ||
|
||
**no-cache** | ||
|
||
*Default: false* | ||
|
||
Do not use cache. | ||
|
||
**verbose** | ||
|
||
*Default: false* | ||
|
||
Increase the verbosity of messages. | ||
|
||
**triggered_by** | ||
|
||
*Default: [twig]* | ||
|
||
This option will specify which file extensions will trigger this task. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?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; | ||
use GrumPHP\Fixer\Provider\FixableProcessResultProvider; | ||
use Symfony\Component\Process\Process; | ||
|
||
/** | ||
* @extends AbstractExternalTask<ProcessFormatterInterface> | ||
*/ | ||
class TwigCsFixer extends AbstractExternalTask | ||
{ | ||
public static function getConfigurableOptions(): ConfigOptionsResolver | ||
{ | ||
$resolver = new OptionsResolver(); | ||
$resolver->setDefaults([ | ||
'paths' => [], | ||
'level' => null, | ||
'config' => null, | ||
'report' => 'text', | ||
'no-cache' => false, | ||
'verbose' => false, | ||
'triggered_by' => ['twig'], | ||
]); | ||
|
||
$resolver->addAllowedTypes('paths', ['array']); | ||
$resolver->addAllowedTypes('level', ['null', 'string']); | ||
$resolver->addAllowedTypes('config', ['null', 'string']); | ||
$resolver->addAllowedTypes('report', ['null', 'string']); | ||
$resolver->addAllowedTypes('no-cache', ['bool']); | ||
$resolver->addAllowedTypes('verbose', ['bool']); | ||
|
||
return ConfigOptionsResolver::fromOptionsResolver($resolver); | ||
} | ||
|
||
public function canRunInContext(ContextInterface $context): bool | ||
{ | ||
return $context instanceof GitPreCommitContext || $context instanceof RunContext; | ||
} | ||
|
||
public function run(ContextInterface $context): TaskResultInterface | ||
{ | ||
$config = $this->getConfig()->getOptions(); | ||
$files = $context->getFiles() | ||
->extensions($config['triggered_by']) | ||
->paths($config['paths']); | ||
|
||
if (\count($files) === 0) { | ||
return TaskResult::createSkipped($this, $context); | ||
} | ||
|
||
$arguments = $this->processBuilder->createArgumentsForCommand('twig-cs-fixer'); | ||
$arguments->add('lint'); | ||
|
||
if ($context instanceof GitPreCommitContext) { | ||
$arguments->addFiles($files); | ||
} | ||
|
||
if ($context instanceof RunContext) { | ||
$arguments->addArgumentArray('%s', $config['paths']); | ||
} | ||
|
||
$arguments->addOptionalArgument('--level=%s', $config['level']); | ||
$arguments->addOptionalArgument('--config=%s', $config['config']); | ||
$arguments->addOptionalArgument('--report=%s', $config['report']); | ||
|
||
$arguments->addOptionalArgument('--no-cache', $config['no-cache']); | ||
$arguments->addOptionalArgument('--verbose', $config['verbose']); | ||
|
||
$process = $this->processBuilder->buildProcess($arguments); | ||
$process->run(); | ||
|
||
if (!$process->isSuccessful()) { | ||
return FixableProcessResultProvider::provide( | ||
TaskResult::createFailed($this, $context, $this->formatter->format($process)), | ||
function () use ($arguments): Process { | ||
$arguments->add('--fix'); | ||
return $this->processBuilder->buildProcess($arguments); | ||
} | ||
); | ||
} | ||
|
||
return TaskResult::createPassed($this, $context); | ||
} | ||
} |
Oops, something went wrong.