X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fpharext%2FPackager.php;h=23bcdc9999c52906cf6edcf0254789503c84500e;hb=2b97fd09b475f30027599e45e793af9610bf2a31;hp=4d432ed5bc7edbe81679667e6cd46f0c1ea49985;hpb=e3ff870fe1f8a3b3eb8f55eea68f6f61d99e640c;p=pharext%2Fpharext diff --git a/src/pharext/Packager.php b/src/pharext/Packager.php index 4d432ed..23bcdc9 100644 --- a/src/pharext/Packager.php +++ b/src/pharext/Packager.php @@ -3,15 +3,13 @@ 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 { - use CliCommand; + use Cli\Command; /** * Extension source directory @@ -19,41 +17,64 @@ class Packager implements Command */ private $source; + /** + * Cleanups + * @var array + */ + private $cleanup = []; + /** * Create the command */ public function __construct() { - $this->args = new CliArgs([ + $this->args = new Cli\Args([ ["h", "help", "Display this help", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT], + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG|Cli\Args::HALT], ["v", "verbose", "More output", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG], ["q", "quiet", "Less output", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG], ["n", "name", "Extension name", - CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG], + Cli\Args::REQUIRED|Cli\Args::SINGLE|Cli\Args::REQARG], ["r", "release", "Extension release version", - CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG], + Cli\Args::REQUIRED|Cli\Args::SINGLE|Cli\Args::REQARG], ["s", "source", "Extension source directory", - CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG], + Cli\Args::REQUIRED|Cli\Args::SINGLE|Cli\Args::REQARG], ["g", "git", "Use `git ls-tree` to determine file list", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG], + ["b", "branch", "Checkout this tag/branch", + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::REQARG], ["p", "pecl", "Use PECL package.xml to determine file list, name and release", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG], ["d", "dest", "Destination directory", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG, + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::REQARG, "."], ["z", "gzip", "Create additional PHAR compressed with gzip", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG], ["Z", "bzip", "Create additional PHAR compressed with bzip", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG], ["S", "sign", "Sign the PHAR with a private key", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG], - [null, "signature", "Dump signature", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT], + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::REQARG], + ["E", "zend", "Mark as Zend Extension", + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG], + [null, "signature", "Show pharext signature", + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG|Cli\Args::HALT], + [null, "license", "Show pharext license", + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG|Cli\Args::HALT], + [null, "version", "Show pharext version", + Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG|Cli\Args::HALT], ]); } + /** + * Perform cleaniup + */ + function __destruct() { + foreach ($this->cleanup as $cleanup) { + $cleanup->run(); + } + } + /** * @inheritdoc * @see \pharext\Command::run() @@ -70,20 +91,24 @@ class Packager implements Command $this->help($prog); exit; } - if ($this->args["signature"]) { - exit($this->signature($prog)); - } - try { - if ($this->args["source"]) { - if ($this->args["pecl"]) { - $this->source = new SourceDir\Pecl($this, $this->args["source"]); - } elseif ($this->args["git"]) { - $this->source = new SourceDir\Git($this, $this->args["source"]); - } else { - $this->source = new SourceDir\Pharext($this, $this->args["source"]); + 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 Cli\Args validation, + * so Cli\Args does not complain about missing arguments, + * which come from SourceDir::getPackageInfo() + */ + $this->loadSource(); } catch (\Exception $e) { $errs[] = $e->getMessage(); } @@ -103,130 +128,201 @@ class Packager implements Command if (!$this->args["quiet"]) { $this->help($prog); } - exit(1); + exit(self::EARGS); } $this->createPackage(); } + + /** + * Download remote source + * @param string $source + * @return string local source + */ + private function download($source) { + if ($this->args->git) { + $task = new Task\GitClone($source, $this->args->branch); + } else { + /* print newline only once */ + $done = false; + $task = new Task\StreamFetch($source, function($bytes_pct) use(&$done) { + if (!$done) { + $this->info(" %3d%% [%s>%s] \r", + floor($bytes_pct*100), + str_repeat("=", round(50*$bytes_pct)), + str_repeat(" ", round(50*(1-$bytes_pct))) + ); + if ($bytes_pct == 1) { + $done = true; + $this->info("\n"); + } + } + }); + } + $local = $task->run($this->verbosity()); + + $this->cleanup[] = new Task\Cleanup($local); + return $local; + } - function signature($prog) { + /** + * Extract local archive + * @param stirng $source + * @return string extracted directory + */ + private function extract($source) { try { - $sig = (new Phar(Phar::running(false)))->getSignature(); - printf("%s signature of %s\n%s", $sig["hash_type"], $prog, - chunk_split($sig["hash"], 64, "\n")); - return 0; + $task = new Task\Extract($source); + $dest = $task->run($this->verbosity()); } catch (\Exception $e) { - $this->error("%s\n", $e->getMessage()); - return 2; + if (false === strpos($e->getMessage(), "checksum mismatch")) { + throw $e; + } + $dest = (new Task\PaxFixup($source))->run($this->verbosity()); } + + $this->cleanup[] = new Task\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() { - $rdi = new \RecursiveDirectoryIterator(__DIR__); - $rii = new \RecursiveIteratorIterator($rdi); - for ($rii->rewind(); $rii->valid(); $rii->next()) { - yield "pharext/". $rii->getSubPathname() => $rii->key(); + private function localize($source) { + if (!stream_is_local($source) || ($this->args->git && isset($this->args->branch))) { + $source = $this->download($source); + $this->cleanup[] = new Task\Cleanup($source); + } + if (!$real = realpath($source)) { + $error = "Cannot find source '$source'"; + if ($this->args->git) { + $error .= "; did you forget to specify --branch for a remote git source?"; + } + throw new Exception($error); + } + if (!is_dir($real)) { + $source = $this->extract($real); + $this->cleanup[] = new Task\Cleanup($source); + if (!$this->args->git) { + $source = (new Task\PeclFixup($source))->run($this->verbosity()); + } } + return $source; } - - private function askpass($prompt = "Password:") { - system("stty -echo", $retval); - if ($retval) { - $this->error("Could not disable echo on the terminal\n"); - } - printf("%s ", $prompt); - $pass = fgets(STDIN, 1024); - system("stty echo"); - if (substr($pass, -1) == "\n") { - $pass = substr($pass, 0, -1); - } - return $pass; + + /** + * Load the source dir + * @throws \pharext\Exception + */ + private function loadSource(){ + if ($this->args["source"]) { + $source = $this->localize($this->args["source"]); + + if ($this->args["pecl"]) { + $this->source = new SourceDir\Pecl($source); + } elseif ($this->args["git"]) { + $this->source = new SourceDir\Git($source); + } elseif (is_file("$source/pharext_package.php")) { + $this->source = include "$source/pharext_package.php"; + } else { + $this->source = new SourceDir\Basic($source); + } + + if (!$this->source instanceof SourceDir) { + throw new Exception("Unknown source dir $source"); + } + + foreach ($this->source->getPackageInfo() as $key => $val) { + /* do not override command line arguments */ + if (!isset($this->args->$key)) { + $this->args->$key = $val; + } + } + } } /** * Creates the extension phar */ private function createPackage() { - $pkguniq = uniqid(); - $pkgtemp = $this->tempname($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); + $meta = array_merge(Metadata::all(), [ + "name" => $this->args->name, + "release" => $this->args->release, + "license" => $this->source->getLicense(), + "type" => $this->args->zend ? "zend_extension" : "extension", + ]); + $file = (new Task\PharBuild($this->source, __DIR__."/../pharext_installer.php", $meta))->run($this->verbosity()); + } catch (\Exception $e) { + $this->error("%s\n", $e->getMessage()); + exit(self::EBUILD); + } + try { if ($this->args->sign) { - $this->info("\nUsing private key to sign phar ... \n"); - $privkey = new Openssl\PrivateKey(realpath($this->args->sign), $this->askpass()); - $privkey->sign($package); + $this->info("Using private key to sign phar ...\n"); + $pass = (new Task\Askpass)->run($this->verbosity()); + $sign = new Task\PharSign($file, $this->args->sign, $pass); + $pkey = $sign->run($this->verbosity()); } - $package->startBuffering(); - $package->buildFromIterator($this->source, $this->source->getBaseDir()); - $package->buildFromIterator($this->bundle(__DIR__)); - $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(); - - if (!chmod($pkgtemp, 0777)) { - $this->error(null); - } elseif ($this->args->verbose) { - $this->info("Created executable phar %s\n", $pkgtemp); - } else { - $this->info("OK\n"); - } - if ($this->args->gzip) { - $this->info("Compressing with gzip ... "); - try { - $package->compress(Phar::GZ) - ->setDefaultStub("pharext_installer.php"); - $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->verbosity()); + + $this->info("Created gzipped phar %s\n", $name); + + if ($this->args->sign) { + $sign = new Task\PharSign($name, $this->args->sign, $pass); + $sign->run($this->verbosity())->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) - ->setDefaultStub("pharext_installer.php"); - $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->verbosity()); + + $this->info("Created bzipped phar %s\n", $name); + + if ($this->args->sign) { + $sign = new Task\PharSign($name, $this->args->sign, $pass); + $sign->run($this->verbosity())->exportPublicKey($name.".pubkey"); } + + } catch (\Exception $e) { + $this->warn("%s\n", $e->getMessage()); + } + } + + try { + $move = new Task\PharRename($file, $this->args->dest, $this->args->name ."-". $this->args->release); + $name = $move->run($this->verbosity()); + + $this->info("Created executable phar %s\n", $name); + + if (isset($pkey)) { + $pkey->exportPublicKey($name.".pubkey"); } - unset($package); } catch (\Exception $e) { $this->error("%s\n", $e->getMessage()); - exit(4); + exit(self::EBUILD); } - - 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(null); - exit(5); - } - $this->info("OK\n"); - if ($this->args->sign && isset($privkey)) { - $keyname = $this->args->dest ."/". basename($pkgfile) . ".pubkey"; - $this->info("Public Key %s ... ", $keyname); - try { - $privkey->exportPublicKey($keyname); - $this->info("OK\n"); - } catch (\Exception $e) { - $this->error("%s", $e->getMessage()); - } - } - } } }