Skip to content

Commit

Permalink
Fixing issues with exotic folder structures
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Mar 19, 2015
1 parent 6cd538d commit c1972a3
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 23 deletions.
2 changes: 1 addition & 1 deletion resources/hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Run the hook command.
# Note: this will be replaced by the real command during copy.
#
$(HOOK_COMMAND)
(cd "${HOOK_EXEC_PATH}" && exec $(HOOK_COMMAND))

# Validate exit code of above command
RC=$?
Expand Down
32 changes: 28 additions & 4 deletions src/GrumPHP/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Console\Application as SymfonyConsole;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Class Application
Expand All @@ -18,6 +19,11 @@ class Application extends SymfonyConsole
const APP_VERSION = '0.1.0';
const APP_CONFIG_FILE = 'grumphp.yml';

/**
* @var ContainerBuilder
*/
protected $container;

/**
* Set up application:
*/
Expand Down Expand Up @@ -49,7 +55,7 @@ protected function getDefaultInputDefinition()
*/
protected function getDefaultCommands()
{
$container = $this->createContainer();
$container = $this->getContainer();
$commands = parent::getDefaultCommands();

$commands[] = new Command\Git\DeInitCommand(
Expand All @@ -72,16 +78,34 @@ protected function getDefaultCommands()
return $commands;
}

protected function getDefaultHelperSet()
{
$container = $this->getContainer();

$helperSet = parent::getDefaultHelperSet();
$helperSet->set(new Helper\PathsHelper(
$container->get('config'),
$container->get('filesystem')
));

return $helperSet;
}


/**
* @return \Symfony\Component\DependencyInjection\ContainerBuilder
*/
protected function createContainer()
protected function getContainer()
{
if ($this->container) {
return $this->container;
}

$input = new ArgvInput();
$input->bind($this->getDefaultInputDefinition());
$configPath = $input->getOption('config');

$container = ContainerFactory::buildFromConfiguration($configPath);
return $container;
$this->container = ContainerFactory::buildFromConfiguration($configPath);
return $this->container;
}
}
13 changes: 11 additions & 2 deletions src/GrumPHP/Console/Command/Git/DeInitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace GrumPHP\Console\Command\Git;

use GrumPHP\Configuration\GrumPHP;
use GrumPHP\Console\Helper\PathsHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -61,10 +62,10 @@ protected function configure()
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$gitHooksPath = $this->grumPHP->getGitDir() . InitCommand::HOOKS_FOLDER;
$gitHooksPath = $this->paths()->getGitHooksDir();

foreach (InitCommand::$hooks as $hook) {
$hookPath = $gitHooksPath . DIRECTORY_SEPARATOR . $hook;
$hookPath = $gitHooksPath . $hook;
if (!$this->filesystem->exists($hookPath)) {
continue;
}
Expand All @@ -74,4 +75,12 @@ public function execute(InputInterface $input, OutputInterface $output)

$output->writeln('<fg=yellow>GrumPHP stopped sniffing your commits! Too bad ...<fg=yellow>');
}

/**
* @return PathsHelper
*/
protected function paths()
{
return $this->getHelper(PathsHelper::HELPER_NAME);
}
}
24 changes: 16 additions & 8 deletions src/GrumPHP/Console/Command/Git/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace GrumPHP\Console\Command\Git;

use GrumPHP\Configuration\GrumPHP;
use GrumPHP\Console\Helper\PathsHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -17,8 +18,6 @@ class InitCommand extends Command

const COMMAND_NAME = 'git:init';

const HOOKS_FOLDER = '/.git/hooks';

/**
* @var array
*/
Expand Down Expand Up @@ -77,12 +76,12 @@ protected function configure()
public function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$gitHooksPath = $this->grumPHP->getGitDir() . self::HOOKS_FOLDER;
$resourceHooksPath = __DIR__ . '/../../../../../resources/hooks';
$gitHooksPath = $this->paths()->getGitHooksDir();
$resourceHooksPath =$this->paths()->getGitHookTemplatesDir();

foreach (self::$hooks as $hook) {
$gitHook = $gitHooksPath . DIRECTORY_SEPARATOR . $hook;
$hookTemplate = $resourceHooksPath . DIRECTORY_SEPARATOR . $hook;
$gitHook = $gitHooksPath . $hook;
$hookTemplate = $resourceHooksPath . $hook;

if (!$this->filesystem->exists($hookTemplate)) {
throw new \RuntimeException(
Expand All @@ -108,7 +107,8 @@ protected function parseHookBody($hook, $templateFile)
{
$content = file_get_contents($templateFile);
$replacements = array(
'$(HOOK_COMMAND)' => $this->generateHookCommand('git:' . $hook),
'${HOOK_EXEC_PATH}' => $this->paths()->getGitHookExecutionPath(),
'$(HOOK_COMMAND)' => $this->generateHookCommand('git:' . $hook),
);

return str_replace(array_keys($replacements), array_values($replacements), $content);
Expand All @@ -123,11 +123,19 @@ protected function generateHookCommand($command)
{
$this->processBuilder->setArguments(array(
'php',
$this->grumPHP->getBinDir() . '/grumphp',
$this->paths()->getBinCommand('grumphp'),
$command,
'--config=' . $this->input->getOption('config'),
));

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

/**
* @return PathsHelper
*/
protected function paths()
{
return $this->getHelper(PathsHelper::HELPER_NAME);
}
}
14 changes: 6 additions & 8 deletions src/GrumPHP/Console/Command/Git/PreCommitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GrumPHP\Collection\FilesCollection;
use GrumPHP\Configuration\GrumPHP;
use GrumPHP\Console\Helper\PathsHelper;
use GrumPHP\Exception\ExceptionInterface;
use GrumPHP\Locator\LocatorInterface;
use GrumPHP\Runner\TaskRunner;
Expand Down Expand Up @@ -68,14 +69,13 @@ public function execute(InputInterface $input, OutputInterface $output)
$this->taskRunner->run($this->getCommittedFiles());
} catch (ExceptionInterface $e) {
// We'll fail hard on any exception not generated in GrumPHP

$output->writeln('<fg=red>' . $this->getAsciiResource('failed') . '</fg=red>');
$output->writeln('<fg=red>' . $this->paths()->getAsciiContent('failed') . '</fg=red>');
$output->writeln('<fg=red>' . $e->getMessage() . '</fg=red>');

return 1;
}

$output->write('<fg=green>' . $this->getAsciiResource('succeeded') . '</fg=green>');
$output->write('<fg=green>' . $this->paths()->getAsciiContent('succeeded') . '</fg=green>');

return 0;
}
Expand All @@ -89,12 +89,10 @@ protected function getCommittedFiles()
}

/**
* @param $name
*
* @return string
* @return PathsHelper
*/
protected function getAsciiResource($name)
protected function paths()
{
return file_get_contents(sprintf(__DIR__ . '/../../../../../resources/ascii/%s.txt', $name));
return $this->getHelper(PathsHelper::HELPER_NAME);
}
}
Loading

0 comments on commit c1972a3

Please sign in to comment.