Skip to content

Commit

Permalink
Change version storage, store branch/tag information for installs, ad…
Browse files Browse the repository at this point in the history
…d multi-license support
  • Loading branch information
Seldaek committed Sep 26, 2011
1 parent e29a1f8 commit 1306d1c
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 75 deletions.
69 changes: 30 additions & 39 deletions src/Packagist/WebBundle/Command/UpdatePackagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
use Packagist\WebBundle\Entity\Tag;
use Packagist\WebBundle\Entity\Author;
use Packagist\WebBundle\Repository\Repository\RepositoryInterface;
use Composer\Package\Version\VersionParser;

/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class UpdatePackagesCommand extends ContainerAwareCommand
{
protected $versionParser;

protected $supportedLinkTypes = array(
'require' => 'RequireLink',
'conflict' => 'ConflictLink',
Expand Down Expand Up @@ -65,6 +68,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$logger = $this->getContainer()->get('logger');
$provider = $this->getContainer()->get('packagist.repository_provider');

$this->versionParser = new VersionParser;

$packages = $doctrine->getRepository('PackagistWebBundle:Package')->getStalePackages();

foreach ($packages as $package) {
Expand All @@ -79,29 +84,32 @@ protected function execute(InputInterface $input, OutputInterface $output)

try {
foreach ($repository->getTags() as $tag => $identifier) {
if ($repository->hasComposerFile($identifier) && $this->parseVersion($tag)) {
if ($repository->hasComposerFile($identifier) && $this->validateTag($tag)) {
$data = $repository->getComposerInformation($identifier);
$data['version_normalized'] = $this->versionParser->normalize($data['version']);
// Strip -dev that could have been left over accidentally in a tag
$data['version'] = preg_replace('{-?dev$}i', '', $data['version']);
$data['version'] = preg_replace('{[.-]?dev$}i', '', $data['version']);
$this->updateInformation($output, $doctrine, $package, $repository, $identifier, $data);
$doctrine->getEntityManager()->flush();
}
}

foreach ($repository->getBranches() as $branch => $identifier) {
if ($repository->hasComposerFile($identifier) && ($parsed = $this->parseBranch($branch))) {
if ($repository->hasComposerFile($identifier) && $this->validateBranch($branch)) {
$data = $repository->getComposerInformation($identifier);
$parsedVersion = $this->parseVersion($data['version']);
$data['version_normalized'] = $this->versionParser->normalize($data['version']);

// Skip branches that contain a version that's been tagged already
foreach ($package->getVersions() as $existingVersion) {
if ($parsedVersion['version'] === $existingVersion->getVersion() && !$existingVersion->getDevelopment()) {
if ($data['version_normalized'] === $existingVersion->getNormalizedVersion() && !$existingVersion->getDevelopment()) {
continue;
}
}

// Force branches to use -dev type releases
$data['version'] = $parsedVersion['version'].'-'.$parsedVersion['type'].'-dev';
// Force branches to use -dev releases
if (!preg_match('{[.-]?dev$}i', $data['version'])) {
$data['version'] .= '-dev';
}

$this->updateInformation($output, $doctrine, $package, $repository, $identifier, $data);
$doctrine->getEntityManager()->flush();
Expand All @@ -118,51 +126,34 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

private function parseBranch($branch)
private function validateBranch($branch)
{
if (in_array($branch, array('master', 'trunk'))) {
return 'master';
return true;
}

if (!preg_match('#^v?(\d+)(\.(?:\d+|[x*]))?(\.[x*])?$#i', $branch, $matches)) {
return false;
}

return $matches[1]
.(!empty($matches[2]) ? strtr($matches[2], '*', 'x') : '.x')
.(!empty($matches[3]) ? strtr($matches[3], '*', 'x') : '.x');
return (Boolean) preg_match('#^v?(\d+)(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?(\.[x*])?$#i', $branch, $matches);
}

private function parseVersion($version)
private function validateTag($version)
{
if (!preg_match('#^v?(\d+)(\.\d+)?(\.\d+)?-?((?:beta|RC|alpha)\d*)?-?(dev)?$#i', $version, $matches)) {
try {
$this->versionParser->normalize($version);
return true;
} catch (\Exception $e) {
return false;
}

return array(
'version' => $matches[1]
.(!empty($matches[2]) ? $matches[2] : '.0')
.(!empty($matches[3]) ? $matches[3] : '.0'),
'type' => !empty($matches[4]) ? strtolower($matches[4]) : '',
'dev' => !empty($matches[5]),
);
}

private function updateInformation(OutputInterface $output, RegistryInterface $doctrine, $package, RepositoryInterface $repository, $identifier, array $data)
{
if (strtolower($data['name']) !== strtolower($package->getName())) {
$output->writeln('<error>Package name seems to have changed for '.$repository->getUrl().'@'.$identifier.', skipping.</error>');
return;
}

$em = $doctrine->getEntityManager();
$version = new Version();

$parsedVersion = $this->parseVersion($data['version']);
$version->setName($data['name']);
$version->setVersion($parsedVersion['version']);
$version->setVersionType($parsedVersion['type']);
$version->setDevelopment($parsedVersion['dev']);
$version->setName($package->getName());
$version->setVersion($data['version']);
$version->setNormalizedVersion($data['version_normalized']);
$version->setDevelopment(substr($data['version'], -4) === '-dev');

// check if we have that version yet
foreach ($package->getVersions() as $existingVersion) {
Expand All @@ -179,12 +170,12 @@ private function updateInformation(OutputInterface $output, RegistryInterface $d

$version->setDescription($data['description']);
$version->setHomepage($data['homepage']);
$version->setLicense($data['license']);
$version->setLicense(is_array($data['license']) ? $data['license'] : array($data['license']));

$version->setPackage($package);
$version->setUpdatedAt(new \DateTime);
$version->setReleasedAt(new \DateTime($data['time']));
$version->setSource(array('type' => $repository->getType(), 'url' => $repository->getUrl()));
$version->setSource($repository->getSource($identifier));
$version->setDist($repository->getDist($identifier));

if (isset($data['type'])) {
Expand Down Expand Up @@ -255,7 +246,7 @@ private function updateInformation(OutputInterface $output, RegistryInterface $d
$link = new $class;
$link->setPackageName($linkPackageName);
$link->setPackageVersion($linkPackageVersion);
$version->{'add'.$linkType}($link);
$version->{'add'.$linkType.'Link'}($link);
$link->setVersion($version);
$em->persist($link);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Packagist/WebBundle/Controller/WebController.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function tagAction($name)

/**
* @Template()
* @Route("/view/{name}", name="view")
* @Route("/view/{name}", name="view", requirements={"name"="[A-Za-z0-9/_-]+"})
*/
public function viewAction($name)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Packagist/WebBundle/Entity/ConflictLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ class ConflictLink extends PackageLink
/**
* @ORM\ManyToOne(targetEntity="Packagist\WebBundle\Entity\Version", inversedBy="conflict")
*/
private $version;
protected $version;
}
5 changes: 5 additions & 0 deletions src/Packagist/WebBundle/Entity/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public function isRepositoryValid(ExecutionContext $context)
$context->addViolation('The package name was not found, your composer.json file must be invalid or missing in your master branch/trunk. Maybe the URL you entered has a typo.', array(), null);
return;
}

if (!preg_match('{^[a-z0-9_-]+/[a-z0-9_-]+$}i', $information['name'])) {
$context->addViolation('The package name '.$information['name'].' is invalid, it should have a vendor name, a forward slash, and a package name, matching <em>[a-z0-9_-]+/[a-z0-9_-]+</em>.', array(), null);
return;
}
}

public function isPackageUnique(ExecutionContext $context)
Expand Down
2 changes: 1 addition & 1 deletion src/Packagist/WebBundle/Entity/ProvideLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ class ProvideLink extends PackageLink
/**
* @ORM\ManyToOne(targetEntity="Packagist\WebBundle\Entity\Version", inversedBy="provide")
*/
private $version;
protected $version;
}
2 changes: 1 addition & 1 deletion src/Packagist/WebBundle/Entity/RecommendLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ class RecommendLink extends PackageLink
/**
* @ORM\ManyToOne(targetEntity="Packagist\WebBundle\Entity\Version", inversedBy="recommend")
*/
private $version;
protected $version;
}
2 changes: 1 addition & 1 deletion src/Packagist/WebBundle/Entity/ReplaceLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ class ReplaceLink extends PackageLink
/**
* @ORM\ManyToOne(targetEntity="Packagist\WebBundle\Entity\Version", inversedBy="replace")
*/
private $version;
protected $version;
}
2 changes: 1 addition & 1 deletion src/Packagist/WebBundle/Entity/RequireLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ class RequireLink extends PackageLink
/**
* @ORM\ManyToOne(targetEntity="Packagist\WebBundle\Entity\Version", inversedBy="require")
*/
private $version;
protected $version;
}
2 changes: 1 addition & 1 deletion src/Packagist/WebBundle/Entity/SuggestLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ class SuggestLink extends PackageLink
/**
* @ORM\ManyToOne(targetEntity="Packagist\WebBundle\Entity\Version", inversedBy="suggest")
*/
private $version;
protected $version;
}
77 changes: 61 additions & 16 deletions src/Packagist/WebBundle/Entity/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,20 @@ class Version
*/
private $version;

/**
* @ORM\Column
* @Assert\NotBlank()
*/
private $normalizedVersion;

/**
* @ORM\Column(type="boolean")
* @Assert\NotBlank()
*/
private $development;

/**
* @ORM\Column(nullable="true")
* @ORM\Column(type="text", nullable="true")
*/
private $license;

Expand Down Expand Up @@ -159,7 +165,12 @@ class Version
public function __construct()
{
$this->tags = new \Doctrine\Common\Collections\ArrayCollection();
$this->requirements = new \Doctrine\Common\Collections\ArrayCollection();
$this->require = new \Doctrine\Common\Collections\ArrayCollection();
$this->replace = new \Doctrine\Common\Collections\ArrayCollection();
$this->conflict = new \Doctrine\Common\Collections\ArrayCollection();
$this->provide = new \Doctrine\Common\Collections\ArrayCollection();
$this->recommend = new \Doctrine\Common\Collections\ArrayCollection();
$this->suggest = new \Doctrine\Common\Collections\ArrayCollection();
$this->authors = new \Doctrine\Common\Collections\ArrayCollection();
$this->createdAt = new \DateTime;
$this->updatedAt = new \DateTime;
Expand All @@ -175,31 +186,45 @@ public function toArray()
foreach ($this->getAuthors() as $author) {
$authors[] = $author->toArray();
}
$requirements = array();
foreach ($this->getRequirements() as $requirement) {
$requirement = $requirement->toArray();
$requirements[key($requirement)] = current($requirement);
}
return array(

$data = array(
'name' => $this->name,
'description' => $this->description,
'keywords' => $tags,
'homepage' => $this->homepage,
'version' => $this->version,
'license' => $this->license,
'license' => $this->getLicense(),
'authors' => $authors,
'require' => $requirements,
'source' => $this->getSource(),
'time' => $this->releasedAt ? $this->releasedAt->format('Y-m-d\TH:i:sP') : null,
'dist' => $this->getDist(),
'type' => $this->type,
'extra' => $this->extra,
);

$supportedLinkTypes = array(
'require',
'conflict',
'provide',
'replace',
'recommend',
'suggest',
);

foreach ($supportedLinkTypes as $linkType) {
foreach ($this->{'get'.$linkType}() as $link) {
$link = $link->toArray();
$data[$linkType][key($link)] = current($link);
}
}

return $data;
}

public function equals(Version $version)
{
return $version->getName() === $this->getName() && $version->getVersion() === $this->getVersion();
return strtolower($version->getName()) === strtolower($this->getName())
&& $version->getNormalizedVersion() === $this->getNormalizedVersion();
}

/**
Expand Down Expand Up @@ -279,7 +304,7 @@ public function getHomepage()
*/
public function setVersion($version)
{
$this->version = ltrim($version, 'vV.');
$this->version = $version;
}

/**
Expand All @@ -292,24 +317,44 @@ public function getVersion()
return $this->version;
}

/**
* Set normalizedVersion
*
* @param string $normalizedVersion
*/
public function setNormalizedVersion($normalizedVersion)
{
$this->normalizedVersion = $normalizedVersion;
}

/**
* Get normalizedVersion
*
* @return string $normalizedVersion
*/
public function getNormalizedVersion()
{
return $this->normalizedVersion;
}

/**
* Set license
*
* @param string $license
*/
public function setLicense($license)
public function setLicense(array $license)
{
$this->license = $license;
$this->license = json_encode($license);
}

/**
* Get license
*
* @return string $license
* @return array $license
*/
public function getLicense()
{
return $this->license;
return json_decode($this->license, true);
}

/**
Expand Down
Loading

0 comments on commit 1306d1c

Please sign in to comment.