Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
:xAdded code to modify Composer JSON and keep it up to date with MW c…
…onfig

Change-Id: I8df66a92971146ab79cd4fcbd181e559115ca240
  • Loading branch information
JeroenDeDauw committed Jan 2, 2014
1 parent 6489222 commit f223335
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 0 deletions.
10 changes: 10 additions & 0 deletions composer-example.json
Expand Up @@ -7,5 +7,15 @@
"ext-mbstring": "*",
"ext-wikidiff2": "*",
"ext-apc": "*"
},
"autoload": {
"psr-0": {
"ComposerHookHandler": "includes/",
"ComposerPackageModifier": "includes/"
}
},
"scripts": {
"pre-update-cmd": "ComposerHookHandler::onPreUpdate",
"pre-install-cmd": "ComposerHookHandler::onPreInstall"
}
}
109 changes: 109 additions & 0 deletions includes/ComposerHookHandler.php
@@ -0,0 +1,109 @@
<?php

use Composer\Package\Link;
use Composer\Package\LinkConstraint\VersionConstraint;
use Composer\Package\Package;
use Composer\Script\Event;

/**
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/

class ComposerHookHandler {

public static function onPreUpdate( Event $event ) {
self::handleChangeEvent( $event );
}

public static function onPreInstall( Event $event ) {
self::handleChangeEvent( $event );
}

private static function handleChangeEvent( Event $event ) {
$package = $event->getComposer()->getPackage();

if ( $package instanceof Package ) {
$packageModifier = new ComposerPackageModifier( $package );
$packageModifier->setProvidesMediaWiki();
}
}

}

class ComposerPackageModifier {

const MEDIAWIKI_PACKAGE_NAME = 'mediawiki/mediawiki';

protected $package;

public function __construct( Package $package ) {
$this->package = $package;
}

public function setProvidesMediaWiki() {
$this->setLinkAsProvides( $this->newMediaWikiLink() );
}

private function setLinkAsProvides( Link $link ) {
$this->package->setProvides( array( $link ) );
}

private function newMediaWikiLink() {
$version = $this->getMediaWikiVersionConstraint();

$link = new Link(
'__root__',
self::MEDIAWIKI_PACKAGE_NAME,
$version,
'provides',
$version->getPrettyString()
);

return $link;
}

private function getMediaWikiVersionConstraint() {
$mvVersion = $this->getMediaWikiVersionString();

$version = new VersionConstraint( '==', $mvVersion );
$version->setPrettyString( $this->getPrettyVersion( $mvVersion ) );

return $version;
}

private function getMediaWikiVersionString() {
$defaultSettings = file_get_contents( __DIR__ . '/DefaultSettings.php' );

$matches = array();
preg_match( "/wgVersion = '([0-9a-zA-Z\.]+)';/", $defaultSettings, $matches );

if ( count( $matches ) !== 2 ) {
throw new RuntimeException( 'Could not extract the MediaWiki version from DefaultSettings.php' );
}

return $this->normalizeVersion( $matches[1] );
}

private function normalizeVersion( $version ) {
$normalizer = new ComposerVersionNormalizer();
return $normalizer->normalize( $version );
}

private function getPrettyVersion( $version ) {
return implode( '.', array_pad( explode( '.', $version ), 4, '0' ) );
}

}

class ComposerVersionNormalizer {

public function normalize( $version ) {
if ( !is_string( $version ) ) {
throw new InvalidArgumentException( '$version must be a string' );
}

return preg_replace( '/(\.\d+)([a-zA-Z]+)/', '$1-$2', $version, 1 );
}

}
85 changes: 85 additions & 0 deletions tests/phpunit/includes/ComposerVersionNormalizerTest.php
@@ -0,0 +1,85 @@
<?php

/**
* @covers ComposerVersionNormalizer
*
* @group ComposerJsonUpdater
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class ComposerVersionNormalizerTest extends PHPUnit_Framework_TestCase {

/**
* @dataProvider nonStringProvider
*/
public function testGivenNonString_normalizeThrowsInvalidArgumentException( $nonString ) {
$normalizer = new ComposerVersionNormalizer();

$this->setExpectedException( 'InvalidArgumentException' );
$normalizer->normalize( $nonString );
}

public function nonStringProvider() {
return array(
array( null ),
array( 42 ),
array( array() ),
array( new stdClass() ),
array( true ),
);
}

/**
* @dataProvider simpleVersionProvider
*/
public function testGivenSimpleVersion_normalizeReturnsAsIs( $simpleVersion ) {
$this->assertRemainsUnchanged( $simpleVersion );
}

protected function assertRemainsUnchanged( $version ) {
$normalizer = new ComposerVersionNormalizer();

$this->assertEquals(
$version,
$normalizer->normalize( $version )
);
}

public function simpleVersionProvider() {
return array(
array( '1.22.0' ),
array( '1.19.2' ),
array( '1.9' ),
);
}

/**
* @dataProvider complexVersionProvider
*/
public function testGivenComplexVersionWithoutDash_normalizeAddsDash( $withoutDash, $withDash ) {
$normalizer = new ComposerVersionNormalizer();

$this->assertEquals(
$withDash,
$normalizer->normalize( $withoutDash )
);
}

public function complexVersionProvider() {
return array(
array( '1.22.0alpha', '1.22.0-alpha' ),
array( '1.22.0RC', '1.22.0-RC' ),
array( '1.19beta', '1.19-beta' ),
array( '1.9RC4', '1.9-RC4' ),
);
}

/**
* @dataProvider complexVersionProvider
*/
public function testGivenComplexVersionWithDash_normalizeReturnsAsIs( $withoutDash, $withDash ) {
$this->assertRemainsUnchanged( $withDash );
}

}

0 comments on commit f223335

Please sign in to comment.