VcsDownloader.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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\Util\ProcessExecutor;
  15. use Composer\IO\IOInterface;
  16. use Composer\Util\Filesystem;
  17. /**
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. abstract class VcsDownloader implements DownloaderInterface, ChangeReportInterface
  21. {
  22. /** @var IOInterface */
  23. protected $io;
  24. /** @var Config */
  25. protected $config;
  26. /** @var ProcessExecutor */
  27. protected $process;
  28. /** @var Filesystem */
  29. protected $filesystem;
  30. public function __construct(IOInterface $io, Config $config, ProcessExecutor $process = null, Filesystem $fs = null)
  31. {
  32. $this->io = $io;
  33. $this->config = $config;
  34. $this->process = $process ?: new ProcessExecutor($io);
  35. $this->filesystem = $fs ?: new Filesystem;
  36. }
  37. /**
  38. * {@inheritDoc}
  39. */
  40. public function getInstallationSource()
  41. {
  42. return 'source';
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. public function download(PackageInterface $package, $path)
  48. {
  49. if (!$package->getSourceReference()) {
  50. throw new \InvalidArgumentException('Package '.$package->getPrettyName().' is missing reference information');
  51. }
  52. $this->io->writeError(" - Installing <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>)");
  53. $this->filesystem->emptyDirectory($path);
  54. $urls = $package->getSourceUrls();
  55. while ($url = array_shift($urls)) {
  56. try {
  57. if (Filesystem::isLocalPath($url)) {
  58. $url = realpath($url);
  59. }
  60. $this->doDownload($package, $path, $url);
  61. break;
  62. } catch (\Exception $e) {
  63. if ($this->io->isDebug()) {
  64. $this->io->writeError('Failed: ['.get_class($e).'] '.$e->getMessage());
  65. } elseif (count($urls)) {
  66. $this->io->writeError(' Failed, trying the next URL');
  67. }
  68. if (!count($urls)) {
  69. throw $e;
  70. }
  71. }
  72. }
  73. $this->io->writeError('');
  74. }
  75. /**
  76. * {@inheritDoc}
  77. */
  78. public function update(PackageInterface $initial, PackageInterface $target, $path)
  79. {
  80. if (!$target->getSourceReference()) {
  81. throw new \InvalidArgumentException('Package '.$target->getPrettyName().' is missing reference information');
  82. }
  83. $name = $target->getName();
  84. if ($initial->getPrettyVersion() == $target->getPrettyVersion()) {
  85. if ($target->getSourceType() === 'svn') {
  86. $from = $initial->getSourceReference();
  87. $to = $target->getSourceReference();
  88. } else {
  89. $from = substr($initial->getSourceReference(), 0, 7);
  90. $to = substr($target->getSourceReference(), 0, 7);
  91. }
  92. $name .= ' '.$initial->getPrettyVersion();
  93. } else {
  94. $from = $initial->getFullPrettyVersion();
  95. $to = $target->getFullPrettyVersion();
  96. }
  97. $this->io->writeError(" - Updating <info>" . $name . "</info> (<comment>" . $from . "</comment> => <comment>" . $to . "</comment>)");
  98. $this->cleanChanges($initial, $path, true);
  99. $urls = $target->getSourceUrls();
  100. while ($url = array_shift($urls)) {
  101. try {
  102. if (Filesystem::isLocalPath($url)) {
  103. $url = realpath($url);
  104. }
  105. $this->doUpdate($initial, $target, $path, $url);
  106. break;
  107. } catch (\Exception $e) {
  108. if ($this->io->isDebug()) {
  109. $this->io->writeError('Failed: ['.get_class($e).'] '.$e->getMessage());
  110. } elseif (count($urls)) {
  111. $this->io->writeError(' Failed, trying the next URL');
  112. } else {
  113. // in case of failed update, try to reapply the changes before aborting
  114. $this->reapplyChanges($path);
  115. throw $e;
  116. }
  117. }
  118. }
  119. $this->reapplyChanges($path);
  120. // print the commit logs if in verbose mode
  121. if ($this->io->isVerbose()) {
  122. $message = 'Pulling in changes:';
  123. $logs = $this->getCommitLogs($initial->getSourceReference(), $target->getSourceReference(), $path);
  124. if (!trim($logs)) {
  125. $message = 'Rolling back changes:';
  126. $logs = $this->getCommitLogs($target->getSourceReference(), $initial->getSourceReference(), $path);
  127. }
  128. if (trim($logs)) {
  129. $logs = implode("\n", array_map(function ($line) {
  130. return ' ' . $line;
  131. }, explode("\n", $logs)));
  132. // escape angle brackets for proper output in the console
  133. $logs = str_replace('<', '\<', $logs);
  134. $this->io->writeError(' '.$message);
  135. $this->io->writeError($logs);
  136. }
  137. }
  138. $this->io->writeError('');
  139. }
  140. /**
  141. * {@inheritDoc}
  142. */
  143. public function remove(PackageInterface $package, $path)
  144. {
  145. $this->io->writeError(" - Removing <info>" . $package->getName() . "</info> (<comment>" . $package->getPrettyVersion() . "</comment>)");
  146. $this->cleanChanges($package, $path, false);
  147. if (!$this->filesystem->removeDirectory($path)) {
  148. throw new \RuntimeException('Could not completely delete '.$path.', aborting.');
  149. }
  150. }
  151. /**
  152. * Download progress information is not available for all VCS downloaders.
  153. * {@inheritDoc}
  154. */
  155. public function setOutputProgress($outputProgress)
  156. {
  157. return $this;
  158. }
  159. /**
  160. * Prompt the user to check if changes should be stashed/removed or the operation aborted
  161. *
  162. * @param PackageInterface $package
  163. * @param string $path
  164. * @param bool $update if true (update) the changes can be stashed and reapplied after an update,
  165. * if false (remove) the changes should be assumed to be lost if the operation is not aborted
  166. * @throws \RuntimeException in case the operation must be aborted
  167. */
  168. protected function cleanChanges(PackageInterface $package, $path, $update)
  169. {
  170. // the default implementation just fails if there are any changes, override in child classes to provide stash-ability
  171. if (null !== $this->getLocalChanges($package, $path)) {
  172. throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes.');
  173. }
  174. }
  175. /**
  176. * Guarantee that no changes have been made to the local copy
  177. *
  178. * @param string $path
  179. * @throws \RuntimeException in case the operation must be aborted or the patch does not apply cleanly
  180. */
  181. protected function reapplyChanges($path)
  182. {
  183. }
  184. /**
  185. * Downloads specific package into specific folder.
  186. *
  187. * @param PackageInterface $package package instance
  188. * @param string $path download path
  189. * @param string $url package url
  190. */
  191. abstract protected function doDownload(PackageInterface $package, $path, $url);
  192. /**
  193. * Updates specific package in specific folder from initial to target version.
  194. *
  195. * @param PackageInterface $initial initial package
  196. * @param PackageInterface $target updated package
  197. * @param string $path download path
  198. * @param string $url package url
  199. */
  200. abstract protected function doUpdate(PackageInterface $initial, PackageInterface $target, $path, $url);
  201. /**
  202. * Fetches the commit logs between two commits
  203. *
  204. * @param string $fromReference the source reference
  205. * @param string $toReference the target reference
  206. * @param string $path the package path
  207. * @return string
  208. */
  209. abstract protected function getCommitLogs($fromReference, $toReference, $path);
  210. }