Files
composer-package-plugin/src/Command/PackageBuildCommand.php

83 lines
3.2 KiB
PHP
Raw Normal View History

2025-12-28 02:28:35 +01:00
<?php
namespace NoccyLabs\Composer\PackagePlugin\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Composer\Command\BaseCommand;
2025-12-28 15:26:33 +01:00
use NoccyLabs\Composer\PackagePlugin\Package\PackageBuilder;
use NoccyLabs\Composer\PackagePlugin\Package\PackagePublisher;
use NoccyLabs\Composer\PackagePlugin\Project\ProjectInfo;
use NoccyLabs\Composer\PackagePlugin\Registry\Credentials\InsecureStore;
use NoccyLabs\Composer\PackagePlugin\Registry\Gitea\GiteaProvider;
use NoccyLabs\Composer\PackagePlugin\Registry\RegistryFactory;
use Symfony\Component\Console\Input\InputArgument;
2025-12-28 15:26:33 +01:00
use Symfony\Component\Console\Input\InputOption;
2025-12-28 02:28:35 +01:00
2026-01-09 17:43:54 +01:00
class PackageBuildCommand extends PackageCommand
2025-12-28 02:28:35 +01:00
{
protected function configure(): void
{
$this
2025-12-28 02:30:24 +01:00
->setName('package:build')
->setAliases([ "package" ])
2025-12-28 15:26:33 +01:00
->setDescription("Package the library into a zipball, or publish directly")
->addOption("publish", null, InputOption::VALUE_REQUIRED, "Publish to registry immediately after building")
->addOption("rm", null, InputOption::VALUE_NONE, "With --publish: remove the .zip after publishing")
2025-12-28 15:26:33 +01:00
->addOption("dirty", null, InputOption::VALUE_NONE, "Build directly from source without cloning")
->addOption("force", null, InputOption::VALUE_NONE, "Build even if the output file already exists")
->addArgument("version", InputArgument::OPTIONAL, "The tag to build (default is latest tag)")
2025-12-28 15:26:33 +01:00
;
2025-12-28 02:28:35 +01:00
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
2025-12-28 15:26:33 +01:00
$registry = $input->getOption("publish");
$version = $input->getArgument("version");
$force = $input->getOption("force");
$builder = new PackageBuilder($output);
$project = ProjectInfo::read(version:$version);
2025-12-28 15:26:33 +01:00
if (file_exists($project->filename) && !$force) {
2025-12-28 15:26:33 +01:00
$output->writeln("<fg=black;bg=yellow>Package file already exists. Pass --force to rebuild it.</>");
} else {
$builder->build($project, $input->getOption("dirty"), $force);
2025-12-28 15:26:33 +01:00
}
if ($registry) {
2026-01-09 17:43:54 +01:00
$credentials = $this->getCredentialsStore();
2025-12-28 15:26:33 +01:00
$providers = [
'gitea' => new GiteaProvider($credentials)
];
$factory = new RegistryFactory($providers);
$publisher = new PackagePublisher($factory, $output);
if (!$registry) {
$output->writeln([
"Missing registry to publish to. Please specify the registry like this:",
"",
" <info>gitea:{server}</> - to publish to <info>server</> as the default user",
" <info>gitea:{server}:{owner}</> - to publish to <info>server</> as <info>owner</>",
2025-12-28 15:26:33 +01:00
""
]);
return self::INVALID;
}
$publisher->publish($project, $registry);
if ($input->getOption("rm")) {
$output->writeln("Unlinking <comment>".$project->filename."</>");
unlink($project->filename);
}
2025-12-28 15:26:33 +01:00
}
2025-12-28 02:28:35 +01:00
return 0;
}
}