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

[FEATURE] Introduce target file support for processed source files #54

Merged
merged 1 commit into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions resources/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@
"path": {
"type": "string",
"title": "Relative path to a file, can contain symbols processable by `fnmatch`"
},
"target": {
"type": "string",
"title": "Relative path to target file, can be used to override the default path"
}
},
"additionalProperties": false,
Expand Down
6 changes: 6 additions & 0 deletions src/Builder/Config/ValueObject/FileCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ final class FileCondition
public function __construct(
private string $path,
string $if,
private ?string $target = null,
) {
$this->if = $if;
}
Expand All @@ -44,4 +45,9 @@ public function getPath(): string
{
return $this->path;
}

public function getTarget(): ?string
{
return $this->target;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function run(Builder\BuildResult $buildResult): bool
);

$writer = $this->writerFactory->get($sharedSourceFile->getPathname());
$processedFile = $writer->write($instructions, $sharedSourceFile);
$processedFile = $writer->write($instructions, $sharedSourceFile, $this->findTargetFile($sharedSourceFile));

$this->processedFiles[] = new Resource\Local\ProcessedFile($sharedSourceFile, $processedFile);

Expand Down
2 changes: 1 addition & 1 deletion src/Builder/Generator/Step/ProcessSourceFilesStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function run(Builder\BuildResult $buildResult): bool
);

$writer = $this->writerFactory->get($sourceFile->getPathname());
$processedFile = $writer->write($instructions, $sourceFile);
$processedFile = $writer->write($instructions, $sourceFile, $this->findTargetFile($sourceFile));

$this->processedFiles[] = new Resource\Local\ProcessedFile($sourceFile, $processedFile);

Expand Down
13 changes: 13 additions & 0 deletions src/Builder/Generator/Step/ProcessingFilesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
use Symfony\Component\Filesystem;
use Symfony\Component\Finder;

use function fnmatch;

/**
* ProcessingFilesTrait.
*
Expand Down Expand Up @@ -78,4 +80,15 @@ protected function shouldProcessFile(Finder\SplFileInfo $file, Builder\BuildInst

return true;
}

protected function findTargetFile(Finder\SplFileInfo $file): ?string
{
foreach ($this->config->getOptions()->getFileConditions() as $fileCondition) {
if (fnmatch($fileCondition->getPath(), $file->getRelativePathname())) {
return $fileCondition->getTarget();
}
}

return null;
}
}
12 changes: 9 additions & 3 deletions src/Builder/Writer/GenericFileWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ public function __construct(
) {
}

public function write(Builder\BuildInstructions $instructions, Finder\SplFileInfo $file): Finder\SplFileInfo
{
public function write(
Builder\BuildInstructions $instructions,
Finder\SplFileInfo $file,
?string $targetFile = null,
): Finder\SplFileInfo {
$targetDirectory = $instructions->getTemporaryDirectory();
$targetFile = Helper\FilesystemHelper::createFileObject($targetDirectory, $file->getRelativePathname());
$targetFile = Helper\FilesystemHelper::createFileObject(
$targetDirectory,
$targetFile ?? $file->getRelativePathname(),
);

$this->filesystem->copy($file->getPathname(), $targetFile->getPathname());

Expand Down
3 changes: 2 additions & 1 deletion src/Builder/Writer/TemplateWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ public function __construct(
public function write(
Builder\BuildInstructions $instructions,
Finder\SplFileInfo $file,
?string $targetFile = null,
array $variables = [],
): Finder\SplFileInfo {
$renderer = $this->renderer->withRootPath($file->getPath());
$renderResult = $renderer->render($instructions, $file->getFilename(), $variables);
$targetFile = Helper\FilesystemHelper::createFileObject(
$instructions->getTemporaryDirectory(),
(string) preg_replace('/\.twig$/', '', $file->getRelativePathname()),
$targetFile ?? (string) preg_replace('/\.twig$/', '', $file->getRelativePathname()),
);

$this->filesystem->dumpFile($targetFile->getPathname(), $renderResult);
Expand Down
6 changes: 5 additions & 1 deletion src/Builder/Writer/WriterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
*/
interface WriterInterface
{
public function write(Builder\BuildInstructions $instructions, Finder\SplFileInfo $file): Finder\SplFileInfo;
public function write(
Builder\BuildInstructions $instructions,
Finder\SplFileInfo $file,
?string $targetFile = null,
): Finder\SplFileInfo;

public static function supports(string $file): bool;
}
2 changes: 2 additions & 0 deletions tests/src/Builder/Config/ConfigFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ public function buildFromFileReturnsHydratedConfigObject(): void
new Src\Builder\Config\ValueObject\StepOptions([
new Src\Builder\Config\ValueObject\FileCondition('dummy-2.'.$type, 'false'),
new Src\Builder\Config\ValueObject\FileCondition('*-3.'.$type, 'false'),
new Src\Builder\Config\ValueObject\FileCondition('dummy-4.'.$type, 'true', 'overrides/dummy-4.'.$type),
]),
),
new Src\Builder\Config\ValueObject\Step(
'processSharedSourceFiles',
new Src\Builder\Config\ValueObject\StepOptions([
new Src\Builder\Config\ValueObject\FileCondition('shared-dummy-2.'.$type, 'false'),
new Src\Builder\Config\ValueObject\FileCondition('shared-*-3.'.$type, 'false'),
new Src\Builder\Config\ValueObject\FileCondition('shared-dummy-4.'.$type, 'true', 'overrides/shared-dummy-4.'.$type),
]),
),
new Src\Builder\Config\ValueObject\Step('mirrorProcessedFiles'),
Expand Down
10 changes: 9 additions & 1 deletion tests/src/Builder/Config/ValueObject/FileConditionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class FileConditionTest extends TestCase

