2014-01-02 19:46:46 +01:00
|
|
|
'use strict';
|
2014-01-29 20:49:15 +01:00
|
|
|
|
2014-01-25 00:38:56 +01:00
|
|
|
var console = require('./console'),
|
|
|
|
File = java.io.File,
|
|
|
|
FileWriter = java.io.FileWriter,
|
|
|
|
PrintWriter = java.io.PrintWriter;
|
2013-12-24 01:09:49 +01:00
|
|
|
/*
|
|
|
|
plugin management
|
|
|
|
*/
|
|
|
|
var _plugins = {};
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
var _plugin = function(/* String */ moduleName, /* Object */ moduleObject, isPersistent ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
//
|
|
|
|
// don't load plugin more than once
|
|
|
|
//
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( typeof _plugins[moduleName] != 'undefined' ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
return _plugins[moduleName].module;
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
2013-12-24 01:09:49 +01:00
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
var pluginData = { persistent: isPersistent, module: moduleObject };
|
|
|
|
if ( typeof moduleObject.store == 'undefined' ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
moduleObject.store = {};
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
2014-01-25 00:38:56 +01:00
|
|
|
_plugins[moduleName] = pluginData;
|
2013-12-24 01:09:49 +01:00
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( isPersistent ) {
|
|
|
|
moduleObject.store = persist( moduleName, moduleObject.store );
|
2014-01-25 00:38:56 +01:00
|
|
|
}
|
|
|
|
return moduleObject;
|
2013-12-24 01:09:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.plugin = _plugin;
|
|
|
|
|
2014-02-10 21:55:32 +01:00
|
|
|
exports.autoload = function( context, pluginDir, logger, options ) {
|
2013-12-24 01:09:49 +01:00
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
var _canonize = function( file ) {
|
2014-01-25 10:04:16 +01:00
|
|
|
return '' + file.canonicalPath.replaceAll('\\\\','/');
|
2014-01-25 00:38:56 +01:00
|
|
|
};
|
|
|
|
/*
|
|
|
|
recursively walk the given directory and return a list of all .js files
|
|
|
|
*/
|
2014-01-29 20:49:15 +01:00
|
|
|
var _listSourceFiles = function( store, dir ) {
|
|
|
|
var files = dir.listFiles(),
|
|
|
|
file;
|
|
|
|
if ( !files ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
return;
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
|
|
|
for ( var i = 0; i < files.length; i++ ) {
|
|
|
|
file = files[i];
|
|
|
|
if ( file.isDirectory( ) ) {
|
|
|
|
_listSourceFiles( store, file );
|
2014-01-25 00:38:56 +01:00
|
|
|
}else{
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( file.canonicalPath.endsWith( '.js' ) ) {
|
|
|
|
store.push( file );
|
2013-12-24 01:09:49 +01:00
|
|
|
}
|
2014-01-25 00:38:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
Reload all of the .js files in the given directory
|
|
|
|
*/
|
2014-01-29 20:49:15 +01:00
|
|
|
(function( pluginDir ) {
|
|
|
|
var sourceFiles = [],
|
|
|
|
property,
|
|
|
|
module,
|
|
|
|
pluginPath;
|
|
|
|
_listSourceFiles( sourceFiles, pluginDir );
|
2013-12-24 01:09:49 +01:00
|
|
|
|
2014-01-25 00:38:56 +01:00
|
|
|
var len = sourceFiles.length;
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( config.verbose ) {
|
2014-02-10 21:55:32 +01:00
|
|
|
console.info( len + ' scriptcraft plugins found in ' + pluginDir );
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
|
|
|
for ( var i = 0; i < len; i++ ) {
|
|
|
|
pluginPath = _canonize( sourceFiles[i] );
|
|
|
|
module = {};
|
|
|
|
|
2014-01-25 00:38:56 +01:00
|
|
|
try {
|
2014-02-10 21:55:32 +01:00
|
|
|
module = require( pluginPath , options);
|
2014-01-29 20:49:15 +01:00
|
|
|
for ( property in module ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
/*
|
2014-02-10 21:55:32 +01:00
|
|
|
all exports in plugins become members of context object
|
2014-01-25 00:38:56 +01:00
|
|
|
*/
|
2014-02-10 21:55:32 +01:00
|
|
|
context[property] = module[property];
|
2013-12-24 01:09:49 +01:00
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
} catch ( e ) {
|
2014-02-10 21:55:32 +01:00
|
|
|
if ( typeof logger != 'undefined' ) {
|
|
|
|
logger.severe( 'Plugin ' + pluginPath + ' ' + e );
|
|
|
|
} else {
|
|
|
|
java.lang.System.out.println( 'Error: Plugin ' + pluginPath + ' ' + e );
|
|
|
|
}
|
2014-01-25 00:38:56 +01:00
|
|
|
}
|
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
}(pluginDir));
|
2013-12-24 01:09:49 +01:00
|
|
|
};
|
2013-12-30 02:07:41 +01:00
|
|
|
|