X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fpharext%2FPackager.php;h=b3be6d09524ccdbb86e60311685689f0adf0a8ee;hb=d829063e596c6fffc4c30fb30f6d462ac9ba2928;hp=a848e2d39ce672fc5a0ff67dfb652376d526c2e8;hpb=fda687da4dad697df7d06753ade76014a5899599;p=pharext%2Fpharext diff --git a/src/pharext/Packager.php b/src/pharext/Packager.php index a848e2d..b3be6d0 100644 --- a/src/pharext/Packager.php +++ b/src/pharext/Packager.php @@ -3,17 +3,15 @@ namespace pharext; use Phar; +use pharext\Cli\Args as CliArgs; +use pharext\Cli\Command as CliCommand; /** * The extension packaging command executed by bin/pharext */ class Packager implements Command { - /** - * Command line arguments - * @var pharext\CliArgs - */ - private $args; + use CliCommand; /** * Extension source directory @@ -21,6 +19,12 @@ class Packager implements Command */ private $source; + /** + * Cleanups + * @var array + */ + private $cleanup = []; + /** * Create the command */ @@ -32,24 +36,47 @@ class Packager implements Command CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], ["q", "quiet", "Less output", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], + ["n", "name", "Extension name", + CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG], + ["r", "release", "Extension release version", + CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG], ["s", "source", "Extension source directory", CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG], - ["g", "git", "Use `git ls-files` instead of the standard ignore filter", + ["g", "git", "Use `git ls-tree` to determine file list", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], - ["p", "pecl", "Use PECL package.xml instead of the standard ignore filter", + ["p", "pecl", "Use PECL package.xml to determine file list, name and release", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], ["d", "dest", "Destination directory", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG, "."], - ["n", "name", "Extension name", - CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG], - ["r", "release", "Extension release version", - CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG], ["z", "gzip", "Create additional PHAR compressed with gzip", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], ["Z", "bzip", "Create additional PHAR compressed with bzip", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], -]); + ["S", "sign", "Sign the PHAR with a private key", + CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG], + ["E", "zend", "Mark as Zend Extension", + CliArgs::OPTIONAL|CliARgs::SINGLE|CliArgs::NOARG], + [null, "signature", "Show pharext signature", + CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT], + [null, "license", "Show pharext license", + CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT], + [null, "version", "Show pharext version", + CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT], + ]); + } + + /** + * Perform cleaniup + */ + function __destruct() { + foreach ($this->cleanup as $cleanup) { + if (is_dir($cleanup)) { + $this->rm($cleanup); + } elseif (file_exists($cleanup)) { + unlink($cleanup); + } + } } /** @@ -57,139 +84,214 @@ class Packager implements Command * @see \pharext\Command::run() */ public function run($argc, array $argv) { + $errs = []; $prog = array_shift($argv); foreach ($this->args->parse(--$argc, $argv) as $error) { - $this->error("%s\n", $error); + $errs[] = $error; } if ($this->args["help"]) { - $this->args->help($prog); + $this->header(); + $this->help($prog); exit; } - - if ($this->args["source"]) { - if ($this->args["pecl"]) { - $this->source = new PeclSourceDir($this, $this->args["source"]); - } elseif ($this->args["git"]) { - $this->source = new GitSourceDir($this, $this->args["source"]); - } else { - $this->source = new FilteredSourceDir($this, $this->args["source"]); + try { + foreach (["signature", "license", "version"] as $opt) { + if ($this->args[$opt]) { + printf("%s\n", $this->metadata($opt)); + exit; + } } + } catch (\Exception $e) { + $this->error("%s\n", $e->getMessage()); + exit(self::EARGS); + } + + try { + /* source needs to be evaluated before CliArgs validation, + * so e.g. name and version can be overriden and CliArgs + * does not complain about missing arguments + */ + if ($this->args["source"]) { + $source = $this->localize($this->args["source"]); + if ($this->args["pecl"]) { + $this->source = new SourceDir\Pecl($this, $source); + } elseif ($this->args["git"]) { + $this->source = new SourceDir\Git($this, $source); + } else { + $this->source = new SourceDir\Pharext($this, $source); + } + } + } catch (\Exception $e) { + $errs[] = $e->getMessage(); } foreach ($this->args->validate() as $error) { - $this->error("%s\n", $error); + $errs[] = $error; } - if (isset($error)) { + if ($errs) { if (!$this->args["quiet"]) { - $this->args->help($prog); + $this->header(); } - exit(1); + foreach ($errs as $err) { + $this->error("%s\n", $err); + } + printf("\n"); + if (!$this->args["quiet"]) { + $this->help($prog); + } + exit(self::EARGS); } $this->createPackage(); } /** - * @inheritdoc - * @see \pharext\Command::getArgs() - */ - public function getArgs() { - return $this->args; - } - - /** - * @inheritdoc - * @see \pharext\Command::info() + * Download remote source + * @param string $source + * @return string local source */ - public function info($fmt) { - if (!$this->args->quiet) { - vprintf($fmt, array_slice(func_get_args(), 1)); + private function download($source) { + $this->info("Fetching remote source %s ...\n", $source); + + if ($this->args->git) { + $task = new Task\GitClone($source); + } else { + $task = new Task\StreamFetch($source, function($bytes_pct) { + $this->debug(" %3d%% [%s>%s] \r", + floor($bytes_pct*100), + str_repeat("=", round(50*$bytes_pct)), + str_repeat(" ", round(50*(1-$bytes_pct))) + ); + }); } + $local = $task->run($this->args->verbose); + $this->debug("\n"); + + $this->cleanup[] = $local; + return $local; } - + /** - * @inheritdoc - * @see \pharext\Command::error() + * Extract local archive + * @param stirng $source + * @return string extracted directory */ - public function error($fmt) { - if (!$this->args->quiet) { - vfprintf(STDERR, "ERROR: $fmt", array_slice(func_get_args(), 1)); - } + private function extract($source) { + $this->debug("Extracting %s ...\n", $source); + + $task = new Task\Extract($source); + $dest = $task->run($this->args->verbose); + + $this->cleanup[] = $dest; + return $dest; } - + /** - * Traverses all pharext source files to bundle - * @return Generator + * Localize a possibly remote source + * @param string $source + * @return string local source directory */ - private function bundle() { - foreach (scandir(__DIR__) as $entry) { - if (fnmatch("*.php", $entry)) { - yield "pharext/$entry" => __DIR__."/$entry"; + private function localize($source) { + if (!stream_is_local($source)) { + $source = $this->download($source); + $this->cleanup[] = $source; + } + $source = realpath($source); + if (!is_dir($source)) { + $source = $this->extract($source); + $this->cleanup[] = $source; + + if ($this->args->pecl) { + $this->debug("Sanitizing PECL dir ...\n"); + $source = (new Task\PeclFixup($source))->run($this->args->verbose); } } + return $source; } - + /** * Creates the extension phar */ private function createPackage() { - $pkguniq = uniqid(); - $pkgtemp = sys_get_temp_dir() ."/{$pkguniq}.phar"; - $pkgdesc = "{$this->args->name}-{$this->args->release}"; - - $this->info("Creating phar %s ...%s", $pkgtemp, $this->args->verbose ? "\n" : " "); try { - $package = new Phar($pkgtemp, 0, "ext.phar"); - $package->startBuffering(); - $package->buildFromIterator($this->source, $this->source->getBaseDir()); - $package->buildFromIterator($this->bundle()); - $package->addFile(__DIR__."/../pharext_installer.php", "pharext_installer.php"); - $package->setDefaultStub("pharext_installer.php"); - $package->setStub("#!/usr/bin/php -dphar.readonly=1\n".$package->getStub()); - $package->stopBuffering(); - - chmod($pkgtemp, 0770); - if ($this->args->verbose) { - $this->info("Created executable phar %s\n", $pkgtemp); - } else { - $this->info("OK\n"); + $meta = array_merge($this->metadata(), [ + "date" => date("Y-m-d"), + "name" => $this->args->name, + "release" => $this->args->release, + "license" => @file_get_contents(current(glob($this->source->getBaseDir()."/LICENSE*"))), + "stub" => "pharext_installer.php", + "type" => $this->args->zend ? "zend_extension" : "extension", + ]); + $file = (new Task\PharBuild($this->source, $meta))->run(); + } catch (\Exception $e) { + $this->error("%s\n", $e->getMessage()); + exit(self::EBUILD); + } + + try { + if ($this->args->sign) { + $this->info("Using private key to sign phar ...\n"); + $pass = (new Task\Askpass)->run($this->args->verbose); + $sign = new Task\PharSign($file, $this->args->sign, $pass); + $pkey = $sign->run($this->args->verbose); } - if ($this->args->gzip) { - $this->info("Compressing with gzip ... "); - try { - $package->compress(Phar::GZ); - $this->info("OK\n"); - } catch (\Exception $e) { - $this->error("%s\n", $e->getMessage()); + + } catch (\Exception $e) { + $this->error("%s\n", $e->getMessage()); + exit(self::ESIGN); + } + + if ($this->args->gzip) { + try { + $gzip = (new Task\PharCompress($file, Phar::GZ))->run(); + $move = new Task\PharRename($gzip, $this->args->dest, $this->args->name ."-". $this->args->release); + $name = $move->run($this->args->verbose); + + $this->info("Created gzipped phar %s\n", $name); + + if ($this->args->sign) { + $sign = new Task\PharSign($name, $this->args->sign, $pass); + $sign->run($this->args->verbose)->exportPublicKey($name.".pubkey"); } + + } catch (\Exception $e) { + $this->warn("%s\n", $e->getMessage()); } - if ($this->args->bzip) { - $this->info("Compressing with bzip ... "); - try { - $package->compress(Phar::BZ2); - $this->info("OK\n"); - } catch (\Exception $e) { - $this->error("%s\n", $e->getMessage()); + } + + if ($this->args->bzip) { + try { + $bzip = (new Task\PharCompress($file, Phar::BZ2))->run(); + $move = new Task\PharRename($bzip, $this->args->dest, $this->args->name ."-". $this->args->release); + $name = $move->run($this->args->verbose); + + $this->info("Created bzipped phar %s\n", $name); + + if ($this->args->sign) { + $sign = new Task\PharSign($name, $this->args->sign, $pass); + $sign->run($this->args->verbose)->exportPublicKey($name.".pubkey"); } + + } catch (\Exception $e) { + $this->warn("%s\n", $e->getMessage()); } - - unset($package); - } catch (\Exception $e) { - $this->error("%s\n", $e->getMessage()); - exit(4); } - foreach (glob($pkgtemp."*") as $pkgtemp) { - $pkgfile = str_replace($pkguniq, "{$pkgdesc}.ext", $pkgtemp); - $pkgname = $this->args->dest ."/". basename($pkgfile); - $this->info("Finalizing %s ... ", $pkgname); - if (!rename($pkgtemp, $pkgname)) { - $this->error("%s\n", error_get_last()["message"]); - exit(5); + try { + $move = new Task\PharRename($file, $this->args->dest, $this->args->name ."-". $this->args->release); + $name = $move->run($this->args->verbose); + + $this->info("Created executable phar %s\n", $name); + + if (isset($pkey)) { + $pkey->exportPublicKey($name.".pubkey"); } - $this->info("OK\n"); - } + + } catch (\Exception $e) { + $this->error("%s\n", $e->getMessage()); + exit(self::EBUILD); + } } }