X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fpharext%2FPackager.php;h=4d432ed5bc7edbe81679667e6cd46f0c1ea49985;hb=e3ff870fe1f8a3b3eb8f55eea68f6f61d99e640c;hp=b6b05e1ac6b467f1b6f19acc57bc3fffc0ba448e;hpb=4e1839950dd39fd793346e724e4157bce8a169bf;p=pharext%2Fpharext diff --git a/src/pharext/Packager.php b/src/pharext/Packager.php index b6b05e1..4d432ed 100644 --- a/src/pharext/Packager.php +++ b/src/pharext/Packager.php @@ -3,6 +3,8 @@ namespace pharext; use Phar; +use pharext\Cli\Args as CliArgs; +use pharext\Cli\Command as CliCommand; /** * The extension packaging command executed by bin/pharext @@ -34,9 +36,9 @@ class Packager implements Command 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, @@ -45,6 +47,10 @@ class Packager implements Command 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], + [null, "signature", "Dump signature", + CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT], ]); } @@ -64,15 +70,22 @@ class Packager implements Command $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"]); + 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"]); + } } + } catch (\Exception $e) { + $errs[] = $e->getMessage(); } foreach ($this->args->validate() as $error) { @@ -86,6 +99,7 @@ class Packager implements Command foreach ($errs as $err) { $this->error("%s\n", $err); } + printf("\n"); if (!$this->args["quiet"]) { $this->help($prog); } @@ -94,68 +108,75 @@ class Packager implements Command $this->createPackage(); } - - /** - * @inheritdoc - * @see \pharext\Command::getArgs() - */ - public function getArgs() { - return $this->args; - } - - /** - * @inheritdoc - * @see \pharext\Command::info() - */ - public function info($fmt) { - if (!$this->args->quiet) { - vprintf($fmt, array_slice(func_get_args(), 1)); - } - } - - /** - * @inheritdoc - * @see \pharext\Command::error() - */ - public function error($fmt) { - if (!$this->args->quiet) { - vfprintf(STDERR, "ERROR: $fmt", array_slice(func_get_args(), 1)); + + function signature($prog) { + 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; + } catch (\Exception $e) { + $this->error("%s\n", $e->getMessage()); + return 2; } } - + /** * Traverses all pharext source files to bundle * @return Generator */ private function bundle() { - foreach (scandir(__DIR__) as $entry) { - if (fnmatch("*.php", $entry)) { - yield "pharext/$entry" => __DIR__."/$entry"; - } + $rdi = new \RecursiveDirectoryIterator(__DIR__); + $rii = new \RecursiveIteratorIterator($rdi); + for ($rii->rewind(); $rii->valid(); $rii->next()) { + yield "pharext/". $rii->getSubPathname() => $rii->key(); + } } + 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; + } + /** * Creates the extension phar */ private function createPackage() { $pkguniq = uniqid(); - $pkgtemp = sys_get_temp_dir() ."/{$pkguniq}.phar"; + $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, 0, "ext.phar"); + $package = new Phar($pkgtemp); + + 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); + } + $package->startBuffering(); $package->buildFromIterator($this->source, $this->source->getBaseDir()); - $package->buildFromIterator($this->bundle()); + $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(); - - chmod($pkgtemp, 0770); - if ($this->args->verbose) { + + if (!chmod($pkgtemp, 0777)) { + $this->error(null); + } elseif ($this->args->verbose) { $this->info("Created executable phar %s\n", $pkgtemp); } else { $this->info("OK\n"); @@ -163,7 +184,8 @@ class Packager implements Command if ($this->args->gzip) { $this->info("Compressing with gzip ... "); try { - $package->compress(Phar::GZ); + $package->compress(Phar::GZ) + ->setDefaultStub("pharext_installer.php"); $this->info("OK\n"); } catch (\Exception $e) { $this->error("%s\n", $e->getMessage()); @@ -172,13 +194,14 @@ class Packager implements Command if ($this->args->bzip) { $this->info("Compressing with bzip ... "); try { - $package->compress(Phar::BZ2); + $package->compress(Phar::BZ2) + ->setDefaultStub("pharext_installer.php"); $this->info("OK\n"); } catch (\Exception $e) { $this->error("%s\n", $e->getMessage()); } } - + unset($package); } catch (\Exception $e) { $this->error("%s\n", $e->getMessage()); @@ -190,10 +213,20 @@ class Packager implements Command $pkgname = $this->args->dest ."/". basename($pkgfile); $this->info("Finalizing %s ... ", $pkgname); if (!rename($pkgtemp, $pkgname)) { - $this->error("%s\n", error_get_last()["message"]); + $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()); + } + } } } }