Skip to content

Commit 1b0731f

Browse files
authored
Merge pull request #268 from jdecool/feat-allow-change-config-file
Allow to change the configuration file to be loaded
2 parents 5f28da1 + 2a8517a commit 1b0731f

File tree

21 files changed

+115
-26
lines changed

21 files changed

+115
-26
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ jobs:
2424
steps:
2525
- name: Checkout
2626
uses: actions/checkout@v2
27+
- name: Setup Node
28+
uses: actions/setup-node@v3
29+
- name: Install less
30+
run: npm install -g less less-plugin-clean-css
2731
- name: Setup PHP
2832
uses: shivammathur/setup-php@v2
2933
with:

src/Application/Cli/DeployCommand.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ protected function configure(): void
7777
InputOption::VALUE_REQUIRED,
7878
'Target branch in which to deploy the website.'
7979
)
80+
->addOption(
81+
'config-file',
82+
'f',
83+
InputOption::VALUE_REQUIRED,
84+
'If specified, use the given file as configuration file.',
85+
'couscous.yml'
86+
)
8087
->addOption(
8188
'config',
8289
null,
@@ -97,10 +104,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
97104
$repositoryUrl = $input->getOption('repository');
98105
/** @var ?string */
99106
$targetBranch = $input->getOption('branch');
107+
/** @var string $configFile */
108+
$configFile = $input->getOption('config-file');
100109
/** @var array */
101110
$cliConfig = $input->getOption('config');
102111

103-
$project = new Project($sourceDirectory, getcwd().'/.couscous/generated');
112+
$project = new Project($configFile, $sourceDirectory, getcwd().'/.couscous/generated');
104113

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

src/Application/Cli/GenerateCommand.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ protected function configure(): void
4848
'Target directory in which to generate the files.',
4949
getcwd().'/.couscous/generated'
5050
)
51+
->addOption(
52+
'config-file',
53+
'f',
54+
InputOption::VALUE_REQUIRED,
55+
'If specified, use the given file as configuration file.',
56+
'couscous.yml'
57+
)
5158
->addOption(
5259
'config',
5360
null,
@@ -63,10 +70,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6370
$source = $input->getArgument('source');
6471
/** @var string */
6572
$target = $input->getOption('target');
73+
/** @var string */
74+
$configFile = $input->getOption('config-file');
6675
/** @var array */
6776
$cliConfig = $input->getOption('config');
6877

69-
$project = new Project($source, $target);
78+
$project = new Project($configFile, $source, $target);
7079

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

src/Application/Cli/PreviewCommand.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ protected function configure(): void
6666
InputOption::VALUE_OPTIONAL,
6767
'If set livereload server is launched from the specified path (global livereload by default)'
6868
)
69+
->addOption(
70+
'config-file',
71+
'f',
72+
InputOption::VALUE_REQUIRED,
73+
'If specified, use the given file as configuration file.',
74+
'couscous.yml'
75+
)
6976
->addOption(
7077
'config',
7178
null,
@@ -80,6 +87,8 @@ protected function configure(): void
8087
*/
8188
protected function execute(InputInterface $input, OutputInterface $output): int
8289
{
90+
/** @var string */
91+
$configFile = $input->getOption('config-file');
8392
/** @var array */
8493
$cliConfig = $input->getOption('config');
8594

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

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

114123
$serverProcess = $this->startWebServer($input, $output, $targetDirectory);
115124
$throwOnServerStop = true;
@@ -134,7 +143,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
134143
$output->write(sprintf('<comment>%d file(s) changed: regenerating</comment>', count($files)));
135144
$output->writeln(sprintf(' (%s)', $this->fileListToDisplay($files, $sourceDirectory)));
136145

137-
$watchlist = $this->generateWebsite($output, $sourceDirectory, $targetDirectory, $cliConfig, true);
146+
$watchlist = $this->generateWebsite(
147+
$output,
148+
$configFile,
149+
$sourceDirectory,
150+
$targetDirectory,
151+
$cliConfig,
152+
true
153+
);
138154
}
139155

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

150166
private function generateWebsite(
151167
OutputInterface $output,
168+
string $configFile,
152169
string $sourceDirectory,
153170
string $targetDirectory,
154171
array $cliConfig,
155172
bool $regenerate = false
156173
): WatchList {
157-
$project = new Project($sourceDirectory, $targetDirectory);
174+
$project = new Project($configFile, $sourceDirectory, $targetDirectory);
158175

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

src/Application/Cli/TravisAutoDeployCommand.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ protected function configure(): void
5858
'Repository you want to generate.',
5959
getcwd()
6060
)
61+
->addOption(
62+
'config-file',
63+
'f',
64+
InputOption::VALUE_REQUIRED,
65+
'If specified, use the given file as configuration file.',
66+
'couscous.yml'
67+
)
6168
->addOption(
6269
'php-version',
6370
null,
@@ -84,8 +91,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8491
$repositoryUrl = sprintf('https://%s@%s', (string) getenv('GH_TOKEN'), (string) getenv('GH_REF'));
8592
/** @var string */
8693
$targetBranch = $input->getOption('branch');
94+
/** @var string */
95+
$configFile = $input->getOption('config-file');
8796

88-
$repository = new Project($sourceDirectory, getcwd().'/.couscous/generated');
97+
$repository = new Project($configFile, $sourceDirectory, getcwd().'/.couscous/generated');
8998

9099
// verify some env variables
91100
$travisBranch = getenv('TRAVIS_BRANCH');

src/Model/Project.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
*/
1414
class Project
1515
{
16+
/**
17+
* Configuration file to load
18+
*/
19+
public $configFile;
20+
1621
/**
1722
* Directory containing the sources files to process.
1823
*
@@ -54,8 +59,9 @@ class Project
5459
*/
5560
protected $files = [];
5661

57-
public function __construct(string $sourceDirectory, string $targetDirectory)
62+
public function __construct(string $configFile, string $sourceDirectory, string $targetDirectory)
5863
{
64+
$this->configFile = "$sourceDirectory/$configFile";
5965
$this->sourceDirectory = $sourceDirectory;
6066
$this->targetDirectory = $targetDirectory;
6167
$this->watchlist = new WatchList();

src/Module/Config/Step/LoadConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(Filesystem $filesystem, Parser $yamlParser, LoggerIn
4343

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

4848
if (!$this->filesystem->exists($filename)) {
4949
$this->logger->notice('No couscous.yml configuration file found, using default config');

tests/FunctionalTest/BaseFunctionalTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public function tearDown(): void
2525
$this->clearGeneratedDirectory();
2626
}
2727

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

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

@@ -71,25 +71,27 @@ public function assertGenerationError($fixtureName, $expectedMessage)
7171
$this->assertStringContainsString($expectedMessage, $output);
7272
}
7373

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

8081
return sprintf(
81-
'%s generate -v --target="%s" %s 2>&1',
82+
'%s generate -v --target="%s" %s %s 2>&1',
8283
$bin,
8384
$targetDirectory,
85+
$configOption,
8486
$fixtureName
8587
);
8688
}
8789

88-
private function generate($fixtureName): array
90+
private function generate($fixtureName, $configFile = null): array
8991
{
9092
$this->clearGeneratedDirectory();
9193

92-
$command = $this->createCommand($fixtureName);
94+
$command = $this->createCommand($fixtureName, $configFile);
9395

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Foo!</title>
5+
</head>
6+
<body>
7+
This is a custom variable
8+
</body>
9+
</html>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
layout: page
3+
customVariable: "This is a custom variable"
4+
---
5+
6+
Hello, this is a test!

0 commit comments

Comments
 (0)