refuse to upgrade pre-v3 packages
[pharext/pharext] / src / pharext / Updater.php
index 7da989db9db0c5147d83e3d0780560f881b1de21..af479c97734cc786fbf657ea603ec6ec476c1ca3 100644 (file)
@@ -4,8 +4,8 @@ namespace pharext;
 
 use Phar;
 use PharFileInfo;
-use RecursiveIteratorIterator;
 use SplFileInfo;
+use pharext\Exception;
 
 class Updater implements Command
 {
@@ -33,7 +33,7 @@ class Updater implements Command
                ]);
        }
 
-               /**
+       /**
         * @inheritdoc
         * @see \pharext\Command::run()
         */
@@ -82,8 +82,16 @@ class Updater implements Command
                }
 
                foreach ($this->args[0] as $file) {
-                       if (file_exists($file)) {
-                               $this->updatePackage(new SplFileInfo($file));
+                       $info = new SplFileInfo($file);
+
+                       while ($info->isLink()) {
+                               $info = new SplFileInfo($info->getLinkTarget());
+                       }
+                       
+                       if ($info->isFile()) {
+                               if (!$this->updatePackage($info)) {
+                                       $this->warn("Cannot upgrade pre-v3 packages\n");
+                               }
                        } else {
                                $this->error("File '%s' does not exist\n", $file);
                                exit(self::EARGS);
@@ -91,10 +99,20 @@ class Updater implements Command
                }
        }
 
+       /**
+        * Replace the pharext core in an .ext.phar package
+        * @param string $temp path to temp phar
+        * @return boolean FALSE if the package is too old (pre-v3) to upgrade
+        */
        private function replacePharext($temp) {
                $phar = new Phar($temp, Phar::CURRENT_AS_SELF);
                $phar->startBuffering();
 
+               if (!$meta = $phar->getMetadata()) {
+                       // don't upgrade pre-v3 packages
+                       return false;
+               }
+
                // replace current pharext files
                $core = (new Task\BundleGenerator)->run($this->verbosity());
                $phar->buildFromIterator($core);
@@ -113,9 +131,22 @@ class Updater implements Command
                $phar->setMetadata([
                        "version" => Metadata::version(),
                        "header" => Metadata::header(),
-               ] + $phar->getMetadata());
+               ] + $meta);
+
+               $this->info("Updated pharext version from '%s' to '%s'\n",
+                       isset($meta["version"]) ? $meta["version"] : "(unknown)",
+                       $phar->getMetadata()["version"]);
+
+               return true;
        }
 
+       /**
+        * Update an .ext.phar package to the current pharext version
+        * @param SplFileInfo $file
+        * @param Phar $phar the parent phar containing $file as dependency
+        * @return boolean FALSE if the package is too old (pre-v3) to upgrade
+        * @throws Exception
+        */
        private function updatePackage(SplFileInfo $file, Phar $phar = null) {
                $this->info("Updating pharext core in '%s'...\n", basename($file));
 
@@ -124,13 +155,20 @@ class Updater implements Command
                if (!copy($file->getPathname(), $temp)) {
                        throw new Exception;
                }
+               if (!chmod($temp, $file->getPerms())) {
+                       throw new Exception;
+               }
                
-               $this->replacePharext($temp);
+               if (!$this->replacePharext($temp)) {
+                       return false;
+               }
 
                if ($phar) {
                        $phar->addFile($temp, $file);
                } elseif (!rename($temp, $file->getPathname())) {
                        throw new Exception;
                }
+
+               return true;
        }
 }