add plugin config options; update pipeline plugin implementation

This commit is contained in:
Iuri Matias 2017-01-15 14:30:41 -05:00
parent 79675f5ec7
commit b72a42cfb3
4 changed files with 58 additions and 19 deletions

View File

@ -45,7 +45,7 @@ var Embark = {
this.config = new Config({env: env}); this.config = new Config({env: env});
this.config.loadConfigFiles(options); this.config.loadConfigFiles(options);
this.logger = new Logger({logLevel: 'debug'}); this.logger = new Logger({logLevel: 'debug'});
this.plugins = new Plugins({plugins: this.config.embarkConfig.plugins}); this.plugins = new Plugins({plugins: this.config.embarkConfig.plugins, logger: this.logger});
this.plugins.loadPlugins(); this.plugins.loadPlugins();
}, },
@ -138,7 +138,7 @@ var Embark = {
callback(); callback();
}, },
function displayLoadedPlugins(callback) { function displayLoadedPlugins(callback) {
var pluginList = self.plugins.pluginList; var pluginList = self.plugins.listPlugins();
if (pluginList.length > 0) { if (pluginList.length > 0) {
self.logger.info("loaded plugins: " + pluginList.join(", ")); self.logger.info("loaded plugins: " + pluginList.join(", "));
} }

View File

@ -18,21 +18,25 @@ Pipeline.prototype.build = function(abi) {
var content = this.assetFiles[targetFile].map(file => { var content = this.assetFiles[targetFile].map(file => {
self.logger.info("reading " + file.filename); self.logger.info("reading " + file.filename);
var pipelinePlugins = this.plugins.getPluginsFor('pipeline');
if (file.filename === 'embark.js') { if (file.filename === 'embark.js') {
return file.content + "\n" + abi; return file.content + "\n" + abi;
} else if (['web3.js', 'ipfs.js', 'ipfs-api.js', 'orbit.js'].indexOf(file.filename) >= 0) {
return file.content;
} else { } else {
if (pipelinePlugins.length > 0) {
pipelinePlugins.forEach(function(plugin) {
file.content = plugin.runPipeline({targetFile: file.filename, source: file.content});
});
}
return file.content; return file.content;
} }
}).join("\n"); }).join("\n");
var pipelinePlugins = this.plugins.getPluginsFor('pipeline');
if (pipelinePlugins.length > 0) {
pipelinePlugins.forEach(function(plugin) {
content = plugin.runPipeline({targetFile: targetFile, source: content});
});
}
var dir = targetFile.split('/').slice(0, -1).join('/'); var dir = targetFile.split('/').slice(0, -1).join('/');
self.logger.info("creating dir " + this.buildDir + dir); self.logger.info("creating dir " + this.buildDir + dir);
mkdirp.sync(this.buildDir + dir); mkdirp.sync(this.buildDir + dir);

View File

@ -1,18 +1,33 @@
/*jshint esversion: 6, loopfunc: true */
var grunt = require('grunt');
// TODO: pass other params like blockchainConfig, contract files, etc.. // TODO: pass other params like blockchainConfig, contract files, etc..
var Plugin = function(options) { var Plugin = function(options) {
this.name = options.name; this.name = options.name;
this.pluginModule = options.pluginModule; this.pluginModule = options.pluginModule;
this.pluginConfig = options.pluginConfig;
this.clientWeb3Providers = []; this.clientWeb3Providers = [];
this.contractsGenerators = []; this.contractsGenerators = [];
this.pipeline = []; this.pipeline = [];
this.pluginTypes = []; this.pluginTypes = [];
this.logger = options.logger;
}; };
Plugin.prototype.loadPlugin = function() { Plugin.prototype.loadPlugin = function() {
this.interceptLogs(this.pluginModule);
(this.pluginModule.call(this, this)); (this.pluginModule.call(this, this));
}; };
Plugin.prototype.interceptLogs = function(context) {
var self = this;
context.console = console;
context.console.error = function(txt) {
// TODO: logger should support an array instead of a single argument
//self.logger.error.apply(self.logger, arguments);
self.logger.error(self.name + " > " + txt);
};
};
// TODO: add deploy provider // TODO: add deploy provider
Plugin.prototype.registerClientWeb3Provider = function(cb) { Plugin.prototype.registerClientWeb3Provider = function(cb) {
this.clientWeb3Providers.push(cb); this.clientWeb3Providers.push(cb);
@ -24,9 +39,9 @@ Plugin.prototype.registerContractsGeneration = function(cb) {
this.pluginTypes.push('contractGeneration'); this.pluginTypes.push('contractGeneration');
}; };
Plugin.prototype.registerPipeline = function(cb) { Plugin.prototype.registerPipeline = function(matcthingFiles, cb) {
// TODO: generate error for more than one pipeline per plugin // TODO: generate error for more than one pipeline per plugin
this.pipeline.push(cb); this.pipeline.push({matcthingFiles: matcthingFiles, cb: cb});
this.pluginTypes.push('pipeline'); this.pluginTypes.push('pipeline');
}; };
@ -47,7 +62,17 @@ Plugin.prototype.generateContracts = function(args) {
}; };
Plugin.prototype.runPipeline = function(args) { Plugin.prototype.runPipeline = function(args) {
return this.pipeline[0].call(this, args); // TODO: should iterate the pipeliens
var pipeline = this.pipeline[0];
//var shouldRunPipeline = pipeline.matcthingFiles.some(match => {
//});
var shouldRunPipeline = grunt.file.isMatch(pipeline.matcthingFiles, args.targetFile);
if (shouldRunPipeline) {
console.log('running plugin');
return pipeline.cb.call(this, args);
} else {
return args.source;
}
}; };
module.exports = Plugin; module.exports = Plugin;

View File

@ -4,20 +4,30 @@ var path = require('path');
var Plugins = function(options) { var Plugins = function(options) {
this.pluginList = options.plugins || []; this.pluginList = options.plugins || [];
this.plugins = []; this.plugins = [];
// TODO: need backup 'NullLogger'
this.logger = options.logger;
}; };
Plugins.prototype.loadPlugins = function() { Plugins.prototype.loadPlugins = function() {
var i, plugin; var pluginConfig;
for (i = 0; i < this.pluginList.length; i++) { for (var pluginName in this.pluginList) {
plugin = this.pluginList[i]; pluginConfig = this.pluginList[pluginName];
this.loadPlugin(plugin); this.loadPlugin(pluginName, pluginConfig);
} }
}; };
Plugins.prototype.loadPlugin = function(pluginName) { Plugins.prototype.listPlugins = function() {
var list = [];
for (var className in this.pluginList) {
list.push(className);
}
return list;
};
Plugins.prototype.loadPlugin = function(pluginName, pluginConfig) {
var plugin = require(path.join(process.env.PWD, 'node_modules', pluginName)); var plugin = require(path.join(process.env.PWD, 'node_modules', pluginName));
var pluginWrapper = new Plugin({name: pluginName, pluginModule: plugin}); var pluginWrapper = new Plugin({name: pluginName, pluginModule: plugin, pluginConfig: pluginConfig, logger: this.logger});
pluginWrapper.loadPlugin(); pluginWrapper.loadPlugin();
this.plugins.push(pluginWrapper); this.plugins.push(pluginWrapper);
}; };