composer / semver

Semantic versioning utilities with the addition of version constraints parsing and checking.
MIT License
3.15k stars 76 forks source link

Comparing versions with ~ #41

Closed reedy closed 8 years ago

reedy commented 8 years ago

Bit of a support request here.

In MediaWiki, we're currently doing if version a !== version b, complain. Where one version is the one defined in composer.json file, and the other is the version of the dependency.

The downstream issue I filed is https://phabricator.wikimedia.org/T141225

The problem is that 1.6.1 does not equal ~1.6

I thought there'd be something we could use in the semver library, but I feel I'm coming up short, without looking for special characters, and doing different sorts of comparisons. What I hoped might work, would be something like the below, swapping !== for ! equalsTo, but it doesn't.

diff --git a/maintenance/checkComposerLockUpToDate.php b/maintenance/checkComposerLockUpToDate.php
index 9ec61dc..6d1ad44 100644
--- a/maintenance/checkComposerLockUpToDate.php
+++ b/maintenance/checkComposerLockUpToDate.php
@@ -2,6 +2,8 @@

 require_once __DIR__ . '/Maintenance.php';

+use Composer\Semver\Comparator;
+
 /**
  * Checks whether your composer-installed dependencies are up to date
  *
@@ -43,7 +45,7 @@ class CheckComposerLockUpToDate extends Maintenance {
                $installed = $lock->getInstalledDependencies();
                foreach ( $json->getRequiredDependencies() as $name => $version ) {
                        if ( isset( $installed[$name] ) ) {
-                               if ( $installed[$name]['version'] !== $version ) {
+                               if ( !Comparator::equalTo( $installed[$name]['version'], $version ) ) {
                                        $this->output(
                                                "$name: {$installed[$name]['version']} installed, $version required.\n"
                                        );

Am I just being stupid, and as such, is there a way to get it to do what I want? Basically, a version comparison, where one version may or may not have next significant release operators etc

Thanks in advance!

alcohol commented 8 years ago

I think you will want to use Composer\Semver\Semver::satisfies().

The Comparator class does not resolve version constraints, it uses raw versions only.

reedy commented 8 years ago

Looks to do the job to me. Thanks! 👍

> var_dump( Composer\Semver\Semver::satisfies( '1.6.1', '~1.6' ) );
/var/www/wiki/mediawiki/core/maintenance/eval.php(78) : eval()'d code:1:
bool(true)

> var_dump( Composer\Semver\Semver::satisfies( '1.7.1', '~1.6' ) );
/var/www/wiki/mediawiki/core/maintenance/eval.php(78) : eval()'d code:1:
bool(true)

> var_dump( Composer\Semver\Semver::satisfies( '1.5.1', '~1.6' ) );
/var/www/wiki/mediawiki/core/maintenance/eval.php(78) : eval()'d code:1:
bool(false)
stof commented 8 years ago

The point to understand is that ~1.6 is not a version. It is a version constraint.