Improve package command, build logic

* Fix typo in README.
* Implement cloning when building, fixing all builds being dirty.
* Add --rm option to package command to remove file after publishing,
  assuming that --publish is provided.
This commit is contained in:
Christopher Vagnetoft
2026-01-09 16:19:53 +01:00
parent d007ec23da
commit b6f283a471
4 changed files with 45 additions and 9 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
/vendor/ /vendor/
/*.zip

View File

@@ -18,10 +18,12 @@ $ composer package
# Create package from working directory # Create package from working directory
$ composer package --dirty $ composer package --dirty
# Create and publish without saving zipball # Create and publish without saving zipball
$ composer package --publish gitea:myserver.tld/myowner $ composer package --rm --publish gitea:myserver.tld
# Create and publish to another owner
$ composer package --publish gitea:myserver.tld:myowner
# Publish latest zipball # Publish latest zipball
$ composer package:publish gitea:myserver.tld/myowner $ composer package:publish gitea:myserver.tld:myowner
``` ```
## Notes ## Notes

View File

@@ -22,6 +22,7 @@ class PackageBuildCommand extends BaseCommand
->setAliases([ "package" ]) ->setAliases([ "package" ])
->setDescription("Package the library into a zipball, or publish directly") ->setDescription("Package the library into a zipball, or publish directly")
->addOption("publish", null, InputOption::VALUE_REQUIRED, "Publish to registry immediately after building") ->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")
->addOption("dirty", null, InputOption::VALUE_NONE, "Build directly from source without cloning") ->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") ->addOption("force", null, InputOption::VALUE_NONE, "Build even if the output file already exists")
; ;
@@ -35,10 +36,10 @@ class PackageBuildCommand extends BaseCommand
$project = ProjectInfo::read(); $project = ProjectInfo::read();
if ($registry && file_exists($project->filename)) { if ($registry && file_exists($project->filename) && !$input->getOption("force")) {
$output->writeln("<fg=black;bg=yellow>Package file already exists. Pass --force to rebuild it.</>"); $output->writeln("<fg=black;bg=yellow>Package file already exists. Pass --force to rebuild it.</>");
} else { } else {
$builder->build($project, $input->getOption("force")); $builder->build($project, $input->getOption("dirty"), $input->getOption("force"));
} }
if ($registry) { if ($registry) {
@@ -55,8 +56,8 @@ class PackageBuildCommand extends BaseCommand
$output->writeln([ $output->writeln([
"Missing registry to publish to. Please specify the registry like this:", "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}</> - to publish to <info>server</> as the default user",
" <info>gitea:<server>/<owner></> - to publish to <info>server</> as <info>owner</>", " <info>gitea:{server}:{owner}</> - to publish to <info>server</> as <info>owner</>",
"" ""
]); ]);
return self::INVALID; return self::INVALID;
@@ -64,6 +65,11 @@ class PackageBuildCommand extends BaseCommand
$publisher->publish($project, $registry); $publisher->publish($project, $registry);
if ($input->getOption("rm")) {
$output->writeln("Unlinking <comment>".$project->filename."</>");
unlink($project->filename);
}
} }
return 0; return 0;

View File

@@ -16,7 +16,7 @@ class PackageBuilder
{ {
} }
public function build(ProjectInfo $project, bool $overwrite = false): void public function build(ProjectInfo $project, bool $dirty, bool $overwrite = false): void
{ {
$packageName = $project->name; $packageName = $project->name;
$packageVersion = $project->version; $packageVersion = $project->version;
@@ -27,14 +27,32 @@ class PackageBuilder
} }
$packageVersion = trim(reset($tags)); $packageVersion = trim(reset($tags));
} }
$currentDir = getcwd();
$this->output->writeln("Creating <info>{$project->filename}</> from <options=bold>{$packageName}</>@<options=bold>{$packageVersion}</>..."); $this->output->writeln("Creating <info>{$project->filename}</> from <options=bold>{$packageName}</>@<options=bold>{$packageVersion}</>...");
$filename = getcwd() . "/" . $project->filename; $filename = $currentDir . "/" . $project->filename;
if (file_exists($filename) && !$overwrite) { if (file_exists($filename) && !$overwrite) {
throw new \Exception("The package file already exists, pass --force to overwrite it."); throw new \Exception("The package file already exists, pass --force to overwrite it.");
} }
// TODO clone repo before build unless dirty if (!$dirty) {
$tmpDir = sys_get_temp_dir()."/".uniqid("package");
$this->output->writeln("- Cloning to <comment>{$tmpDir}</>...");
exec("git clone ".escapeshellarg($currentDir)." ".escapeshellarg($tmpDir)." 2>&1", $out, $ret);
if ($ret !== 0) {
$this->output->writeln("<error>".join("\n", $out)."</>");
throw new \Exception("Unable to clone local repository into temporary directory");
}
chdir($tmpDir);
$this->output->writeln("- Checking out <info>{$packageVersion}</>...");
exec("git checkout ".escapeshellarg($packageVersion)." 2>&1", $out, $ret);
if ($ret !== 0) {
$this->output->writeln("<error>".join("\n", $out)."</>");
throw new \Exception("Unable to checkout tag {$packageVersion} in temporary directory");
}
}
$files = $this->listFiles(); $files = $this->listFiles();
$total = array_sum(array_map(filesize(...), $files)); $total = array_sum(array_map(filesize(...), $files));
@@ -54,6 +72,15 @@ class PackageBuilder
} else { } else {
$this->packWithCommand($filename, $files); $this->packWithCommand($filename, $files);
} }
if (!$dirty && $tmpDir) {
chdir($currentDir);
exec("rm -rf ".escapeshellarg($tmpDir), $out, $ret);
if ($ret !== 0) {
$this->output->writeln("<error>".join("\n", $out)."</>");
throw new \Exception("Unable to cleanup temporary directory");
}
}
} }
private function listFiles(): array private function listFiles(): array