2014-01-02 19:46:46 +01:00
|
|
|
'use strict';
|
2014-12-23 15:31:20 +01:00
|
|
|
/*global persist,exports,config,__plugin,require*/
|
2014-09-30 00:42:41 +02:00
|
|
|
var File = java.io.File,
|
2015-05-31 17:44:42 +02:00
|
|
|
FileWriter = java.io.FileWriter,
|
|
|
|
PrintWriter = java.io.PrintWriter,
|
|
|
|
find = require('./find');
|
2013-12-24 01:09:49 +01:00
|
|
|
/*
|
|
|
|
plugin management
|
|
|
|
*/
|
|
|
|
var _plugins = {};
|
|
|
|
|
2015-05-31 17:44:42 +02:00
|
|
|
function _plugin(/* 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;
|
2015-05-31 17:44:42 +02:00
|
|
|
}
|
2013-12-24 01:09:49 +01:00
|
|
|
|
2015-05-31 17:44:42 +02:00
|
|
|
function _autoload( context, pluginDir, options ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
/*
|
|
|
|
Reload all of the .js files in the given directory
|
|
|
|
*/
|
2015-05-31 17:44:42 +02:00
|
|
|
var sourceFiles = [],
|
2014-01-29 20:49:15 +01:00
|
|
|
property,
|
|
|
|
module,
|
|
|
|
pluginPath;
|
2015-05-31 17:44:42 +02:00
|
|
|
sourceFiles = find(pluginDir);
|
2013-12-24 01:09:49 +01:00
|
|
|
|
2015-05-31 17:44:42 +02:00
|
|
|
var len = sourceFiles.length;
|
|
|
|
if ( config && config.verbose ) {
|
|
|
|
console.info( len + ' scriptcraft plugins found in ' + pluginDir );
|
|
|
|
}
|
2014-09-30 00:42:41 +02:00
|
|
|
|
2015-05-31 17:44:42 +02:00
|
|
|
for ( var i = 0; i < len; i++ ) {
|
2014-09-30 00:42:41 +02:00
|
|
|
|
2015-05-31 17:44:42 +02:00
|
|
|
pluginPath = sourceFiles[i];
|
|
|
|
if (!pluginPath.match(/\.js$/)){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
module = {};
|
2014-01-29 20:49:15 +01:00
|
|
|
|
2015-05-31 17:44:42 +02:00
|
|
|
try {
|
|
|
|
module = require( pluginPath , options);
|
|
|
|
for ( property in module ) {
|
|
|
|
/*
|
|
|
|
all exports in plugins become members of context object
|
|
|
|
*/
|
|
|
|
context[property] = module[property];
|
2014-01-25 00:38:56 +01:00
|
|
|
}
|
2015-05-31 17:44:42 +02:00
|
|
|
} catch ( e ) {
|
|
|
|
var msg = 'Plugin ' + pluginPath + ' ' + e ;
|
|
|
|
console.error( msg );
|
2014-01-25 00:38:56 +01:00
|
|
|
}
|
2015-05-31 17:44:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
exports.plugin = _plugin;
|
|
|
|
exports.autoload = _autoload;
|
2013-12-30 02:07:41 +01:00
|
|
|
|