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

Better naming (build -> export) #9

Merged
merged 1 commit into from
Jan 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
* @author Marco Lipparini <developer@liarco.net>
*/
#[AsCommand(
name: 'nft:build-assets',
description: 'Builds a new assets folder with all files shuffled using the current mapping',
name: 'nft:export-assets',
description: 'Exports a new assets folder with all the files shuffling them using the current mapping (if any)',
)]
class BuildAssetsCommand extends Command
class ExportAssetsCommand extends Command
{
public function __construct(
private readonly CollectionManager $collectionManager,
Expand All @@ -41,28 +41,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$symfonyStyle = new SymfonyStyle($input, $output);

if (! $symfonyStyle->confirm(
"I'm about to delete the shuffled assets directory and its content. Are you sure?",
"I'm about to delete the exported assets directory and its content. Are you sure?",
false,
)) {
$symfonyStyle->warning('Aborting...');

return Command::SUCCESS;
}

$this->collectionManager->clearShuffledAssets();
$symfonyStyle->info('Deleting old data...');
$this->collectionManager->clearExportedAssets();

$symfonyStyle->info('Exporting new data...');
$symfonyStyle->progressStart($this->collectionManager->getMaxTokenId());

foreach (range(1, $this->collectionManager->getMaxTokenId()) as $tokenId) {
$this->collectionManager->storeShuffledAsset($tokenId);
$this->collectionManager->storeExportedAsset($tokenId);
gc_collect_cycles();

$symfonyStyle->progressAdvance();
}

$symfonyStyle->progressFinish();

$symfonyStyle->success('Assets built successfully!');
$symfonyStyle->success('Assets exported successfully!');

return Command::SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
* @author Marco Lipparini <developer@liarco.net>
*/
#[AsCommand(
name: 'nft:build-metadata',
description: 'Builds a new metadata folder with all files shuffled using the current mapping',
name: 'nft:export-metadata',
description: 'Exports a new metadata folder with all the files updating and shuffling them using the current mapping (if any)',
)]
class BuildMetadataCommand extends Command
class ExportMetadataCommand extends Command
{
/**
* @var string
Expand All @@ -49,7 +49,7 @@ protected function configure(): void
->addArgument(
self::URI_PREFIX,
InputArgument::REQUIRED,
'The URI prefix where the assets have been uploaded to',
'The URI prefix where the exported assets have been uploaded to',
)
;
}
Expand All @@ -64,28 +64,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if (! $symfonyStyle->confirm(
"I'm about to delete the shuffled metadata directory and its content. Are you sure?",
"I'm about to delete the exported metadata directory and its content. Are you sure?",
false,
)) {
$symfonyStyle->warning('Aborting...');

return Command::SUCCESS;
}

$this->collectionManager->clearShuffledMetadata();
$symfonyStyle->info('Deleting old data...');
$this->collectionManager->clearExportedMetadata();

$symfonyStyle->info('Exporting new data...');
$symfonyStyle->progressStart($this->collectionManager->getMaxTokenId());

foreach (range(1, $this->collectionManager->getMaxTokenId()) as $tokenId) {
$this->collectionManager->storeShuffledMetadata($tokenId, trim($uriPrefix, '/'));
$this->collectionManager->storeExportedMetadata($tokenId, trim($uriPrefix, '/'));
gc_collect_cycles();

$symfonyStyle->progressAdvance();
}

$symfonyStyle->progressFinish();

$symfonyStyle->success('Metadata built successfully!');
$symfonyStyle->success('Metadata exported successfully!');

return Command::SUCCESS;
}
Expand Down
25 changes: 21 additions & 4 deletions src/Command/ShuffleCollectionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
namespace App\Command;

use App\Service\CollectionManager;
use RuntimeException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -26,7 +28,7 @@
*/
#[AsCommand(
name: 'nft:shuffle-collection',
description: 'Generates a new shuffle mapping for all the tokens (or the given range)',
description: 'Generates a new shuffle mapping for all the tokens (or a given range)',
)]
class ShuffleCollectionCommand extends Command
{
Expand Down Expand Up @@ -75,7 +77,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
) ? (int) $maxTokenIdOptionValue : $this->collectionManager->getMaxTokenId();

