/** * @param ProcessArgumentsCollection $defaultArguments * @param ProcessBuilder $processBuilder * @return string */ public function formatErrorMessage(ProcessArgumentsCollection $defaultArguments, ProcessBuilder $processBuilder) { if (empty($this->suggestedFiles)) { return ''; } $defaultArguments->addArgumentArray('%s', $this->suggestedFiles); return sprintf('%sYou can fix some errors by running following command:%s', PHP_EOL . PHP_EOL, PHP_EOL . $processBuilder->buildProcess($defaultArguments)->getCommandLine()); }
/** * @param ProcessArgumentsCollection $arguments * * @return Process * @throws \GrumPHP\Exception\PlatformException */ public function buildProcess(ProcessArgumentsCollection $arguments) { $builder = SymfonyProcessBuilder::create($arguments->getValues()); $builder->setTimeout($this->config->getProcessTimeout()); $process = $builder->getProcess(); $this->logProcessInVerboseMode($process); $this->guardWindowsCmdMaxInputStringLimitation($process); return $process; }
/** * {@inheritdoc} */ public function run(ContextInterface $context) { $files = $context->getFiles()->name('*.php'); if (0 === count($files)) { return; } $config = $this->getConfiguration(); $arguments = $this->processBuilder->createArgumentsForCommand('php-cs-fixer'); $arguments->add('--format=json'); $arguments->add('--dry-run'); $arguments->addOptionalArgument('--level=%s', $config['level']); $arguments->addOptionalArgument('--config=%s', $config['config']); $arguments->addOptionalArgument('--config-file=%s', $config['config_file']); $arguments->addOptionalArgument('--verbose', $config['verbose']); $arguments->addOptionalCommaSeparatedArgument('--fixers=%s', $config['fixers']); $arguments->add('fix'); $messages = array(); $suggest = array('You can fix all errors by running following commands:'); $errorCount = 0; foreach ($files as $file) { $fileArguments = new ProcessArgumentsCollection($arguments->getValues()); $fileArguments->add($file); $process = $this->processBuilder->buildProcess($fileArguments); $process->run(); if (!$process->isSuccessful()) { $output = $process->getOutput(); $json = json_decode($output, true); if ($json) { if (isset($json['files'][0]['name']) && isset($json['files'][0]['appliedFixers'])) { $messages[] = sprintf('%s) %s (%s)', ++$errorCount, $json['files'][0]['name'], implode(',', $json['files'][0]['appliedFixers'])); } elseif (isset($json['files'][0]['name'])) { $messages[] = sprintf('%s) %s', ++$errorCount, $json['files'][0]['name']); } $suggest[] = str_replace(array("'--dry-run' ", "'--format=json' "), '', $process->getCommandLine()); } else { $messages[] = $output; } } } if (count($messages)) { throw new RuntimeException(implode("\n", $messages) . "\n" . "\n" . implode("\n", $suggest)); } }
/** * @param ContextInterface $context * @param ProcessArgumentsCollection $arguments * @param FilesCollection $files * * @return TaskResult */ protected function runOnChangedFiles(ContextInterface $context, ProcessArgumentsCollection $arguments, FilesCollection $files) { $hasErrors = false; $messages = array(); $suggestions = array(); foreach ($files as $file) { $fileArguments = new ProcessArgumentsCollection($arguments->getValues()); $fileArguments->add($file); $process = $this->processBuilder->buildProcess($fileArguments); $process->run(); if (!$process->isSuccessful()) { $hasErrors = true; $messages[] = $this->formatter->format($process); $suggestions[] = $this->formatter->formatSuggestion($process); } } if ($hasErrors) { $errorMessage = $this->formatter->formatErrorMessage($messages, $suggestions); return TaskResult::createFailed($this, $context, $errorMessage); } return TaskResult::createPassed($this, $context); }
/** * {@inheritdoc} */ public function run(ContextInterface $context) { $files = $context->getFiles()->name('*.php'); if (0 === count($files)) { return; } $config = $this->getConfiguration(); $arguments = ProcessArgumentsCollection::forExecutable($this->getCommandLocation()); $arguments->addOptionalArgument('--configuration=%s', $config['config_file']); $this->processBuilder->setArguments($arguments->getValues()); $process = $this->processBuilder->getProcess(); $process->run(); if (!$process->isSuccessful()) { throw new RuntimeException($process->getOutput()); } }
/** * {@inheritdoc} */ public function run(ContextInterface $context) { $files = $context->getFiles()->name('*.php'); if (0 === count($files)) { return; } $config = $this->getConfiguration(); $arguments = ProcessArgumentsCollection::forExecutable($this->getCommandLocation()); $arguments->addRequiredArgument('--standard=%s', $config['standard']); $arguments->addOptionalArgument('--warning-severity=0', !$config['show_warnings']); $arguments->addOptionalArgument('--tab-width=%s', $config['tab_width']); $arguments->addOptionalCommaSeparatedArgument('--sniffs=%s', $config['sniffs']); $arguments->addOptionalCommaSeparatedArgument('--ignore=%s', $config['ignore_patterns']); $arguments->addFiles($files); $this->processBuilder->setArguments($arguments->getValues()); $process = $this->processBuilder->getProcess(); $process->run(); if (!$process->isSuccessful()) { throw new RuntimeException($process->getOutput()); } }
/** * {@inheritdoc} */ public function run(ContextInterface $context) { $files = $context->getFiles()->name('*.php'); if (0 === count($files)) { return; } $config = $this->getConfiguration(); if (empty($config['keywords'])) { return; } $arguments = ProcessArgumentsCollection::forExecutable($this->getCommandLocation()); $arguments->add('grep'); $arguments->add('--cached'); $arguments->add('-n'); $arguments->addArgumentArray('-e %s', $config['keywords']); $arguments->addFiles($files); $this->processBuilder->setArguments($arguments->getValues()); $process = $this->processBuilder->getProcess(); $process->run(); if ($process->isSuccessful()) { throw new RuntimeException(sprintf("You have blacklisted keywords in your commit:\n%s", $process->getOutput())); } }
/** * @param ProcessArgumentsCollection $arguments * @param array $config * @return ProcessArgumentsCollection */ protected function addArgumentsFromConfig(ProcessArgumentsCollection $arguments, array $config) { $arguments->addOptionalArgument('--standard=%s', $config['standard']); $arguments->addOptionalArgument('--warning-severity=0', !$config['show_warnings']); $arguments->addOptionalArgument('--tab-width=%s', $config['tab_width']); $arguments->addOptionalArgument('--encoding=%s', $config['encoding']); $arguments->addOptionalCommaSeparatedArgument('--sniffs=%s', $config['sniffs']); $arguments->addOptionalCommaSeparatedArgument('--ignore=%s', $config['ignore_patterns']); return $arguments; }
/** * @param ProcessArgumentsCollection $arguments * * @return Process */ public function buildProcess(ProcessArgumentsCollection $arguments) { $builder = SymfonyProcessBuilder::create($arguments->getValues()); return $builder->getProcess(); }
/** * @param ProcessArgumentsCollection $arguments * * @return Process */ public function buildProcess(ProcessArgumentsCollection $arguments) { $builder = SymfonyProcessBuilder::create($arguments->getValues()); $builder->setTimeout($this->config->getProcessTimeout()); return $builder->getProcess(); }