VcsDownloader.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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\Dumper\ArrayDumper;
  14. use Composer\Package\PackageInterface;
  15. use Composer\Package\Version\VersionGuesser;
  16. use Composer\Package\Version\VersionParser;
  17. use Composer\Util\ProcessExecutor;
  18. use Composer\IO\IOInterface;
  19. use Composer\Util\Filesystem;
  20. /**
  21. * @author Jordi Boggiano <j.boggiano@seld.be>
  22. */
  23. abstract class VcsDownloader implements DownloaderInterface, ChangeReportInterface, VcsCapableDownloaderInterface
  24. {
  25. /** @var IOInterface */
  26. protected $io;
  27. /** @var Config */
  28. protected $config;
  29. /** @var ProcessExecutor */
  30. protected $process;
  31. /** @var Filesystem */
  32. protected $filesystem;
  33. public function __construct(IOInterface $io, Config $config, ProcessExecutor $process = null, Filesystem $fs = null)
  34. {
  35. $this->io = $io;
  36. $this->config = $config;
  37. $this->process = $process ?: new ProcessExecutor($io);
  38. $this->filesystem = $fs ?: new Filesystem($this->process);
  39. }
  40. /**
  41. * {@inheritDoc}
  42. */
  43. public function getInstallationSource()
  44. {
  45. return 'source';
  46. }
  47. /**
  48. * {@inheritDoc}
  49. */
  50. public function download(PackageInterface $package, $path)
  51. {
  52. // noop for now, ideally we would do a git fetch already here, or make sure the cached git repo is synced, etc.
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function install(PackageInterface $package, $path)
  58. {
  59. if (!$package->getSourceReference()) {
  60. throw new \InvalidArgumentException('Package '.$package->getPrettyName().' is missing reference information');
  61. }
  62. $this->io->writeError(" - Installing <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>): ", false);
  63. $this->filesystem->emptyDirectory($path);
  64. $urls = $package->getSourceUrls();
  65. while ($url = array_shift($urls)) {
  66. try {
  67. if (Filesystem::isLocalPath($url)) {
  68. // realpath() below will not understand
  69. // url that starts with "file://"
  70. $needle = 'file://';
  71. $isFileProtocol = false;
  72. if (0 === strpos($url, $needle)) {
  73. $url = substr($url, strlen($needle));
  74. $isFileProtocol = true;
  75. }
  76. // realpath() below will not understand %20 spaces etc.
  77. if (false !== strpos($url, '%')) {
  78. $url = rawurldecode($url);
  79. }
  80. $url = realpath($url);
  81. if ($isFileProtocol) {
  82. $url = $needle . $url;
  83. }
  84. }
  85. $this->doInstall($package, $path, $url);
  86. break;
  87. } catch (\Exception $e) {
  88. // rethrow phpunit exceptions to avoid hard to debug bug failures
  89. if ($e instanceof \PHPUnit_Framework_Exception) {
  90. throw $e;
  91. }
  92. if ($this->io->isDebug()) {
  93. $this->io->writeError('Failed: ['.get_class($e).'] '.$e->getMessage());
  94. } elseif (count($urls)) {
  95. $this->io->writeError(' Failed, trying the next URL');
  96. }
  97. if (!count($urls)) {
  98. throw $e;
  99. }
  100. }
  101. }
  102. }
  103. /**
  104. * {@inheritDoc}
  105. */
  106. public function update(PackageInterface $initial, PackageInterface $target, $path)
  107. {
  108. if (!$target->getSourceReference()) {
  109. throw new \InvalidArgumentException('Package '.$target->getPrettyName().' is missing reference information');
  110. }
  111. $name = $target->getName();
  112. if ($initial->getPrettyVersion() == $target->getPrettyVersion()) {
  113. if ($target->getSourceType() === 'svn') {
  114. $from = $initial->getSourceReference();
  115. $to = $target->getSourceReference();
  116. } else {
  117. $from = substr($initial->getSourceReference(), 0, 7);
  118. $to = substr($target->getSourceReference(), 0, 7);
  119. }
  120. $name .= ' '.$initial->getPrettyVersion();
  121. } else {
  122. $from = $initial->getFullPrettyVersion();
  123. $to = $target->getFullPrettyVersion();
  124. }
  125. $actionName = VersionParser::isUpgrade($initial->getVersion(), $target->getVersion()) ? 'Updating' : 'Downgrading';
  126. $this->io->writeError(" - " . $actionName . " <info>" . $name . "</info> (<comment>" . $from . "</comment> => <comment>" . $to . "</comment>): ", false);
  127. $this->cleanChanges($initial, $path, true);
  128. $urls = $target->getSourceUrls();
  129. $exception = null;
  130. while ($url = array_shift($urls)) {
  131. try {
  132. if (Filesystem::isLocalPath($url)) {
  133. $url = realpath($url);
  134. }
  135. $this->doUpdate($initial, $target, $path, $url);
  136. $exception = null;
  137. break;
  138. } catch (\Exception $exception) {
  139. // rethrow phpunit exceptions to avoid hard to debug bug failures
  140. if ($exception instanceof \PHPUnit_Framework_Exception) {
  141. throw $exception;
  142. }
  143. if ($this->io->isDebug()) {
  144. $this->io->writeError('Failed: ['.get_class($exception).'] '.$exception->getMessage());
  145. } elseif (count($urls)) {
  146. $this->io->writeError(' Failed, trying the next URL');
  147. }
  148. }
  149. }
  150. $this->reapplyChanges($path);
  151. // print the commit logs if in verbose mode and VCS metadata is present
  152. // because in case of missing metadata code would trigger another exception
  153. if (!$exception && $this->io->isVerbose() && $this->hasMetadataRepository($path)) {
  154. $message = 'Pulling in changes:';
  155. $logs = $this->getCommitLogs($initial->getSourceReference(), $target->getSourceReference(), $path);
  156. if (!trim($logs)) {
  157. $message = 'Rolling back changes:';
  158. $logs = $this->getCommitLogs($target->getSourceReference(), $initial->getSourceReference(), $path);
  159. }
  160. if (trim($logs)) {
  161. $logs = implode("\n", array_map(function ($line) {
  162. return ' ' . $line;
  163. }, explode("\n", $logs)));
  164. // escape angle brackets for proper output in the console
  165. $logs = str_replace('<', '\<', $logs);
  166. $this->io->writeError(' '.$message);
  167. $this->io->writeError($logs);
  168. }
  169. }
  170. if (!$urls && $exception) {
  171. throw $exception;
  172. }
  173. }
  174. /**
  175. * {@inheritDoc}
  176. */
  177. public function remove(PackageInterface $package, $path)
  178. {
  179. $this->io->writeError(" - Removing <info>" . $package->getName() . "</info> (<comment>" . $package->getPrettyVersion() . "</comment>)");
  180. $this->cleanChanges($package, $path, false);
  181. if (!$this->filesystem->removeDirectory($path)) {
  182. throw new \RuntimeException('Could not completely delete '.$path.', aborting.');
  183. }
  184. }
  185. /**
  186. * Download progress information is not available for all VCS downloaders.
  187. * {@inheritDoc}
  188. */
  189. public function setOutputProgress($outputProgress)
  190. {
  191. return $this;
  192. }
  193. /**
  194. * {@inheritDoc}
  195. */
  196. public function getVcsReference(PackageInterface $package, $path)
  197. {
  198. $parser = new VersionParser;
  199. $guesser = new VersionGuesser($this->config, $this->process, $parser);
  200. $dumper = new ArrayDumper;
  201. $packageConfig = $dumper->dump($package);
  202. if ($packageVersion = $guesser->guessVersion($packageConfig, $path)) {
  203. return $packageVersion['commit'];
  204. }
  205. }
  206. /**
  207. * Prompt the user to check if changes should be stashed/removed or the operation aborted
  208. *
  209. * @param PackageInterface $package
  210. * @param string $path
  211. * @param bool $update if true (update) the changes can be stashed and reapplied after an update,
  212. * if false (remove) the changes should be assumed to be lost if the operation is not aborted
  213. * @throws \RuntimeException in case the operation must be aborted
  214. */
  215. protected function cleanChanges(PackageInterface $package, $path, $update)
  216. {
  217. // the default implementation just fails if there are any changes, override in child classes to provide stash-ability
  218. if (null !== $this->getLocalChanges($package, $path)) {
  219. throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes.');
  220. }
  221. }
  222. /**
  223. * Guarantee that no changes have been made to the local copy
  224. *
  225. * @param string $path
  226. * @throws \RuntimeException in case the operation must be aborted or the patch does not apply cleanly
  227. */
  228. protected function reapplyChanges($path)
  229. {
  230. }
  231. /**
  232. * Downloads specific package into specific folder.
  233. *
  234. * @param PackageInterface $package package instance
  235. * @param string $path download path
  236. * @param string $url package url
  237. */
  238. abstract protected function doInstall(PackageInterface $package, $path, $url);
  239. /**
  240. * Updates specific package in specific folder from initial to target version.
  241. *
  242. * @param PackageInterface $initial initial package
  243. * @param PackageInterface $target updated package
  244. * @param string $path download path
  245. * @param string $url package url
  246. */
  247. abstract protected function doUpdate(PackageInterface $initial, PackageInterface $target, $path, $url);
  248. /**
  249. * Fetches the commit logs between two commits
  250. *
  251. * @param string $fromReference the source reference
  252. * @param string $toReference the target reference
  253. * @param string $path the package path
  254. * @return string
  255. */
  256. abstract protected function getCommitLogs($fromReference, $toReference, $path);
  257. /**
  258. * Checks if VCS metadata repository has been initialized
  259. * repository example: .git|.svn|.hg
  260. *
  261. * @param string $path
  262. * @return bool
  263. */
  264. abstract protected function hasMetadataRepository($path);
  265. }