Skip to content

Commit

Permalink
[FEATURE] Implement concept for artifact migration
Browse files Browse the repository at this point in the history
  • Loading branch information
eliashaeussler committed Mar 22, 2023
1 parent 66b5e33 commit eb5bd75
Show file tree
Hide file tree
Showing 52 changed files with 2,137 additions and 188 deletions.
3 changes: 3 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
DependencyInjection\Loader\Configurator\ContainerConfigurator $configurator,
DependencyInjection\ContainerBuilder $container,
): void {
$container->registerForAutoconfiguration(Builder\Artifact\Migration\Version::class)
->addTag('artifact.version_migration')
;
$container->registerForAutoconfiguration(Builder\Writer\WriterInterface::class)
->addTag('builder.writer')
;
Expand Down
6 changes: 5 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
CPSIT\ProjectBuilder\:
resource: '../src/*'
exclude:
- '../src/Builder/Artifact/*'
- '../src/Builder/Artifact/*.php'
- '../src/Builder/Config/*'
- '../src/Builder/Generator/Step/CleanUpStep.php'
- '../src/Builder/Generator/Step/DumpBuildArtifactStep.php'
Expand All @@ -20,6 +20,10 @@ services:
- '../src/Resource/Local/ProcessedFile.php'
- '../src/Template/TemplateSource.php'

CPSIT\ProjectBuilder\Builder\ArtifactReader:
arguments:
$versions: !tagged_iterator artifact.version_migration

CPSIT\ProjectBuilder\Builder\Config\Config:
alias: 'app.config'

Expand Down
5 changes: 5 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersion;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector;
use Rector\Set\ValueObject\LevelSetList;

Expand All @@ -37,6 +38,10 @@
__DIR__.'/tests/src/Fixtures/Templates/*/vendor/*',

AddLiteralSeparatorToNumberRector::class,
JsonThrowOnErrorRector::class => [
__DIR__.'/src/Builder/ArtifactGenerator.php',
__DIR__.'/src/Builder/ArtifactReader.php',
],
]);

