implement compiler plugin

This commit is contained in:
Iuri Matias 2017-01-28 21:31:09 -05:00
parent 878b5aa2da
commit 6822a9b08c
5 changed files with 48 additions and 9 deletions

View File

@ -2,8 +2,41 @@ var shelljs = require('shelljs');
var shelljs_global = require('shelljs/global'); var shelljs_global = require('shelljs/global');
var fs = require('fs'); var fs = require('fs');
var solc = require('solc'); var solc = require('solc');
var merge = require('merge');
var Compiler = function() { var Compiler = function(options) {
this.plugins = options.plugins;
};
Compiler.prototype.compile_contracts = function(contractFiles) {
var available_compilers = {
//".se": this.compile_serpent
".sol": this.compile_solidity
};
var compilerPlugins = this.plugins.getPluginsFor('compilers');
if (compilerPlugins.length > 0) {
compilerPlugins.forEach(function(plugin) {
plugin.compilers.forEach(function(compilerObject) {
available_compilers[compilerObject.extension] = compilerObject.cb;
});
});
}
var compiledObject = {};
// TODO: warn about files it doesn't know how to compile
for (var extension in available_compilers) {
var compiler = available_compilers[extension];
var matchingFiles = contractFiles.filter(function(file) {
return (file.filename.match(/\.[0-9a-z]+$/)[0] === extension);
});
Object.assign(compiledObject, compiler.call(compiler, matchingFiles || []));
}
return compiledObject;
}; };
Compiler.prototype.compile_solidity = function(contractFiles) { Compiler.prototype.compile_solidity = function(contractFiles) {
@ -13,6 +46,7 @@ Compiler.prototype.compile_solidity = function(contractFiles) {
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/','');
console.log("normal compile " + filename);
input[filename] = contractFiles[i].content.toString(); input[filename] = contractFiles[i].content.toString();
} }

View File

@ -27,22 +27,18 @@ var ContractsManager = function(options) {
this.contractsConfig = options.contractsConfig; this.contractsConfig = options.contractsConfig;
this.contracts = {}; this.contracts = {};
this.logger = options.logger; this.logger = options.logger;
this.plugins = options.plugins;
this.contractDependencies = {}; this.contractDependencies = {};
}; };
ContractsManager.prototype.compileContracts = function() {
var compiler = new Compiler();
return compiler.compile_solidity(this.contractFiles);
};
ContractsManager.prototype.build = function(done) { ContractsManager.prototype.build = function(done) {
var self = this; var self = this;
async.waterfall([ async.waterfall([
function compileContracts(callback) { function compileContracts(callback) {
var compiler = new Compiler(); var compiler = new Compiler({plugins: self.plugins});
try { try {
self.compiledContracts = compiler.compile_solidity(self.contractFiles); self.compiledContracts = compiler.compile_contracts(self.contractFiles);
} catch(err) { } catch(err) {
return callback(new Error(err.message)); return callback(new Error(err.message));
} }

View File

@ -202,7 +202,8 @@ var Embark = {
var contractsManager = new ContractsManager({ var contractsManager = new ContractsManager({
contractFiles: self.config.contractsFiles, contractFiles: self.config.contractsFiles,
contractsConfig: self.config.contractsConfig, contractsConfig: self.config.contractsConfig,
logger: Embark.logger logger: Embark.logger,
plugins: self.plugins
}); });
contractsManager.build(callback); contractsManager.build(callback);
}, },

View File

@ -16,6 +16,7 @@ var Plugin = function(options) {
this.console = []; this.console = [];
this.contractsConfigs = []; this.contractsConfigs = [];
this.contractsFiles = []; this.contractsFiles = [];
this.compilers = [];
this.pluginTypes = []; this.pluginTypes = [];
this.logger = options.logger; this.logger = options.logger;
}; };
@ -112,6 +113,11 @@ Plugin.prototype.registerContractConfiguration = function(config) {
this.pluginTypes.push('contractsConfig'); this.pluginTypes.push('contractsConfig');
}; };
Plugin.prototype.registerCompiler = function(extension, cb) {
this.compilers.push({extension: extension, cb: cb});
this.pluginTypes.push('compilers');
};
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);

View File

@ -7,6 +7,8 @@ var TestLogger = require('./test_logger.js');
var Config = require('./config.js'); var Config = require('./config.js');
var ABIGenerator = require('./abi.js'); var ABIGenerator = require('./abi.js');
// TODO: load config file
// TODO: include plugins
var Test = function(options) { var Test = function(options) {
try { try {
this.sim = require('ethereumjs-testrpc'); this.sim = require('ethereumjs-testrpc');