UpdatePackagesCommand.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /*
  3. * This file is part of Packagist.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. * Nils Adermann <naderman@naderman.de>
  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 Packagist\WebBundle\Command;
  12. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  13. use Symfony\Bridge\Doctrine\RegistryInterface;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\HttpKernel\KernelInterface;
  18. use Symfony\Component\Finder\Finder;
  19. use Packagist\WebBundle\Entity\Version;
  20. use Packagist\WebBundle\Entity\Tag;
  21. use Packagist\WebBundle\Entity\Author;
  22. use Packagist\WebBundle\Entity\Requirement;
  23. use Packagist\WebBundle\Repository\Repository\RepositoryInterface;
  24. /**
  25. * @author Jordi Boggiano <j.boggiano@seld.be>
  26. */
  27. class UpdatePackagesCommand extends ContainerAwareCommand
  28. {
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected function configure()
  33. {
  34. $this
  35. ->setName('pkg:update')
  36. ->setDefinition(array(
  37. ))
  38. ->setDescription('Updates packages')
  39. ->setHelp(<<<EOF
  40. EOF
  41. )
  42. ;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function execute(InputInterface $input, OutputInterface $output)
  48. {
  49. $doctrine = $this->getContainer()->get('doctrine');
  50. $logger = $this->getContainer()->get('logger');
  51. $provider = $this->getContainer()->get('packagist.repository_provider');
  52. $packages = $doctrine->getRepository('PackagistWebBundle:Package')->getStalePackages();
  53. foreach ($packages as $package) {
  54. $repository = $provider->getRepository($package->getRepository());
  55. if (!$repository) {
  56. $output->writeln('<error>Unsupported repository: '.$package->getRepository().'</error>');
  57. continue;
  58. }
  59. $output->writeln('Importing '.$repository->getUrl());
  60. try {
  61. foreach ($repository->getTags() as $tag => $identifier) {
  62. if ($repository->hasComposerFile($identifier) && $this->parseVersion($tag)) {
  63. $data = $repository->getComposerInformation($identifier);
  64. // Strip -dev that could have been left over accidentally in a tag
  65. $data['version'] = preg_replace('{-?dev$}i', '', $data['version']);
  66. $this->updateInformation($output, $doctrine, $package, $repository, $identifier, $data);
  67. }
  68. }
  69. foreach ($repository->getBranches() as $branch => $identifier) {
  70. if ($repository->hasComposerFile($identifier) && ($parsed = $this->parseBranch($branch))) {
  71. $data = $repository->getComposerInformation($identifier);
  72. $parsedVersion = $this->parseVersion($data['version']);
  73. // Skip branches that contain a version that's been tagged already
  74. foreach ($package->getVersions() as $existingVersion) {
  75. if ($parsedVersion['version'] === $existingVersion->getVersion() && !$existingVersion->getDevelopment()) {
  76. continue;
  77. }
  78. }
  79. // Force branches to use -dev type releases
  80. $data['version'] = $parsedVersion['version'].'-'.$parsedVersion['type'].'-dev';
  81. $this->updateInformation($output, $doctrine, $package, $repository, $identifier, $data);
  82. }
  83. }
  84. $package->setUpdatedAt(new \DateTime);
  85. $package->setCrawledAt(new \DateTime);
  86. $doctrine->getEntityManager()->flush();
  87. } catch (\Exception $e) {
  88. $output->writeln('<error>Exception: '.$e->getMessage().', skipping package.</error>');
  89. continue;
  90. }
  91. }
  92. }
  93. private function parseBranch($branch)
  94. {
  95. if (in_array($branch, array('master', 'trunk'))) {
  96. return 'master';
  97. }
  98. if (!preg_match('#^v?(\d+)(\.(?:\d+|[x*]))?(\.[x*])?$#i', $branch, $matches)) {
  99. return false;
  100. }
  101. return $matches[1]
  102. .(!empty($matches[2]) ? strtr($matches[2], '*', 'x') : '.x')
  103. .(!empty($matches[3]) ? strtr($matches[3], '*', 'x') : '.x');
  104. }
  105. private function parseVersion($version)
  106. {
  107. if (!preg_match('#^v?(\d+)(\.\d+)?(\.\d+)?-?((?:beta|RC|alpha)\d*)?-?(dev)?$#i', $version, $matches)) {
  108. return false;
  109. }
  110. return array(
  111. 'version' => $matches[1]
  112. .(!empty($matches[2]) ? $matches[2] : '.0')
  113. .(!empty($matches[3]) ? $matches[3] : '.0'),
  114. 'type' => !empty($matches[4]) ? strtolower($matches[4]) : '',
  115. 'dev' => !empty($matches[5]),
  116. );
  117. }
  118. private function updateInformation(OutputInterface $output, RegistryInterface $doctrine, $package, RepositoryInterface $repository, $identifier, array $data)
  119. {
  120. if ($data['name'] !== $package->getName()) {
  121. $output->writeln('<error>Package name seems to have changed for '.$repository->getUrl().'@'.$identifier.', skipping.</error>');
  122. return;
  123. }
  124. $em = $doctrine->getEntityManager();
  125. $version = new Version();
  126. $parsedVersion = $this->parseVersion($data['version']);
  127. $version->setName($data['name']);
  128. $version->setVersion($parsedVersion['version']);
  129. $version->setVersionType($parsedVersion['type']);
  130. $version->setDevelopment($parsedVersion['dev']);
  131. // check if we have that version yet
  132. foreach ($package->getVersions() as $existingVersion) {
  133. if ($existingVersion->equals($version)) {
  134. if ($existingVersion->getDevelopment()) {
  135. $version = $existingVersion;
  136. break;
  137. }
  138. return;
  139. }
  140. }
  141. $em->persist($version);
  142. $version->setDescription($data['description']);
  143. $version->setHomepage($data['homepage']);
  144. $version->setLicense($data['license']);
  145. $version->setPackage($package);
  146. $version->setUpdatedAt(new \DateTime);
  147. $version->setReleasedAt(new \DateTime($data['time']));
  148. $version->setSource(array('type' => $repository->getType(), 'url' => $repository->getUrl()));
  149. $version->setDist($repository->getDist($identifier));
  150. if (isset($data['type'])) {
  151. $version->setType($data['type']);
  152. if ($data['type'] && $data['type'] !== $package->getType()) {
  153. $package->setType($data['type']);
  154. }
  155. }
  156. if (isset($data['extra']) && is_array($data['extra'])) {
  157. $version->setExtra($data['extra']);
  158. }
  159. if (isset($data['keywords'])) {
  160. foreach ($data['keywords'] as $keyword) {
  161. $version->addTags(Tag::getByName($em, $keyword, true));
  162. }
  163. }
  164. if (isset($data['authors'])) {
  165. foreach ($data['authors'] as $authorData) {
  166. $author = null;
  167. // skip authors with no information
  168. if (!isset($authorData['email']) && !isset($authorData['name'])) {
  169. continue;
  170. }
  171. if (isset($authorData['email'])) {
  172. $author = $doctrine->getRepository('PackagistWebBundle:Author')->findOneByEmail($authorData['email']);
  173. }
  174. if (!$author) {
  175. $author = new Author();
  176. $em->persist($author);
  177. }
  178. foreach (array('email', 'name', 'homepage') as $field) {
  179. if (isset($authorData[$field])) {
  180. $author->{'set'.$field}($authorData[$field]);
  181. }
  182. }
  183. $author->setUpdatedAt(new \DateTime);
  184. $version->addAuthors($author);
  185. $author->addVersions($version);
  186. }
  187. }
  188. if (isset($data['require'])) {
  189. foreach ($data['require'] as $requireName => $requireVersion) {
  190. $requirement = new Requirement();
  191. $em->persist($requirement);
  192. $requirement->setPackageName($requireName);
  193. $requirement->setPackageVersion($requireVersion);
  194. $version->addRequirements($requirement);
  195. $requirement->setVersion($version);
  196. }
  197. }
  198. }
  199. }