Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
6489222
commit f223335
Showing
3 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 ); | ||
| } | ||
|
|
||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 ); | ||
| } | ||
|
|
||
| } |