From 0ca2a9b9bad8e91c6d54b7d52f4817a3667c51fb Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Fri, 9 Jan 2026 16:53:33 +0100 Subject: [PATCH] Add support for building specific version * Improve version resolution logic. * Allow providing a specific version when building package. --- src/Command/PackageBuildCommand.php | 6 +++++- src/Project/ProjectInfo.php | 23 +++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Command/PackageBuildCommand.php b/src/Command/PackageBuildCommand.php index 357b314..45213aa 100644 --- a/src/Command/PackageBuildCommand.php +++ b/src/Command/PackageBuildCommand.php @@ -11,6 +11,7 @@ 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; use Symfony\Component\Console\Input\InputOption; class PackageBuildCommand extends BaseCommand @@ -25,6 +26,7 @@ class PackageBuildCommand extends BaseCommand ->addOption("rm", null, InputOption::VALUE_NONE, "With --publish: remove the .zip after publishing") ->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)") ; } @@ -34,7 +36,9 @@ class PackageBuildCommand extends BaseCommand $builder = new PackageBuilder($output); - $project = ProjectInfo::read(); + $version = $input->getArgument("version"); + + $project = ProjectInfo::read(version:$version); if ($registry && file_exists($project->filename) && !$input->getOption("force")) { $output->writeln("Package file already exists. Pass --force to rebuild it."); diff --git a/src/Project/ProjectInfo.php b/src/Project/ProjectInfo.php index 09ae094..65d94dc 100644 --- a/src/Project/ProjectInfo.php +++ b/src/Project/ProjectInfo.php @@ -6,25 +6,36 @@ class ProjectInfo { private static ?ProjectInfo $project = null; - public static function read(): ProjectInfo + public static function read(?string $version = null): ProjectInfo { return self::$project ? self::$project - : (self::$project = self::readProject()); + : (self::$project = self::readProject(version: $version)); } - private static function readProject(?string $path = null): ProjectInfo + private static function readProject(?string $path = null, ?string $version = null): ProjectInfo { $path ??= getcwd(); + if (!file_exists($path."/composer.json")) { + throw new \Exception("No composer project found in {$path}"); + } + $json = file_get_contents($path."/composer.json"); $manifest = json_decode($json); - if (file_exists($path."/.git")) { - $version = trim(exec("git describe --tags 2>/dev/null")) ?: null; + if (!file_exists($path."/.git")) { + throw new \Exception("Project is not under git version control"); } + if (!$version) { - $version = $manifest->version ?? "1.0.0"; + $gitVersion = trim(exec("git describe --tags 2>/dev/null")) ?: null; + $version = $gitVersion ?? ($manifest->version ?? "1.0.0"); + } else { + exec("git tag 2>/dev/null", $tags, $ret); + if (!in_array($version, $tags)) { + throw new \Exception("Unable to find version {$version} in the list of git tags"); + } } $packageName = $manifest->name;