Ship your dependencies as phars inside the phar
[pharext/pharext] / src / pharext / Packager.php
index 3408c75d8b0ac8c637c93a423cea631167fa6499..f27c38a0a8b0f7e623b71cf514dc1f9b0b6850e5 100644 (file)
@@ -9,11 +9,7 @@ use Phar;
  */
 class Packager implements Command
 {
-       /**
-        * Command line arguments
-        * @var pharext\CliArgs
-        */
-       private $args;
+       use CliCommand;
        
        /**
         * Extension source directory
@@ -32,6 +28,10 @@ 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",
@@ -41,15 +41,11 @@ class Packager implements Command
                        ["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],
-]);
+               ]);
        }
        
        /**
@@ -57,33 +53,48 @@ 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 {
+                       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"]);
+                               } elseif (realpath($this->args["source"]."/pharext_package.php")) {
+                                       $this->source = new PharextSourceDir($this, $this->args["source"]);
+                               } else {
+                                       $this->source = new FilteredSourceDir($this, $this->args["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();
+                       }
+                       foreach ($errs as $err) {
+                               $this->error("%s\n", $err);
+                       }
+                       printf("\n");
+                       if (!$this->args["quiet"]) {
+                               $this->help($prog);
                        }
                        exit(1);
                }
@@ -91,34 +102,6 @@ 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));
-               }
-       }
-       
        /**
         * Traverses all pharext source files to bundle
         * @return Generator
@@ -136,12 +119,12 @@ class Packager implements Command
         */
        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);
                        $package->startBuffering();
                        $package->buildFromIterator($this->source, $this->source->getBaseDir());
                        $package->buildFromIterator($this->bundle());
@@ -150,21 +133,30 @@ class Packager implements Command
                        $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");
                        }
                        if ($this->args->gzip) {
                                $this->info("Compressing with gzip ... ");
-                               $package->compress(Phar::GZ);
-                               $this->info("OK\n");
+                               try {
+                                       $package->compress(Phar::GZ);
+                                       $this->info("OK\n");
+                               } catch (\Exception $e) {
+                                       $this->error("%s\n", $e->getMessage());
+                               }
                        }
                        if ($this->args->bzip) {
                                $this->info("Compressing with bzip ... ");
-                               $package->compress(Phar::BZ2);
-                               $this->info("OK\n");
+                               try {
+                                       $package->compress(Phar::BZ2);
+                                       $this->info("OK\n");
+                               } catch (\Exception $e) {
+                                       $this->error("%s\n", $e->getMessage());
+                               }
                        }
                        
                        unset($package);
@@ -174,11 +166,11 @@ class Packager implements Command
                }
 
                foreach (glob($pkgtemp."*") as $pkgtemp) {
-                       $pkgfile = str_replace($pkguniq, "{$pkgdesc}-ext", $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"]);
+                               $this->error(null);
                                exit(5);
                        }
                        $this->info("OK\n");