diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 474a6054..0d22cff3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/src/Application/Cli/DeployCommand.php b/src/Application/Cli/DeployCommand.php index e701fa36..671892d8 100644 --- a/src/Application/Cli/DeployCommand.php +++ b/src/Application/Cli/DeployCommand.php @@ -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, @@ -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; diff --git a/src/Application/Cli/GenerateCommand.php b/src/Application/Cli/GenerateCommand.php index 75b82a53..13e546a7 100644 --- a/src/Application/Cli/GenerateCommand.php +++ b/src/Application/Cli/GenerateCommand.php @@ -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, @@ -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; diff --git a/src/Application/Cli/PreviewCommand.php b/src/Application/Cli/PreviewCommand.php index 089ca3a7..d9dd79ba 100644 --- a/src/Application/Cli/PreviewCommand.php +++ b/src/Application/Cli/PreviewCommand.php @@ -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, @@ -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'); @@ -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; @@ -134,7 +143,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->write(sprintf('%d file(s) changed: regenerating', 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); @@ -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; diff --git a/src/Application/Cli/TravisAutoDeployCommand.php b/src/Application/Cli/TravisAutoDeployCommand.php index 43081704..3ebc5942 100644 --- a/src/Application/Cli/TravisAutoDeployCommand.php +++ b/src/Application/Cli/TravisAutoDeployCommand.php @@ -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, @@ -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'); diff --git a/src/Model/Project.php b/src/Model/Project.php index 3ee627e9..d0615cfa 100644 --- a/src/Model/Project.php +++ b/src/Model/Project.php @@ -13,6 +13,11 @@ */ class Project { + /** + * Configuration file to load + */ + public $configFile; + /** * Directory containing the sources files to process. * @@ -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(); diff --git a/src/Module/Config/Step/LoadConfig.php b/src/Module/Config/Step/LoadConfig.php index 32fd4c58..0ae087ea 100644 --- a/src/Module/Config/Step/LoadConfig.php +++ b/src/Module/Config/Step/LoadConfig.php @@ -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'); diff --git a/tests/FunctionalTest/BaseFunctionalTest.php b/tests/FunctionalTest/BaseFunctionalTest.php index fc5e1aff..48e519e8 100644 --- a/tests/FunctionalTest/BaseFunctionalTest.php +++ b/tests/FunctionalTest/BaseFunctionalTest.php @@ -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)); @@ -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); diff --git a/tests/FunctionalTest/Fixture/config-file/expected/index.html b/tests/FunctionalTest/Fixture/config-file/expected/index.html new file mode 100644 index 00000000..99a07e04 --- /dev/null +++ b/tests/FunctionalTest/Fixture/config-file/expected/index.html @@ -0,0 +1,9 @@ + + + + Foo! + + + This is a custom variable + + diff --git a/tests/FunctionalTest/Fixture/config-file/source/README.md b/tests/FunctionalTest/Fixture/config-file/source/README.md new file mode 100644 index 00000000..47e90471 --- /dev/null +++ b/tests/FunctionalTest/Fixture/config-file/source/README.md @@ -0,0 +1,6 @@ +--- +layout: page +customVariable: "This is a custom variable" +--- + +Hello, this is a test! diff --git a/tests/FunctionalTest/Fixture/config-file/source/project-config-file.yaml b/tests/FunctionalTest/Fixture/config-file/source/project-config-file.yaml new file mode 100644 index 00000000..69dd25c0 --- /dev/null +++ b/tests/FunctionalTest/Fixture/config-file/source/project-config-file.yaml @@ -0,0 +1 @@ +title: Foo! diff --git a/tests/FunctionalTest/Fixture/config-file/source/website/page.twig b/tests/FunctionalTest/Fixture/config-file/source/website/page.twig new file mode 100644 index 00000000..aec48c06 --- /dev/null +++ b/tests/FunctionalTest/Fixture/config-file/source/website/page.twig @@ -0,0 +1,9 @@ + + + + {{ title }} + + + {{ customVariable }} + + \ No newline at end of file diff --git a/tests/FunctionalTest/Fixture/config-file/source/website/public/.gitkeep b/tests/FunctionalTest/Fixture/config-file/source/website/public/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/tests/FunctionalTest/GenerationTest.php b/tests/FunctionalTest/GenerationTest.php index 3d8327ba..4d30e9d2 100644 --- a/tests/FunctionalTest/GenerationTest.php +++ b/tests/FunctionalTest/GenerationTest.php @@ -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'); + } } diff --git a/tests/UnitTest/Mock/MockProject.php b/tests/UnitTest/Mock/MockProject.php index 118c11ff..ec4d9fd1 100644 --- a/tests/UnitTest/Mock/MockProject.php +++ b/tests/UnitTest/Mock/MockProject.php @@ -9,7 +9,7 @@ class MockProject extends Project { public function __construct() { - parent::__construct('', ''); + parent::__construct('', '', ''); $this->metadata = new Metadata(); $this->watchlist = new MockWatchList(); diff --git a/tests/UnitTest/Model/ProjectTest.php b/tests/UnitTest/Model/ProjectTest.php index 1cf41bf7..1491e45f 100644 --- a/tests/UnitTest/Model/ProjectTest.php +++ b/tests/UnitTest/Model/ProjectTest.php @@ -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'); @@ -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'); @@ -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'); diff --git a/tests/UnitTest/Module/Core/Step/AddCnameTest.php b/tests/UnitTest/Module/Core/Step/AddCnameTest.php index 73a082a0..3fa1f5c5 100644 --- a/tests/UnitTest/Module/Core/Step/AddCnameTest.php +++ b/tests/UnitTest/Module/Core/Step/AddCnameTest.php @@ -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'; diff --git a/tests/UnitTest/Module/Core/Step/ClearTargetDirectoryTest.php b/tests/UnitTest/Module/Core/Step/ClearTargetDirectoryTest.php index c61f339d..07b3ef76 100644 --- a/tests/UnitTest/Module/Core/Step/ClearTargetDirectoryTest.php +++ b/tests/UnitTest/Module/Core/Step/ClearTargetDirectoryTest.php @@ -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() diff --git a/tests/UnitTest/Module/Markdown/Step/ProcessMarkdownFileNameTest.php b/tests/UnitTest/Module/Markdown/Step/ProcessMarkdownFileNameTest.php index aecf36f6..721cf2bb 100644 --- a/tests/UnitTest/Module/Markdown/Step/ProcessMarkdownFileNameTest.php +++ b/tests/UnitTest/Module/Markdown/Step/ProcessMarkdownFileNameTest.php @@ -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(); @@ -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) { diff --git a/tests/UnitTest/Module/Markdown/Step/RenderMarkdownTest.php b/tests/UnitTest/Module/Markdown/Step/RenderMarkdownTest.php index 7c11233c..6016313c 100644 --- a/tests/UnitTest/Module/Markdown/Step/RenderMarkdownTest.php +++ b/tests/UnitTest/Module/Markdown/Step/RenderMarkdownTest.php @@ -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); diff --git a/tests/UnitTest/Module/Markdown/Step/RewriteMarkdownLinksTest.php b/tests/UnitTest/Module/Markdown/Step/RewriteMarkdownLinksTest.php index b8bcf7eb..5dcfcc0e 100644 --- a/tests/UnitTest/Module/Markdown/Step/RewriteMarkdownLinksTest.php +++ b/tests/UnitTest/Module/Markdown/Step/RewriteMarkdownLinksTest.php @@ -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(); @@ -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(); @@ -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();