SelfUpdateCommand.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Console\Command;
  12. use PhpCsFixer\ToolInfo;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. /**
  18. * @author Igor Wiedler <igor@wiedler.ch>
  19. * @author Stephane PY <py.stephane1@gmail.com>
  20. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  21. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  22. * @author SpacePossum
  23. *
  24. * @internal
  25. */
  26. final class SelfUpdateCommand extends Command
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. protected function configure()
  32. {
  33. $this
  34. ->setName('self-update')
  35. ->setAliases(['selfupdate'])
  36. ->setDefinition(
  37. [
  38. new InputOption('--force', '-f', InputOption::VALUE_NONE, 'Force update to next major version if available.'),
  39. ]
  40. )
  41. ->setDescription('Update php-cs-fixer.phar to the latest stable version.')
  42. ->setHelp(
  43. <<<'EOT'
  44. The <info>%command.name%</info> command replace your php-cs-fixer.phar by the
  45. latest version released on:
  46. <comment>https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases</comment>
  47. <info>$ php php-cs-fixer.phar %command.name%</info>
  48. EOT
  49. )
  50. ;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. protected function execute(InputInterface $input, OutputInterface $output)
  56. {
  57. if (!ToolInfo::isInstalledAsPhar()) {
  58. $output->writeln('<error>Self-update is available only for PHAR version.</error>');
  59. return 1;
  60. }
  61. $remoteTag = $this->getLatestTag();
  62. if (null === $remoteTag) {
  63. $output->writeln('<error>Unable to determine newest version.</error>');
  64. return 0;
  65. }
  66. $currentVersion = 'v'.$this->getApplication()->getVersion();
  67. if ($currentVersion === $remoteTag) {
  68. $output->writeln('<info>php-cs-fixer is already up to date.</info>');
  69. return 0;
  70. }
  71. $remoteVersionParsed = $this->parseVersion($remoteTag);
  72. $currentVersionParsed = $this->parseVersion($currentVersion);
  73. if ($remoteVersionParsed[0] > $currentVersionParsed[0] && true !== $input->getOption('force')) {
  74. $output->writeln(sprintf('<info>A new major version of php-cs-fixer is available</info> (<comment>%s</comment>)', $remoteTag));
  75. $output->writeln(sprintf('<info>Before upgrading please read</info> https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/%s/UPGRADE.md', $remoteTag));
  76. $output->writeln('<info>If you are ready to upgrade run this command with</info> <comment>-f</comment>');
  77. $output->writeln('<info>Checking for new minor/patch version...</info>');
  78. // test if there is a new minor version available
  79. $remoteTag = $this->getLatestNotMajorUpdateTag($currentVersion);
  80. if ($currentVersion === $remoteTag) {
  81. $output->writeln('<info>No minor update for php-cs-fixer.</info>');
  82. return 0;
  83. }
  84. }
  85. $localFilename = realpath($_SERVER['argv'][0]) ?: $_SERVER['argv'][0];
  86. if (!is_writable($localFilename)) {
  87. $output->writeln(sprintf('<error>No permission to update %s file.</error>', $localFilename));
  88. return 1;
  89. }
  90. $tempFilename = basename($localFilename, '.phar').'-tmp.phar';
  91. $remoteFilename = sprintf('https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/%s/php-cs-fixer.phar', $remoteTag);
  92. try {
  93. $copyResult = @copy($remoteFilename, $tempFilename);
  94. if (false === $copyResult) {
  95. $output->writeln(sprintf('<error>Unable to download new version %s from the server.</error>', $remoteTag));
  96. return 1;
  97. }
  98. chmod($tempFilename, 0777 & ~umask());
  99. // test the phar validity
  100. $phar = new \Phar($tempFilename);
  101. // free the variable to unlock the file
  102. unset($phar);
  103. rename($tempFilename, $localFilename);
  104. $output->writeln(sprintf('<info>php-cs-fixer updated</info> (<comment>%s</comment>)', $remoteTag));
  105. } catch (\Exception $e) {
  106. if (!$e instanceof \UnexpectedValueException && !$e instanceof \PharException) {
  107. throw $e;
  108. }
  109. unlink($tempFilename);
  110. $output->writeln(sprintf('<error>The download of %s is corrupt (%s).</error>', $remoteTag, $e->getMessage()));
  111. $output->writeln('<error>Please re-run the self-update command to try again.</error>');
  112. return 1;
  113. }
  114. }
  115. /**
  116. * @return null|string
  117. */
  118. private function getLatestTag()
  119. {
  120. $raw = file_get_contents(
  121. 'https://api.github.com/repos/FriendsOfPHP/PHP-CS-Fixer/releases/latest',
  122. null,
  123. stream_context_create($this->getStreamContextOptions())
  124. );
  125. if (false === $raw) {
  126. return null;
  127. }
  128. $json = json_decode($raw, true);
  129. if (null === $json) {
  130. return null;
  131. }
  132. return $json['tag_name'];
  133. }
  134. /**
  135. * @param string $currentTag in format v?\d.\d.\d
  136. *
  137. * @return string in format v?\d.\d.\d
  138. */
  139. private function getLatestNotMajorUpdateTag($currentTag)
  140. {
  141. $currentTagParsed = $this->parseVersion($currentTag);
  142. $nextVersionParsed = $currentTagParsed;
  143. do {
  144. $nextTag = sprintf('v%d.%d.%d', $nextVersionParsed[0], ++$nextVersionParsed[1], 0);
  145. } while ($this->hasRemoteTag($nextTag));
  146. $nextVersionParsed = $this->parseVersion($nextTag);
  147. --$nextVersionParsed[1];
  148. // check if new minor found, otherwise start looking for new patch from the current patch number
  149. if ($currentTagParsed[1] === $nextVersionParsed[1]) {
  150. $nextVersionParsed[2] = $currentTagParsed[2];
  151. }
  152. do {
  153. $nextTag = sprintf('v%d.%d.%d', $nextVersionParsed[0], $nextVersionParsed[1], ++$nextVersionParsed[2]);
  154. } while ($this->hasRemoteTag($nextTag));
  155. return sprintf('v%d.%d.%d', $nextVersionParsed[0], $nextVersionParsed[1], $nextVersionParsed[2] - 1);
  156. }
  157. /**
  158. * @param string $method HTTP method
  159. *
  160. * @return array
  161. */
  162. private function getStreamContextOptions($method = 'GET')
  163. {
  164. return [
  165. 'http' => [
  166. 'header' => 'User-Agent: FriendsOfPHP/PHP-CS-Fixer',
  167. 'method' => $method,
  168. ],
  169. ];
  170. }
  171. /**
  172. * @param string $tag
  173. *
  174. * @return bool
  175. */
  176. private function hasRemoteTag($tag)
  177. {
  178. $url = 'https://api.github.com/repos/FriendsOfPHP/PHP-CS-Fixer/releases/tags/'.$tag;
  179. stream_context_set_default(
  180. $this->getStreamContextOptions('HEAD')
  181. );
  182. $headers = get_headers($url);
  183. if (!is_array($headers) || count($headers) < 1) {
  184. throw new \RuntimeException(sprintf('Failed to get headers for "%s".', $url));
  185. }
  186. return 1 === preg_match('#^HTTP\/\d.\d 200#', $headers[0]);
  187. }
  188. /**
  189. * @param string $tag version in format v?\d.\d.\d
  190. *
  191. * @return int[]
  192. */
  193. private function parseVersion($tag)
  194. {
  195. $tag = explode('.', $tag);
  196. if ('v' === $tag[0][0]) {
  197. $tag[0] = substr($tag[0], 1);
  198. }
  199. return [(int) $tag[0], (int) $tag[1], (int) $tag[2]];
  200. }
  201. }