Skip to content

Commit

Permalink
Auto-load modules based on glob pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
atdt committed Jun 27, 2012
1 parent 66e8901 commit f261e15
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 42 deletions.
50 changes: 42 additions & 8 deletions SkelJS.hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@

class SkelJSHooks {

public static $dir;

/**
* globRelative
*
* Searches for files matching pattern relative to the path of the SkelJS
* extension
*
* @param $pattern string glob pattern
*/
public static function globRelative( $pattern ) {
$dirlen = strlen( self::$dir );
$matches = glob( self::$dir . $pattern );
foreach ( $matches as &$match ) {
$match = substr( $match, $dirlen );
}
unset( $match );
return $matches;
}


/**
* BeforePageDisplay hook
*
Expand All @@ -17,18 +38,31 @@ class SkelJSHooks {
* @param $skin Skin current skin
*/
public static function beforePageDisplay( $out, $skin ) {
$out->addJsConfigVars( array (
"wgAnswer" => 42
) );
$out->addModules( 'ext.SkelJS' );
return true;
}

/**
* ResourceLoaderGetConfigVars hook
*
* Adds enabled/disabled switches for Vector modules (XXX Changeme)
*/
public static function resourceLoaderGetConfigVars( &$vars ) {
return true;
}
/**
* ResourceLoaderTestModules hook handler.
* @param $testModules: array of javascript testing modules. 'qunit' is fed using tests/qunit/QUnitTestResources.php.
* @param $resourceLoader object
* @return bool
*/
public static function addTestModules( array &$testModules, ResourceLoader &$resourceLoader ) {
$testModules['qunit']['ext.SkelJS.tests'] = array(
'scripts' => 'tests/ext.SkelJS.tests.js',
'dependencies' => $wgResourceModules['ext.SkelJS']['scripts'],
'localBasePath' => self::$dir,
'remoteExtPath' => 'SkelJS'
);
return true;
}

}

SkelJSHooks::$dir = dirname( __FILE__ );

/* vim: set sw=8:ts=8:sts=0: */
28 changes: 11 additions & 17 deletions SkelJS.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @version 0.0.1
*/

$localBasePath = dirname( __FILE__ );


// -------
// Setup
Expand All @@ -25,33 +25,27 @@
'descriptionmsg' => 'skeljs-desc',
);

$wgExtensionMessagesFiles['SkelJS'] = $localBasePath . '/SkelJS.i18n.php';
$wgAutoloadClasses['SkelJSHooks'] = dirname( __FILE__ ) . '/SkelJS.hooks.php';

$wgResourceModules['ext.SkelJS'] = array(
'scripts' => glob( 'lib/*.js' ),
'localBasePath' => $localBasePath,
'remoteExtPath' => 'SkelJS'
);
$wgExtensionMessagesFiles['SkelJS'] = dirname( __FILE__ ) . '/SkelJS.i18n.php';


// ---------
// Testing
//
// TODO: Preconfigure 'top' and 'bottom' module.
// Cf http://www.mediawiki.org/wiki/ResourceLoader/Migration_guide_for_extension_developers#Inline_JavaScript

$wgEnableJavaScriptTest = true;
$testModules['qunit']['SkelJS.tests'] = array(
'scripts' => glob( 'tests/*.test.js' ),
'dependencies' => $wgResourceModules['ext.SkelJS']['scripts'],
'localBasePath' => $localBasePath,
'remoteExtPath' => 'SkelJS'
$wgResourceModules['ext.SkelJS'] = array(
'scripts' => SkelJSHooks::globRelative( '/modules/*.js' ),
'localBasePath' => SkelJSHooks::$dir,
'remoteExtPath' => 'SkelJS'
);


// -------
// Hooks
//

$wgAutoloadClasses['SkelJSHooks'] = $localBasePath . '/SkelJS.hooks.php';
$wgHooks['BeforePageDisplay'][] = 'SkelJSHooks::beforePageDisplay';

$wgHooks['ResourceLoaderTestModules'][] = 'SkelJSHooks::addTestModules';

/* vim: set sw=8:ts=8:sts=0: */
1 change: 1 addition & 0 deletions modules/ext.SkelJS.bottom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var bottom = true;
6 changes: 5 additions & 1 deletion modules/ext.SkelJS.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
console.log( 'SkelJS Ready!' );
( function ( window, console ) {
if ( typeof console !== undefined ) {
console.log( 'SkelJS ready!' );
}
} ( window, window.console ) );
1 change: 1 addition & 0 deletions modules/ext.SkelJS.top.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var top = true;
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "MyExtension",
"name": "SkelJS",
"description": "JavaScript-Driven MediaWiki Extension Skeleton",
"authors": [
"Ori Livneh <ori@wikimedia.org>"
],
"version": "0.0.1",
"engine": "node >= 0.6",
"main": "./lib/e3.js",
"repository" : { "type":"git", "url":"git://gerrit.wikimedia.org/mediawiki/extensions/E3Experiments.git" },
"main": "./lib/ext.SkelJS.js",
"repository" : { "type":"git", "url":"git://gerrit.wikimedia.org/mediawiki/extensions/SkelJS.git" },
"devDependencies": {
"grunt": "0.3.9"
},
Expand Down
12 changes: 12 additions & 0 deletions tests/ext.SkelJS.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if ( typeof QUnit.newMwEnvironment !== 'undefined' ) {
module( 'ext.SkelJS', QUnit.newMwEnvironment() );
}

// -------
// tests
//

test( 'Dummy test', function () {
var x = 4;
ok( x / 2 === 2 );
} );
12 changes: 0 additions & 12 deletions tests/misc.tests.js

This file was deleted.

2 changes: 1 addition & 1 deletion tests/tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<script src="../modules/ext.SkelJS.js"></script>

<!-- test suites -->
<script src="misc.tests.js"></script>
<script src="ext.SkelJS.tests.js"></script>

</body>
</html>

0 comments on commit f261e15

Please sign in to comment.