protected function setUp(): void
{
$this->subject = new Src\Config\ValueObject\FileCondition('foo', 'bar');
$this->subject = new Src\Config\ValueObject\FileCondition('foo', 'bar', 'target');
}

/**
Expand Down Expand Up @@ -76,4 +76,12 @@ public function conditionMatchesChecksIfConditionMatches(): void
self::assertFalse($this->subject->conditionMatches($expressionLanguage, ['bar' => false]));
self::assertTrue($this->subject->conditionMatches($expressionLanguage, ['bar' => true]));
}

/**
* @test
*/
public function getTargetReturnsTarget(): void
{
self::assertSame('target', $this->subject->getTarget());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ public function runProcessesSourceFilesAndAppliesStep(): void
$actual = $this->subject->run($this->result);

self::assertTrue($actual);
self::assertCount(1, $this->subject->getProcessedFiles());
self::assertSame('shared-dummy.yaml', $this->subject->getProcessedFiles()[0]->getTargetFile()->getRelativePathname());
self::assertCount(2, $this->subject->getProcessedFiles());
self::assertSame('overrides/shared-dummy-4.yaml', $this->subject->getProcessedFiles()[0]->getTargetFile()->getRelativePathname());
self::assertSame('shared-dummy.yaml', $this->subject->getProcessedFiles()[1]->getTargetFile()->getRelativePathname());
self::assertFileExists($this->result->getInstructions()->getTemporaryDirectory().'/shared-dummy.yaml');
self::assertFileDoesNotExist($this->result->getInstructions()->getTemporaryDirectory().'/shared-dummy-2.yaml');
self::assertFileDoesNotExist($this->result->getInstructions()->getTemporaryDirectory().'/shared-dummy-3.yaml');
self::assertFileDoesNotExist($this->result->getInstructions()->getTemporaryDirectory().'/shared-dummy-4.yaml');
self::assertFileExists($this->result->getInstructions()->getTemporaryDirectory().'/overrides/shared-dummy-4.yaml');
self::assertTrue($this->result->isStepApplied($this->subject));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ public function runProcessesSourceFilesAndAppliesStep(): void
$actual = $this->subject->run($this->result);

self::assertTrue($actual);
self::assertCount(1, $this->subject->getProcessedFiles());
self::assertSame('dummy.yaml', $this->subject->getProcessedFiles()[0]->getTargetFile()->getRelativePathname());
self::assertCount(2, $this->subject->getProcessedFiles());
self::assertSame('overrides/dummy-4.yaml', $this->subject->getProcessedFiles()[0]->getTargetFile()->getRelativePathname());
self::assertSame('dummy.yaml', $this->subject->getProcessedFiles()[1]->getTargetFile()->getRelativePathname());
self::assertFileExists($this->result->getInstructions()->getTemporaryDirectory().'/dummy.yaml');
self::assertFileDoesNotExist($this->result->getInstructions()->getTemporaryDirectory().'/dummy-2.yaml');
self::assertFileDoesNotExist($this->result->getInstructions()->getTemporaryDirectory().'/dummy-3.yaml');
self::assertFileDoesNotExist($this->result->getInstructions()->getTemporaryDirectory().'/dummy-4.yaml');
self::assertFileExists($this->result->getInstructions()->getTemporaryDirectory().'/overrides/dummy-4.yaml');
self::assertTrue($this->result->isStepApplied($this->subject));
}

