Exemple #1
0
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  *
  * @return int|void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $this->input = $input;
     $gitHooksPath = $this->paths()->getGitHooksDir();
     $resourceHooksPath = $this->paths()->getGitHookTemplatesDir() . $this->grumPHP->getHooksPreset();
     $resourceHooksPath = $this->paths()->getPathWithTrailingSlash($resourceHooksPath);
     $customHooksPath = $this->paths()->getPathWithTrailingSlash($this->grumPHP->getHooksDir());
     // Some git clients do not automatically create a git hooks folder.
     if (!$this->filesystem->exists($gitHooksPath)) {
         $this->filesystem->mkdir($gitHooksPath);
         $output->writeln(sprintf('<fg=yellow>Created git hooks folder at: %s</fg=yellow>', $gitHooksPath));
     }
     foreach (self::$hooks as $hook) {
         $gitHook = $gitHooksPath . $hook;
         $hookTemplate = new SplFileInfo($resourceHooksPath . $hook);
         if ($customHooksPath && $this->filesystem->exists($customHooksPath . $hook)) {
             $hookTemplate = new SplFileInfo($customHooksPath . $hook);
         }
         if (!$this->filesystem->exists($hookTemplate)) {
             throw new RuntimeException(sprintf('Could not find hook template for %s at %s.', $hook, $hookTemplate));
         }
         $content = $this->parseHookBody($hook, $hookTemplate);
         $this->filesystem->dumpFile($gitHook, $content);
         $this->filesystem->chmod($gitHook, 0775);
     }
     $output->writeln('<fg=yellow>Watch out! GrumPHP is sniffing your commits!<fg=yellow>');
 }
Exemple #2
0
 /**
  * @param $defaultPath
  *
  * @return string
  */
 private function locateConfigFileWithDistSupport($defaultPath)
 {
     $distPath = strpos($defaultPath, -5) !== '.dist' ? $defaultPath . '.dist' : $defaultPath;
     if ($this->filesystem->exists($defaultPath) || !$this->filesystem->exists($distPath)) {
         return $defaultPath;
     }
     return $distPath;
 }
Exemple #3
0
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  *
  * @return int|void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $gitHooksPath = $this->paths()->getGitHooksDir();
     foreach (InitCommand::$hooks as $hook) {
         $hookPath = $gitHooksPath . $hook;
         if (!$this->filesystem->exists($hookPath)) {
             continue;
         }
         $this->filesystem->remove($hookPath);
     }
     $output->writeln('<fg=yellow>GrumPHP stopped sniffing your commits! Too bad ...<fg=yellow>');
 }
Exemple #4
0
 /**
  * @param $path
  *
  * @return bool
  */
 public function pathValidator($path)
 {
     if (!$this->filesystem->exists($path)) {
         throw new RuntimeException(sprintf('The path %s could not be found!', $path));
     }
     return $path;
 }
 function it_should_locate_config_file_on_empty_composer_configuration(Filesystem $filesystem, PackageInterface $package)
 {
     $package->getExtra()->willReturn([]);
     $filesystem->exists($this->pathArgument('/composer/grumphp.yml'))->willReturn(true);
     $filesystem->isAbsolutePath($this->pathArgument('/composer/grumphp.yml'))->willReturn(true);
     $this->locate('/composer', $package)->shouldMatch($this->pathRegex('/composer/grumphp.yml'));
 }
Exemple #6
0
 /**
  * @param string $path path to grumphp.yml
  *
  * @return ContainerBuilder
  */
 public static function buildFromConfiguration($path)
 {
     $container = new ContainerBuilder();
     $container->setProxyInstantiator(new RuntimeInstantiator());
     // Add compiler passes:
     $container->addCompilerPass(new Compiler\ExtensionCompilerPass());
     $container->addCompilerPass(new Compiler\PhpParserCompilerPass());
     $container->addCompilerPass(new Compiler\TaskCompilerPass());
     $container->addCompilerPass(new RegisterListenersPass('event_dispatcher', 'grumphp.event_listener', 'grumphp.event_subscriber'));
     // Load basic service file + custom user configuration
     $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../../../resources/config'));
     $loader->load('formatter.yml');
     $loader->load('linters.yml');
     $loader->load('parameters.yml');
     $loader->load('parsers.yml');
     $loader->load('services.yml');
     $loader->load('subscribers.yml');
     $loader->load('tasks.yml');
     $loader->load('util.yml');
     // Load grumphp.yml file:
     $filesystem = new Filesystem();
     if ($filesystem->exists($path)) {
         $loader->load($path);
     }
     // Compile configuration to make sure that tasks are added to the taskrunner
     $container->compile();
     return $container;
 }
Exemple #7
0
 /**
  * Find the relative bin directory
  *
  * @return string
  */
 public function getBinDir()
 {
     $binDir = $this->config->getBinDir();
     if (!$this->fileSystem->exists($binDir)) {
         throw new RuntimeException('The configured BIN directory could not be found.');
     }
     return $this->getRelativePath($binDir);
 }
Exemple #8
0
 /**
  * {@inheritdoc}
  */
 public function run(ContextInterface $context)
 {
     $configuration = $this->getConfiguration();
     $percentage = round(min(100, max(0, (double) $configuration['level'])), 2);
     $cloverFile = $configuration['clover_file'];
     if (!$this->filesystem->exists($cloverFile)) {
         return TaskResult::createFailed($this, $context, 'Invalid input file provided');
     }
     if (!$percentage) {
         return TaskResult::createFailed($this, $context, 'An integer checked percentage must be given as second parameter');
     }
     $xml = new SimpleXMLElement($this->filesystem->readFromFileInfo(new SplFileInfo($cloverFile)));
     $totalElements = (string) current($xml->xpath('/coverage/project/metrics/@elements'));
     $checkedElements = (string) current($xml->xpath('/coverage/project/metrics/@coveredelements'));
     $coverage = round($checkedElements / $totalElements * 100, 2);
     if ($coverage < $percentage) {
         $message = sprintf('Code coverage is %1$d%%, which is below the accepted %2$d%%' . PHP_EOL, $coverage, $percentage);
         return TaskResult::createFailed($this, $context, $message);
     }
     return TaskResult::createPassed($this, $context);
 }