Skip to content

Commit

Permalink
Merge pull request #13 from pluswerk/feature/grumphp-0.19
Browse files Browse the repository at this point in the history
✨ make bom fixer task compatible with grumphp 0.19
  • Loading branch information
Kanti committed Jun 15, 2020
2 parents 02bce40 + 8921601 commit 75d2c30
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ git:
php:
- 7.2
- 7.3
- 7.4
- nightly

env:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"require": {
"php": ">=7.2",
"phpro/grumphp": "0.16.* || 0.17.*"
"phpro/grumphp": "0.19.*"
},
"require-dev": {
"squizlabs/php_codesniffer": ">=3.5.0 <4.0.0"
Expand Down
8 changes: 2 additions & 6 deletions grumphp.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
parameters:
git_dir: .
bin_dir: vendor/bin
stop_on_failure: false
ignore_unstaged_changes: false #brocken
grumphp:
tasks:
composer:
with_dependencies: false
Expand All @@ -21,7 +17,7 @@ parameters:
detect_key_conflicts: true
phpcs:
standard: "PSR12"
warning_severity: 900000
warning_severity: 0
tab_width: 4
yamllint: ~
plus_bom_fixer:
Expand Down
54 changes: 34 additions & 20 deletions src/BomFixerTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@

use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\AbstractExternalTask;
use GrumPHP\Task\Config\EmptyTaskConfig;
use GrumPHP\Task\Config\TaskConfigInterface;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use GrumPHP\Task\Context\RunContext;
use GrumPHP\Task\TaskInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class BomFixerTask extends AbstractExternalTask
final class BomFixerTask implements TaskInterface
{
public function getName(): string
/** @var TaskConfigInterface */
private $config;

public function __construct()
{
return 'plus_bom_fixer';
$this->config = new EmptyTaskConfig();
}

public function getConfigurableOptions(): OptionsResolver
public static function getConfigurableOptions(): OptionsResolver
{
$resolver = new OptionsResolver();
$resolver->setDefaults(
Expand All @@ -31,15 +36,27 @@ public function getConfigurableOptions(): OptionsResolver
return $resolver;
}

public function getConfig(): TaskConfigInterface
{
return $this->config;
}

public function withConfig(TaskConfigInterface $config): TaskInterface
{
$new = clone $this;
$new->config = $config;

return $new;
}

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

public function run(ContextInterface $context): TaskResultInterface
{
$config = $this->getConfiguration();
$files = $context->getFiles()->extensions($config['triggered_by']);
$files = $context->getFiles()->extensions($this->config->getOptions()['triggered_by']);
if (0 === count($files)) {
return TaskResult::createSkipped($this, $context);
}
Expand All @@ -64,18 +81,20 @@ public function run(ContextInterface $context): TaskResultInterface
}

if (count($shouldGetFixedLog) > 0) {
return TaskResult::createFailed(
$this,
$context,
implode(PHP_EOL, $shouldGetFixedLog) . PHP_EOL
$errorMessage = implode(PHP_EOL, $shouldGetFixedLog) . PHP_EOL
. 'you can use this to fix them:' . PHP_EOL
. $fixCommand
);
. $fixCommand;
return TaskResult::createFailed($this, $context, $errorMessage);
}
return TaskResult::createPassed($this, $context);
}

protected function fileInfoSearch(string $filename, string $search): bool
private function isFileWithBOM(string $filename): bool
{
return $this->fileInfoSearch($filename, 'BOM');
}

private function fileInfoSearch(string $filename, string $search): bool
{
$output = [];
exec('file ' . '"' . $filename . '"', $output, $returnVar);
Expand All @@ -84,9 +103,4 @@ protected function fileInfoSearch(string $filename, string $search): bool
}
return false;
}

public function isFileWithBOM(string $filename): bool
{
return $this->fileInfoSearch($filename, 'BOM');
}
}
3 changes: 1 addition & 2 deletions src/ExtensionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ class ExtensionLoader implements ExtensionInterface
public function load(ContainerBuilder $container)
{
return $container->register('task.plus_bom_fixer', BomFixerTask::class)
->addArgument(new Reference('config'))
->addArgument(new Reference('process_builder'))
->addArgument(new Reference('formatter.raw_process'))
->addTag('grumphp.task', ['config' => 'plus_bom_fixer']);
->addTag('grumphp.task', ['task' => 'plus_bom_fixer']);
}
}

0 comments on commit 75d2c30

Please sign in to comment.