VcsDownloader.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Downloader;
  12. use Composer\Config;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Package\Version\VersionParser;
  15. use Composer\Util\ProcessExecutor;
  16. use Composer\IO\IOInterface;
  17. use Composer\Util\Filesystem;
  18. /**
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. */
  21. abstract class VcsDownloader implements DownloaderInterface
  22. {
  23. protected $io;
  24. protected $config;
  25. protected $process;
  26. protected $filesystem;
  27. public function __construct(IOInterface $io, Config $config, ProcessExecutor $process = null, Filesystem $fs = null)
  28. {
  29. $this->io = $io;
  30. $this->config = $config;
  31. $this->process = $process ?: new ProcessExecutor($io);
  32. $this->filesystem = $fs ?: new Filesystem;
  33. }
  34. /**
  35. * {@inheritDoc}
  36. */
  37. public function getInstallationSource()
  38. {
  39. return 'source';
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function download(PackageInterface $package, $path)
  45. {
  46. if (!$package->getSourceReference()) {
  47. throw new \InvalidArgumentException('Package '.$package->getPrettyName().' is missing reference information');
  48. }
  49. $this->io->write(" - Installing <info>" . $package->getName() . "</info> (<comment>" . VersionParser::formatVersion($package) . "</comment>)");
  50. $this->filesystem->removeDirectory($path);
  51. $this->doDownload($package, $path);
  52. $this->io->write('');
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function update(PackageInterface $initial, PackageInterface $target, $path)
  58. {
  59. if (!$target->getSourceReference()) {
  60. throw new \InvalidArgumentException('Package '.$target->getPrettyName().' is missing reference information');
  61. }
  62. $name = $target->getName();
  63. if ($initial->getPrettyVersion() == $target->getPrettyVersion()) {
  64. if ($target->getSourceType() === 'svn') {
  65. $from = $initial->getSourceReference();
  66. $to = $target->getSourceReference();
  67. } else {
  68. $from = substr($initial->getSourceReference(), 0, 7);
  69. $to = substr($target->getSourceReference(), 0, 7);
  70. }
  71. $name .= ' '.$initial->getPrettyVersion();
  72. } else {
  73. $from = VersionParser::formatVersion($initial);
  74. $to = VersionParser::formatVersion($target);
  75. }
  76. $this->io->write(" - Updating <info>" . $name . "</info> (<comment>" . $from . "</comment> => <comment>" . $to . "</comment>)");
  77. $this->cleanChanges($path, true);
  78. try {
  79. $this->doUpdate($initial, $target, $path);
  80. } catch (\Exception $e) {
  81. // in case of failed update, try to reapply the changes before aborting
  82. $this->reapplyChanges($path);
  83. throw $e;
  84. }
  85. $this->reapplyChanges($path);
  86. //print the commit logs if in verbose mode
  87. if ($this->io->isVerbose()) {
  88. $message = 'Pulling in changes:';
  89. $logs = $this->getCommitLogs($initial->getSourceReference(), $target->getSourceReference(), $path);
  90. if (!trim($logs)) {
  91. $message = 'Rolling back changes:';
  92. $logs = $this->getCommitLogs($target->getSourceReference(), $initial->getSourceReference(), $path);
  93. }
  94. if (trim($logs)) {
  95. $logs = implode("\n", array_map(function ($line) {
  96. return ' ' . $line;
  97. }, explode("\n", $logs)));
  98. $this->io->write(' '.$message);
  99. $this->io->write($logs);
  100. }
  101. }
  102. $this->io->write('');
  103. }
  104. /**
  105. * {@inheritDoc}
  106. */
  107. public function remove(PackageInterface $package, $path)
  108. {
  109. $this->io->write(" - Removing <info>" . $package->getName() . "</info> (<comment>" . $package->getPrettyVersion() . "</comment>)");
  110. $this->cleanChanges($path, false);
  111. if (!$this->filesystem->removeDirectory($path)) {
  112. // retry after a bit on windows since it tends to be touchy with mass removals
  113. if (!defined('PHP_WINDOWS_VERSION_BUILD') || (usleep(250) && !$this->filesystem->removeDirectory($path))) {
  114. throw new \RuntimeException('Could not completely delete '.$path.', aborting.');
  115. }
  116. }
  117. }
  118. /**
  119. * Download progress information is not available for all VCS downloaders.
  120. * {@inheritDoc}
  121. */
  122. public function setOutputProgress($outputProgress)
  123. {
  124. return $this;
  125. }
  126. /**
  127. * Prompt the user to check if changes should be stashed/removed or the operation aborted
  128. *
  129. * @param string $path
  130. * @param bool $update if true (update) the changes can be stashed and reapplied after an update,
  131. * if false (remove) the changes should be assumed to be lost if the operation is not aborted
  132. * @throws \RuntimeException in case the operation must be aborted
  133. */
  134. protected function cleanChanges($path, $update)
  135. {
  136. // the default implementation just fails if there are any changes, override in child classes to provide stash-ability
  137. if (null !== $this->getLocalChanges($path)) {
  138. throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes.');
  139. }
  140. }
  141. /**
  142. * Guarantee that no changes have been made to the local copy
  143. *
  144. * @param string $path
  145. * @throws \RuntimeException in case the operation must be aborted or the patch does not apply cleanly
  146. */
  147. protected function reapplyChanges($path)
  148. {
  149. }
  150. /**
  151. * Downloads specific package into specific folder.
  152. *
  153. * @param PackageInterface $package package instance
  154. * @param string $path download path
  155. */
  156. abstract protected function doDownload(PackageInterface $package, $path);
  157. /**
  158. * Updates specific package in specific folder from initial to target version.
  159. *
  160. * @param PackageInterface $initial initial package
  161. * @param PackageInterface $target updated package
  162. * @param string $path download path
  163. */
  164. abstract protected function doUpdate(PackageInterface $initial, PackageInterface $target, $path);
  165. /**
  166. * Checks for changes to the local copy
  167. *
  168. * @param string $path package directory
  169. * @return string|null changes or null
  170. */
  171. abstract public function getLocalChanges($path);
  172. /**
  173. * Fetches the commit logs between two commits
  174. *
  175. * @param string $fromReference the source reference
  176. * @param string $toReference the target reference
  177. * @param string $path the package path
  178. * @return string
  179. */
  180. abstract protected function getCommitLogs($fromReference, $toReference, $path);
  181. }