-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Introduce build artifacts for created projects
Resolves: #12
- Loading branch information
1 parent
39c09ba
commit 3903bb9
Showing
13 changed files
with
484 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<?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; | ||
|
||
use Composer\Package; | ||
use CPSIT\ProjectBuilder\Resource; | ||
use JsonSerializable; | ||
use Symfony\Component\Finder; | ||
|
||
use function array_map; | ||
use function time; | ||
|
||
/** | ||
* BuildArtifact. | ||
* | ||
* @author Elias Häußler <e.haeussler@familie-redlich.de> | ||
* @license GPL-3.0-or-later | ||
* | ||
* @internal | ||
* | ||
* @phpstan-type TBuildArtifact array{ artifact: TArtifact, template: TTemplateArtifact, generator: TGeneratorArtifact, result: TResultArtifact} | ||
* @phpstan-type TArtifact array{version: int, path: string, date: int} | ||
* @phpstan-type TPackageArtifact array{name: string, version: string, sourceReference: string|null, sourceUrl: string|null, distUrl: string|null} | ||
* @phpstan-type TTemplateArtifact array{identifier: string, hash: string, package: TPackageArtifact, provider: array{name: string, url: string}} | ||
* @phpstan-type TGeneratorArtifact array{package: TPackageArtifact} | ||
* @phpstan-type TResultArtifact array{properties: array<string, mixed>, steps: list<array{type: string, applied: bool}>, processedFiles: list<array{source: string, target: string}>} | ||
*/ | ||
final class BuildArtifact implements JsonSerializable | ||
{ | ||
private const VERSION = 1; | ||
|
||
public function __construct( | ||
private Finder\SplFileInfo $path, | ||
private BuildResult $buildResult, | ||
private Package\RootPackageInterface $rootPackage, | ||
) { | ||
} | ||
|
||
public function getPath(): Finder\SplFileInfo | ||
{ | ||
return $this->path; | ||
} | ||
|
||
/** | ||
* @phpstan-return TBuildArtifact | ||
*/ | ||
public function dump(): array | ||
{ | ||
return [ | ||
'artifact' => [ | ||
'version' => self::VERSION, | ||
'path' => $this->path->getRelativePathname(), | ||
'date' => time(), | ||
], | ||
'template' => $this->buildTemplateArtifact(), | ||
'generator' => $this->buildGeneratorArtifact(), | ||
'result' => $this->buildResultArtifact(), | ||
]; | ||
} | ||
|
||
/** | ||
* @phpstan-return TTemplateArtifact | ||
*/ | ||
private function buildTemplateArtifact(): array | ||
{ | ||
$config = $this->buildResult->getInstructions()->getConfig(); | ||
$package = $config->getSource()->getPackage(); | ||
$provider = $config->getSource()->getProvider(); | ||
|
||
return [ | ||
'identifier' => $config->getIdentifier(), | ||
'hash' => $config->buildHash(), | ||
'package' => $this->decoratePackage($package), | ||
'provider' => [ | ||
'name' => $provider::getName(), | ||
'url' => $provider->getUrl(), | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @phpstan-return TGeneratorArtifact | ||
*/ | ||
private function buildGeneratorArtifact(): array | ||
{ | ||
return [ | ||
'package' => $this->decoratePackage($this->rootPackage), | ||
]; | ||
} | ||
|
||
/** | ||
* @phpstan-return TResultArtifact | ||
*/ | ||
private function buildResultArtifact(): array | ||
{ | ||
return [ | ||
'properties' => $this->buildResult->getInstructions()->getTemplateVariables(), | ||
'steps' => array_map( | ||
fn (Config\ValueObject\Step $step) => [ | ||
'type' => $step->getType(), | ||
'applied' => $this->buildResult->isStepApplied($step->getType()), | ||
], | ||
$this->buildResult->getInstructions()->getConfig()->getSteps(), | ||
), | ||
'processedFiles' => array_map( | ||
fn (Resource\Local\ProcessedFile $processedFile) => [ | ||
'source' => $processedFile->getOriginalFile()->getRelativePathname(), | ||
'target' => $processedFile->getTargetFile()->getRelativePathname(), | ||
], | ||
$this->buildResult->getProcessedFiles(), | ||
), | ||
]; | ||
} | ||
|
||
/** | ||
* @phpstan-return TPackageArtifact | ||
*/ | ||
private function decoratePackage(Package\PackageInterface $package): array | ||
{ | ||
return [ | ||
'name' => $package->getName(), | ||
'version' => $package->getVersion(), | ||
'sourceReference' => $package->getSourceReference(), | ||
'sourceUrl' => $package->getSourceUrl(), | ||
'distUrl' => $package->getDistUrl(), | ||
]; | ||
} | ||
|
||
/** | ||
* @phpstan-return TBuildArtifact | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
return $this->dump(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.