Skip to content

Commit

Permalink
Make it possible to support dist files.
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Mar 30, 2016
1 parent 7cdf12f commit 9583413
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ bin
!bin/grumphp
vendor
composer.lock
grumphp.yml
File renamed without changes.
5 changes: 5 additions & 0 deletions resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ services:
- '%bin_dir%'
- '@executable_finder'

locator.configuration_file:
class: GrumPHP\Locator\ConfigurationFile
arguments:
- '@filesystem'

locator.changed_files:
class: GrumPHP\Locator\ChangedFiles
arguments:
Expand Down
34 changes: 12 additions & 22 deletions src/GrumPHP/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GrumPHP\Configuration\ContainerFactory;
use GrumPHP\IO\ConsoleIO;
use GrumPHP\Locator\ConfigurationFile;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Symfony\Component\Console\Application as SymfonyConsole;
Expand All @@ -24,7 +25,6 @@ class Application extends SymfonyConsole
{
const APP_NAME = 'GrumPHP';
const APP_VERSION = '0.8.0';
const APP_CONFIG_FILE = 'grumphp.yml';

/**
* @var ContainerBuilder
Expand All @@ -36,15 +36,21 @@ class Application extends SymfonyConsole
*/
protected $configDefaultPath;

/**
* @var string
*/
protected $filesystem;

/**
* Set up application:
*/
public function __construct()
{
parent::__construct(self::APP_NAME, self::APP_VERSION);

$this->filesystem = new Filesystem();
$this->container = $this->getContainer();
$this->setDispatcher($this->container->get('event_dispatcher'));

parent::__construct(self::APP_NAME, self::APP_VERSION);
}

/**
Expand All @@ -69,23 +75,14 @@ protected function getDefaultInputDefinition()
/**
* @return string
*/
protected function getConfigDefaultPath()
public function getConfigDefaultPath()
{
if ($this->configDefaultPath) {
return $this->configDefaultPath;
}

$this->configDefaultPath = getcwd() . DIRECTORY_SEPARATOR . self::APP_CONFIG_FILE;
$composerFile = 'composer.json';

if (!file_exists($composerFile)) {
return $this->configDefaultPath;
}

$composer = json_decode(file_get_contents($composerFile), true);
if (isset($composer['extra']['grumphp']['config-default-path'])) {
$this->configDefaultPath = $composer['extra']['grumphp']['config-default-path'];
}
$locator = new ConfigurationFile($this->filesystem);
$this->configDefaultPath = $locator->locate(getcwd());

return $this->configDefaultPath;
}
Expand Down Expand Up @@ -160,13 +157,6 @@ protected function getContainer()
$input = new ArgvInput();
$configPath = $input->getParameterOption(array('--config', '-c'), $this->getConfigDefaultPath());

// Make sure to set the full path when it is declared relative
// This will fix some issues in windows.
$filesystem = new Filesystem();
if (!$filesystem->isAbsolutePath($configPath)) {
$configPath = getcwd() . DIRECTORY_SEPARATOR . $configPath;
}

// Build the service container:
$this->container = ContainerFactory::buildFromConfiguration($configPath);

Expand Down
28 changes: 25 additions & 3 deletions src/GrumPHP/Console/Command/Git/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GrumPHP\Configuration\GrumPHP;
use GrumPHP\Console\Helper\PathsHelper;
use GrumPHP\Exception\FileNotFoundException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -130,16 +131,37 @@ protected function parseHookBody($hook, $templateFile)
*/
protected function generateHookCommand($command)
{
$configPath = rtrim($this->paths()->getRelativePath($this->input->getOption('config')), '/\\');
$this->processBuilder->setArguments(array(
$this->paths()->getBinCommand('grumphp', true),
$command,
'--config=' . $configPath,
$command
));

if ($configFile = $this->useExoticConfigFile()) {
$this->processBuilder->add('--config=%s', $configFile);
}

return $this->processBuilder->getProcess()->getCommandLine();
}

/**
* This method will tell you which exotic configuration file should be used.
*
* @return null|string
*/
protected function useExoticConfigFile()
{
try {
$configPath = $this->paths()->getAbsolutePath($this->input->getOption('config'));
if ($configPath != $this->getApplication()->getConfigDefaultPath()) {
return rtrim($this->paths()->getRelativePath($configPath), '/\\');
}
} catch (FileNotFoundException $e) {
// no config file should be set.
}

return null;
}

/**
* @return PathsHelper
*/
Expand Down
15 changes: 13 additions & 2 deletions src/GrumPHP/Console/Helper/PathsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function getGitHookExecutionPath()
{
$gitPath = $this->getGitDir();

return $this->fileSystem->makePathRelative($this->getWorkingDir(), realpath($gitPath));
return $this->fileSystem->makePathRelative($this->getWorkingDir(), $this->getAbsolutePath($gitPath));
}

/**
Expand Down Expand Up @@ -198,14 +198,25 @@ public function getBinCommand($command, $forceUnix = false)
* @throws FileNotFoundException If file doesn't exists
*/
public function getRelativePath($path)
{
$realpath = $this->getAbsolutePath($path);
return $this->fileSystem->makePathRelative($realpath, $this->getWorkingDir());
}

/**
* @param $path
*
* @return mixed
*/
public function getAbsolutePath($path)
{
$path = trim($path);
$realpath = realpath($path);
if (false === $realpath) {
throw new FileNotFoundException($path);
}

return $this->fileSystem->makePathRelative($realpath, $this->getWorkingDir());
return $realpath;
}

/**
Expand Down
87 changes: 87 additions & 0 deletions src/GrumPHP/Locator/ConfigurationFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace GrumPHP\Locator;

use Symfony\Component\Filesystem\Filesystem;

/**
* Class ConfigurationFile
*
* @package GrumPHP\Locator
*/
class ConfigurationFile
{
const APP_CONFIG_FILE = 'grumphp.yml';

/**
* @var Filesystem
*/
private $filesystem;

/**
* ConfigurationFile constructor.
*
* @param Filesystem $filesystem
*/
public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
}

/**
* @param string $workingDir
*
* @return string
*/
public function locate($workingDir)
{
$defaultPath = $workingDir . DIRECTORY_SEPARATOR . self::APP_CONFIG_FILE;
$defaultPath = $this->locateConfigFileWithDistSupport($defaultPath);
$defaultPath = $this->useConfigPathFromComposer($workingDir, $defaultPath);

// Make sure to set the full path when it is declared relative
// This will fix some issues in windows.
if (!$this->filesystem->isAbsolutePath($defaultPath)) {
$defaultPath = $workingDir . DIRECTORY_SEPARATOR . $defaultPath;
}

return $defaultPath;
}

/**
* @param $workingDir
* @param $defaultPath
*
* @return string
*/
private function useConfigPathFromComposer($workingDir, $defaultPath)
{
$composerFile = $workingDir . DIRECTORY_SEPARATOR . 'composer.json';
if (!$this->filesystem->exists($composerFile)) {
return $defaultPath;
}

$composerData = json_decode(file_get_contents($composerFile), true);
if (!isset($composerData['extra']['grumphp']['config-default-path'])) {
return $defaultPath;
}

$composerDefaultPath = $composerData['extra']['grumphp']['config-default-path'];
return $this->locateConfigFileWithDistSupport($composerDefaultPath);
}

/**
* @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;
}
}

0 comments on commit 9583413

Please sign in to comment.