2017-01-15 19:30:41 +00:00
|
|
|
/*jshint esversion: 6, loopfunc: true */
|
2017-03-29 15:37:30 +00:00
|
|
|
let fs = require('./fs.js');
|
|
|
|
let utils = require('../utils/utils.js');
|
|
|
|
let camelcase = require("underscore.string").camelcase;
|
|
|
|
|
|
|
|
let Plugin = function(options) {
|
|
|
|
this.config = {};
|
|
|
|
for (let opt in options) {
|
|
|
|
if (options.hasOwnProperty(opt)) {
|
|
|
|
this.config[opt] = options[opt];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let requiredOptions = ['name', 'pluginModule', 'pluginPath', 'config', 'interceptLogs', 'logger'];
|
|
|
|
|
|
|
|
for (let i = 0; requiredOptions.length > i; i++) {
|
|
|
|
if (!(utils.contains(Object.keys(this.config), requiredOptions[i]))) {
|
|
|
|
throw new Error('Missing required plugin configuration key: ' + requiredOptions[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 02:33:31 +00:00
|
|
|
this.clientWeb3Providers = [];
|
|
|
|
this.contractsGenerators = [];
|
2016-12-10 15:20:04 +00:00
|
|
|
this.pipeline = [];
|
2017-01-26 11:34:00 +00:00
|
|
|
this.pipelineFiles = [];
|
2017-01-16 12:00:41 +00:00
|
|
|
this.console = [];
|
2017-01-26 11:34:00 +00:00
|
|
|
this.contractsConfigs = [];
|
|
|
|
this.contractsFiles = [];
|
2017-01-29 02:31:09 +00:00
|
|
|
this.compilers = [];
|
2017-03-16 11:31:52 +00:00
|
|
|
this.serviceChecks = [];
|
2016-12-07 02:33:31 +00:00
|
|
|
this.pluginTypes = [];
|
2017-03-29 15:37:30 +00:00
|
|
|
|
|
|
|
if (!(this instanceof Plugin)) {
|
|
|
|
return new Plugin();
|
|
|
|
}
|
2016-12-07 02:33:31 +00:00
|
|
|
};
|
|
|
|
|
2017-03-29 15:37:30 +00:00
|
|
|
Plugin.prototype.runPlugin = Plugin.prototype.run = function() {
|
2017-02-06 11:42:58 +00:00
|
|
|
if (this.shouldInterceptLogs) {
|
|
|
|
this.interceptLogs(this.pluginModule);
|
|
|
|
}
|
2017-03-29 15:37:30 +00:00
|
|
|
let fullyQualifiedPath = this.pathToFile(this.config.pluginModule);
|
|
|
|
this.call(this.loadPluginFile(fullyQualifiedPath), this);
|
2016-12-07 02:33:31 +00:00
|
|
|
};
|
|
|
|
|
2017-01-26 11:34:00 +00:00
|
|
|
Plugin.prototype.loadPluginFile = function(filename) {
|
|
|
|
return fs.readFileSync(this.pathToFile(filename)).toString();
|
|
|
|
};
|
|
|
|
|
|
|
|
Plugin.prototype.pathToFile = function(filename) {
|
2017-02-18 19:10:01 +00:00
|
|
|
return utils.joinPath(this.pluginPath, filename);
|
2017-01-26 11:34:00 +00:00
|
|
|
};
|
|
|
|
|
2017-01-15 19:30:41 +00:00
|
|
|
Plugin.prototype.interceptLogs = function(context) {
|
2017-03-29 15:37:30 +00:00
|
|
|
let self = this;
|
2017-02-06 11:42:58 +00:00
|
|
|
// TODO: this is a bit nasty, figure out a better way
|
|
|
|
context.console = context.console || console;
|
2017-02-26 23:47:52 +00:00
|
|
|
|
|
|
|
//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);
|
|
|
|
//};
|
2017-01-16 12:00:41 +00:00
|
|
|
context.console.log = function(txt) {
|
|
|
|
self.logger.info(self.name + " > " + txt);
|
|
|
|
};
|
|
|
|
context.console.warn = function(txt) {
|
|
|
|
self.logger.warn(self.name + " > " + txt);
|
|
|
|
};
|
|
|
|
context.console.info = function(txt) {
|
|
|
|
self.logger.info(self.name + " > " + txt);
|
|
|
|
};
|
|
|
|
context.console.debug = function(txt) {
|
2017-01-26 11:34:00 +00:00
|
|
|
// TODO: ue JSON.stringify
|
2017-01-16 12:00:41 +00:00
|
|
|
self.logger.debug(self.name + " > " + txt);
|
|
|
|
};
|
|
|
|
context.console.trace = function(txt) {
|
|
|
|
self.logger.trace(self.name + " > " + txt);
|
|
|
|
};
|
2017-01-15 19:30:41 +00:00
|
|
|
};
|
|
|
|
|
2017-03-29 15:37:30 +00:00
|
|
|
Plugin.prototype.register = function (classname, cb) {
|
|
|
|
let camelCasedClassname = camelcase(classname);
|
|
|
|
this[camelCasedClassname].push(cb);
|
|
|
|
this.pluginTypes.push({class: classname, call: cb});
|
|
|
|
};
|
|
|
|
|
|
|
|
["ClientWeb3Provider", "ContractsGeneration", "Pipeline", "Deployer",
|
|
|
|
"ConsoleCommand", "ServiceCheck", "ContractConfiguration", "Compiler"].forEach(function (name) {
|
|
|
|
Plugin.prototype["register" + name] = function (cb) {
|
|
|
|
Plugin.prototype.register.call(name, cb);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
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');
|
|
|
|
};
|
|
|
|
|
2017-02-03 11:30:08 +00:00
|
|
|
Plugin.prototype.addFileToPipeline = function(file, intendedPath, options) {
|
|
|
|
this.pipelineFiles.push({file: file, intendedPath: intendedPath, options: options});
|
2017-01-26 11:34:00 +00:00
|
|
|
this.pluginTypes.push('pipelineFiles');
|
|
|
|
};
|
|
|
|
|
|
|
|
Plugin.prototype.addContractFile = function(file) {
|
|
|
|
this.contractsFiles.push(file);
|
|
|
|
this.pluginTypes.push('contractFiles');
|
|
|
|
};
|
|
|
|
|
2017-01-16 12:00:41 +00:00
|
|
|
Plugin.prototype.registerConsoleCommand = function(cb) {
|
|
|
|
this.console.push(cb);
|
|
|
|
this.pluginTypes.push('console');
|
|
|
|
};
|
|
|
|
|
2017-03-16 11:31:52 +00:00
|
|
|
Plugin.prototype.registerServiceCheck = function(checkName, checkFn, time) {
|
|
|
|
this.serviceChecks.push({checkName: checkName, checkFn: checkFn, time: time});
|
|
|
|
this.pluginTypes.push('serviceChecks');
|
|
|
|
};
|
|
|
|
|
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");
|
|
|
|
};
|
|
|
|
|
2017-01-26 11:34:00 +00:00
|
|
|
Plugin.prototype.registerContractConfiguration = function(config) {
|
|
|
|
this.contractsConfigs.push(config);
|
|
|
|
this.pluginTypes.push('contractsConfig');
|
|
|
|
};
|
|
|
|
|
2017-01-29 02:31:09 +00:00
|
|
|
Plugin.prototype.registerCompiler = function(extension, cb) {
|
|
|
|
this.compilers.push({extension: extension, cb: cb});
|
|
|
|
this.pluginTypes.push('compilers');
|
|
|
|
};
|
|
|
|
|
2017-01-16 12:00:41 +00:00
|
|
|
Plugin.prototype.runCommands = function(cmd, options) {
|
|
|
|
return this.console.map(function(cb) {
|
|
|
|
return cb.call(this, cmd, options);
|
|
|
|
}).join("\n");
|
|
|
|
};
|
|
|
|
|
2017-01-26 11:34:00 +00:00
|
|
|
Plugin.prototype.runFilePipeline = function() {
|
2017-03-29 15:37:30 +00:00
|
|
|
let self = this;
|
2017-01-26 11:34:00 +00:00
|
|
|
|
|
|
|
return this.pipelineFiles.map(function(file) {
|
2017-03-29 15:37:30 +00:00
|
|
|
let obj = {};
|
2017-01-26 11:34:00 +00:00
|
|
|
obj.filename = file.file.replace('./','');
|
|
|
|
obj.content = self.loadPluginFile(file.file).toString();
|
2017-02-03 11:30:08 +00:00
|
|
|
obj.intendedPath = file.intendedPath;
|
2017-01-26 11:34:00 +00:00
|
|
|
obj.options = file.options;
|
2017-02-03 11:30:08 +00:00
|
|
|
obj.path = self.pathToFile(obj.filename);
|
2017-01-26 11:34:00 +00:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-12-10 15:20:04 +00:00
|
|
|
Plugin.prototype.runPipeline = function(args) {
|
2017-02-07 02:08:11 +00:00
|
|
|
// TODO: should iterate the pipelines
|
2017-03-29 15:37:30 +00:00
|
|
|
let pipeline = this.pipeline[0];
|
|
|
|
let shouldRunPipeline = utils.fileMatchesPattern(pipeline.matcthingFiles, args.targetFile);
|
2017-01-15 19:30:41 +00:00
|
|
|
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;
|