|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Webs\QA\Command; |
| 4 | + |
| 5 | +use Composer\Command\BaseCommand; |
| 6 | +use Symfony\Component\Console\Input\InputArgument; |
| 7 | +use Symfony\Component\Console\Input\InputInterface; |
| 8 | +use Symfony\Component\Console\Input\InputOption; |
| 9 | +use Symfony\Component\Console\Output\OutputInterface; |
| 10 | +use Symfony\Component\Process\Exception\ProcessFailedException; |
| 11 | +use Symfony\Component\Process\Process; |
| 12 | + |
| 13 | +class CodeBeautifierFixer extends BaseCommand |
| 14 | +{ |
| 15 | + protected $input; |
| 16 | + protected $output; |
| 17 | + protected $source = array('src','app','tests'); |
| 18 | + protected $description = 'Code Beautifier and Fixer'; |
| 19 | + |
| 20 | + protected function configure() |
| 21 | + { |
| 22 | + // Alias is composer qa:cbf |
| 23 | + $this->setName('qa:code-beautifier-fixer') |
| 24 | + ->setDescription($this->description) |
| 25 | + ->addArgument( |
| 26 | + 'source', |
| 27 | + InputArgument::IS_ARRAY|InputArgument::OPTIONAL, |
| 28 | + 'List of directories to search Default:src,app,tests' |
| 29 | + ) |
| 30 | + ->addOption( |
| 31 | + 'standard', |
| 32 | + null, |
| 33 | + InputOption::VALUE_REQUIRED, |
| 34 | + 'List of standards Default:PSR1,PSR2', |
| 35 | + 'PSR1,PSR2' |
| 36 | + ) |
| 37 | + ->addOption( |
| 38 | + 'no-diff', |
| 39 | + null, |
| 40 | + InputOption::VALUE_NONE, |
| 41 | + 'Ignore changed files Important: Using `git stash`, maybe fail in some cases' |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 46 | + { |
| 47 | + $start = microtime(true); |
| 48 | + $this->input = $input; |
| 49 | + $this->output = $output; |
| 50 | + $this->output->writeln('<comment>Running ' . $this->description . '...</comment>'); |
| 51 | + |
| 52 | + $cbf = 'vendor/bin/phpcbf'; |
| 53 | + if(!file_exists($cbf)){ |
| 54 | + $process = new Process('phpcbf --help'); |
| 55 | + $process->run(); |
| 56 | + if ($process->isSuccessful()) { |
| 57 | + $cbf = 'phpcbf'; |
| 58 | + } else { |
| 59 | + throw new ProcessFailedException($process); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + $process = new Process($cbf . ' --version'); |
| 64 | + $process->run(); |
| 65 | + $this->output->writeln($process->getOutput()); |
| 66 | + |
| 67 | + if($input->getOption('no-diff')){ |
| 68 | + $process = new Process('git stash'); |
| 69 | + $process->run(); |
| 70 | + if(!$process->isSuccessful()){ |
| 71 | + throw new ProcessFailedException($process); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + $cmd = $cbf . ' --standard='.$input->getOption('standard') . ' ' . $this->getSource(); |
| 76 | + $process = new Process($cmd); |
| 77 | + $process->setTimeout(3600); |
| 78 | + $process->run(); |
| 79 | + $exitCode = $process->getExitCode(); |
| 80 | + |
| 81 | + $changes = true; |
| 82 | + if(strpos($process->getOutput(), 'No fixable errors were found') !== false){ |
| 83 | + $changes = false; |
| 84 | + $this->output->writeln('No changes'.PHP_EOL); |
| 85 | + } |
| 86 | + |
| 87 | + $changed = ''; |
| 88 | + if($changes){ |
| 89 | + $process = new Process('git status -s'); |
| 90 | + $process->run(); |
| 91 | + $changed = $process->getOutput(); |
| 92 | + $this->output->writeln($changed); |
| 93 | + } |
| 94 | + |
| 95 | + $filesChanged = count(preg_split('/\n|\r/', $changed)); |
| 96 | + if($changes && $filesChanged < 25 && $input->getOption('no-diff')){ |
| 97 | + $process = new Process( |
| 98 | + 'git -c color.ui=always diff --compaction-heuristic -U0 --patch --minimal --patience' |
| 99 | + ); |
| 100 | + $process->run(); |
| 101 | + $this->output->writeln($process->getOutput()); |
| 102 | + } |
| 103 | + |
| 104 | + if($input->getOption('no-diff')){ |
| 105 | + $process = new Process('git stash apply'); |
| 106 | + $process->run(); |
| 107 | + if(!$process->isSuccessful()){ |
| 108 | + throw new ProcessFailedException($process); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + $end = microtime(true); |
| 113 | + $time = round($end-$start); |
| 114 | + |
| 115 | + $this->output->writeln('<comment>Command executed `' . $cmd . '` in ' . $time . ' seconds</comment>'); |
| 116 | + exit($exitCode); |
| 117 | + } |
| 118 | + |
| 119 | + protected function getSource() |
| 120 | + { |
| 121 | + if($this->input->getArgument('source')){ |
| 122 | + $this->source = $this->input->getArgument('source'); |
| 123 | + } |
| 124 | + |
| 125 | + $dirs = array(); |
| 126 | + foreach ($this->source as $dir) { |
| 127 | + if(is_dir($dir)){ |
| 128 | + $dirs[] = $dir; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return implode(' ', $dirs); |
| 133 | + } |
| 134 | +} |
0 commit comments