mirror of https://github.com/embarklabs/embark.git
add plugin config options; update pipeline plugin implementation
This commit is contained in:
parent
79675f5ec7
commit
b72a42cfb3
|
@ -45,7 +45,7 @@ var Embark = {
|
|||
this.config = new Config({env: env});
|
||||
this.config.loadConfigFiles(options);
|
||||
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();
|
||||
},
|
||||
|
||||
|
@ -138,7 +138,7 @@ var Embark = {
|
|||
callback();
|
||||
},
|
||||
function displayLoadedPlugins(callback) {
|
||||
var pluginList = self.plugins.pluginList;
|
||||
var pluginList = self.plugins.listPlugins();
|
||||
if (pluginList.length > 0) {
|
||||
self.logger.info("loaded plugins: " + pluginList.join(", "));
|
||||
}
|
||||
|
|
|
@ -18,21 +18,25 @@ Pipeline.prototype.build = function(abi) {
|
|||
|
||||
var content = this.assetFiles[targetFile].map(file => {
|
||||
self.logger.info("reading " + file.filename);
|
||||
|
||||
var pipelinePlugins = this.plugins.getPluginsFor('pipeline');
|
||||
|
||||
if (file.filename === 'embark.js') {
|
||||
return file.content + "\n" + abi;
|
||||
} else if (['web3.js', 'ipfs.js', 'ipfs-api.js', 'orbit.js'].indexOf(file.filename) >= 0) {
|
||||
return file.content;
|
||||
} else {
|
||||
|
||||
if (pipelinePlugins.length > 0) {
|
||||
pipelinePlugins.forEach(function(plugin) {
|
||||
file.content = plugin.runPipeline({targetFile: file.filename, source: file.content});
|
||||
});
|
||||
}
|
||||
|
||||
return file.content;
|
||||
}
|
||||
}).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('/');
|
||||
self.logger.info("creating dir " + this.buildDir + dir);
|
||||
mkdirp.sync(this.buildDir + dir);
|
||||
|
|
|
@ -1,18 +1,33 @@
|
|||
/*jshint esversion: 6, loopfunc: true */
|
||||
var grunt = require('grunt');
|
||||
|
||||
// TODO: pass other params like blockchainConfig, contract files, etc..
|
||||
var Plugin = function(options) {
|
||||
this.name = options.name;
|
||||
this.pluginModule = options.pluginModule;
|
||||
this.pluginConfig = options.pluginConfig;
|
||||
this.clientWeb3Providers = [];
|
||||
this.contractsGenerators = [];
|
||||
this.pipeline = [];
|
||||
this.pluginTypes = [];
|
||||
this.logger = options.logger;
|
||||
};
|
||||
|
||||
Plugin.prototype.loadPlugin = function() {
|
||||
this.interceptLogs(this.pluginModule);
|
||||
(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
|
||||
Plugin.prototype.registerClientWeb3Provider = function(cb) {
|
||||
this.clientWeb3Providers.push(cb);
|
||||
|
@ -24,9 +39,9 @@ Plugin.prototype.registerContractsGeneration = function(cb) {
|
|||
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
|
||||
this.pipeline.push(cb);
|
||||
this.pipeline.push({matcthingFiles: matcthingFiles, cb: cb});
|
||||
this.pluginTypes.push('pipeline');
|
||||
};
|
||||
|
||||
|
@ -47,7 +62,17 @@ Plugin.prototype.generateContracts = 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;
|
||||
|
|
|
@ -4,20 +4,30 @@ var path = require('path');
|
|||
var Plugins = function(options) {
|
||||
this.pluginList = options.plugins || [];
|
||||
this.plugins = [];
|
||||
// TODO: need backup 'NullLogger'
|
||||
this.logger = options.logger;
|
||||
};
|
||||
|
||||
Plugins.prototype.loadPlugins = function() {
|
||||
var i, plugin;
|
||||
for (i = 0; i < this.pluginList.length; i++) {
|
||||
plugin = this.pluginList[i];
|
||||
this.loadPlugin(plugin);
|
||||
var pluginConfig;
|
||||
for (var pluginName in this.pluginList) {
|
||||
pluginConfig = this.pluginList[pluginName];
|
||||
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 pluginWrapper = new Plugin({name: pluginName, pluginModule: plugin});
|
||||
var pluginWrapper = new Plugin({name: pluginName, pluginModule: plugin, pluginConfig: pluginConfig, logger: this.logger});
|
||||
pluginWrapper.loadPlugin();
|
||||
this.plugins.push(pluginWrapper);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue