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.yaml (key=value)',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: couscous.yaml -> couscous.yml

[]
);
}

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');
$tempConfigRaw = $input->getOption('config');

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

$project->metadata['tempConfigRaw'] = $tempConfigRaw;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tempConfigRaw doesn't sound ideal to me, how about something like cliConfig?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cliConfig works, albeit a bit clunky to read. I think tempConfigRaw was a leftover name from when I was storing the processed array in tempConfig and wanted to differentiate.


// 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.yaml (key=value)',
[]
);
}

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

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

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

$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.yaml (key=value)',
[]
);
}

Expand All @@ -72,6 +79,8 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$tempConfigRaw = $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, $tempConfigRaw);

$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, $tempConfigRaw, true);
}

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

$project->metadata['tempConfigRaw'] = $tempConfigRaw;
$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
42 changes: 42 additions & 0 deletions src/Module/Config/Step/OverrideConfigFromCLI.php
@@ -0,0 +1,42 @@
<?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['tempConfigRaw']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor detail but you can return early to avoid 1 level of indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good tip!

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

$keys[] = $explosion[0];
$values[] = $explosion[1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using 2 arrays wouldn't it be possible to use one and do:

$config[$explosion[0]] = $explosion[1];

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it would! I'm just a silly. :)

}

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

$project->metadata->setMany(array_combine($keys, $values));
}
}
}