From 6822a9b08cfe5daf896fdcf11be6266823af3057 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 28 Jan 2017 21:31:09 -0500 Subject: [PATCH] implement compiler plugin --- lib/compiler.js | 36 +++++++++++++++++++++++++++++++++++- lib/contracts.js | 10 +++------- lib/index.js | 3 ++- lib/plugin.js | 6 ++++++ lib/test.js | 2 ++ 5 files changed, 48 insertions(+), 9 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index 3fc39dea..cf29c982 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -2,8 +2,41 @@ var shelljs = require('shelljs'); var shelljs_global = require('shelljs/global'); var fs = require('fs'); 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) { @@ -13,6 +46,7 @@ Compiler.prototype.compile_solidity = function(contractFiles) { for (var i = 0; i < contractFiles.length; i++){ // TODO: this depends on the config var filename = contractFiles[i].filename.replace('app/contracts/',''); + console.log("normal compile " + filename); input[filename] = contractFiles[i].content.toString(); } diff --git a/lib/contracts.js b/lib/contracts.js index 33973257..cd45e77b 100644 --- a/lib/contracts.js +++ b/lib/contracts.js @@ -27,22 +27,18 @@ var ContractsManager = function(options) { this.contractsConfig = options.contractsConfig; this.contracts = {}; this.logger = options.logger; + this.plugins = options.plugins; this.contractDependencies = {}; }; -ContractsManager.prototype.compileContracts = function() { - var compiler = new Compiler(); - return compiler.compile_solidity(this.contractFiles); -}; - ContractsManager.prototype.build = function(done) { var self = this; async.waterfall([ function compileContracts(callback) { - var compiler = new Compiler(); + var compiler = new Compiler({plugins: self.plugins}); try { - self.compiledContracts = compiler.compile_solidity(self.contractFiles); + self.compiledContracts = compiler.compile_contracts(self.contractFiles); } catch(err) { return callback(new Error(err.message)); } diff --git a/lib/index.js b/lib/index.js index b1540498..6c2fc826 100644 --- a/lib/index.js +++ b/lib/index.js @@ -202,7 +202,8 @@ var Embark = { var contractsManager = new ContractsManager({ contractFiles: self.config.contractsFiles, contractsConfig: self.config.contractsConfig, - logger: Embark.logger + logger: Embark.logger, + plugins: self.plugins }); contractsManager.build(callback); }, diff --git a/lib/plugin.js b/lib/plugin.js index ba7b25f5..c225a10f 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -16,6 +16,7 @@ var Plugin = function(options) { this.console = []; this.contractsConfigs = []; this.contractsFiles = []; + this.compilers = []; this.pluginTypes = []; this.logger = options.logger; }; @@ -112,6 +113,11 @@ Plugin.prototype.registerContractConfiguration = function(config) { 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) { return this.console.map(function(cb) { return cb.call(this, cmd, options); diff --git a/lib/test.js b/lib/test.js index 8835651b..4930c722 100644 --- a/lib/test.js +++ b/lib/test.js @@ -7,6 +7,8 @@ var TestLogger = require('./test_logger.js'); var Config = require('./config.js'); var ABIGenerator = require('./abi.js'); +// TODO: load config file +// TODO: include plugins var Test = function(options) { try { this.sim = require('ethereumjs-testrpc');