X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fpharext%2FPackager.php;h=d66e83b51783c933082853d9bd8528a3dcc952ad;hb=c8adc57c38cc535e0dcda979565b7639896e7370;hp=28b705069ed8d9f01c9534362bdb381b2ab73619;hpb=50e5714c543ed50557a1b644c6df27b8d298b6e1;p=pharext%2Fpharext diff --git a/src/pharext/Packager.php b/src/pharext/Packager.php index 28b7050..d66e83b 100644 --- a/src/pharext/Packager.php +++ b/src/pharext/Packager.php @@ -3,6 +3,9 @@ namespace pharext; use Phar; +use PharData; +use pharext\Cli\Args as CliArgs; +use pharext\Cli\Command as CliCommand; /** * The extension packaging command executed by bin/pharext @@ -17,6 +20,12 @@ class Packager implements Command */ private $source; + /** + * Cleanups + * @var array + */ + private $cleanup = []; + /** * Create the command */ @@ -24,8 +33,6 @@ class Packager implements Command $this->args = new CliArgs([ ["h", "help", "Display this help", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT], - [null, "signature", "Dump signature", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT], ["v", "verbose", "More output", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], ["q", "quiet", "Less output", @@ -36,9 +43,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, @@ -47,11 +54,26 @@ 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 *.ext.phar with a private key", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG] + ["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], ]); } + /** + * Perform cleaniup + */ + function __destruct() { + foreach ($this->cleanup as $cleanup) { + if (is_dir($cleanup)) { + $this->rm($cleanup); + } else { + unlink($cleanup); + } + } + } + /** * @inheritdoc * @see \pharext\Command::run() @@ -73,15 +95,18 @@ class Packager implements Command } 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 PeclSourceDir($this, $this->args["source"]); + $this->source = new SourceDir\Pecl($this, $source); } elseif ($this->args["git"]) { - $this->source = new GitSourceDir($this, $this->args["source"]); - } elseif (realpath($this->args["source"]."/pharext_package.php")) { - $this->source = new PharextSourceDir($this, $this->args["source"]); + $this->source = new SourceDir\Git($this, $source); } else { - $this->source = new FilteredSourceDir($this, $this->args["source"]); + $this->source = new SourceDir\Pharext($this, $source); } } } catch (\Exception $e) { @@ -94,7 +119,10 @@ class Packager implements Command if ($errs) { if (!$this->args["quiet"]) { - $this->header(); + if (!headers_sent()) { + /* only display header, if we didn't generate any output yet */ + $this->header(); + } } foreach ($errs as $err) { $this->error("%s\n", $err); @@ -109,6 +137,11 @@ class Packager implements Command $this->createPackage(); } + /** + * Dump program signature + * @param string $prog + * @return int exit code + */ function signature($prog) { try { $sig = (new Phar(Phar::running(false)))->getSignature(); @@ -121,18 +154,99 @@ class Packager implements Command } } + /** + * Download remote source + * @param string $source + * @return string local source + */ + private function download($source) { + if ($this->args["git"]) { + $this->info("Cloning %s ... ", $source); + $local = new Tempdir("gitclone"); + $cmd = new ExecCmd("git", $this->args->verbose); + $cmd->run(["clone", $source, $local]); + if (!$this->args->verbose) { + $this->info("OK\n"); + } + } else { + $this->info("Fetching remote source %s ... ", $source); + if (!$remote = fopen($source, "r")) { + $this->error(null); + exit(2); + } + $local = new Tempfile("remote"); + if (!stream_copy_to_stream($remote, $local->getStream())) { + $this->error(null); + exit(2); + } + $local->closeStream(); + $this->info("OK\n"); + } + + $this->cleanup[] = $local; + return $local->getPathname(); + } + + /** + * Extract local archive + * @param stirng $source + * @return string extracted directory + */ + private function extract($source) { + $dest = new Tempdir("local"); + if ($this->args->verbose) { + $this->info("Extracting to %s ... ", $dest); + } + $archive = new PharData($source); + $archive->extractTo($dest); + $this->info("OK\n"); + $this->cleanup[] = $dest; + return $dest; + } + + /** + * Localize a possibly remote source + * @param string $source + * @return string local source directory + */ + private function localize($source) { + if (!stream_is_local($source)) { + $source = $this->download($source); + } + if (!is_dir($source)) { + $source = $this->extract($source); + if ($this->args["pecl"]) { + $this->info("Sanitizing PECL dir ... "); + $dirs = glob("$source/*", GLOB_ONLYDIR); + $files = array_diff(glob("$source/*"), $dirs); + $source = current($dirs); + foreach ($files as $file) { + rename($file, "$source/" . basename($file)); + } + $this->info("OK\n"); + } + } + return $source; + } + /** * 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(); + } } - + + /** + * Ask for password on the console + * @param string $prompt + * @return string password + */ private function askpass($prompt = "Password:") { system("stty -echo", $retval); if ($retval) { @@ -158,7 +272,7 @@ class Packager implements Command $this->info("Creating phar %s ...%s", $pkgtemp, $this->args->verbose ? "\n" : " "); try { $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()); @@ -167,7 +281,7 @@ class Packager implements Command $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());