Expand Down
21 changes: 21 additions & 0 deletions tests/src/Builder/Writer/GenericFileWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,25 @@ public function writeCopiesGivenFileToTemporaryDirectory(): void

(new Filesystem\Filesystem())->remove(dirname($expected));
}

/**
* @test
*/
public function writeCopiesGivenFileToGivenTargetFile(): void
{
$instructions = new Src\Builder\BuildInstructions(
self::$container->get('app.config'),
'foo',
);
$sourceFile = __FILE__;
$file = new Finder\SplFileInfo($sourceFile, dirname($sourceFile), basename($sourceFile));

$expected = $instructions->getTemporaryDirectory().'/overrides/foo.php';
$actual = $this->subject->write($instructions, $file, 'overrides/foo.php');

self::assertSame($expected, $actual->getPathname());
self::assertFileExists($expected);

(new Filesystem\Filesystem())->remove(dirname($expected));
}
}
38 changes: 37 additions & 1 deletion tests/src/Builder/Writer/TemplateWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,43 @@ public function writeWritesRenderedTemplateFileToTemporaryDirectory(): void
$file = new Finder\SplFileInfo($templateFile, dirname($templateFile), basename($templateFile));

$expected = $instructions->getTemporaryDirectory().'/dump.json';
$actual = $this->subject->write($instructions, $file, ['bar' => 'bar']);
$actual = $this->subject->write($instructions, $file, variables: ['bar' => 'bar']);

self::assertSame($expected, $actual->getPathname());
self::assertFileExists($expected);

$expectedJson = [
'instructions' => [
'sourceDirectory' => dirname(__DIR__, 2).'/templates/src',
'sharedSourceDirectory' => dirname(__DIR__, 2).'/templates/shared',
],
'foo' => 'foo',
'bar' => 'bar',
];

self::assertJson($actual->getContents());
self::assertSame($expectedJson, json_decode($actual->getContents(), true, 512, JSON_THROW_ON_ERROR));

(new Filesystem\Filesystem())->remove(dirname($expected));
}

/**
* @test
*/
public function writeWritesRenderedTemplateFileToGivenTargetFile(): void
{
$instructions = new Src\Builder\BuildInstructions(
self::$container->get('app.config'),
'foo',
);
$instructions->addTemplateVariable('foo', 'foo');
$instructions->addTemplateVariable('bar', 'foo');

$templateFile = dirname(__DIR__, 3).'/templates/dump.json.twig';
$file = new Finder\SplFileInfo($templateFile, dirname($templateFile), basename($templateFile));

$expected = $instructions->getTemporaryDirectory().'/overrides/dump.json';
$actual = $this->subject->write($instructions, $file, 'overrides/dump.json', ['bar' => 'bar']);

self::assertSame($expected, $actual->getPathname());
self::assertFileExists($expected);
Expand Down
10 changes: 10 additions & 0 deletions tests/src/Fixtures/Templates/json-template/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
{
"path": "*-3.json",
"if": "false"
},
{
"path": "dummy-4.json",
"if": "true",
"target": "overrides/dummy-4.json"
}
]
}
Expand All @@ -30,6 +35,11 @@
{
"path": "shared-*-3.json",
"if": "false"
},
{
"path": "shared-dummy-4.json",
"if": "true",
"target": "overrides/shared-dummy-4.json"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "world"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "world"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "world"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "{{ bar.name }}"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "world"
}
6 changes: 6 additions & 0 deletions tests/src/Fixtures/Templates/yaml-template/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ steps:
if: 'false'
- path: '*-3.yaml'
if: 'false'
- path: dummy-4.yaml
if: 'true'
target: 'overrides/dummy-4.yaml'
- type: processSharedSourceFiles
options:
fileConditions:
- path: shared-dummy-2.yaml
if: 'false'
- path: 'shared-*-3.yaml'
if: 'false'
- path: shared-dummy-4.yaml
if: 'true'
target: 'overrides/shared-dummy-4.yaml'
- type: mirrorProcessedFiles

properties:
Expand Down