1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
<?php
/*
* This file is part of Packagist.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
* Nils Adermann <naderman@naderman.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Packagist\WebBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Finder\Finder;
use Packagist\WebBundle\Entity\Version;
use Packagist\WebBundle\Entity\Tag;
use Packagist\WebBundle\Entity\Author;
use Packagist\WebBundle\Entity\Requirement;
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class UpdatePackagesCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('pkg:update')
->setDefinition(array(
))
->setDescription('Updates packages')
->setHelp(<<<EOF
EOF
)
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$doctrine = $this->getContainer()->get('doctrine');
$logger = $this->getContainer()->get('logger');
$provider = $this->getContainer()->get('packagist.repository_provider');
$packages = $doctrine->getRepository('PackagistWebBundle:Package')->getStalePackages();
foreach ($packages as $package) {
$repository = $provider->getRepository($package->getRepository());
if (!$repository) {
$output->writeln('<error>Unsupported repository: '.$package->getRepository().'</error>');
continue;
}
$output->writeln('Importing '.$repository->getUrl());
try {
foreach ($repository->getTags() as $tag => $identifier) {
// TODO parse tag name (or fetch composer file?) w/ composer version parser, if no match, ignore the tag
$this->fetchInformation($output, $doctrine, $package, $repository, $identifier);
}
foreach ($repository->getBranches() as $branch => $identifier) {
// TODO parse branch name, matching a "$num.x.x" version scheme, + the master one
// use for all "x.y.z-dev" versions, usable through "latest-dev"
$this->fetchInformation($output, $doctrine, $package, $repository, $identifier);
}
$package->setUpdatedAt(new \DateTime);
$package->setCrawledAt(new \DateTime);
$doctrine->getEntityManager()->flush();
} catch (\Exception $e) {
$output->writeln('<error>Exception: '.$e->getMessage().', skipping package.</error>');
continue;
}
}
}
protected function fetchInformation(OutputInterface $output, RegistryInterface $doctrine, $package, $repository, $identifier)
{
$data = $repository->getComposerInformation($identifier);
$em = $doctrine->getEntityManager();
// check if we have that version yet
foreach ($package->getVersions() as $version) {
if ($version->getVersion() === $data['version']) {
return;
}
}
if ($data['name'] !== $package->getName()) {
$output->writeln('<error>Package name seems to have changed for '.$repository->getUrl().'@'.$identifier.', skipping.</error>');
return;
}
$version = new Version();
$em->persist($version);
foreach (array('name', 'description', 'homepage', 'license', 'version') as $field) {
if (isset($data[$field])) {
$version->{'set'.$field}($data[$field]);
}
}
$version->setPackage($package);
$version->setUpdatedAt(new \DateTime);
$version->setReleasedAt(new \DateTime($data['time']));
$version->setSource(array('type' => $repository->getType(), 'url' => $repository->getUrl()));
$version->setDist($repository->getDist($identifier));
if (isset($data['keywords'])) {
foreach ($data['keywords'] as $keyword) {
$version->addTags(Tag::getByName($em, $keyword, true));
}
}
if (isset($data['authors'])) {
foreach ($data['authors'] as $authorData) {
$author = null;
// skip authors with no information
if (!isset($authorData['email']) && !isset($authorData['name'])) {
continue;
}
if (isset($authorData['email'])) {
$author = $doctrine->getRepository('PackagistWebBundle:Author')->findOneByEmail($authorData['email']);
}
if (!$author) {
$author = new Author();
$em->persist($author);
}
foreach (array('email', 'name', 'homepage') as $field) {
if (isset($authorData[$field])) {
$author->{'set'.$field}($authorData[$field]);
}
}
$author->setUpdatedAt(new \DateTime);
$version->addAuthors($author);
$author->addVersions($version);
}
}
if (isset($data['require'])) {
foreach ($data['require'] as $requireName => $requireVersion) {
$requirement = new Requirement();
$em->persist($requirement);
$requirement->setPackageName($requireName);
$requirement->setPackageVersion($requireVersion);
$version->addRequirements($requirement);
$requirement->setVersion($version);
}
}
}
}
|