Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override couscous.yml configuration from command line #187

Merged
merged 9 commits into from Jan 24, 2017
10 changes: 10 additions & 0 deletions src/Application/Cli/DeployCommand.php
Expand Up @@ -82,6 +82,13 @@ protected function configure()
null,
InputOption::VALUE_REQUIRED,
'Target branch in which to deploy the website.'
)
->addOption(
'config',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'If specified will override entries in couscous.yml (key=value)',
[]
);
}

Expand All @@ -93,9 +100,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
$sourceDirectory = $input->getArgument('source');
$repositoryUrl = $input->getOption('repository');
$targetBranch = $input->getOption('branch');
$cliConfig = $input->getOption('config');

$project = new Project($sourceDirectory, getcwd().'/.couscous/generated');

$project->metadata['cliConfig'] = $cliConfig;

// Generate the website
$this->generator->generate($project, $output);

Expand Down
11 changes: 11 additions & 0 deletions src/Application/Cli/GenerateCommand.php
Expand Up @@ -46,13 +46,24 @@ protected function configure()
InputOption::VALUE_REQUIRED,
'Target directory in which to generate the files.',
getcwd().'/.couscous/generated'
)
->addOption(
'config',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'If specified will override entries in couscous.yml (key=value)',
[]
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$cliConfig = $input->getOption('config');

$project = new Project($input->getArgument('source'), $input->getOption('target'));

$project->metadata['cliConfig'] = $cliConfig;

$this->generator->generate($project, $output);
}
}
16 changes: 13 additions & 3 deletions src/Application/Cli/PreviewCommand.php
Expand Up @@ -64,6 +64,13 @@ protected function configure()
null,
InputOption::VALUE_OPTIONAL,
'If set livereload server is launched from the specified path (global livereload by default)'
)
->addOption(
'config',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'If specified will override entries in couscous.yml (key=value)',
[]
);
}

Expand All @@ -72,6 +79,8 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$cliConfig = $input->getOption('config');

if (!$this->isSupported()) {
$output->writeln('<error>PHP 5.4 or above is required to run the internal webserver</error>');

Expand All @@ -95,7 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->startLivereload($input->getOption('livereload'), $output, $sourceDirectory, $targetDirectory);
}

$watchlist = $this->generateWebsite($output, $sourceDirectory, $targetDirectory);
$watchlist = $this->generateWebsite($output, $sourceDirectory, $targetDirectory, $cliConfig);

$serverProcess = $this->startWebServer($input, $output, $targetDirectory);
$throwOnServerStop = true;
Expand All @@ -112,7 +121,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}


// Watch for changes
while ($serverProcess->isRunning()) {
$files = $watchlist->getChangedFiles();
Expand All @@ -121,7 +129,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->write(sprintf('<comment>%d file(s) changed: regenerating</comment>', count($files)));
$output->writeln(sprintf(' (%s)', $this->fileListToDisplay($files, $sourceDirectory)));

$watchlist = $this->generateWebsite($output, $sourceDirectory, $targetDirectory, true);
$watchlist = $this->generateWebsite($output, $sourceDirectory, $targetDirectory, $cliConfig, true);
}

sleep(1);
Expand All @@ -138,10 +146,12 @@ private function generateWebsite(
OutputInterface $output,
$sourceDirectory,
$targetDirectory,
$cliConfig,
$regenerate = false
) {
$project = new Project($sourceDirectory, $targetDirectory);

$project->metadata['cliConfig'] = $cliConfig;
$project->metadata['preview'] = true;

$project->regenerate = $regenerate;
Expand Down
1 change: 1 addition & 0 deletions src/Application/config.php
Expand Up @@ -8,6 +8,7 @@
'steps' => [
DI\get('Couscous\Module\Config\Step\SetDefaultConfig'),
DI\get('Couscous\Module\Config\Step\LoadConfig'),
DI\get('Couscous\Module\Config\Step\OverrideConfigFromCLI'),
DI\get('Couscous\Module\Config\Step\OverrideBaseUrlForPreview'),

DI\get('Couscous\Module\Scripts\Step\ExecuteBeforeScripts'),
Expand Down
43 changes: 43 additions & 0 deletions src/Module/Config/Step/OverrideConfigFromCLI.php
@@ -0,0 +1,43 @@
<?php

namespace Couscous\Module\Config\Step;

use Couscous\Model\Project;
use Psr\Log\LoggerInterface;

/**
* Override config variables when specified using --config option.
*
* @author D.J. Marcolesco <dj.marcolesco@gmail.com>
*/
class OverrideConfigFromCLI implements \Couscous\Step
{
/**
* @var LoggerInterface
*/
private $logger;

public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}

public function __invoke(Project $project)
{
if (!$project->metadata['cliConfig']) {
return;
}

$cliConfig = [];
foreach ($project->metadata['cliConfig'] as $item) {
$explosion = explode('=', $item, 2);
$this->logger->notice('Overriding global config: '.$explosion[0].' = "'.$explosion[1].'"');

$cliConfig[$explosion[0]] = $explosion[1];
}

unset($project->metadata['cliConfig']);

$project->metadata->setMany($cliConfig);
}
}
62 changes: 62 additions & 0 deletions tests/UnitTest/Module/Config/Step/OverrideConfigFromCLITest.php
@@ -0,0 +1,62 @@
<?php

namespace Couscous\Tests\UnitTest\Module\Config\Step;

use Couscous\Module\Config\Step\OverrideConfigFromCLI;
use Couscous\Tests\UnitTest\Mock\MockProject;

/**
* @covers \Couscous\Module\Config\Step\OverrideConfigFromCLI
*/
class OverrideConfigFromCLITest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function should_override_title_if_specified()
{
$project = new MockProject();
$project->metadata['title'] = 'foo';
$project->metadata['cliConfig'] = ['title=bar'];

$logger = $this->getMock("Psr\Log\LoggerInterface");

$step = new OverrideConfigFromCLI($logger);
$step->__invoke($project);

$this->assertEquals('bar', $project->metadata['title']);
}

/**
* @test
*/
public function should_not_override_title_if_not_specified()
{
$project = new MockProject();
$project->metadata['title'] = 'foo';
$project->metadata['cliConfig'] = [];

$logger = $this->getMock("Psr\Log\LoggerInterface");

$step = new OverrideConfigFromCLI($logger);
$step->__invoke($project);

$this->assertEquals('foo', $project->metadata['title']);
}

/**
* @test
*/
public function should_not_override_title_if_no_cliConfig()
{
$project = new MockProject();
$project->metadata['title'] = 'foo';

$logger = $this->getMock("Psr\Log\LoggerInterface");

$step = new OverrideConfigFromCLI($logger);
$step->__invoke($project);

$this->assertEquals('foo', $project->metadata['title']);
}
}