$rectorConfig->phpVersion(PhpVersion::PHP_81);
Expand Down
2 changes: 1 addition & 1 deletion resources/build-artifact.schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://json-schema.org/draft-04/schema",
"$schema": "https://json-schema.org/draft/2019-09/schema#",
"type": "object",
"title": "Build artifact for projects generated with the Project Builder",
"properties": {
Expand Down
31 changes: 26 additions & 5 deletions src/Builder/Artifact/Artifact.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,38 @@
*
* @internal
*
* @template T of array<string, mixed>
* @phpstan-type ArtifactType array{
* artifact: BuildArtifact,
* template: TemplateArtifact,
* generator: GeneratorArtifact,
* result: ResultArtifact
* }
*/
abstract class Artifact implements JsonSerializable
final class Artifact implements JsonSerializable
{
public function __construct(
public readonly BuildArtifact $artifact,
public readonly TemplateArtifact $template,
public readonly GeneratorArtifact $generator,
public readonly ResultArtifact $result,
) {
}

/**
* @return T
* @phpstan-return ArtifactType
*/
abstract public function dump(): array;
public function dump(): array
{
return [
'artifact' => $this->artifact,
'template' => $this->template,
'generator' => $this->generator,
'result' => $this->result,
];
}

/**
* @return T
* @phpstan-return ArtifactType
*/
public function jsonSerialize(): array
{
Expand Down
49 changes: 16 additions & 33 deletions src/Builder/Artifact/BuildArtifact.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@

namespace CPSIT\ProjectBuilder\Builder\Artifact;

use Composer\Package;
use CPSIT\ProjectBuilder\Builder;
use CPSIT\ProjectBuilder\Helper;
use Symfony\Component\Finder;

use function time;
use JsonSerializable;

/**
* BuildArtifact.
Expand All @@ -37,41 +32,29 @@
* @license GPL-3.0-or-later
*
* @internal
*
* @extends Artifact<array{
* artifact: array{version: int, file: string, date: int},
* template: TemplateArtifact,
* generator: GeneratorArtifact,
* result: ResultArtifact
* }>
*/
final class BuildArtifact extends Artifact
final class BuildArtifact implements JsonSerializable
{
private const VERSION = 1;

public function __construct(
private readonly string $file,
private readonly Builder\BuildResult $buildResult,
private readonly Package\RootPackageInterface $rootPackage,
public readonly int $version,
public readonly string $path,
public readonly int $date,
) {
}

public function dump(): array
/**
* @return array{
* version: int,
* path: string,
* date: int,
* }
*/
public function jsonSerialize(): array
{
return [
'artifact' => [
'version' => self::VERSION,
'file' => $this->file,
'date' => time(),
],
'template' => new TemplateArtifact($this->buildResult),
'generator' => new GeneratorArtifact($this->rootPackage),
'result' => new ResultArtifact($this->buildResult),
'version' => $this->version,
'path' => $this->path,
'date' => $this->date,
];
}

public function getFile(): Finder\SplFileInfo
{
return Helper\FilesystemHelper::createFileObject($this->buildResult->getWrittenDirectory(), $this->file);
}
}
32 changes: 13 additions & 19 deletions src/Builder/Artifact/GeneratorArtifact.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace CPSIT\ProjectBuilder\Builder\Artifact;

use Composer\Package;
use JsonSerializable;

/**
* GeneratorArtifact.
Expand All @@ -32,32 +32,26 @@
* @license GPL-3.0-or-later
*
* @internal
*
* @extends Artifact<array{
* package: PackageArtifact,
* executor: string
* }>
*/
final class GeneratorArtifact extends Artifact
final class GeneratorArtifact implements JsonSerializable
{
public function __construct(
private readonly Package\RootPackageInterface $rootPackage,
public readonly PackageArtifact $package,
public readonly string $executor,
) {
}

public function dump(): array
/**
* @return array{
* package: PackageArtifact,
* executor: string,
* }
*/
public function jsonSerialize(): array
{
return [
'package' => new PackageArtifact($this->rootPackage),
'executor' => $this->determineExecutor(),
'package' => $this->package,
'executor' => $this->executor,
];
}

private function determineExecutor(): string
{
return match (getenv('PROJECT_BUILDER_EXECUTOR')) {
'docker' => 'docker',
default => 'composer',
};
}
}
64 changes: 64 additions & 0 deletions src/Builder/Artifact/Migration/BaseVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Composer package "cpsit/project-builder".
*
* Copyright (C) 2023 Elias Häußler <e.haeussler@familie-redlich.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace CPSIT\ProjectBuilder\Builder\Artifact\Migration;

use CPSIT\ProjectBuilder\Helper;

use function is_callable;

/**
* BaseVersion.
*
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-3.0-or-later
*/
abstract class BaseVersion implements Version
{
/**
* @param array<string, mixed> $artifact
* @param non-empty-string $path
* @param non-empty-string|null $targetPath
*/
protected function remapValue(
array &$artifact,
string $path,
string $targetPath = null,
mixed $newValue = null,
): void {
$currentValue = Helper\ArrayHelper::getValueByPath($artifact, $path);

if (is_callable($newValue)) {
$newValue = $newValue($currentValue);
} elseif (null === $newValue) {
$newValue = $currentValue;
}

if (null === $targetPath) {
Helper\ArrayHelper::setValueByPath($artifact, $path, $newValue);
} else {
Helper\ArrayHelper::setValueByPath($artifact, $targetPath, $newValue);
Helper\ArrayHelper::removeByPath($artifact, $path);
}
}
}
46 changes: 46 additions & 0 deletions src/Builder/Artifact/Migration/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Composer package "cpsit/project-builder".
*
* Copyright (C) 2023 Elias Häußler <e.haeussler@familie-redlich.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace CPSIT\ProjectBuilder\Builder\Artifact\Migration;

/**
* Version.
*
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-3.0-or-later
*
* @internal
*/
interface Version
{
/**
* @param array<string, mixed> $artifact
*
* @return array<string, mixed>
*/
public function migrate(array $artifact): array;

public static function getSourceVersion(): int;

public static function getTargetVersion(): int;
}
56 changes: 56 additions & 0 deletions src/Builder/Artifact/Migration/Version1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Composer package "cpsit/project-builder".
*
* Copyright (C) 2023 Elias Häußler <e.haeussler@familie-redlich.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace CPSIT\ProjectBuilder\Builder\Artifact\Migration;

/**
* Version1.
*
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-3.0-or-later
*/
final class Version1 extends BaseVersion
{
public function migrate(array $artifact): array
{
// Silent migration from artifact.file to artifact.path
// since this was wrong implemented in the first place
$this->remapValue(
$artifact,
'artifact.file',
'artifact.path',
);

return $artifact;
}

public static function getSourceVersion(): int
{
return 1;
}

public static function getTargetVersion(): int
{
return 1;
}
}
Loading

0 comments on commit eb5bd75

Please sign in to comment.