-
Notifications
You must be signed in to change notification settings - Fork 438
feat: Add task for Twig-CS-Fixer #1131
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
92ae07d
feat: Add task for twig-cs-fixer
e58e060
doc: Fix md header
85f4fa1
Update doc/tasks.md
antoniovj1 0897abf
Update doc/tasks/twigcsfixer.md
antoniovj1 9650c89
feat: Improve file filtering and add missing config
3fc91de
fix: Remove useless config params. Improve tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,130 @@ | ||
# 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: 'NOTICE' | ||
config: ~ | ||
report: 'text' | ||
fix: false | ||
no-cache: false | ||
debug: false | ||
quiet: false | ||
version: false | ||
ansi: false | ||
no-ansi: false | ||
no-interaction: false | ||
verbose: false | ||
triggered_by: ['twig'] | ||
``` | ||
|
||
**paths** | ||
|
||
*Default: null* | ||
|
||
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. | ||
|
||
|
||
**fix** | ||
|
||
*Default: false* | ||
|
||
Fix the violations. | ||
|
||
**no-cache** | ||
|
||
*Default: false* | ||
|
||
Do not use cache. | ||
|
||
**debug** | ||
|
||
*Default: false* | ||
|
||
Display debugging information. | ||
|
||
**quiet** | ||
|
||
*Default: false* | ||
|
||
Do not output any message. | ||
|
||
**version** | ||
|
||
*Default: false* | ||
|
||
Display this application version. | ||
|
||
**ansi** | ||
|
||
*Default: false* | ||
|
||
Force ANSI output. | ||
|
||
**no-ansi** | ||
|
||
*Default: false* | ||
|
||
Disable ANSI output. | ||
|
||
**no-interaction** | ||
|
||
*Default: false* | ||
|
||
Do not ask any interactive question. | ||
|
||
**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 hidden or 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 hidden or 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,118 @@ | ||
<?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' => 'NOTICE', | ||
veewee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'config' => null, | ||
'report' => 'text', | ||
'fix' => false, | ||
veewee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'no-cache' => false, | ||
'debug' => false, | ||
veewee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'quiet' => false, | ||
'version' => false, | ||
'ansi' => false, | ||
veewee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'no-ansi' => false, | ||
'no-interaction' => false, | ||
'verbose' => false, | ||
'triggered_by' => ['twig'], | ||
]); | ||
|
||
$resolver->addAllowedTypes('paths', ['array']); | ||
$resolver->addAllowedTypes('level', ['string']); | ||
$resolver->addAllowedTypes('config', ['null', 'string']); | ||
$resolver->addAllowedTypes('report', ['string']); | ||
$resolver->addAllowedTypes('fix', ['bool']); | ||
$resolver->addAllowedTypes('no-cache', ['bool']); | ||
$resolver->addAllowedTypes('debug', ['bool']); | ||
$resolver->addAllowedTypes('quiet', ['bool']); | ||
$resolver->addAllowedTypes('version', ['bool']); | ||
$resolver->addAllowedTypes('ansi', ['bool']); | ||
$resolver->addAllowedTypes('no-ansi', ['bool']); | ||
$resolver->addAllowedTypes('no-interaction', ['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); | ||
veewee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if ($context instanceof RunContext) { | ||
foreach ($config['paths'] as $path) { | ||
antoniovj1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$arguments->add($path); | ||
} | ||
} | ||
|
||
$arguments->addOptionalArgument('--level=%s', $config['level']); | ||
$arguments->addOptionalArgument('--config=%s', $config['config']); | ||
$arguments->addOptionalArgument('--report=%s', $config['report']); | ||
|
||
$arguments->addOptionalArgument('--fix', $config['fix']); | ||
$arguments->addOptionalArgument('--no-cache', $config['no-cache']); | ||
$arguments->addOptionalArgument('--debug', $config['debug']); | ||
$arguments->addOptionalArgument('--quiet', $config['quiet']); | ||
$arguments->addOptionalArgument('--version', $config['version']); | ||
$arguments->addOptionalArgument('--ansi', $config['ansi']); | ||
$arguments->addOptionalArgument('--no-ansi', $config['no-ansi']); | ||
$arguments->addOptionalArgument('--no-interaction', $config['no-interaction']); | ||
$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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.