diff --git a/src/Application/Cli/DeployCommand.php b/src/Application/Cli/DeployCommand.php index ae3cb60a..41609e74 100644 --- a/src/Application/Cli/DeployCommand.php +++ b/src/Application/Cli/DeployCommand.php @@ -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)', + [] ); } @@ -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); diff --git a/src/Application/Cli/GenerateCommand.php b/src/Application/Cli/GenerateCommand.php index 9190a2a6..4afcf7e8 100644 --- a/src/Application/Cli/GenerateCommand.php +++ b/src/Application/Cli/GenerateCommand.php @@ -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); } } diff --git a/src/Application/Cli/PreviewCommand.php b/src/Application/Cli/PreviewCommand.php index 21bf977d..d4619c2e 100644 --- a/src/Application/Cli/PreviewCommand.php +++ b/src/Application/Cli/PreviewCommand.php @@ -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)', + [] ); } @@ -72,6 +79,8 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $cliConfig = $input->getOption('config'); + if (!$this->isSupported()) { $output->writeln('PHP 5.4 or above is required to run the internal webserver'); @@ -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; @@ -112,7 +121,6 @@ protected function execute(InputInterface $input, OutputInterface $output) } } - // Watch for changes while ($serverProcess->isRunning()) { $files = $watchlist->getChangedFiles(); @@ -121,7 +129,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->write(sprintf('%d file(s) changed: regenerating', 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); @@ -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; diff --git a/src/Application/config.php b/src/Application/config.php index cb6325ba..52b9ecc2 100644 --- a/src/Application/config.php +++ b/src/Application/config.php @@ -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'), diff --git a/src/Module/Config/Step/OverrideConfigFromCLI.php b/src/Module/Config/Step/OverrideConfigFromCLI.php new file mode 100644 index 00000000..0e2fc938 --- /dev/null +++ b/src/Module/Config/Step/OverrideConfigFromCLI.php @@ -0,0 +1,43 @@ + + */ +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); + } +} diff --git a/tests/UnitTest/Module/Config/Step/OverrideConfigFromCLITest.php b/tests/UnitTest/Module/Config/Step/OverrideConfigFromCLITest.php new file mode 100644 index 00000000..6e12fb2b --- /dev/null +++ b/tests/UnitTest/Module/Config/Step/OverrideConfigFromCLITest.php @@ -0,0 +1,62 @@ +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']); + } +}