Skip to content

Commit

Permalink
PHAR directory where to store manifests follows now the --resource-di…
Browse files Browse the repository at this point in the history
…r option and manifests discoverable follows the .box.manifest.bin contents
  • Loading branch information
llaville committed Aug 27, 2024
1 parent 4b0ae27 commit 988f46c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 21 deletions.
2 changes: 1 addition & 1 deletion resources/default_stub.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ $withManifest = array_search('--manifest', $argv);
$withoutAnsi = array_search('--no-ansi', $argv);

if ($withManifest !== false) {
$manifestDir = '.box.manifests/';
$manifestDir = '%manifest_dir%';
$resources = (($argc - 1 > $withManifest) && !str_starts_with($argv[$withManifest + 1], '-')) ? [$argv[$withManifest + 1]] : ['%manifest_files%'];

foreach ($resources as $resource) {
Expand Down
5 changes: 3 additions & 2 deletions src/Console/Command/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Bartlett\BoxManifest\Composer\ManifestOptions;
use Bartlett\BoxManifest\Helper\BoxHelper;
use Bartlett\BoxManifest\Helper\ManifestFormat;
use Bartlett\BoxManifest\Pipeline\AbstractStage;
use Bartlett\BoxManifest\Pipeline\BuildStage;
use Bartlett\BoxManifest\Pipeline\CompileStage;
use Bartlett\BoxManifest\Pipeline\ConfigureStage;
Expand Down Expand Up @@ -118,7 +119,7 @@ protected function configure(): void
null,
InputOption::VALUE_REQUIRED,
'Directory where to store your manifest files into the PHP Archive',
'.box.manifests/'
AbstractStage::BOX_MANIFESTS_DIR
),
new InputOption(
ManifestOptions::IMMUTABLE_OPTION,
Expand Down Expand Up @@ -226,7 +227,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'template' => $templatePath,
'resources' => $resources,
'map' => $config->getFileMapper()->getMap(),
'resourceDir' => $io->getTypedOption(ManifestOptions::RESOURCE_DIR_OPTION)->asNullableString(),
'resourceDir' => $io->getTypedOption(ManifestOptions::RESOURCE_DIR_OPTION)->asString(),
'sbomSpec' => (new ManifestOptions($io))->getSbomSpec(),
'outputFormat' => (new ManifestOptions($io))->getFormat(true),
'output' => (new ManifestOptions($io))->getOutputFile() ?? 'php://stdout',
Expand Down
18 changes: 13 additions & 5 deletions src/Helper/ManifestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace Bartlett\BoxManifest\Helper;

use Bartlett\BoxManifest\Pipeline\AbstractStage;
use Bartlett\BoxManifest\StubGenerator;

use Symfony\Component\Console\Helper\Helper;
Expand Down Expand Up @@ -81,17 +82,24 @@ public function get(array $resources): ?string
* @param string[]|null $resources
* @param string[][]|null $mapFiles
*/
public function getStubGenerator(?string $templatePath = null, array $resources = null, array $mapFiles = null, ?string $version = null): StubGenerator
{
public function getStubGenerator(
?string $templatePath = null,
?array $resources = null,
?array $mapFiles = null,
?string $version = null,
?string $resourceDir = null
): StubGenerator {
if (null === $templatePath) {
$templatePath = dirname(__DIR__, 2) . '/resources/default_stub.template';
}
if (null === $resourceDir) {
$resourceDir = AbstractStage::BOX_MANIFESTS_DIR;
}
if (!empty($mapFiles) && empty($resources)) {
$manifestDir = '.box.manifests/';
$resources = [];
foreach ($mapFiles as $mapFile) {
foreach ($mapFile as $target) {
if (str_starts_with($target, $manifestDir)) {
if (str_starts_with($target, $resourceDir)) {
$resources[] = $target;
}
}
Expand All @@ -100,7 +108,7 @@ public function getStubGenerator(?string $templatePath = null, array $resources
if (empty($resources)) {
$resources = ManifestFile::values();
}
return new StubGenerator($templatePath, $resources, $version ?? '@dev');
return new StubGenerator($templatePath, $resources, $version ?? '@dev', $resourceDir);
}

public function getName(): string
Expand Down
19 changes: 19 additions & 0 deletions src/Pipeline/AbstractStage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;

use function file_exists;
use function file_get_contents;
use function fopen;
use function is_string;
use function realpath;
use function sprintf;
use function unserialize;

/**
* @author Laurent Laville
* @since Release 4.0.0
*/
abstract readonly class AbstractStage
{
public const BOX_MANIFESTS_DIR = '.box.manifests/';
protected const META_DATA_FILE = '.box.manifests.bin';

private ?HelperInterface $debugFormatterHelper;
Expand Down Expand Up @@ -90,4 +94,19 @@ protected function debugPrintStage(string|iterable $messages, bool $newline = fa
$stream->write($messages, $newline);
fclose($stream->getStream());
}

/**
* @return array<string, string>
*/
protected function getMetaData(): array
{
if (file_exists(self::META_DATA_FILE)) {
// @phpstan-ignore argument.type
$metadata = unserialize(file_get_contents(self::META_DATA_FILE));
} else {
$metadata = [];
}

return $metadata;
}
}
9 changes: 1 addition & 8 deletions src/Pipeline/BuildStage.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,7 @@ public function __invoke(array $payload): array
}
}

if (file_exists(self::META_DATA_FILE)) {
// @phpstan-ignore argument.type
$metadata = unserialize(file_get_contents(self::META_DATA_FILE));
} else {
$metadata = [];
}

$manifests = serialize(array_merge($metadata, $payload['response']['artifacts'] ?? []));
$manifests = serialize(array_merge($this->getMetaData(), $payload['response']['artifacts'] ?? []));
$this->writeToStream(self::META_DATA_FILE, $manifests, 'Unable to write Manifests Metadata');

$this->debugPrintStage(
Expand Down
8 changes: 6 additions & 2 deletions src/Pipeline/StubStage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Bartlett\BoxManifest\Helper\ManifestHelper;

use function array_keys;
use function str_replace;

/**
Expand All @@ -24,14 +25,17 @@ public function __invoke(array $payload): array
{
$config = $payload['configuration'];

$resources = empty($payload['resources']) ? array_keys($this->getMetaData()) : $payload['resources'];

/** @var ManifestHelper $helper */
$helper = $this->command->getHelper(ManifestHelper::NAME);

$stubGenerator = $helper->getStubGenerator(
$payload['template'],
$payload['resources'],
$resources,
$payload['map'],
$payload['versions']['boxManifest']
$payload['versions']['boxManifest'],
$payload['resourceDir']
);

if ($config->getConfigurationFile()) {
Expand Down
7 changes: 4 additions & 3 deletions src/StubGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class StubGenerator
public function __construct(
string $templatePath,
array $resources,
string $version
string $version,
string $resourceDir
) {
if (!file_exists($templatePath) || !is_readable($templatePath)) {
throw new InvalidArgumentException(sprintf('Filename "%s" does not exists or is not readable.', $templatePath));
Expand All @@ -44,8 +45,8 @@ public function __construct(
$template = ltrim($template, "<?php\n");

$this->stubTemplate = str_replace(
['%manifest_files%', '%version%'],
[implode("', '", $resources), $version],
['%manifest_dir%', '%manifest_files%', '%version%'],
[$resourceDir, implode("', '", $resources), $version],
$template
);
}
Expand Down

0 comments on commit 988f46c

Please sign in to comment.