Skip to content

Commit

Permalink
Merge pull request #268 from jdecool/feat-allow-change-config-file
Browse files Browse the repository at this point in the history
Allow to change the configuration file to be loaded
  • Loading branch information
mnapoli committed Feb 21, 2023
2 parents 5f28da1 + 2a8517a commit 1b0731f
Show file tree
Hide file tree
Showing 21 changed files with 115 additions and 26 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node
uses: actions/setup-node@v3
- name: Install less
run: npm install -g less less-plugin-clean-css
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
Expand Down
11 changes: 10 additions & 1 deletion src/Application/Cli/DeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ protected function configure(): void
InputOption::VALUE_REQUIRED,
'Target branch in which to deploy the website.'
)
->addOption(
'config-file',
'f',
InputOption::VALUE_REQUIRED,
'If specified, use the given file as configuration file.',
'couscous.yml'
)
->addOption(
'config',
null,
Expand All @@ -97,10 +104,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$repositoryUrl = $input->getOption('repository');
/** @var ?string */
$targetBranch = $input->getOption('branch');
/** @var string $configFile */
$configFile = $input->getOption('config-file');
/** @var array */
$cliConfig = $input->getOption('config');

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

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

Expand Down
11 changes: 10 additions & 1 deletion src/Application/Cli/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ protected function configure(): void
'Target directory in which to generate the files.',
getcwd().'/.couscous/generated'
)
->addOption(
'config-file',
'f',
InputOption::VALUE_REQUIRED,
'If specified, use the given file as configuration file.',
'couscous.yml'
)
->addOption(
'config',
null,
Expand All @@ -63,10 +70,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$source = $input->getArgument('source');
/** @var string */
$target = $input->getOption('target');
/** @var string */
$configFile = $input->getOption('config-file');
/** @var array */
$cliConfig = $input->getOption('config');

$project = new Project($source, $target);
$project = new Project($configFile, $source, $target);

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

Expand Down
23 changes: 20 additions & 3 deletions src/Application/Cli/PreviewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ protected function configure(): void
InputOption::VALUE_OPTIONAL,
'If set livereload server is launched from the specified path (global livereload by default)'
)
->addOption(
'config-file',
'f',
InputOption::VALUE_REQUIRED,
'If specified, use the given file as configuration file.',
'couscous.yml'
)
->addOption(
'config',
null,
Expand All @@ -80,6 +87,8 @@ protected function configure(): void
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var string */
$configFile = $input->getOption('config-file');
/** @var array */
$cliConfig = $input->getOption('config');

Expand Down Expand Up @@ -109,7 +118,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->startLivereload($livereload, $output, $sourceDirectory, $targetDirectory);
}

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

$serverProcess = $this->startWebServer($input, $output, $targetDirectory);
$throwOnServerStop = true;
Expand All @@ -134,7 +143,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$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, $cliConfig, true);
$watchlist = $this->generateWebsite(
$output,
$configFile,
$sourceDirectory,
$targetDirectory,
$cliConfig,
true
);
}

sleep(1);
Expand All @@ -149,12 +165,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int

