X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fpharext%2FUpdater.php;h=af479c97734cc786fbf657ea603ec6ec476c1ca3;hb=f55545591b80c22e8e3ca4dffe2ac68c59090835;hp=7da989db9db0c5147d83e3d0780560f881b1de21;hpb=b1fff040f69249e6060c01c14a50b49ba2ed20f3;p=pharext%2Fpharext diff --git a/src/pharext/Updater.php b/src/pharext/Updater.php index 7da989d..af479c9 100644 --- a/src/pharext/Updater.php +++ b/src/pharext/Updater.php @@ -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; } }