Skip to content

Commit

Permalink
Show outdated packages depends if exist
Browse files Browse the repository at this point in the history
  • Loading branch information
soullivaneuh committed Apr 19, 2016
1 parent d6b3034 commit 4e27927
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 22 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

composer-versions-check is a plugin for Composer.

It warns user for outdated packages from last major versions after update command.
It warns user for outdated packages from last major versions after update command.

[![Latest Stable Version](https://poser.pugx.org/sllh/composer-versions-check/v/stable)](https://packagist.org/packages/sllh/composer-versions-check)
[![Latest Unstable Version](https://poser.pugx.org/sllh/composer-versions-check/v/unstable)](https://packagist.org/packages/sllh/composer-versions-check)
Expand Down Expand Up @@ -43,3 +43,19 @@ composer require sllh/composer-versions-check
That's it! Composer will enable automatically the plugin as soon it's installed.

Just run `composer update` command to see the plugin working.

## Configuration

You can configure the plugin via the [`COMPOSER_HOME/config.json`](https://getcomposer.org/doc/03-cli.md#composer-home) file. Here is the default one:

```json
{
"config": {
"sllh-composer-versions-check": {
"show-links": true
}
}
}
```

* `show-links`: Shows outdated package links. Set to `false` to get a shorter output.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
},
"require-dev": {
"composer/composer": "^1.0",
"sllh/php-cs-fixer-styleci-bridge": "^2.0"
"sllh/php-cs-fixer-styleci-bridge": "^2.0",
"symfony/phpunit-bridge": "^2.7.4|^3.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
20 changes: 19 additions & 1 deletion src/OutdatedPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SLLH\ComposerVersionsCheck;

use Composer\Package\Link;
use Composer\Package\PackageInterface;

/**
Expand All @@ -19,14 +20,23 @@ final class OutdatedPackage
*/
private $last;

/**
* @var Link[]
*/
private $links = array();

/**
* @param PackageInterface $actual
* @param PackageInterface $last
* @param Link[] $links
*/
public function __construct(PackageInterface $actual, PackageInterface $last)
public function __construct(PackageInterface $actual, PackageInterface $last, array $links = null)
{
$this->actual = $actual;
$this->last = $last;
if (null !== $links) {
$this->links = $links;
}
}

/**
Expand All @@ -44,4 +54,12 @@ public function getLast()
{
return $this->last;
}

/**
* @return Link[]
*/
public function getLinks()
{
return $this->links;
}
}
60 changes: 54 additions & 6 deletions src/VersionsCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SLLH\ComposerVersionsCheck;

use Composer\Package\Link;
use Composer\Package\LinkConstraint\VersionConstraint;
use Composer\Package\PackageInterface;
use Composer\Package\RootPackageInterface;
Expand Down Expand Up @@ -41,36 +42,43 @@ public function checkPackages(ArrayRepository $distRepository, WritableRepositor
;

$higherPackages = $distRepository->findPackages($package->getName(), $versionConstraint);

// Remove not stable packages if unwanted
if (true === $rootPackage->getPreferStable()) {
$higherPackages = array_filter($higherPackages, function (PackageInterface $package) {
return 'stable' === $package->getStability();
});
}

// We got higher packages! Let's push it.
if (count($higherPackages) > 0) {
// PHP 5.3 BC
$that = $this;

// Sort packages by highest version to lowest
usort($higherPackages, function (PackageInterface $p1, PackageInterface $p2) use ($that) {
return $that->versionCompare($p1->getVersion(), '<', $p2->getVersion());
});

// Push actual and last package on outdated array
array_push($this->outdatedPackages, new OutdatedPackage($package, $higherPackages[0]));
array_push($this->outdatedPackages, new OutdatedPackage($package, $higherPackages[0], $this->getPackageDepends($localRepository, $package)));
}
}
}

/**
* @param bool $showDepends
*
* @return string
*/
public function getOutput()
public function getOutput($showDepends = true)
{
$output = array();

if (0 === count($this->outdatedPackages)) {
$output[] = '<info>All packages are up to date.</info>';
} else {
$this->createNotUpToDateOutput($output);
$this->createNotUpToDateOutput($output, $showDepends);
}

return implode(PHP_EOL, $output).PHP_EOL;
Expand Down Expand Up @@ -99,7 +107,37 @@ public function versionCompare($version1, $operator, $version2)
return Comparator::compare($version1, $operator, $version2);
}

private function createNotUpToDateOutput(array &$output)
/**
* @param WritableRepositoryInterface $localRepository
* @param PackageInterface $needle
*
* @return Link[]
*/
private function getPackageDepends(WritableRepositoryInterface $localRepository, PackageInterface $needle)
{
$depends = array();

foreach ($localRepository->getPackages() as $package) {
// Skip root package
if ($package instanceof RootPackageInterface) {
continue;
}

foreach ($package->getRequires() as $link) {
if ($link->getTarget() === $needle->getName() && !in_array($link, $depends, true)) {
$depends[] = $link;
}
}
}

return $depends;
}

/**
* @param array $output
* @param bool $showLinks
*/
private function createNotUpToDateOutput(array &$output, $showLinks = true)
{
$outdatedPackagesCount = count($this->outdatedPackages);
$output[] = sprintf(
Expand All @@ -116,8 +154,18 @@ private function createNotUpToDateOutput(array &$output)
$outdatedPackage->getActual()->getPrettyVersion(),
$outdatedPackage->getLast()->getPrettyVersion()
);
}

$output[] = '';
if (true === $showLinks) {
foreach ($outdatedPackage->getLinks() as $depend) {
$output[] = sprintf(
' Required by <info>%s</info> (<comment>%s</comment>)',
$depend->getSource(),
$depend->getPrettyConstraint()
);
}
}

$output[] = '';
}
}
}
33 changes: 32 additions & 1 deletion src/VersionsCheckPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ final class VersionsCheckPlugin implements PluginInterface, EventSubscriberInter
*/
private $preferLowest;

