Skip to content

Commit

Permalink
[FEATURE] Introduce target file support for processed source files
Browse files Browse the repository at this point in the history
This commit introduces a new schema configuration "target" for file
conditions within step configuration for processed source files and
processed shared source files. It allows to override the default
path determination behavior, e.g. when multiple files exist as
source files, while they should have the same target name, depending
on a given file condition.

Example:

Two source files `LICENSE.GPL-2.0.txt` and `LICENSE.GPL-3.0.txt` exist.
Both files must be named `LICENSE.txt` in the resulting project.
The file processing itself depends on given conditions: If the user
selects "GPL 2.0" as license, `LICENSE.GPL-2.0.txt` will be processed,
if "GPL 3.0" is selected, `LICENSE.GPL-3.0.txt` will be processed.

Example configuration:

steps:
  - type: processSourceFiles
    fileConditions:
      - path: 'LICENSE.GPL-2.0.txt'
        if: 'license == "GPL-2.0"'
        target: 'LICENSE.txt'
      - path: 'LICENSE.GPL-3.0.txt'
        if: 'license == "GPL-3.0"'
        target: 'LICENSE.txt'
  • Loading branch information
eliashaeussler committed Dec 7, 2022
1 parent c871a66 commit b9a0bf2
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 9 deletions.
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;
}
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());
}
}
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

0 comments on commit b9a0bf2

Please sign in to comment.