private function generateWebsite(
OutputInterface $output,
string $configFile,
string $sourceDirectory,
string $targetDirectory,
array $cliConfig,
bool $regenerate = false
): WatchList {
$project = new Project($sourceDirectory, $targetDirectory);
$project = new Project($configFile, $sourceDirectory, $targetDirectory);

$project->metadata['cliConfig'] = $cliConfig;
$project->metadata['preview'] = true;
Expand Down
11 changes: 10 additions & 1 deletion src/Application/Cli/TravisAutoDeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ protected function configure(): void
'Repository you want to generate.',
getcwd()
)
->addOption(
'config-file',
'f',
InputOption::VALUE_REQUIRED,
'If specified, use the given file as configuration file.',
'couscous.yml'
)
->addOption(
'php-version',
null,
Expand All @@ -84,8 +91,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$repositoryUrl = sprintf('https://%s@%s', (string) getenv('GH_TOKEN'), (string) getenv('GH_REF'));
/** @var string */
$targetBranch = $input->getOption('branch');
/** @var string */
$configFile = $input->getOption('config-file');

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

// verify some env variables
$travisBranch = getenv('TRAVIS_BRANCH');
Expand Down
8 changes: 7 additions & 1 deletion src/Model/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
*/
class Project
{
/**
* Configuration file to load
*/
public $configFile;

/**
* Directory containing the sources files to process.
*
Expand Down Expand Up @@ -54,8 +59,9 @@ class Project
*/
protected $files = [];

public function __construct(string $sourceDirectory, string $targetDirectory)
public function __construct(string $configFile, string $sourceDirectory, string $targetDirectory)
{
$this->configFile = "$sourceDirectory/$configFile";
$this->sourceDirectory = $sourceDirectory;
$this->targetDirectory = $targetDirectory;
$this->watchlist = new WatchList();
Expand Down
2 changes: 1 addition & 1 deletion src/Module/Config/Step/LoadConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(Filesystem $filesystem, Parser $yamlParser, LoggerIn

public function __invoke(Project $project): void
{
$filename = $project->sourceDirectory.'/'.self::FILENAME;
$filename = $project->configFile;

if (!$this->filesystem->exists($filename)) {
$this->logger->notice('No couscous.yml configuration file found, using default config');
Expand Down
14 changes: 8 additions & 6 deletions tests/FunctionalTest/BaseFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public function tearDown(): void
$this->clearGeneratedDirectory();
}

public function assertGeneratedWebsite($fixtureName)
public function assertGeneratedWebsite($fixtureName, $configFile = null)
{
[$output, $return] = $this->generate($fixtureName);
[$output, $return] = $this->generate($fixtureName, $configFile);

$this->assertSame(0, $return, implode(PHP_EOL, $output));

Expand Down Expand Up @@ -71,25 +71,27 @@ public function assertGenerationError($fixtureName, $expectedMessage)
$this->assertStringContainsString($expectedMessage, $output);
}

private function createCommand($fixtureName): string
private function createCommand($fixtureName, $configFile = null): string
{
$bin = realpath(__DIR__.'/../../bin/couscous');
$fixtureName = __DIR__.'/Fixture/'.$fixtureName.'/source';
$targetDirectory = __DIR__.'/generated';
$configOption = ($configFile !== null) ? "--config-file=$configFile" : '';

return sprintf(
'%s generate -v --target="%s" %s 2>&1',
'%s generate -v --target="%s" %s %s 2>&1',
$bin,
$targetDirectory,
$configOption,
$fixtureName
);
}

private function generate($fixtureName): array
private function generate($fixtureName, $configFile = null): array
{
$this->clearGeneratedDirectory();

$command = $this->createCommand($fixtureName);
$command = $this->createCommand($fixtureName, $configFile);

exec($command, $output, $return);

Expand Down
9 changes: 9 additions & 0 deletions tests/FunctionalTest/Fixture/config-file/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Foo!</title>
</head>
<body>
This is a custom variable
</body>
</html>
6 changes: 6 additions & 0 deletions tests/FunctionalTest/Fixture/config-file/source/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
layout: page
customVariable: "This is a custom variable"
---

Hello, this is a test!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
title: Foo!
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
{{ customVariable }}
</body>
</html>
Empty file.
8 changes: 8 additions & 0 deletions tests/FunctionalTest/GenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,12 @@ public function testIncludeWithExclude()
{
$this->assertGeneratedWebsite('include-with-exclude');
}

/**
* Test to specify non default configuration file
*/
public function testCustomConfigFile()
{
$this->assertGeneratedWebsite('config-file', 'project-config-file.yaml');
}
}
2 changes: 1 addition & 1 deletion tests/UnitTest/Mock/MockProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MockProject extends Project
{
public function __construct()
{
parent::__construct('', '');
parent::__construct('', '', '');

$this->metadata = new Metadata();
$this->watchlist = new MockWatchList();
Expand Down
6 changes: 3 additions & 3 deletions tests/UnitTest/Model/ProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ProjectTest extends TestCase
*/
public function it_should_contain_files()
{
$project = new Project('source', 'target');
$project = new Project('config', 'source', 'target');

$file1 = $this->createFile('file1');
$file2 = $this->createFile('file2');
Expand All @@ -43,7 +43,7 @@ public function it_should_contain_files()
*/
public function replace_should_replace_files()
{
$project = new Project('source', 'target');
$project = new Project('config', 'source', 'target');

$file1 = $this->createFile('file1');
$file2 = $this->createFile('file2');
Expand All @@ -60,7 +60,7 @@ public function replace_should_replace_files()
*/
public function it_should_return_files_by_type()
{
$project = new Project('source', 'target');
$project = new Project('config', 'source', 'target');

$file1 = new MarkdownFile('file1', 'Hello');
$file2 = new HtmlFile('file2', 'Hello');
Expand Down
2 changes: 1 addition & 1 deletion tests/UnitTest/Module/Core/Step/AddCnameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class AddCnameTest extends TestCase
*/
public function it_should_add_the_cname_file()
{
$project = new Project('foo', 'bar');
$project = new Project('config', 'foo', 'bar');

$project->metadata = new Metadata();
$project->metadata['cname'] = 'https://www.couscous.io';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ClearTargetDirectoryTest extends TestCase
*/
public function it_should_not_clear_dot_files()
{
$project = new Project('foo', dirname(__DIR__, 3) .'/Fixture/directory-with-dot-files');
$project = new Project('config', 'foo', dirname(__DIR__, 3) .'/Fixture/directory-with-dot-files');

$filesystem = $this->getMockBuilder(Filesystem::class)
->disableOriginalConstructor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testRenameReadmeMessyFilename()
public function testNonMarkdownFileNotRenamed()
{
$file = new LazyFile('foo.txt', 'foo.txt');
$project = new Project('foo', 'bar');
$project = new Project('config', 'foo', 'bar');
$project->addFile($file);

$step = new ProcessMarkdownFileName();
Expand All @@ -67,7 +67,7 @@ public function testNonMarkdownFileNotRenamed()
private function assertFileRenamed($expected, $filename, $meta = false)
{
$file = new MarkdownFile($filename, '');
$project = new Project('foo', 'bar');
$project = new Project('config', 'foo', 'bar');
$project->addFile($file);

if ($meta) {
Expand Down
2 changes: 1 addition & 1 deletion tests/UnitTest/Module/Markdown/Step/RenderMarkdownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private function assertGeneratedHtml($markdown, $expectedHtml)
/** @var RenderMarkdown $step */
$step = $container->get(RenderMarkdown::class);

$project = new Project('foo', 'bar');
$project = new Project('config', 'foo', 'bar');
$project->addFile(new MarkdownFile('foo.md', $markdown));

$step->__invoke($project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testReplaceLinks()
MARKDOWN;

$file = new MarkdownFile('foo', $markdown);
$project = new Project('foo', 'bar');
$project = new Project('config', 'foo', 'bar');
$project->addFile($file);

$step = new RewriteMarkdownLinks();
Expand All @@ -71,7 +71,7 @@ public function testReplacesMultipleLinksPerLine()
MARKDOWN;

$file = new MarkdownFile('foo', $markdown);
$project = new Project('foo', 'bar');
$project = new Project('config', 'foo', 'bar');
$project->addFile($file);

$step = new RewriteMarkdownLinks();
Expand All @@ -93,7 +93,7 @@ public function testPreservesQueryString()
MARKDOWN;

$file = new MarkdownFile('foo', $markdown);
$project = new Project('foo', 'bar');
$project = new Project('config', 'foo', 'bar');
$project->addFile($file);

$step = new RewriteMarkdownLinks();
Expand Down

0 comments on commit 1b0731f

Please sign in to comment.