Skip to content

Commit

Permalink
bug #19 Fixed default config path guessing (HeahDude, javiereguiluz)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.0.x-dev branch.

Discussion
----------

Fixed default config path guessing

Closes #16.

What do you think of doing it this way?

Commits
-------

bec7bbf Minor refactor
20226ab Fixed default config path guessing
  • Loading branch information
javiereguiluz committed May 28, 2017
2 parents 0cd7703 + bec7bbf commit e84cdc2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 58 deletions.
21 changes: 2 additions & 19 deletions src/Command/DeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace EasyCorp\Bundle\EasyDeployBundle\Command;

use EasyCorp\Bundle\EasyDeployBundle\Context;
use EasyCorp\Bundle\EasyDeployBundle\Exception\SymfonyVersionException;
use EasyCorp\Bundle\EasyDeployBundle\Helper\SymfonyConfigPathGuesser;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -21,7 +21,6 @@
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Config\FileLocator;
use Symfony\Component\HttpKernel\Kernel;

class DeployCommand extends Command
{
Expand Down Expand Up @@ -62,7 +61,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
return $this->configFilePath = $customConfigPath;
}

$defaultConfigPath = $this->getDefaultConfigPath($input->getArgument('stage'));
$defaultConfigPath = SymfonyConfigPathGuesser::guess($this->projectDir, $input->getArgument('stage'));
if (is_readable($defaultConfigPath)) {
return $this->configFilePath = $defaultConfigPath;
}
Expand All @@ -80,22 +79,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$deployer->doDeploy();
}

private function getDefaultConfigPath(string $stageName) : string
{
$symfonyVersion = Kernel::MAJOR_VERSION;
$defaultConfigPaths = [
2 => sprintf('%s/app/config/deploy_%s.php', $this->projectDir, $stageName),
3 => sprintf('%s/app/config/deploy_%s.php', $this->projectDir, $stageName),
4 => sprintf('%s/etc/%s/deploy.php', $this->projectDir, $stageName),
];

if (!isset($defaultConfigPaths[$symfonyVersion])) {
throw new SymfonyVersionException($symfonyVersion);
}

return $defaultConfigPaths[$symfonyVersion];
}

private function createDefaultConfigFile(InputInterface $input, OutputInterface $output, string $defaultConfigPath, string $stageName) : void
{
$helper = $this->getHelper('question');
Expand Down
21 changes: 2 additions & 19 deletions src/Command/RollbackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
namespace EasyCorp\Bundle\EasyDeployBundle\Command;

use EasyCorp\Bundle\EasyDeployBundle\Context;
use EasyCorp\Bundle\EasyDeployBundle\Exception\SymfonyVersionException;
use EasyCorp\Bundle\EasyDeployBundle\Helper\SymfonyConfigPathGuesser;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\Kernel;

class RollbackCommand extends Command
{
Expand Down Expand Up @@ -57,7 +56,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
return $this->configFilePath = $customConfigPath;
}

$defaultConfigPath = $this->getDefaultConfigPath($input->getArgument('stage'));
$defaultConfigPath = SymfonyConfigPathGuesser::guess($this->projectDir, $input->getArgument('stage'));
if (is_readable($defaultConfigPath)) {
return $this->configFilePath = $defaultConfigPath;
}
Expand All @@ -74,20 +73,4 @@ protected function execute(InputInterface $input, OutputInterface $output)
$deployer->initialize($context);
$deployer->doRollback();
}

private function getDefaultConfigPath(string $stageName) : string
{
$symfonyVersion = Kernel::MAJOR_VERSION;
$defaultConfigPaths = [
2 => sprintf('%s/app/config/deploy_%s.php', $this->projectDir, $stageName),
3 => sprintf('%s/app/config/deploy_%s.php', $this->projectDir, $stageName),
4 => sprintf('%s/etc/%s/deploy.php', $this->projectDir, $stageName),
];

if (!isset($defaultConfigPaths[$symfonyVersion])) {
throw new SymfonyVersionException($symfonyVersion);
}

return $defaultConfigPaths[$symfonyVersion];
}
}
20 changes: 0 additions & 20 deletions src/Exception/SymfonyVersionException.php

This file was deleted.

34 changes: 34 additions & 0 deletions src/Helper/SymfonyConfigPathGuesser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the EasyDeploy project.
*
* (c) Javier Eguiluz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace EasyCorp\Bundle\EasyDeployBundle\Helper;

/**
* @author Jules Pietri <[email protected]>
*/
class SymfonyConfigPathGuesser
{
private const LEGACY_CONFIG_DIR = '%s/app/config';
private const CONFIG_DIR = '%s/etc';

public static function guess(string $projectDir, string $stage): string
{
if (is_dir($configDir = sprintf(self::CONFIG_DIR, $projectDir))) {
return sprintf('%s/%s/deploy.php', $configDir, $stage);
}

if (is_dir($configDir = sprintf(self::LEGACY_CONFIG_DIR, $projectDir))) {
return sprintf('%s/deploy_%s.php', $configDir, $stage);
}

throw new \RuntimeException(sprintf('None of the usual Symfony config dirs exist in the application. Create one of these dirs before continuing: "%s" or "%s".', self::CONFIG_DIR, self::LEGACY_CONFIG_DIR));
}
}

0 comments on commit e84cdc2

Please sign in to comment.