if (! $symfonyStyle->confirm(
'You are about to shuffle your collection starting from token #'.$minTokenId.' up to token #'.$maxTokenId.'. This will overwrite the previous shuffle mapping. Are you sure?',
"I'm about to generate a new shuffle mapping for your collection starting from token #".$minTokenId.' up to token #'.$maxTokenId.'. This will overwrite the previous shuffle mapping. Are you sure?',
false,
)) {
$symfonyStyle->warning('Aborting...');
Expand All @@ -85,9 +87,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->collectionManager->shuffle($minTokenId, $maxTokenId);

$symfonyStyle->success('Collection shuffled successfully!');
$symfonyStyle->info('The new shuffle mapping has been generated and added to the storage...');

$symfonyStyle->info('Remember to run "bin/console cache:clear" to invalidate metadata cache!');
// Clear app cache...
$symfonyStyle->info('Running "bin/console cache:clear"...');

$command = $this->getApplication()?->find('cache:clear');

if (! $command instanceof Command) {
throw new RuntimeException('Could not find the "cache:clear" command.');
}

$returnCode = $command->run(new ArrayInput([]), $output);

if (Command::SUCCESS !== $returnCode) {
throw new RuntimeException('An error occurred while clearing the cache.');
}

$symfonyStyle->success('New shuffle mapping generated successfully!');

return Command::SUCCESS;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Contract/CollectionFilesystemDriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ interface CollectionFilesystemDriverInterface
/**
* @var string
*/
final public const SHUFFLED_METADATA_PATH = '/shuffled/metadata';
final public const EXPORTED_METADATA_PATH = '/exported/metadata';

/**
* @var string
*/
final public const SHUFFLED_ASSETS_PATH = '/shuffled/assets';
final public const EXPORTED_ASSETS_PATH = '/exported/assets';

public function getAssetsExtension(): string;

Expand Down Expand Up @@ -93,14 +93,14 @@ public function getShuffleMapping(): ?array;
*/
public function storeNewShuffleMapping(array $newShuffleMapping): void;

public function clearShuffledMetadata(): void;
public function clearExportedMetadata(): void;

public function clearShuffledAssets(): void;
public function clearExportedAssets(): void;

/**
* @param array<string, mixed> $metadata
*/
public function storeShuffledMetadata(int $tokenId, array $metadata): void;
public function storeExportedMetadata(int $tokenId, array $metadata): void;

public function storeShuffledAsset(int $tokenId, SplFileInfo $originalAsset): void;
public function storeExportedAsset(int $tokenId, SplFileInfo $originalAsset): void;
}
16 changes: 8 additions & 8 deletions src/FilesystemDriver/LocalFilesystemDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,33 +130,33 @@ public function storeNewShuffleMapping(array $newShuffleMapping): void
FileSystem::write($this->localCollectionPath.self::MAPPING_PATH, Json::encode($newShuffleMapping));
}

public function clearShuffledMetadata(): void
public function clearExportedMetadata(): void
{
FileSystem::delete($this->localCollectionPath.self::SHUFFLED_METADATA_PATH);
FileSystem::delete($this->localCollectionPath.self::EXPORTED_METADATA_PATH);
}

public function clearShuffledAssets(): void
public function clearExportedAssets(): void
{
FileSystem::delete($this->localCollectionPath.self::SHUFFLED_ASSETS_PATH);
FileSystem::delete($this->localCollectionPath.self::EXPORTED_ASSETS_PATH);
}

/**
* @param array<string, mixed> $metadata
*/
public function storeShuffledMetadata(int $tokenId, array $metadata): void
public function storeExportedMetadata(int $tokenId, array $metadata): void
{
FileSystem::write(
$this->localCollectionPath.self::SHUFFLED_METADATA_PATH.'/'.$tokenId.'.json',
$this->localCollectionPath.self::EXPORTED_METADATA_PATH.'/'.$tokenId.'.json',
Json::encode($metadata, Json::PRETTY),
null,
);
}

