2017-01-15 19:30:41 +00:00
|
|
|
/*jshint esversion: 6, loopfunc: true */
|
|
|
|
var grunt = require('grunt');
|
2016-12-07 02:33:31 +00:00
|
|
|
|
|
|
|
// TODO: pass other params like blockchainConfig, contract files, etc..
|
|
|
|
var Plugin = function(options) {
|
|
|
|
this.name = options.name;
|
|
|
|
this.pluginModule = options.pluginModule;
|
2017-01-15 19:30:41 +00:00
|
|
|
this.pluginConfig = options.pluginConfig;
|
2016-12-07 02:33:31 +00:00
|
|
|
this.clientWeb3Providers = [];
|
|
|
|
this.contractsGenerators = [];
|
2016-12-10 15:20:04 +00:00
|
|
|
this.pipeline = [];
|
2016-12-07 02:33:31 +00:00
|
|
|
this.pluginTypes = [];
|
2017-01-15 19:30:41 +00:00
|
|
|
this.logger = options.logger;
|
2016-12-07 02:33:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Plugin.prototype.loadPlugin = function() {
|
2017-01-15 19:30:41 +00:00
|
|
|
this.interceptLogs(this.pluginModule);
|
2016-12-07 02:33:31 +00:00
|
|
|
(this.pluginModule.call(this, this));
|
|
|
|
};
|
|
|
|
|
2017-01-15 19:30:41 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-12-07 02:33:31 +00:00
|
|
|
// TODO: add deploy provider
|
|
|
|
Plugin.prototype.registerClientWeb3Provider = function(cb) {
|
|
|
|
this.clientWeb3Providers.push(cb);
|
|
|
|
this.pluginTypes.push('clientWeb3Provider');
|
|
|
|
};
|
|
|
|
|
|
|
|
Plugin.prototype.registerContractsGeneration = function(cb) {
|
|
|
|
this.contractsGenerators.push(cb);
|
|
|
|
this.pluginTypes.push('contractGeneration');
|
|
|
|
};
|
|
|
|
|
2017-01-15 19:30:41 +00:00
|
|
|
Plugin.prototype.registerPipeline = function(matcthingFiles, cb) {
|
2016-12-10 15:20:04 +00:00
|
|
|
// TODO: generate error for more than one pipeline per plugin
|
2017-01-15 19:30:41 +00:00
|
|
|
this.pipeline.push({matcthingFiles: matcthingFiles, cb: cb});
|
2016-12-10 15:20:04 +00:00
|
|
|
this.pluginTypes.push('pipeline');
|
|
|
|
};
|
|
|
|
|
2016-12-07 02:33:31 +00:00
|
|
|
Plugin.prototype.has = function(pluginType) {
|
|
|
|
return this.pluginTypes.indexOf(pluginType) >= 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
Plugin.prototype.generateProvider = function(args) {
|
|
|
|
return this.clientWeb3Providers.map(function(cb) {
|
|
|
|
return cb.call(this, args);
|
|
|
|
}).join("\n");
|
|
|
|
};
|
|
|
|
|
|
|
|
Plugin.prototype.generateContracts = function(args) {
|
|
|
|
return this.contractsGenerators.map(function(cb) {
|
|
|
|
return cb.call(this, args);
|
|
|
|
}).join("\n");
|
|
|
|
};
|
|
|
|
|
2016-12-10 15:20:04 +00:00
|
|
|
Plugin.prototype.runPipeline = function(args) {
|
2017-01-15 19:30:41 +00:00
|
|
|
// 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) {
|
|
|
|
return pipeline.cb.call(this, args);
|
|
|
|
} else {
|
|
|
|
return args.source;
|
|
|
|
}
|
2016-12-10 15:20:04 +00:00
|
|
|
};
|
|
|
|
|
2016-12-07 02:33:31 +00:00
|
|
|
module.exports = Plugin;
|