|
| 1 | +#!/usr/bin/php |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Script for updating Magento with 2.3 requirements that can't be done by composer update or setup:upgrade |
| 5 | + * |
| 6 | + * Steps included: |
| 7 | + * - Require new version of the metapackage |
| 8 | + * - Updating "require-dev" section |
| 9 | + * - Add "Zend\\Mvc\\Controller\\": "setup/src/Zend/Mvc/Controller/" to composer.json "autoload":"psr-4" section |
| 10 | + * - Updating Magento/Updater if it's installed |
| 11 | + * |
| 12 | + * Copyright © Magento, Inc. All rights reserved. |
| 13 | + * See COPYING.txt for license details. |
| 14 | + */ |
| 15 | +declare(strict_types=1); |
| 16 | + |
| 17 | +$_scriptName = basename(__FILE__); |
| 18 | + |
| 19 | +define( |
| 20 | + 'SYNOPSIS', |
| 21 | +<<<SYNOPSIS |
| 22 | +Updates Magento with 2.3 requirements that can't be done by `composer update` or `bin/magento setup:upgrade`. |
| 23 | +This should be run prior to running `composer update` or `bin/magento setup:upgrade`. |
| 24 | +
|
| 25 | +Steps included: |
| 26 | + - Require new version of the metapackage |
| 27 | + - Updating "require-dev" section |
| 28 | + - Add "Zend\\Mvc\\Controller\\": "setup/src/Zend/Mvc/Controller/" to composer.json "autoload":"psr-4" section |
| 29 | + - Updating Magento/Updater if it's installed |
| 30 | +
|
| 31 | +Usage: php -f $_scriptName -- --root="</path/to/magento/root/>" [--composer=</path/to/composer/executable>] |
| 32 | + [--edition=<community|enterprise>] [--version=<magento_package_version>] [--repo=<composer_repo_url>] |
| 33 | + [--help] |
| 34 | +
|
| 35 | +Required: |
| 36 | + --root="</path/to/magento/root/>" |
| 37 | + Path to the Magento installation root directory |
| 38 | +
|
| 39 | +Optional: |
| 40 | + --composer="</path/to/composer/executable>" |
| 41 | + Path to the composer executable |
| 42 | + - Default: The composer found in PATH |
| 43 | + |
| 44 | + --edition=<community|enterprise> |
| 45 | + The Magento edition to upgrade to. Open Source = 'community', Commerce = 'enterprise' |
| 46 | + - Default: The edition currently required in composer.json |
| 47 | + |
| 48 | + --version=<magento_package_version> |
| 49 | + The Magento version to upgrade to |
| 50 | + - Default: The value for the "version" field in composer.json |
| 51 | + |
| 52 | + --repo=<composer_repo_url> |
| 53 | + The Magento composer repository to pull new packages from |
| 54 | + - Default: The Magento repository configured in composer.json |
| 55 | +
|
| 56 | + --help |
| 57 | + Display this message |
| 58 | +SYNOPSIS |
| 59 | +); |
| 60 | + |
| 61 | + |
| 62 | +$opts = getopt('', [ |
| 63 | + 'root:', |
| 64 | + 'composer:', |
| 65 | + 'edition:', |
| 66 | + 'version:', |
| 67 | + 'repo:', |
| 68 | + 'help' |
| 69 | +]); |
| 70 | + |
| 71 | +if (isset($opts['help'])) { |
| 72 | + echo SYNOPSIS . PHP_EOL; |
| 73 | + exit(0); |
| 74 | +} |
| 75 | + |
| 76 | +try { |
| 77 | + if (empty($opts['root'])) { |
| 78 | + throw new BadMethodCallException("Magento root must be given with '--root'" . PHP_EOL . PHP_EOL . SYNOPSIS); |
| 79 | + } |
| 80 | + |
| 81 | + $rootDir = $opts['root']; |
| 82 | + if (!is_dir($rootDir)) { |
| 83 | + throw new InvalidArgumentException("Magento root directory '$rootDir' does not exist"); |
| 84 | + } |
| 85 | + |
| 86 | + $cmd = (!empty($opts['composer']) ? $opts['composer'] : 'composer') . " --working-dir='$rootDir'"; |
| 87 | + $jsonData = json_decode(file_get_contents("$rootDir/composer.json"), true); |
| 88 | + |
| 89 | + $version = !empty($opts['version']) ? $opts['version'] : $jsonData['version']; |
| 90 | + if (empty($version)) { |
| 91 | + throw new InvalidArgumentException('Value not found for "version" field in composer.json'); |
| 92 | + } |
| 93 | + |
| 94 | + if (!empty($opts['edition'])) { |
| 95 | + $edition = $opts['edition']; |
| 96 | + } |
| 97 | + else { |
| 98 | + $editionRegex = '|^magento/product\-(?<edition>[a-z]+)\-edition$|'; |
| 99 | + |
| 100 | + foreach (array_keys($jsonData["require"]) as $requiredPackage) { |
| 101 | + if (preg_match($editionRegex, $requiredPackage, $matches)) { |
| 102 | + $edition = $matches['edition']; |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + if (empty($edition)) { |
| 107 | + throw new InvalidArgumentException('No valid Magento edition found in composer.json requirements'); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + echo "Backing up $rootDir/composer.json" . PHP_EOL; |
| 112 | + copy("$rootDir/composer.json", "$rootDir/composer.json.bak"); |
| 113 | + |
| 114 | + echo "Updating Magento product requirement to magento/product-$edition-edition=$version" . PHP_EOL; |
| 115 | + if ($edition == "enterprise") { |
| 116 | + execVerbose("$cmd remove --verbose magento/product-community-edition --no-update"); |
| 117 | + } |
| 118 | + execVerbose("$cmd require --verbose magento/product-$edition-edition=$version --no-update"); |
| 119 | + |
| 120 | + echo 'Updating "require-dev" section of composer.json' . PHP_EOL; |
| 121 | + execVerbose("$cmd require --dev --verbose " . |
| 122 | + "phpunit/phpunit:~6.2.0 " . |
| 123 | + "friendsofphp/php-cs-fixer:~2.10.1 " . |
| 124 | + "lusitanian/oauth:~0.8.10 " . |
| 125 | + "pdepend/pdepend:2.5.2 " . |
| 126 | + "sebastian/phpcpd:~3.0.0 " . |
| 127 | + "squizlabs/php_codesniffer:3.2.2 --no-update"); |
| 128 | + |
| 129 | + execVerbose("$cmd remove --dev --verbose sjparkinson/static-review fabpot/php-cs-fixer --no-update"); |
| 130 | + |
| 131 | + echo 'Adding "Zend\\\\Mvc\\\\Controller\\\\": "setup/src/Zend/Mvc/Controller/" to "autoload":"psr-4"' . PHP_EOL; |
| 132 | + $jsonData = json_decode(file_get_contents("$rootDir/composer.json"), true); |
| 133 | + $jsonData["autoload"]["psr-4"]["Zend\\Mvc\\Controller\\"] = "setup/src/Zend/Mvc/Controller/"; |
| 134 | + |
| 135 | + $jsonData["version"] = $version; |
| 136 | + file_put_contents("$rootDir/composer.json", json_encode($jsonData, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT)); |
| 137 | + |
| 138 | + if (file_exists("$rootDir/update")) { |
| 139 | + echo "Replacing Magento/Updater" . PHP_EOL; |
| 140 | + |
| 141 | + $mageUrls = []; |
| 142 | + if (isset($opts['repo'])) { |
| 143 | + $mageUrls[] = $opts['repo']; |
| 144 | + } |
| 145 | + else { |
| 146 | + $composerUrls = array_map(function ($r) { return $r["url"]; }, |
| 147 | + array_filter($jsonData['repositories']), function ($r) { return $r["type"] == "composer"; }); |
| 148 | + $mageUrls = array_filter($composerUrls, function($u) { return strpos($u, ".mage") !== false; }); |
| 149 | + |
| 150 | + if (count($mageUrls) == 0) { |
| 151 | + throw new InvalidArgumentException('No Magento composer repository urls found in composer.json'); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + echo "Backing up $rootDir/update" . PHP_EOL; |
| 156 | + rename("$rootDir/update", "$rootDir/update.bak"); |
| 157 | + $newPackage = "magento/project-$edition-edition=$version"; |
| 158 | + foreach ($mageUrls as $repoUrl) { |
| 159 | + try { |
| 160 | + deleteFilepath("$rootDir/temp_update"); |
| 161 | + execVerbose("$cmd create-project --repository=$repoUrl $newPackage $rootDir/temp_update --no-install"); |
| 162 | + rename("$rootDir/temp_update/update", "$rootDir/update"); |
| 163 | + echo "Upgraded Magento/Updater from magento/project-$edition-edition $version on $repoUrl" . PHP_EOL; |
| 164 | + unset($exception); |
| 165 | + break; |
| 166 | + } |
| 167 | + catch (Exception $e) { |
| 168 | + echo "Failed to find Magento package on $repoUrl" . PHP_EOL; |
| 169 | + $exception = $e; |
| 170 | + } |
| 171 | + } |
| 172 | + deleteFilepath("$rootDir/temp_update"); |
| 173 | + |
| 174 | + if (isset($exception)) { |
| 175 | + throw $exception; |
| 176 | + } |
| 177 | + } |
| 178 | +} catch (Exception $e) { |
| 179 | + if ($e->getPrevious()) { |
| 180 | + $message = (string)$e->getPrevious(); |
| 181 | + } else { |
| 182 | + $message = $e->getMessage(); |
| 183 | + } |
| 184 | + |
| 185 | + try { |
| 186 | + error_log($message . PHP_EOL . PHP_EOL . "Error encountered; resetting backups" . PHP_EOL); |
| 187 | + if (file_exists("$rootDir/update.bak")) { |
| 188 | + deleteFilepath("$rootDir/update_temp"); |
| 189 | + deleteFilepath("$rootDir/update"); |
| 190 | + rename("$rootDir/update.bak", "$rootDir/update"); |
| 191 | + } |
| 192 | + |
| 193 | + if (file_exists("$rootDir/composer.json.bak")) { |
| 194 | + deleteFilepath("$rootDir/composer.json"); |
| 195 | + rename("$rootDir/composer.json.bak", "$rootDir/composer.json"); |
| 196 | + } |
| 197 | + } |
| 198 | + catch (Exception $e) { |
| 199 | + error_log($e->getMessage() . PHP_EOL); |
| 200 | + } |
| 201 | + |
| 202 | + exit($e->getCode() == 0 ? 1 : $e->getCode()); |
| 203 | +} |
| 204 | + |
| 205 | +/** |
| 206 | + * Execute a command with automatic escaping of arguments |
| 207 | + * |
| 208 | + * @param string $command |
| 209 | + * @return array |
| 210 | + * @throws Exception |
| 211 | + */ |
| 212 | +function execVerbose($command) |
| 213 | +{ |
| 214 | + $args = func_get_args(); |
| 215 | + $args = array_map('escapeshellarg', $args); |
| 216 | + $args[0] = $command; |
| 217 | + $command = call_user_func_array('sprintf', $args); |
| 218 | + echo $command . PHP_EOL; |
| 219 | + exec($command . " 2>&1", $output, $exitCode); |
| 220 | + $outputString = join(PHP_EOL, $output); |
| 221 | + if (0 !== $exitCode) { |
| 222 | + throw new Exception($outputString, $exitCode); |
| 223 | + } |
| 224 | + echo $outputString . PHP_EOL; |
| 225 | + return $output; |
| 226 | +} |
| 227 | + |
| 228 | +/** |
| 229 | + * Deletes a file or a directory and all its contents |
| 230 | + * |
| 231 | + * @param string $path |
| 232 | + * @throws Exception |
| 233 | + */ |
| 234 | +function deleteFilepath($path) { |
| 235 | + if (!file_exists($path)) { |
| 236 | + return; |
| 237 | + } |
| 238 | + if (is_dir($path)) { |
| 239 | + $files = array_diff(scandir($path), array('..', '.')); |
| 240 | + foreach ($files as $file) { |
| 241 | + deleteFilepath("$path/$file"); |
| 242 | + } |
| 243 | + rmdir($path); |
| 244 | + } |
| 245 | + else { |
| 246 | + unlink($path); |
| 247 | + } |
| 248 | + if (file_exists($path)) { |
| 249 | + throw new Exception("Failed to delete $path"); |
| 250 | + } |
| 251 | +} |
0 commit comments