diff options
author | portix <none@none> | 2012-11-28 02:41:26 +0100 |
---|---|---|
committer | portix <none@none> | 2012-11-28 02:41:26 +0100 |
commit | b1153dd74dfb5f4be2cd855ccef12083fcb8f17f (patch) | |
tree | c61c35a236d75687b6d7a6db9d86b504d173313b /scripts | |
parent | 0086ae6b1982b960bdf2fcab3761e657dd9a897f (diff) | |
download | dwb-b1153dd74dfb5f4be2cd855ccef12083fcb8f17f.zip |
Introducing exports for extensions; coding style in extensions.js
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/lib/extensions.js | 423 |
1 files changed, 240 insertions, 183 deletions
diff --git a/scripts/lib/extensions.js b/scripts/lib/extensions.js index f81f0fef..9337adb2 100644 --- a/scripts/lib/extensions.js +++ b/scripts/lib/extensions.js @@ -3,206 +3,263 @@ var _debug = false; var _registered = {}; var _configLoaded = false; - var getPlugin = function(name, filename) { - var ret = null; - try { - if (system.fileTest(filename, FileTest.exists)) { - ret = include(filename); + + var getPlugin = function(name, filename) + { + var ret = null; + try + { + if (system.fileTest(filename, FileTest.exists)) + ret = include(filename); } - } - catch(e) { - extensions.error(name, "Error in line " + e.line + " parsing " + filename); - } - return ret; - }; - var getStack = function(offset) { - if (arguments.length === 0) { - offset = 0; - } - try { - throw Error (message); - } - catch (e) { - var stack = e.stack.match(/[^\n]+/g); - return "STACK: [" + stack.slice(offset+1).join("] [")+"]"; - } - }; - var _unload = function (name, removeConfig) { - if (_registered[name] !== undefined) { - if (_registered[name].end instanceof Function) { - _registered[name].end(); - extensions.message(name, "Extension unloaded."); + catch(e) + { + extensions.error(name, "Error in line " + e.line + " parsing " + filename); } - if (removeConfig) - delete _config[name]; - delete _registered[name]; - return true; - } - return false; + return ret; }; - Object.defineProperties(extensions, { - "warning" : { - value : function (name, message) { - io.print("\033[1mDWB EXTENSION WARNING: \033[0mextension \033[1m" + name + "\033[0m: " + message, "stderr"); - } - }, - "enableDebugging" : { - set : function (value) { - if (typeof value == "boolean") - _debug = value; + var getStack = function(offset) + { + if (arguments.length === 0) + offset = 0; + + try + { + throw Error (message); } - }, - "debug" : { - value : function (name, message) { - if (_debug) { - io.print("\033[1mDWB EXTENSION DEBUG: \033[0mextension \033[1m" + name + "\033[0m\n" + getStack(1), "stderr"); - } + catch (e) + { + var stack = e.stack.match(/[^\n]+/g); + return "STACK: [" + stack.slice(offset+1).join("] [")+"]"; } - }, - "error" : { - value : function (name, a, b) { - var message = ""; - if (a instanceof Error) { - if (a.message) { - message = a.message; + }; + var _unload = function (name, removeConfig) + { + if (_registered[name] !== undefined) + { + if (_registered[name].end instanceof Function) + { + _registered[name].end(); + extensions.message(name, "Extension unloaded."); } - else if (arguments.length > 2) - message = b; - else - b = ""; - io.print("\033[31mDWB EXTENSION ERROR: \033[0mextension \033[1m" + name + "\033[0m in line " + a.line + ": " + - message + "\nSTACK: [" + a.stack.match(/[^\n]+/g).join("] [") + "]", "stderr"); - } - else { - io.print("\033[31mDWB EXTENSION ERROR: \033[0mextension \033[1m" + name + "\033[0m: " + a + "\n" + getStack(1), "stderr"); - } - } - }, - "message" : { - value : function (name, message) { - io.print("\033[1mDWB EXTENSION: \033[0mextension \033[1m" + name + "\033[0m: " + message, "stderr"); + if (removeConfig) + delete _config[name]; + + delete _registered[name]; + + replace(name); + + return true; } - }, - "getConfig" : { - value : function(c, dc) { - var k, config = {}; - if (c === null || c === undefined) - config = dc; - else { - for (k in dc) { - config[k] = typeof c[k] === typeof dc[k] ? c[k] : dc[k]; + return false; + }; + Object.defineProperties(extensions, + { + "warning" : + { + value : function (name, message) + { + io.print("\033[1mDWB EXTENSION WARNING: \033[0mextension \033[1m" + name + "\033[0m: " + message, "stderr"); } - } - return config; - } - }, - "load" : { - value : function(name, c) { - if (_registered[name] !== undefined) { - extensions.error(name, "Already loaded."); - } - var boldname = "\033[1m" + name + "\033[0m"; + }, + "enableDebugging" : + { + set : function (value) + { + if (typeof value == "boolean") + _debug = value; + } + }, + "debug" : + { + value : function (name, message) { + if (_debug) { + io.print("\033[1mDWB EXTENSION DEBUG: \033[0mextension \033[1m" + name + "\033[0m\n" + getStack(1), "stderr"); + } + } + }, + "error" : + { + value : function (name, a, b) { + var message = ""; + if (a instanceof Error) { + if (a.message) { + message = a.message; + } + else if (arguments.length > 2) + message = b; + else + b = ""; + io.print("\033[31mDWB EXTENSION ERROR: \033[0mextension \033[1m" + name + "\033[0m in line " + a.line + ": " + + message + "\nSTACK: [" + a.stack.match(/[^\n]+/g).join("] [") + "]", "stderr"); + } + else { + io.print("\033[31mDWB EXTENSION ERROR: \033[0mextension \033[1m" + name + "\033[0m: " + a + "\n" + getStack(1), "stderr"); + } + } + }, + "message" : + { + value : function (name, message) + { + io.print("\033[1mDWB EXTENSION: \033[0mextension \033[1m" + name + "\033[0m: " + message, "stderr"); + } + }, + "getConfig" : + { + value : function(c, dc) + { + var k, config = {}; + if (c === null || c === undefined) + config = dc; + else + { + for (k in dc) + config[k] = typeof c[k] === typeof dc[k] ? c[k] : dc[k]; + } + return config; + } + }, + "load" : + { + value : function(name, c) + { + if (_registered[name] !== undefined) + extensions.error(name, "Already loaded."); - var config, dataBase, pluginPath, plugin = null, key, filename; - var extConfig = null; + var boldname = "\033[1m" + name + "\033[0m"; - /* Get default config if the config hasn't been read yet */ - if (arguments.length == 2) { - extConfig = c; - _config[name] = c; - } - if (!_configLoaded) { - if (system.fileTest(data.configDir + "/extensionrc", FileTest.regular)) { - try { - config = include(data.configDir + "/extensionrc"); - } - catch (e) { - extensions.error(name, "loading config failed : " + e); - } - if (config === null) { - extensions.warning(name, "Could not load config."); - } - else { - for (key in config) { - _config[key] = config[key]; + var config, dataBase, pluginPath, plugin = null, key, filename; + var extConfig = null; + + /* Get default config if the config hasn't been read yet */ + if (arguments.length == 2) + { + extConfig = c; + _config[name] = c; } - } - _configLoaded = true; - } - } - if (extConfig === null) - extConfig = _config[name] || null; + if (!_configLoaded) + { + if (system.fileTest(data.configDir + "/extensionrc", FileTest.regular)) + { + try + { + config = include(data.configDir + "/extensionrc"); + } + catch (e) + { + extensions.error(name, "loading config failed : " + e); + } + if (config === null) + { + extensions.warning(name, "Could not load config."); + } + else + { + for (key in config) + _config[key] = config[key]; + } + _configLoaded = true; + } + } + if (extConfig === null) + extConfig = _config[name] || null; + + /* Load extension */ + if (data.userDataDir) + { + filename = data.userDataDir + "/extensions/" + name; + plugin = getPlugin(name, data.userDataDir + "/extensions/" + name); + } + if (plugin === null) + { + plugin = getPlugin(name, data.systemDataDir + "/extensions/" + name); + if (plugin === null) + { + extensions.error(name, "Couldn't find extension."); + return false; + } + } + try + { + plugin._name = name; - /* Load extension */ - if (data.userDataDir) { - filename = data.userDataDir + "/extensions/" + name; - plugin = getPlugin(name, data.userDataDir + "/extensions/" + name); - } - if (plugin === null) { - plugin = getPlugin(name, data.systemDataDir + "/extensions/" + name); - if (plugin === null) { - extensions.error(name, "Couldn't find extension."); - return false; + if (plugin.defaultConfig) + extConfig = extensions.getConfig(extConfig, plugin.defaultConfig); + + if (plugin.init(extConfig)) + { + _registered[name] = plugin; + + if (plugin.exports) { + replace(name, plugin.exports); + } + + extensions.message(name, "Successfully loaded and initialized."); + return plugin.exports || null; + } + else + { + extensions.error(name, "Initialization failed."); + return null; + } + } + catch (e) + { + extensions.error(name, "Initialization failed: " + e); + return null; + } } - } - try { - plugin._name = name; - if (plugin.defaultConfig) { - extConfig = extensions.getConfig(extConfig, plugin.defaultConfig); + }, + "unload" : + { + value : function(name) + { + return _unload(name, true); } - if (plugin.init(extConfig)) { - _registered[name] = plugin; - extensions.message(name, "Successfully loaded and initialized."); - return true; + }, + "reload" : + { + value : function () + { + _unload(arguments[0], false); + return extensions.load.apply(this, arguments); } - else { - extensions.error(name, "Initialization failed."); - return false; + }, + "toggle" : + { + value : function(name, c) + { + if (_registered[name] !== undefined) + { + _unload(name); + return false; + } + else + { + extensions.load(name, c); + return true; + } + } + }, + "bind" : + { + value : function(name, shortcut, options) + { + if (!name || !shortcut) + return; + + if (options.load === undefined || options.load) + extensions.load(name, options.config); + + bind(shortcut, function () { + if (extensions.toggle(name, options.config)) + io.notify("Extension " + name + " enabled"); + else + io.notify("Extension " + name + " disabled"); + }, options.command); } - } - catch (e) { - extensions.error(name, "Initialization failed: " + e); - return false; - } - } - }, - "unload" : { - value : function(name) { - return _unload(name, true); - } - }, - "reload" : { - value : function () { - _unload(arguments[0], false); - return extensions.load.apply(this, arguments); - } - }, - "toggle" : { - value : function(name, c) { - if (_registered[name] !== undefined) { - _unload(name); - return false; - } - else { - extensions.load(name, c); - return true; - } - } - }, - "bind" : { - value : function(name, shortcut, options) { - if (!name || !shortcut) - return; - if (options.load === undefined || options.load) - extensions.load(name, options.config); - bind(shortcut, function () { - if (extensions.toggle(name, options.config)) - io.notify("Extension " + name + " enabled"); - else - io.notify("Extension " + name + " disabled"); - }, options.command); } - } }); })(); Object.freeze(extensions); |