Files
pterodactyl-panel/app/Console/Commands/Extensions/PackCommand.php
T

89 lines
4.6 KiB
PHP
Raw Normal View History

2026-10-05 18:10:18 -04:00
<?php
declare(strict_types=1);
namespace Pterodactyl\Console\Commands\Extensions;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Pterodactyl\Exceptions\Extensions\InvalidExtensionException;
use Pterodactyl\Services\Extensions\ExtensionAssetPublisher;
2026-10-07 16:22:56 -04:00
use Pterodactyl\Services\Extensions\ExtensionComposerInspector;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Services\Extensions\ExtensionManifest;
use Pterodactyl\Services\Extensions\ExtensionManifestValidator;
use ZipArchive;
#[Description('Package the runtime files of a built extension into a .pteroext archive.')]
#[Signature('p:extension:pack {path : Path to the unpacked extension.} {--output= : Archive filename.} {--force : Overwrite an existing archive.}')]
class PackCommand extends Command
{
2026-10-07 16:22:56 -04:00
public function handle(ExtensionManifestValidator $validator, ExtensionAssetPublisher $assets, ExtensionComposerInspector $composer): int
2026-10-05 18:10:18 -04:00
{
try {
$manifest = $validator->fromDirectory($this->argument('path'));
throw_if($reason = $assets->unusableBuildReason($manifest), InvalidExtensionException::class, $reason);
2026-10-07 16:22:56 -04:00
foreach ($composer->warnings($manifest) as $warning) {
$this->components->warn($warning);
}
2026-10-05 18:10:18 -04:00
$output = $this->option('output') ?: getcwd().DIRECTORY_SEPARATOR.$manifest->id.'-'.$manifest->version.'.pteroext';
throw_if(is_file($output) && ! $this->option('force'), InvalidExtensionException::class, 'Archive exists; pass --force to overwrite it.');
File::ensureDirectoryExists(dirname($output));
$temporary = dirname($output).DIRECTORY_SEPARATOR.'.extension-'.Str::uuid();
$zip = new ZipArchive;
$opened = false;
try {
throw_unless($zip->open($temporary, ZipArchive::CREATE | ZipArchive::EXCL) === true, InvalidExtensionException::class, 'Unable to open archive.');
$opened = true;
throw_unless($zip->addFile($manifest->path(ExtensionManifest::FILENAME), ExtensionManifest::FILENAME), InvalidExtensionException::class, 'Unable to add extension manifest to archive.');
2026-10-07 16:22:56 -04:00
// The panel reads the PSR-4 map from composer.json when the manifest has none.
if (is_file($manifest->path('composer.json'))) {
throw_unless($zip->addFile($manifest->path('composer.json'), 'composer.json'), InvalidExtensionException::class, 'Unable to add composer.json to archive.');
}
2026-10-05 18:10:18 -04:00
$directories = array_unique(['routes', 'database', 'resources', 'dist', 'vendor', ...array_values($manifest->autoload)]);
foreach ($directories as $directory) {
if (! is_dir($manifest->path($directory))) {
continue;
}
foreach (File::allFiles($manifest->path($directory)) as $file) {
if ($file->isLink() || array_any(explode('/', str_replace('\\', '/', $file->getRelativePathname())), fn (string $part): bool => str_starts_with($part, '.') || in_array($part, ['node_modules', 'tests', 'test', 'coverage'], true))) {
continue;
}
$relative = $directory.'/'.$file->getRelativePathname();
throw_unless($zip->addFile($file->getPathname(), $relative), InvalidExtensionException::class, "Unable to add {$relative} to archive.");
}
}
$icon = $manifest->iconFile();
if ($icon !== null && is_file($manifest->path($icon)) && $zip->locateName($icon) === false) {
throw_unless($zip->addFile($manifest->path($icon), $icon), InvalidExtensionException::class, "Unable to add {$icon} to archive.");
}
2026-10-05 18:10:18 -04:00
throw_unless($zip->close(), InvalidExtensionException::class, 'Unable to finish archive.');
$opened = false;
throw_unless(File::move($temporary, $output), InvalidExtensionException::class, 'Unable to save archive.');
} finally {
if ($opened) {
$zip->close();
}
File::delete($temporary);
}
} catch (InvalidExtensionException $invalidExtensionException) {
$this->components->error($invalidExtensionException->getMessage());
return self::FAILURE;
}
$this->components->info("Packaged {$manifest->id}: {$output}");
return self::SUCCESS;
}
}