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

View File

@@ -22,6 +22,7 @@ class PackageBuildCommand extends BaseCommand
->setAliases([ "package" ])
->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")
->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")
;
@@ -35,10 +36,10 @@ class PackageBuildCommand extends BaseCommand
$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.</>");
} else {
$builder->build($project, $input->getOption("force"));
$builder->build($project, $input->getOption("dirty"), $input->getOption("force"));
}
if ($registry) {
@@ -55,8 +56,8 @@ class PackageBuildCommand extends BaseCommand
$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</>",
" <info>gitea:{server}</> - to publish to <info>server</> as the default user",
" <info>gitea:{server}:{owner}</> - to publish to <info>server</> as <info>owner</>",
""
]);
return self::INVALID;
@@ -64,6 +65,11 @@ class PackageBuildCommand extends BaseCommand
$publisher->publish($project, $registry);
if ($input->getOption("rm")) {
$output->writeln("Unlinking <comment>".$project->filename."</>");
unlink($project->filename);
}
}
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;
$packageVersion = $project->version;
@@ -27,14 +27,32 @@ class PackageBuilder
}
$packageVersion = trim(reset($tags));
}
$currentDir = getcwd();
$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) {
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();
$total = array_sum(array_map(filesize(...), $files));
@@ -54,6 +72,15 @@ class PackageBuilder
} else {
$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