Merge pull request #740 from cweider/plugin-cleanup

Plugin cleanup
This commit is contained in:
John McLear 2012-09-12 04:52:23 -07:00
commit 389e0d09b0
10 changed files with 146 additions and 129 deletions

View File

@ -29,6 +29,7 @@ var plugins = require("ep_etherpad-lite/static/js/pluginfw/plugins");
var hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks");
var npm = require("npm/lib/npm.js");
hooks.plugins = plugins;
//set loglevel
log4js.setGlobalLogLevel(settings.loglevel);

View File

@ -68,7 +68,8 @@
, "security.js"
, "$security.js"
, "json2.js"
, "pluginfw/plugins.js"
, "pluginfw/client_plugins.js"
, "pluginfw/shared.js"
, "pluginfw/hooks.js"
, "pluginfw/parent_require.js"
]

View File

@ -244,7 +244,7 @@ require.setGlobalKeyPath("require");\n\
<script type="text/javascript">\
parent_req = require("ep_etherpad-lite/static/js/pluginfw/parent_require");\
parent_req.getRequirementFromParent(require, "ep_etherpad-lite/static/js/pluginfw/hooks");\
parent_req.getRequirementFromParent(require, "ep_etherpad-lite/static/js/pluginfw/plugins");\
parent_req.getRequirementFromParent(require, "ep_etherpad-lite/static/js/pluginfw/client_plugins");\
</script>\
');

View File

@ -23,7 +23,7 @@ var editor, _, $, jQuery, plugins, Ace2Common;
Ace2Common = require('./ace2_common');
plugins = require('ep_etherpad-lite/static/js/pluginfw/plugins');
plugins = require('ep_etherpad-lite/static/js/pluginfw/client_plugins');
$ = jQuery = require('./rjquery').$;
_ = require("./underscore");

View File

@ -0,0 +1,36 @@
var $, jQuery;
$ = jQuery = require("ep_etherpad-lite/static/js/rjquery").$;
var _ = require("underscore");
var pluginUtils = require('./shared');
exports.loaded = false;
exports.plugins = {};
exports.parts = [];
exports.hooks = {};
exports.baseURL = '';
exports.ensure = function (cb) {
if (!exports.loaded)
exports.update(cb);
else
cb();
};
exports.update = function (cb) {
// It appears that this response (see #620) may interrupt the current thread
// of execution on Firefox. This schedules the response in the run-loop,
// which appears to fix the issue.
var callback = function () {setTimeout(cb, 0);};
jQuery.getJSON(exports.baseURL + 'pluginfw/plugin-definitions.json', function(data) {
exports.plugins = data.plugins;
exports.parts = data.parts;
exports.hooks = pluginUtils.extractHooks(exports.parts, "client_hooks");
exports.loaded = true;
callback();
}).error(function(xhr, s, err){
console.error("Failed to load plugin-definitions: " + err);
callback();
});
};

View File

