2013-08-05 19:11:32 +04:00
|
|
|
|
2013-09-24 14:46:30 +04:00
|
|
|
var _ = require('underscore'),
|
|
|
|
when = require('when'),
|
2013-11-13 07:52:31 +04:00
|
|
|
errors = require('../errorHandling'),
|
2013-12-06 12:51:35 +04:00
|
|
|
api = require('../api'),
|
2013-11-28 06:45:01 +04:00
|
|
|
loader = require('./loader'),
|
2013-12-06 12:51:35 +04:00
|
|
|
// Holds the available plugins
|
|
|
|
availablePlugins = {};
|
2013-11-28 06:45:01 +04:00
|
|
|
|
2013-08-05 19:11:32 +04:00
|
|
|
|
|
|
|
function getInstalledPlugins() {
|
2013-12-06 12:51:35 +04:00
|
|
|
return api.settings.read('installedPlugins').then(function (installed) {
|
2013-09-24 14:46:30 +04:00
|
|
|
installed.value = installed.value || '[]';
|
2013-08-05 19:11:32 +04:00
|
|
|
|
|
|
|
try {
|
|
|
|
installed = JSON.parse(installed.value);
|
|
|
|
} catch (e) {
|
|
|
|
return when.reject(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return installed;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveInstalledPlugins(installedPlugins) {
|
|
|
|
return getInstalledPlugins().then(function (currentInstalledPlugins) {
|
|
|
|
var updatedPluginsInstalled = _.uniq(installedPlugins.concat(currentInstalledPlugins));
|
|
|
|
|
2013-12-06 12:51:35 +04:00
|
|
|
return api.settings.edit('installedPlugins', updatedPluginsInstalled);
|
2013-08-05 19:11:32 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2013-12-06 18:13:15 +04:00
|
|
|
init: function () {
|
2013-11-13 07:52:31 +04:00
|
|
|
var pluginsToLoad;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// We have to parse the value because it's a string
|
2013-12-06 12:51:35 +04:00
|
|
|
api.settings.read('activePlugins').then(function (aPlugins) {
|
|
|
|
pluginsToLoad = JSON.parse(aPlugins.value) || [];
|
|
|
|
});
|
2013-11-13 07:52:31 +04:00
|
|
|
} catch (e) {
|
|
|
|
errors.logError(
|
|
|
|
'Failed to parse activePlugins setting value: ' + e.message,
|
|
|
|
'Your plugins will not be loaded.',
|
|
|
|
'Check your settings table for typos in the activePlugins value. It should look like: ["plugin-1", "plugin2"] (double quotes required).'
|
|
|
|
);
|
|
|
|
return when.resolve();
|
|
|
|
}
|
|
|
|
|
2013-08-05 19:11:32 +04:00
|
|
|
// Grab all installed plugins, install any not already installed that are in pluginsToLoad.
|
|
|
|
return getInstalledPlugins().then(function (installedPlugins) {
|
|
|
|
var loadedPlugins = {},
|
|
|
|
recordLoadedPlugin = function (name, loadedPlugin) {
|
|
|
|
// After loading the plugin, add it to our hash of loaded plugins
|
|
|
|
loadedPlugins[name] = loadedPlugin;
|
|
|
|
|
|
|
|
return when.resolve(loadedPlugin);
|
|
|
|
},
|
|
|
|
loadPromises = _.map(pluginsToLoad, function (plugin) {
|
|
|
|
// If already installed, just activate the plugin
|
|
|
|
if (_.contains(installedPlugins, plugin)) {
|
2013-12-06 18:13:15 +04:00
|
|
|
return loader.activatePluginByName(plugin).then(function (loadedPlugin) {
|
2013-08-05 19:11:32 +04:00
|
|
|
return recordLoadedPlugin(plugin, loadedPlugin);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install, then activate the plugin
|
2013-12-06 18:13:15 +04:00
|
|
|
return loader.installPluginByName(plugin).then(function () {
|
|
|
|
return loader.activatePluginByName(plugin);
|
2013-08-05 19:11:32 +04:00
|
|
|
}).then(function (loadedPlugin) {
|
|
|
|
return recordLoadedPlugin(plugin, loadedPlugin);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return when.all(loadPromises).then(function () {
|
|
|
|
// Save our installed plugins to settings
|
|
|
|
return saveInstalledPlugins(_.keys(loadedPlugins));
|
|
|
|
}).then(function () {
|
2013-11-13 07:52:31 +04:00
|
|
|
// Extend the loadedPlugins onto the available plugins
|
2013-11-28 06:45:01 +04:00
|
|
|
_.extend(availablePlugins, loadedPlugins);
|
2013-11-13 07:52:31 +04:00
|
|
|
}).otherwise(function (err) {
|
|
|
|
errors.logError(
|
|
|
|
err.message || err,
|
|
|
|
'The plugin will not be loaded',
|
|
|
|
'Check with the plugin creator, or read the plugin documentation for more details on plugin requirements'
|
|
|
|
);
|
2013-08-05 19:11:32 +04:00
|
|
|
});
|
|
|
|
});
|
2013-11-28 06:45:01 +04:00
|
|
|
},
|
|
|
|
availablePlugins: availablePlugins
|
2013-08-05 19:11:32 +04:00
|
|
|
};
|