packager: allow overrideing package info from cli
[pharext/pharext] / src / pharext / Packager.php
index efd72b657913327f74c9360280f506a1ba8946b2..a422e972ece90453169560561b84195a6f24817d 100644 (file)
@@ -3,15 +3,14 @@
 namespace pharext;
 
 use Phar;
-use pharext\Cli\Args as CliArgs;
-use pharext\Cli\Command as CliCommand;
+use pharext\Exception;
 
 /**
  * The extension packaging command executed by bin/pharext
  */
 class Packager implements Command
 {
-       use CliCommand;
+       use Cli\Command;
        
        /**
         * Extension source directory
@@ -29,40 +28,42 @@ class Packager implements Command
         * 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],
+                               Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::REQARG],
                        ["E", "zend", "Mark as Zend Extension",
-                               CliArgs::OPTIONAL|CliARgs::SINGLE|CliArgs::NOARG],
+                               Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG],
                        [null, "signature", "Show pharext signature",
-                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
+                               Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG|Cli\Args::HALT],
                        [null, "license", "Show pharext license",
-                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
+                               Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG|Cli\Args::HALT],
                        [null, "version", "Show pharext version",
-                               CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
+                               Cli\Args::OPTIONAL|Cli\Args::SINGLE|Cli\Args::NOARG|Cli\Args::HALT],
                ]);
        }
        
@@ -71,11 +72,7 @@ class Packager implements Command
         */
        function __destruct() {
                foreach ($this->cleanup as $cleanup) {
-                       if (is_dir($cleanup)) {
-                               $this->rm($cleanup);
-                       } elseif (file_exists($cleanup)) {
-                               unlink($cleanup);
-                       }
+                       $cleanup->run();
                }
        }
        
@@ -104,24 +101,15 @@ class Packager implements Command
                        }
                } catch (\Exception $e) {
                        $this->error("%s\n", $e->getMessage());
-                       exit(2);
+                       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
+                       /* source needs to be evaluated before Cli\Args validation, 
+                        * so Cli\Args does not complain about missing arguments,
+                        * which come from SourceDir::getPackageInfo()
                         */
-                       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);
-                               }
-                       }
+                       $this->loadSource();
                } catch (\Exception $e) {
                        $errs[] = $e->getMessage();
                }
@@ -141,7 +129,7 @@ class Packager implements Command
                        if (!$this->args["quiet"]) {
                                $this->help($prog);
                        }
-                       exit(1);
+                       exit(self::EARGS);
                }
                
                $this->createPackage();
@@ -153,23 +141,28 @@ class Packager implements Command
         * @return string local source
         */
        private function download($source) {
-               $this->info("Fetching remote source %s ...\n", $source);
-               
                if ($this->args->git) {
-                       $task = new Task\GitClone($source);
+                       $task = new Task\GitClone($source, $this->args->branch);
                } 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)))
-                               );
+                       /* 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->args->verbose);
-               $this->debug("\n");
+               $local = $task->run($this->verbosity());
 
-               $this->cleanup[] = $local;
+               $this->cleanup[] = new Task\Cleanup($local);
                return $local;
        }
 
@@ -179,12 +172,17 @@ class Packager implements Command
         * @return string extracted directory
         */
        private function extract($source) {
-               $this->debug("Extracting %s ...\n", $source);
-               
-               $task = new Task\Extract($source);
-               $dest = $task->run($this->args->verbose);
+               try {
+                       $task = new Task\Extract($source);
+                       $dest = $task->run($this->verbosity());
+               } catch (\Exception $e) {
+                       if (false === strpos($e->getMessage(), "checksum mismatch")) {
+                               throw $e;
+                       }
+                       $dest = (new Task\PaxFixup($source))->run($this->verbosity());
+               }
                
-               $this->cleanup[] = $dest;
+               $this->cleanup[] = new Task\Cleanup($dest);
                return $dest;
        }
 
@@ -194,61 +192,94 @@ class Packager implements Command
         * @return string local source directory
         */
        private function localize($source) {
-               if (!stream_is_local($source)) {
+               if (!stream_is_local($source) || ($this->args->git && isset($this->args->branch))) {
                        $source = $this->download($source);
-                       $this->cleanup[] = $source;
+                       $this->cleanup[] = new Task\Cleanup($source);
                }
                $source = realpath($source);
                if (!is_dir($source)) {
                        $source = $this->extract($source);
-                       $this->cleanup[] = $source;
+                       $this->cleanup[] = new Task\Cleanup($source);
                        
-                       if ($this->args->pecl) {
-                               $this->debug("Sanitizing PECL dir ...\n");
-                               $source = (new Task\PeclFixup($source))->run($this->args->verbose);
+                       if (!$this->args->git) {
+                               $source = (new Task\PeclFixup($source))->run($this->verbosity());
                        }
                }
                return $source;
        }
 
+       /**
+        * 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() {
                try {
-                       $meta = array_merge($this->metadata(), [
-                               "date" => date("Y-m-d"),
+                       $meta = array_merge(Metadata::all(), [
                                "name" => $this->args->name,
                                "release" => $this->args->release,
-                               "license" => @file_get_contents(current(glob($this->source->getBaseDir()."/LICENSE*"))),
-                               "stub" => "pharext_installer.php",
+                               "license" => $this->source->getLicense(),
                                "type" => $this->args->zend ? "zend_extension" : "extension",
                        ]);
-                       $file = (new Task\PharBuild($this->source, $meta))->run();
+                       $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("Using private key to sign phar ...\n");
-                               $pass = (new Task\Askpass)->run($this->args->verbose);
+                               $pass = (new Task\Askpass)->run($this->verbosity());
                                $sign = new Task\PharSign($file, $this->args->sign, $pass);
-                               $pkey = $sign->run($this->args->verbose);
+                               $pkey = $sign->run($this->verbosity());
                        }
 
                } catch (\Exception $e) {
                        $this->error("%s\n", $e->getMessage());
-                       exit(4);
+                       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);
+                               $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->args->verbose)->exportPublicKey($name.".pubkey");
+                                       $sign->run($this->verbosity())->exportPublicKey($name.".pubkey");
                                }
 
                        } catch (\Exception $e) {
@@ -260,13 +291,13 @@ class Packager implements Command
                        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);
+                               $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->args->verbose)->exportPublicKey($name.".pubkey");
+                                       $sign->run($this->verbosity())->exportPublicKey($name.".pubkey");
                                }
 
                        } catch (\Exception $e) {
@@ -276,7 +307,7 @@ class Packager implements Command
 
                try {
                        $move = new Task\PharRename($file, $this->args->dest, $this->args->name ."-". $this->args->release);
-                       $name = $move->run($this->args->verbose);
+                       $name = $move->run($this->verbosity());
 
                        $this->info("Created executable phar %s\n", $name);
 
@@ -286,7 +317,7 @@ class Packager implements Command
 
                } catch (\Exception $e) {
                        $this->error("%s\n", $e->getMessage());
-                       exit(4);
+                       exit(self::EBUILD);
                }
        }
 }