@ -1,4 +1,3 @@
var plugins = require("./plugins");
var async = require("async");
var _ = require("underscore");
@ -71,8 +70,8 @@ exports.flatten = function (lst) {
exports.callAll = function (hook_name, args) {
if (!args) args = {};
if (plugins.hooks[hook_name] === undefined) return [];
return _.flatten(_.map(plugins.hooks[hook_name], function (hook) {
if (exports.plugins.hooks[hook_name] === undefined) return [];
return _.flatten(_.map(exports.plugins.hooks[hook_name], function (hook) {
return hookCallWrapper(hook, hook_name, args);
}), true);
}
@ -80,9 +79,9 @@ exports.callAll = function (hook_name, args) {
exports.aCallAll = function (hook_name, args, cb) {
if (!args) args = {};
if (!cb) cb = function () {};
if (plugins.hooks[hook_name] === undefined) return cb(null, []);
if (exports.plugins.hooks[hook_name] === undefined) return cb(null, []);
async.map(
plugins.hooks[hook_name],
exports.plugins.hooks[hook_name],
function (hook, cb) {
hookCallWrapper(hook, hook_name, args, function (res) { cb(null, res); });
},
@ -94,8 +93,8 @@ exports.aCallAll = function (hook_name, args, cb) {
exports.callFirst = function (hook_name, args) {
if (!args) args = {};
if (plugins.hooks[hook_name] === undefined) return [];
return exports.syncMapFirst(plugins.hooks[hook_name], function (hook) {
if (exports.plugins.hooks[hook_name] === undefined) return [];
return exports.syncMapFirst(exports.plugins.hooks[hook_name], function (hook) {
return hookCallWrapper(hook, hook_name, args);
});
}
@ -103,9 +102,9 @@ exports.callFirst = function (hook_name, args) {
exports.aCallFirst = function (hook_name, args, cb) {
if (!args) args = {};
if (!cb) cb = function () {};
if (plugins.hooks[hook_name] === undefined) return cb(null, []);
if (exports.plugins.hooks[hook_name] === undefined) return cb(null, []);
exports.mapFirst(
plugins.hooks[hook_name],
exports.plugins.hooks[hook_name],
function (hook, cb) {
hookCallWrapper(hook, hook_name, args, function (res) { cb(null, res); });
},

View File

@ -1,30 +1,21 @@
exports.isClient = typeof global != "object";
var npm = require("npm/lib/npm.js");
var readInstalled = require("./read-installed.js");
var relativize = require("npm/lib/utils/relativize.js");
var readJson = require("npm/lib/utils/read-json.js");
var path = require("path");
var async = require("async");
var fs = require("fs");
var tsort = require("./tsort");
var util = require("util");
var _ = require("underscore");
var _;
if (!exports.isClient) {
var npm = require("npm/lib/npm.js");
var readInstalled = require("./read-installed.js");
var relativize = require("npm/lib/utils/relativize.js");
var readJson = require("npm/lib/utils/read-json.js");
var path = require("path");
var async = require("async");
var fs = require("fs");
var tsort = require("./tsort");
var util = require("util");
_ = require("underscore");
}else{
var $, jQuery;
$ = jQuery = require("ep_etherpad-lite/static/js/rjquery").$;
_ = require("ep_etherpad-lite/static/js/underscore");
}
var pluginUtils = require('./shared');
exports.prefix = 'ep_';
exports.loaded = false;
exports.plugins = {};
exports.parts = [];
exports.hooks = {};
exports.baseURL = '';
exports.ensure = function (cb) {
if (!exports.loaded)
@ -43,7 +34,7 @@ exports.formatParts = function () {
exports.formatHooks = function (hook_set_name) {
var res = [];
var hooks = exports.extractHooks(exports.parts, hook_set_name || "hooks");
var hooks = pluginUtils.extractHooks(exports.parts, hook_set_name || "hooks");
_.chain(hooks).keys().forEach(function (hook_name) {
_.forEach(hooks[hook_name], function (hook) {
@ -53,84 +44,6 @@ exports.formatHooks = function (hook_set_name) {
return "<dl>" + res.join("\n") + "</dl>";
};
exports.loadFn = function (path, hookName) {
var functionName
, parts = path.split(":");
// on windows: C:\foo\bar:xyz
if(parts[0].length == 1) {
if(parts.length == 3)
functionName = parts.pop();
path = parts.join(":");
}else{
path = parts[0];
functionName = parts[1];
}
var fn = require(path);
functionName = functionName ? functionName : hookName;
_.each(functionName.split("."), function (name) {
fn = fn[name];
});
return fn;
};
exports.extractHooks = function (parts, hook_set_name) {
var hooks = {};
_.each(parts,function (part) {
_.chain(part[hook_set_name] || {})
.keys()
.each(function (hook_name) {
if (hooks[hook_name] === undefined) hooks[hook_name] = [];
var hook_fn_name = part[hook_set_name][hook_name];
/* On the server side, you can't just
* require("pluginname/whatever") if the plugin is installed as
* a dependency of another plugin! Bah, pesky little details of
* npm... */
if (!exports.isClient) {
hook_fn_name = path.normalize(path.join(path.dirname(exports.plugins[part.plugin].package.path), hook_fn_name));
}
try {
var hook_fn = exports.loadFn(hook_fn_name, hook_name);
if (!hook_fn) {
throw "Not a function";
}
} catch (exc) {
console.error("Failed to load '" + hook_fn_name + "' for '" + part.full_name + "/" + hook_set_name + "/" + hook_name + "': " + exc.toString())
}
if (hook_fn) {
hooks[hook_name].push({"hook_name": hook_name, "hook_fn": hook_fn, "hook_fn_name": hook_fn_name, "part": part});
}
});
});
return hooks;
};
if (exports.isClient) {
exports.update = function (cb) {
// It appears that this response (see #620) may interrupt the current thread
// of execution on Firefox. This schedules the response in the run-loop,
// which appears to fix the issue.
var callback = function () {setTimeout(cb, 0);};
jQuery.getJSON(exports.baseURL + 'pluginfw/plugin-definitions.json', function(data) {
exports.plugins = data.plugins;
exports.parts = data.parts;
exports.hooks = exports.extractHooks(exports.parts, "client_hooks");
exports.loaded = true;
callback();
}).error(function(xhr, s, err){
console.error("Failed to load plugin-definitions: " + err);
callback();
});
};
} else {
exports.callInit = function (cb) {
var hooks = require("./hooks");
async.map(
@ -153,6 +66,10 @@ exports.callInit = function (cb) {
);
}
exports.pathNormalization = function (part, hook_fn_name) {
return path.normalize(path.join(path.dirname(exports.plugins[part.plugin].package.path), hook_fn_name));
}
exports.update = function (cb) {
exports.getPackages(function (er, packages) {
var parts = [];
@ -161,14 +78,14 @@ exports.update = function (cb) {
async.forEach(
Object.keys(packages),
function (plugin_name, cb) {
exports.loadPlugin(packages, plugin_name, plugins, parts, cb);
loadPlugin(packages, plugin_name, plugins, parts, cb);
},
function (err) {
if (err) cb(err);
exports.plugins = plugins;
exports.parts = exports.sortParts(parts);
exports.hooks = exports.extractHooks(exports.parts, "hooks");
exports.loaded = true;
exports.plugins = plugins;
exports.parts = sortParts(parts);
exports.hooks = pluginUtils.extractHooks(exports.parts, "hooks", exports.pathNormalization);
exports.loaded = true;
exports.callInit(cb);
}
);
@ -200,9 +117,9 @@ exports.getPackages = function (cb) {
flatten(tmp);
cb(null, packages);
});
};
};
exports.loadPlugin = function (packages, plugin_name, plugins, parts, cb) {
function loadPlugin(packages, plugin_name, plugins, parts, cb) {
var plugin_path = path.resolve(packages[plugin_name].path, "ep.json");
fs.readFile(
plugin_path,
@ -226,9 +143,9 @@ exports.getPackages = function (cb) {
cb();
}
);
};
}
exports.partsToParentChildList = function (parts) {
function partsToParentChildList(parts) {
var res = [];
_.chain(parts).keys().forEach(function (name) {
_.each(parts[name].post || [], function (child_name) {
@ -242,18 +159,15 @@ exports.partsToParentChildList = function (parts) {
}
});
return res;
};
}
// Used only in Node, so no need for _
exports.sortParts = function(parts) {
function sortParts(parts) {
return tsort(
exports.partsToParentChildList(parts)
partsToParentChildList(parts)
).filter(
function (name) { return parts[name] !== undefined; }
).map(
function (name) { return parts[name]; }
);
};
}
}

View File

@ -0,0 +1,61 @@
var _ = require("underscore");
function loadFn(path, hookName) {
var functionName
, parts = path.split(":");
// on windows: C:\foo\bar:xyz
if (parts[0].length == 1) {
if (parts.length == 3) {
functionName = parts.pop();
}
path = parts.join(":");
} else {
path = parts[0];
functionName = parts[1];
}
var fn = require(path);
functionName = functionName ? functionName : hookName;
_.each(functionName.split("."), function (name) {
fn = fn[name];
});
return fn;
};
function extractHooks(parts, hook_set_name, normalizer) {
var hooks = {};
_.each(parts,function (part) {
_.chain(part[hook_set_name] || {})
.keys()
.each(function (hook_name) {
if (hooks[hook_name] === undefined) hooks[hook_name] = [];
var hook_fn_name = part[hook_set_name][hook_name];
/* On the server side, you can't just
* require("pluginname/whatever") if the plugin is installed as
* a dependency of another plugin! Bah, pesky little details of
* npm... */
if (normalizer) {
hook_fn_name = normalizer(part, hook_fn_name);
}
try {
var hook_fn = loadFn(hook_fn_name, hook_name);
if (!hook_fn) {
throw "Not a function";
}
} catch (exc) {
console.error("Failed to load '" + hook_fn_name + "' for '" + part.full_name + "/" + hook_set_name + "/" + hook_name + "': " + exc.toString())
}
if (hook_fn) {
hooks[hook_name].push({"hook_name": hook_name, "hook_fn": hook_fn, "hook_fn_name": hook_fn_name, "part": part});
}
});
});
return hooks;
};
exports.extractHooks = extractHooks;

View File

@ -369,11 +369,13 @@
document.domain = document.domain; // for comet
}
var plugins = require('ep_etherpad-lite/static/js/pluginfw/plugins');
var plugins = require('ep_etherpad-lite/static/js/pluginfw/client_plugins');
var hooks = require('ep_etherpad-lite/static/js/pluginfw/hooks');
plugins.baseURL = baseURL;
plugins.update(function () {
hooks.plugins = plugins;
// Call documentReady hook
$(function() {
hooks.aCallAll('documentReady');

View File

@ -175,10 +175,13 @@
document.domain = document.domain; // for comet
}
var plugins = require('ep_etherpad-lite/static/js/pluginfw/plugins');
var plugins = require('ep_etherpad-lite/static/js/pluginfw/client_plugins');
plugins.baseURL = baseURL;
plugins.update(function () {
var hooks = require('ep_etherpad-lite/static/js/pluginfw/hooks');
hooks.plugins = plugins;
var timeslider = require('ep_etherpad-lite/static/js/timeslider')
timeslider.baseURL = baseURL;
timeslider.init();