/**
* @var array
*/
private $options = array();

/**
* {@inheritdoc}
*/
Expand All @@ -46,6 +51,7 @@ public function activate(Composer $composer, IOInterface $io)
$this->composer = $composer;
$this->io = $io;
$this->versionsCheck = new VersionsCheck();
$this->options = $this->resolveOptions();
}

/**
Expand Down Expand Up @@ -84,6 +90,31 @@ public function postUpdate(Event $event)
$this->checkVersions($this->composer->getRepositoryManager(), $this->composer->getPackage());
}

/**
* Tries to get plugin options and resolves them.
*
* @return array
*/
private function resolveOptions()
{
$pluginConfig = $this->composer->getConfig()
? $this->composer->getConfig()->get('sllh-composer-versions-check')
: null
;

$options = array(
'show-links' => true,
);

if (null === $pluginConfig) {
return $options;
}

$options['show-links'] = isset($pluginConfig['show-links']) ? (bool) $pluginConfig['show-links'] : true;

return $options;
}

/**
* @param RepositoryManager $repositoryManager
* @param RootPackageInterface $rootPackage
Expand All @@ -98,6 +129,6 @@ private function checkVersions(RepositoryManager $repositoryManager, RootPackage
);
}

$this->io->write($this->versionsCheck->getOutput(), false);
$this->io->write($this->versionsCheck->getOutput($this->options['show-links']), false);
}
}
8 changes: 7 additions & 1 deletion tests/OutdatedPackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ public function testCreation()
{
$actual = new Package('foo/bar', '1.0.0', 'v1.0.0');
$last = new Package('foo/bar', '2.4.3', 'v2.4.3');
$links = array(
$this->getMock('Composer\Package\PackageInterface'),
$this->getMock('Composer\Package\PackageInterface'),
$this->getMock('Composer\Package\PackageInterface'),
);

$outdatedPackage = new OutdatedPackage($actual, $last);
$outdatedPackage = new OutdatedPackage($actual, $last, $links);

$this->assertSame($actual, $outdatedPackage->getActual());
$this->assertSame($last, $outdatedPackage->getLast());
$this->assertSame($links, $outdatedPackage->getLinks());
}
}
Loading

0 comments on commit 4e27927

Please sign in to comment.