public function storeShuffledAsset(int $tokenId, SplFileInfo $originalAsset): void
public function storeExportedAsset(int $tokenId, SplFileInfo $originalAsset): void
{
FileSystem::copy(
$originalAsset->getPathname(),
$this->localCollectionPath.self::SHUFFLED_ASSETS_PATH.'/'.$tokenId.'.'.$this->assetsExtension,
$this->localCollectionPath.self::EXPORTED_ASSETS_PATH.'/'.$tokenId.'.'.$this->assetsExtension,
);
}
}
35 changes: 19 additions & 16 deletions src/FilesystemDriver/S3FilesystemDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function getHiddenAssetExtension(): string
*/
public function getMetadata(int $tokenId): array
{
$metadataPath = $this->buildS3Path(self::METADATA_PATH.'/'.$tokenId.'.json');
$metadataPath = $this->generateS3Path(self::METADATA_PATH.'/'.$tokenId.'.json');
$metadata = Json::decode(FileSystem::read($metadataPath), Json::FORCE_ARRAY);

if (! is_array($metadata)) {
Expand All @@ -89,15 +89,18 @@ public function getMetadata(int $tokenId): array

public function getAssetFileInfo(int $tokenId): SplFileInfo
{
return new File($this->buildS3Path(self::ASSETS_PATH.'/'.$tokenId.'.'.$this->assetsExtension));
return new File($this->generateS3Path(self::ASSETS_PATH.'/'.$tokenId.'.'.$this->assetsExtension));
}

/**
* @inheritdoc
*/
public function getHiddenMetadata(): array
{
$metadata = Json::decode(FileSystem::read($this->buildS3Path(self::HIDDEN_METADATA_PATH)), Json::FORCE_ARRAY);
$metadata = Json::decode(
FileSystem::read($this->generateS3Path(self::HIDDEN_METADATA_PATH)),
Json::FORCE_ARRAY,
);

if (! is_array($metadata)) {
throw new LogicException('Unexpected metadata value (it must be an array).');
Expand All @@ -110,15 +113,15 @@ public function getHiddenMetadata(): array

public function getHiddenAssetFileInfo(): SplFileInfo
{
return new File($this->buildS3Path(self::HIDDEN_ASSET_PATH.$this->hiddenAssetExtension));
return new File($this->generateS3Path(self::HIDDEN_ASSET_PATH.$this->hiddenAssetExtension));
}

/**
* @return object[]
*/
public function getAbi(): array
{
$abi = Json::decode(FileSystem::read($this->buildS3Path(self::ABI_PATH)));
$abi = Json::decode(FileSystem::read($this->generateS3Path(self::ABI_PATH)));

if (! is_array($abi)) {
throw new LogicException('Unexpected ABI value (it must be an array).');
Expand All @@ -132,7 +135,7 @@ public function getAbi(): array
public function getShuffleMapping(): ?array
{
if (empty($this->shuffleMapping)) {
$mappingPath = $this->buildS3Path(self::MAPPING_PATH);
$mappingPath = $this->generateS3Path(self::MAPPING_PATH);

if (! is_file($mappingPath)) {
return null;
Expand All @@ -149,40 +152,40 @@ public function getShuffleMapping(): ?array

public function storeNewShuffleMapping(array $newShuffleMapping): void
{
FileSystem::write($this->buildS3Path(self::MAPPING_PATH), Json::encode($newShuffleMapping), null);
FileSystem::write($this->generateS3Path(self::MAPPING_PATH), Json::encode($newShuffleMapping), null);
}

public function clearShuffledMetadata(): void
public function clearExportedMetadata(): void
{
FileSystem::delete($this->buildS3Path(self::SHUFFLED_METADATA_PATH));
FileSystem::delete($this->generateS3Path(self::EXPORTED_METADATA_PATH));
}

public function clearShuffledAssets(): void
public function clearExportedAssets(): void
{
FileSystem::delete($this->buildS3Path(self::SHUFFLED_ASSETS_PATH));
FileSystem::delete($this->generateS3Path(self::EXPORTED_ASSETS_PATH));
}

/**
* @param array<string, mixed> $metadata
*/
public function storeShuffledMetadata(int $tokenId, array $metadata): void
public function storeExportedMetadata(int $tokenId, array $metadata): void
{
FileSystem::write(
$this->buildS3Path(self::SHUFFLED_METADATA_PATH.'/'.$tokenId.'.json'),
$this->generateS3Path(self::EXPORTED_METADATA_PATH.'/'.$tokenId.'.json'),
Json::encode($metadata, Json::PRETTY),
null,
);
}

public function storeShuffledAsset(int $tokenId, SplFileInfo $originalAsset): void
public function storeExportedAsset(int $tokenId, SplFileInfo $originalAsset): void
{
FileSystem::copy(
$originalAsset->getPathname(),
$this->buildS3Path(self::SHUFFLED_ASSETS_PATH.'/'.$tokenId.'.'.$this->assetsExtension),
$this->generateS3Path(self::EXPORTED_ASSETS_PATH.'/'.$tokenId.'.'.$this->assetsExtension),
);
}

private function buildS3Path(string $relativePath): string
private function generateS3Path(string $relativePath): string
{
$keyPrefix = empty($this->objectsKeyPrefix) ? '' : trim($this->objectsKeyPrefix, '/').'/';

Expand Down
Loading