implement several plugins

This commit is contained in:
Iuri Matias 2017-01-26 06:34:00 -05:00
parent 9d2f9c3054
commit 2e9a4bb28a
7 changed files with 150 additions and 17 deletions

View File

@ -9,6 +9,7 @@ var Compiler = function() {
Compiler.prototype.compile_solidity = function(contractFiles) { Compiler.prototype.compile_solidity = function(contractFiles) {
var input = {}; var input = {};
// TODO: need to match only the .sol files
for (var i = 0; i < contractFiles.length; i++){ for (var i = 0; i < contractFiles.length; i++){
// TODO: this depends on the config // TODO: this depends on the config
var filename = contractFiles[i].filename.replace('app/contracts/',''); var filename = contractFiles[i].filename.replace('app/contracts/','');

View File

@ -2,6 +2,7 @@ var fs = require('fs');
var grunt = require('grunt'); var grunt = require('grunt');
var merge = require('merge'); var merge = require('merge');
var path = require('path'); var path = require('path');
var Plugins = require('./plugins.js');
// TODO: add wrapper for fs so it can also work in the browser // TODO: add wrapper for fs so it can also work in the browser
// can work with both read and save // can work with both read and save
@ -15,6 +16,7 @@ var Config = function(options) {
this.contractsFiles = []; this.contractsFiles = [];
this.configDir = options.configDir || 'config/'; this.configDir = options.configDir || 'config/';
this.chainsFile = options.chainsFile || './chains.json'; this.chainsFile = options.chainsFile || './chains.json';
this.logger = options.logger;
}; };
Config.prototype.loadConfigFiles = function(options) { Config.prototype.loadConfigFiles = function(options) {
@ -22,8 +24,13 @@ Config.prototype.loadConfigFiles = function(options) {
this.loadPipelineConfigFile(); this.loadPipelineConfigFile();
this.loadBlockchainConfigFile(); this.loadBlockchainConfigFile();
this.plugins = new Plugins({plugins: this.embarkConfig.plugins, logger: this.logger});
this.plugins.loadPlugins();
this.loadContractsConfigFile(); this.loadContractsConfigFile();
this.loadChainTrackerFile(); this.loadChainTrackerFile();
this.loadPluginContractFiles();
}; };
Config.prototype.reloadConfig = function() { Config.prototype.reloadConfig = function() {
@ -39,11 +46,25 @@ Config.prototype.loadBlockchainConfigFile = function() {
}; };
Config.prototype.loadContractsConfigFile = function() { Config.prototype.loadContractsConfigFile = function() {
var configObject = {};
var configPlugins = this.plugins.getPluginsFor('contractsConfig');
if (configPlugins.length > 0) {
configPlugins.forEach(function(plugin) {
plugin.contractsConfigs.forEach(function(pluginConfig) {
configObject = merge.recursive(configObject, pluginConfig);
});
});
}
var contractsConfig = JSON.parse(fs.readFileSync(this.configDir + "contracts.json")); var contractsConfig = JSON.parse(fs.readFileSync(this.configDir + "contracts.json"));
var defaultContractsConfig = contractsConfig['default']; configObject = merge.recursive(configObject, contractsConfig);
var envContractsConfig = contractsConfig[this.env]; var defaultContractsConfig = configObject['default'];
var envContractsConfig = configObject[this.env];
var mergedConfig = merge.recursive(defaultContractsConfig, envContractsConfig); var mergedConfig = merge.recursive(defaultContractsConfig, envContractsConfig);
console.log(JSON.stringify(mergedConfig));
this.contractsConfig = mergedConfig; this.contractsConfig = mergedConfig;
}; };
@ -82,18 +103,33 @@ Config.prototype.loadFiles = function(files) {
return file.indexOf('.') >= 0; return file.indexOf('.') >= 0;
}).filter(function(file) { }).filter(function(file) {
if (file === 'embark.js') { if (file === 'embark.js') {
readFiles.push({filename: 'web3.js', content: fs.readFileSync(path.join(__dirname, "/../js/web3.js")).toString()}); readFiles.push({filename: 'web3.js', content: fs.readFileSync(path.join(__dirname, "/../js/web3.js")).toString(), path: path.join(__dirname, "/../js/web3.js")});
readFiles.push({filename: 'ipfs.js', content: fs.readFileSync(path.join(__dirname, "/../js/ipfs.js")).toString()}); readFiles.push({filename: 'ipfs.js', content: fs.readFileSync(path.join(__dirname, "/../js/ipfs.js")).toString(), path: path.join(__dirname, "/../js/ipfs.js")});
// TODO: remove duplicated files if funcitonality is the same for storage and orbit // TODO: remove duplicated files if funcitonality is the same for storage and orbit
readFiles.push({filename: 'ipfs-api.js', content: fs.readFileSync(path.join(__dirname, "/../js/ipfs-api.min.js")).toString()}); readFiles.push({filename: 'ipfs-api.js', content: fs.readFileSync(path.join(__dirname, "/../js/ipfs-api.min.js")).toString(), path: path.join(__dirname, "/../js/ipfs-api.min.js")});
readFiles.push({filename: 'orbit.js', content: fs.readFileSync(path.join(__dirname, "/../js/orbit.min.js")).toString()}); readFiles.push({filename: 'orbit.js', content: fs.readFileSync(path.join(__dirname, "/../js/orbit.min.js")).toString(), path: path.join(__dirname, "/../js/orbit.min.js")});
readFiles.push({filename: 'embark.js', content: fs.readFileSync(path.join(__dirname, "/../js/build/embark.bundle.js")).toString()}); readFiles.push({filename: 'embark.js', content: fs.readFileSync(path.join(__dirname, "/../js/build/embark.bundle.js")).toString(), path: path.join(__dirname, "/../js/build/embark.bundle.js")});
readFiles.push({filename: 'embark-plugins.js', content: "", path: ""});
} else { } else {
readFiles.push({filename: file, content: fs.readFileSync(file).toString()}); readFiles.push({filename: file, content: fs.readFileSync(file).toString(), path: file});
} }
}); });
return readFiles; return readFiles;
}; };
Config.prototype.loadPluginContractFiles = function() {
var self = this;
var contractsPlugins = this.plugins.getPluginsFor('contractFiles');
if (contractsPlugins.length > 0) {
contractsPlugins.forEach(function(plugin) {
plugin.contractsFiles.forEach(function(file) {
var filename = file.replace('./','');
self.contractsFiles.push({filename: filename, content: plugin.loadPluginFile(file), path: plugin.pathToFile(file)});
});
});
}
};
module.exports = Config; module.exports = Config;

View File

@ -8,7 +8,11 @@ var adjustGas = function(contract) {
var maxGas, adjustedGas; var maxGas, adjustedGas;
if (contract.gas === 'auto') { if (contract.gas === 'auto') {
if (contract.deploy) { if (contract.deploy) {
maxGas = Math.max(contract.gasEstimates.creation[0], contract.gasEstimates.creation[1], 500000); if (contract.gasEstimates.creation !== undefined) {
maxGas = Math.max(contract.gasEstimates.creation[0], contract.gasEstimates.creation[1], 500000);
} else {
maxGas = 500000;
}
} else { } else {
maxGas = 500000; maxGas = 500000;
} }

View File

@ -25,7 +25,6 @@ var ServicesMonitor = require('./services.js');
var Console = require('./console.js'); var Console = require('./console.js');
var IPFS = require('./ipfs.js'); var IPFS = require('./ipfs.js');
var Swarm = require('./swarm.js'); var Swarm = require('./swarm.js');
var Plugins = require('./plugins.js');
var Embark = { var Embark = {
@ -42,11 +41,12 @@ var Embark = {
}, },
initConfig: function(env, options) { initConfig: function(env, options) {
this.config = new Config({env: env});
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, logger: this.logger});
this.plugins.loadPlugins(); this.config = new Config({env: env, logger: this.logger});
this.config.loadConfigFiles(options);
this.plugins = this.config.plugins;
}, },
redeploy: function(env) { redeploy: function(env) {

View File

@ -23,13 +23,61 @@ Pipeline.prototype.build = function(abi) {
if (file.filename === 'embark.js') { if (file.filename === 'embark.js') {
return file.content + "\n" + abi; return file.content + "\n" + abi;
} else if (file.filename === 'embark-plugins.js') {
var filesFromPlugins = [];
var filePlugins = self.plugins.getPluginsFor('pipelineFiles');
if (filePlugins.length > 0) {
filePlugins.forEach(function(plugin) {
try {
var fileObjects = plugin.runFilePipeline();
for (var i=0; i < fileObjects.length; i++) {
var fileObject = fileObjects[i];
console.debug(JSON.stringify(fileObject));
filesFromPlugins.push(fileObject);
}
}
catch(err) {
self.logger.error(err.message);
}
});
}
var fileContents = filesFromPlugins.map(function(file) {
if (pipelinePlugins.length > 0) {
pipelinePlugins.forEach(function(plugin) {
console.log(plugin.name + ": trying " + file.filename);
try {
if (file.options && file.options.skipPipeline) {
console.log("skipping");
return;
}
file.content = plugin.runPipeline({targetFile: file.filename, source: file.content});
}
catch(err) {
self.logger.error(err.message);
}
});
}
return file.content;
});
return fileContents.join('\n');
} else if (['web3.js', 'ipfs.js', 'ipfs-api.js', 'orbit.js'].indexOf(file.filename) >= 0) { } else if (['web3.js', 'ipfs.js', 'ipfs-api.js', 'orbit.js'].indexOf(file.filename) >= 0) {
return file.content; return file.content;
} else { } else {
if (pipelinePlugins.length > 0) { if (pipelinePlugins.length > 0) {
pipelinePlugins.forEach(function(plugin) { pipelinePlugins.forEach(function(plugin) {
file.content = plugin.runPipeline({targetFile: file.filename, source: file.content}); try {
file.content = plugin.runPipeline({targetFile: file.filename, source: file.content});
}
catch(err) {
self.logger.error(err.message);
}
}); });
} }

View File

@ -1,15 +1,21 @@
/*jshint esversion: 6, loopfunc: true */ /*jshint esversion: 6, loopfunc: true */
var grunt = require('grunt'); var grunt = require('grunt');
var fs = require('fs');
var path = require('path');
// 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.pluginPath = options.pluginPath;
this.pluginConfig = options.pluginConfig; this.pluginConfig = options.pluginConfig;
this.clientWeb3Providers = []; this.clientWeb3Providers = [];
this.contractsGenerators = []; this.contractsGenerators = [];
this.pipeline = []; this.pipeline = [];
this.pipelineFiles = [];
this.console = []; this.console = [];
this.contractsConfigs = [];
this.contractsFiles = [];
this.pluginTypes = []; this.pluginTypes = [];
this.logger = options.logger; this.logger = options.logger;
}; };
@ -19,6 +25,14 @@ Plugin.prototype.loadPlugin = function() {
(this.pluginModule.call(this, this)); (this.pluginModule.call(this, this));
}; };
Plugin.prototype.loadPluginFile = function(filename) {
return fs.readFileSync(this.pathToFile(filename)).toString();
};
Plugin.prototype.pathToFile = function(filename) {
return path.join(this.pluginPath, filename);
};
Plugin.prototype.interceptLogs = function(context) { Plugin.prototype.interceptLogs = function(context) {
var self = this; var self = this;
context.console = console; context.console = console;
@ -37,6 +51,7 @@ Plugin.prototype.interceptLogs = function(context) {
self.logger.info(self.name + " > " + txt); self.logger.info(self.name + " > " + txt);
}; };
context.console.debug = function(txt) { context.console.debug = function(txt) {
// TODO: ue JSON.stringify
self.logger.debug(self.name + " > " + txt); self.logger.debug(self.name + " > " + txt);
}; };
context.console.trace = function(txt) { context.console.trace = function(txt) {
@ -61,6 +76,16 @@ Plugin.prototype.registerPipeline = function(matcthingFiles, cb) {
this.pluginTypes.push('pipeline'); this.pluginTypes.push('pipeline');
}; };
Plugin.prototype.addFileToPipeline = function(file, options) {
this.pipelineFiles.push({file: file, options: options});
this.pluginTypes.push('pipelineFiles');
};
Plugin.prototype.addContractFile = function(file) {
this.contractsFiles.push(file);
this.pluginTypes.push('contractFiles');
};
Plugin.prototype.registerConsoleCommand = function(cb) { Plugin.prototype.registerConsoleCommand = function(cb) {
this.console.push(cb); this.console.push(cb);
this.pluginTypes.push('console'); this.pluginTypes.push('console');
@ -82,12 +107,30 @@ Plugin.prototype.generateContracts = function(args) {
}).join("\n"); }).join("\n");
}; };
Plugin.prototype.registerContractConfiguration = function(config) {
this.contractsConfigs.push(config);
this.pluginTypes.push('contractsConfig');
};
Plugin.prototype.runCommands = function(cmd, options) { Plugin.prototype.runCommands = function(cmd, options) {
return this.console.map(function(cb) { return this.console.map(function(cb) {
return cb.call(this, cmd, options); return cb.call(this, cmd, options);
}).join("\n"); }).join("\n");
}; };
Plugin.prototype.runFilePipeline = function() {
var self = this;
return this.pipelineFiles.map(function(file) {
var obj = {};
obj.filename = file.file.replace('./','');
obj.content = self.loadPluginFile(file.file).toString();
obj.options = file.options;
return obj;
});
};
Plugin.prototype.runPipeline = function(args) { Plugin.prototype.runPipeline = function(args) {
// TODO: should iterate the pipeliens // TODO: should iterate the pipeliens
var pipeline = this.pipeline[0]; var pipeline = this.pipeline[0];

View File

@ -25,9 +25,10 @@ Plugins.prototype.listPlugins = function() {
}; };
Plugins.prototype.loadPlugin = function(pluginName, pluginConfig) { Plugins.prototype.loadPlugin = function(pluginName, pluginConfig) {
var plugin = require(path.join(process.env.PWD, 'node_modules', pluginName)); var pluginPath = path.join(process.env.PWD, 'node_modules', pluginName);
var plugin = require(pluginPath);
var pluginWrapper = new Plugin({name: pluginName, pluginModule: plugin, pluginConfig: pluginConfig, logger: this.logger}); var pluginWrapper = new Plugin({name: pluginName, pluginModule: plugin, pluginConfig: pluginConfig, logger: this.logger, pluginPath: pluginPath});
pluginWrapper.loadPlugin(); pluginWrapper.loadPlugin();
this.plugins.push(pluginWrapper); this.plugins.push(pluginWrapper);
}; };