diff --git a/.gitignore b/.gitignore index 57872d0..0321250 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /vendor/ +/*.zip diff --git a/README.md b/README.md index af23550..726a633 100644 --- a/README.md +++ b/README.md @@ -18,10 +18,12 @@ $ composer package # Create package from working directory $ composer package --dirty # 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 -$ composer package:publish gitea:myserver.tld/myowner +$ composer package:publish gitea:myserver.tld:myowner ``` ## Notes diff --git a/src/Command/PackageBuildCommand.php b/src/Command/PackageBuildCommand.php index 3242186..357b314 100644 --- a/src/Command/PackageBuildCommand.php +++ b/src/Command/PackageBuildCommand.php @@ -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("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:", "", - " gitea: - to publish to server as the default user", - " gitea:/ - to publish to server as owner", + " gitea:{server} - to publish to server as the default user", + " gitea:{server}:{owner} - to publish to server as owner", "" ]); return self::INVALID; @@ -64,6 +65,11 @@ class PackageBuildCommand extends BaseCommand $publisher->publish($project, $registry); + if ($input->getOption("rm")) { + $output->writeln("Unlinking ".$project->filename.""); + unlink($project->filename); + } + } return 0; diff --git a/src/Package/PackageBuilder.php b/src/Package/PackageBuilder.php index e56b8f9..687f133 100644 --- a/src/Package/PackageBuilder.php +++ b/src/Package/PackageBuilder.php @@ -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 {$project->filename} from {$packageName}@{$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 {$tmpDir}..."); + exec("git clone ".escapeshellarg($currentDir)." ".escapeshellarg($tmpDir)." 2>&1", $out, $ret); + if ($ret !== 0) { + $this->output->writeln("".join("\n", $out).""); + throw new \Exception("Unable to clone local repository into temporary directory"); + } + chdir($tmpDir); + $this->output->writeln("- Checking out {$packageVersion}..."); + exec("git checkout ".escapeshellarg($packageVersion)." 2>&1", $out, $ret); + if ($ret !== 0) { + $this->output->writeln("".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("".join("\n", $out).""); + throw new \Exception("Unable to cleanup temporary directory"); + } + } } private function listFiles(): array