Jump to content

Manual talk:CheckComposerLockUpToDate.php

About this board

Iowajason (talkcontribs)

The version comparison in this script is a basic "not equals" comparison. The Composer version syntax is significantly more complicated. Is this by design (any design deliberation discussion URL) or just a "haven't got to it yet" situation?

Iowajason (talkcontribs)

My install is updated with the following code, which allows some more flexibility. Works for my install, but no test cases scripted.

<?php

require_once __DIR__ . '/Maintenance.php';

/**

* Checks whether your composer-installed dependencies are up to date

*

* Composer creates a "composer.lock" file which specifies which versions are installed

* (via `composer install`). It has a hash, which can be compared to the value of

* the composer.json file to see if dependencies are up to date.

*/

class CheckComposerLockUpToDate extends Maintenance {

public function __construct() {

parent::__construct();

$this->addDescription(

'Checks whether your composer.lock file is up to date with the current composer.json' );

}

public function execute() {

global $IP;

$lockLocation = "$IP/composer.lock";

$jsonLocation = "$IP/composer.json";

if ( !file_exists( $lockLocation ) ) {

// Maybe they're using mediawiki/vendor?

$lockLocation = "$IP/vendor/composer.lock";

if ( !file_exists( $lockLocation ) ) {

$this->error(

'Could not find composer.lock file. Have you run "composer install"?',

1

);

}

}

$lock = new ComposerLock( $lockLocation );

$json = new ComposerJson( $jsonLocation );

// Check all the dependencies to see if any are old

$found = false;

$installed = $lock->getInstalledDependencies();

foreach ( $json->getRequiredDependencies() as $name => $version ) {

if ( isset( $installed[$name] ) ) {

/*     if ( $installed[$name]['version'] !== $version ) {

$this->output(

"$name: {$installed[$name]['version']} installed, $version required.\n"

);

$found = true;

}*/

if (!($this->installedVersionMeetsRequirement($version , trim($installed[$name]['version'])))) {

$this->output(

"$name: {$installed[$name]['version']} installed, $version required.\n"

);

$found = true;

}

} else {

$this->output( "$name: not installed, $version required.\n" );

$found = true;

}

}

if ( $found ) {

$this->error(

'Error: your composer.lock file is not up to date. ' .

'Run "composer update" to install newer dependencies',

1

);

} else {

// We couldn't find any out-of-date dependencies, so assume everything is ok!

$this->output( "Your composer.lock file is up to date with current dependencies!\n" );

}

}

protected function installedVersionMeetsRequirement($requirement, $installed) {

//$this->output("Version comparing requirement:".$requirement." to installed:".$installed."\n");

if ($installed == $requirement) return true; //Simplest case, the exact case

$andPos = strpos($requirement, "||");

if ($andPos !== false) { //check for and position and leverage recursive nature of this function

$req1 = trim(substr($requirement, 0,$andPos));

$req2 = trim(substr($requirement,$andPos+2));

return ($this->composerVersionMeetsRequirement($req1, $installed) && $this->composerVersionMeetsRequirement($req2, $installed));

}

//$this->output("past and pos\n");

$spacePos = strpos($requirement, " ");

if ($spacePos !== false) { //check for and position and leverage recursive nature of this function

$req1 = trim(substr($requirement, 0,$spacePos));

$req2 = trim(substr($requirement,$spacePos+1));

return ($this->composerVersionMeetsRequirement($req1, $installed) || $this->composerVersionMeetsRequirement($req2, $installed));

}

//$this->output("past space pos\n");

$commaPos = strpos($requirement, ",");

if ($commaPos !== false) { //check for and position and leverage recursive nature of this function

$req1 = trim(substr($requirement, 0,$commaPos));

$req2 = trim(substr($requirement,$commaPos+1));

return ($this->composerVersionMeetsRequirement($req1, $installed) || $this->composerVersionMeetsRequirement($req2, $installed));

}

//$this->output("past commas pos\n");

$isTilde = substr($requirement, 0,1) == "~";

if ($isTilde) {

//$this->output("Version tilde comparing requirement:".$curReq." to installed:".$curInst."\n");

$reqVersion = substr($requirement, 1);

$reqVersion = explode(".", $reqVersion);

$reqDepth = count($reqVersion);

$instVersion = explode(".", $installed);

$allowed = true;

for ($i = 0;$i<$reqDepth;$i++){

//$this->output("Version tilde comparing requirement:".$curReq." to installed:".$curInst."\n");

$curReq = $reqVersion[$i];

$curInst = $instVersion[$i];

}

}

//$this->output("past tilde pos\n");

$isCaret = substr($requirement, 0,1) == "^";

if ($isCaret) {

//$this->output("Version caret comparing requirement:".print_r($requirement,true)." to installed:".print_r($installed,true)."\n");

$reqVersion = substr($requirement, 1);

$reqVersion = explode(".", $reqVersion);

$reqDepth = count($reqVersion);

$instVersion = explode(".", $installed);

$allowed = true;

//$this->output("Version caret comparing requirement:".print_r($reqVersion,true)." to installed:".print_r($instVersion,true)."\n");

for ($i = 0;$i<$reqDepth;$i++){

$curReq = $reqVersion[$i];

$curInst = $instVersion[$i];

//$this->output("Version caret detail comparing requirement:".$curReq." to installed:".$curInst."\n");

if ($curReq != $curInst) return false;

}

return true;

}

//$this->output("past caret pos\n");

$isWildcard = (strpos($requirement,"*") > -1);

if ($isWildcard) {

//$this->output("Version wildcard comparing requirement:".print_r($requirement,true)." to installed:".print_r($installed,true)."\n");

$reqVersion = substr($requirement, 0);

$reqVersion = explode(".", $reqVersion);

$reqDepth = count($reqVersion);

$instVersion = explode(".", $installed);

$allowed = true;

//$this->output("Version wildcard comparing requirement:".print_r($reqVersion,true)." to installed:".print_r($instVersion,true)."\n");

for ($i = 0;$i<$reqDepth;$i++){

$curReq = $reqVersion[$i];

$curInst = $instVersion[$i];

//$this->output("Version wildcard detail comparing requirement:".$curReq." to installed:".$curInst."\n");

if ($curReq =="*") return true;

if ($curReq != $curInst) return false;

}

return true;

}

return false;

}

}

$maintClass = 'CheckComposerLockUpToDate';

require_once RUN_MAINTENANCE_IF_MAIN;

Reply to "Version comparison"
There are no older topics