download progress, cleanups
[pharext/pharext] / src / pharext / Packager.php
index f2687654f673a8623bc2e2ed8a704ea42b6a86d4..d3e4000a3f45c505829b336c8528f8a40e531553 100644 (file)
@@ -3,6 +3,7 @@
 namespace pharext;
 
 use Phar;
+use PharData;
 use pharext\Cli\Args as CliArgs;
 use pharext\Cli\Command as CliCommand;
 
@@ -19,6 +20,12 @@ class Packager implements Command
         */
        private $source;
        
+       /**
+        * Cleanups
+        * @var array
+        */
+       private $cleanup = [];
+       
        /**
         * Create the command
         */
@@ -54,6 +61,19 @@ class Packager implements Command
                ]);
        }
        
+       /**
+        * Perform cleaniup
+        */
+       function __destruct() {
+               foreach ($this->cleanup as $cleanup) {
+                       if (is_dir($cleanup)) {
+                               $this->rm($cleanup);
+                       } else {
+                               unlink($cleanup);
+                       }
+               }
+       }
+       
        /**
         * @inheritdoc
         * @see \pharext\Command::run()
@@ -75,13 +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 SourceDir\Pecl($this, $this->args["source"]);
+                                       $this->source = new SourceDir\Pecl($this, $source);
                                } elseif ($this->args["git"]) {
-                                       $this->source = new SourceDir\Git($this, $this->args["source"]);
+                                       $this->source = new SourceDir\Git($this, $source);
                                } else {
-                                       $this->source = new SourceDir\Pharext($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,6 +154,98 @@ class Packager implements Command
                }
        }
 
+       /**
+        * Download remote source
+        * @param string $source
+        * @return string local source
+        */
+       private function download($source) {
+               $this->info("Fetching remote source %s ... ", $source);
+               if ($this->args["git"]) {
+                       $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 {
+                       $context = stream_context_create([],["notification" => function($notification, $severity, $message, $code, $bytes_cur, $bytes_max) {
+                               switch ($notification) {
+                                       case STREAM_NOTIFY_CONNECT:
+                                               $this->debug("\n");
+                                               break;
+                                       case STREAM_NOTIFY_PROGRESS:
+                                               if ($bytes_max) {
+                                                       $bytes_pct = $bytes_cur/$bytes_max;
+                                                       $this->debug("\r %3d%% [%s>%s] ",
+                                                               $bytes_pct*100,
+                                                               str_repeat("=", round(70*$bytes_pct)), 
+                                                               str_repeat(" ", round(70*(1-$bytes_pct)))
+                                                       );
+                                               }
+                                               break;
+                                       case STREAM_NOTIFY_COMPLETED:
+                                               /* this is not generated, why? */
+                                               break;
+                               }
+                       }]);
+                       if (!$remote = fopen($source, "r", false, $context)) {
+                               $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;
+       }
+
+       /**
+        * Extract local archive
+        * @param stirng $source
+        * @return string extracted directory
+        */
+       private function extract($source) {
+               $dest = new Tempdir("local");
+               $this->debug("Extracting %s to %s ... ", $source, $dest);
+               $archive = new PharData($source);
+               $archive->extractTo($dest);
+               $this->debug("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->debug("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->debug("OK\n");
+                       }
+               }
+               return $source;
+       }
+
        /**
         * Traverses all pharext source files to bundle
         * @return Generator
@@ -133,7 +258,12 @@ class Packager implements Command
                        
                }
        }
-       
+
+       /**
+        * Ask for password on the console
+        * @param string $prompt
+        * @return string password
+        */
        private function askpass($prompt = "Password:") {
                system("stty -echo", $retval);
                if ($retval) {
@@ -153,13 +283,13 @@ class Packager implements Command
         */
        private function createPackage() {
                $pkguniq = uniqid();
-               $pkgtemp = $this->tempname($pkguniq, "phar");
+               $pkgtemp = sprintf("%s/%s.phar", sys_get_temp_dir(), $pkguniq);
                $pkgdesc = "{$this->args->name}-{$this->args->release}";
        
                $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());
@@ -177,7 +307,7 @@ class Packager implements Command
                        if (!chmod($pkgtemp, 0777)) {
                                $this->error(null);
                        } elseif ($this->args->verbose) {
-                               $this->info("Created executable phar %s\n", $pkgtemp);
+                               $this->debug("Created executable phar %s\n", $pkgtemp);
                        } else {
                                $this->info("OK\n");
                        }