From 467713fe8701f4a66e3a7534af494b53a82f2625 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 30 Aug 2015 21:35:34 -0400 Subject: [PATCH] support solc compiler; display syntax errors --- lib/compiler.js | 29 +++++++++++++++++++++++-- lib/config/contracts.js | 3 +-- test/compiler.js | 37 ++++++++++++++++++++++++++++++++ test/support/contracts/error.sol | 14 ++++++++++++ 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 test/compiler.js create mode 100644 test/support/contracts/error.sol diff --git a/lib/compiler.js b/lib/compiler.js index e57af716a..56b373c48 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -1,3 +1,5 @@ +var shelljs = require('shelljs'); +var shelljs_global = require('shelljs/global'); var web3 = require('web3'); Compiler = function(blockchainConfig) { @@ -18,8 +20,31 @@ Compiler.prototype.init = function(env) { console.log("address is : " + primaryAddress); }; -Compiler.prototype.compile = function(source) { - return web3.eth.compile.solidity(source); +Compiler.prototype.compile = function(contractFile) { + var cmd, result, output, json, compiled_object; + + cmd = "solc --input-file " + contractFile + " --combined-json binary,json-abi"; + + result = exec(cmd, {silent: true}); + output = result.output; + + if (result.code === 1) { + throw new Error(result.output); + } + + json = JSON.parse(output).contracts; + compiled_object = {} + + for (var className in json) { + var contract = json[className]; + + compiled_object[className] = {}; + compiled_object[className].code = contract.binary; + compiled_object[className].info = {}; + compiled_object[className].info.abiDefinition = JSON.parse(contract["json-abi"]); + } + + return compiled_object; }; module.exports = Compiler; diff --git a/lib/config/contracts.js b/lib/config/contracts.js index 5a42e00ac..ee9624421 100644 --- a/lib/config/contracts.js +++ b/lib/config/contracts.js @@ -79,9 +79,8 @@ ContractsConfig.prototype.compileContracts = function(env) { // compile files for (j = 0; j < this.contractFiles.length; j++) { contractFile = this.contractFiles[j]; - source = fs.readFileSync(contractFile).toString() - compiled_contracts = this.compiler.compile(source); + compiled_contracts = this.compiler.compile(contractFile); for (var className in compiled_contracts) { var contract = compiled_contracts[className]; diff --git a/test/compiler.js b/test/compiler.js new file mode 100644 index 000000000..196be7916 --- /dev/null +++ b/test/compiler.js @@ -0,0 +1,37 @@ +var Compiler = require('../lib/compiler.js'); +var assert = require('assert'); + +describe('embark.compiler', function() { + + describe('compile a file', function() { + var files = [ + 'test/support/contracts/simple_storage.sol' + ]; + + it("should build a correct compiled object", function() { + var compiler = new Compiler(); + + var compiledFile = compiler.compile(files[0]); + + assert.equal(compiledFile.SimpleStorage.code, '606060405260405160208060f78339016040526060805190602001505b806000600050819055505b5060c28060356000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900480632a1afcd914604b57806360fe47b114606a5780636d4ce63c14607b576049565b005b605460045060b9565b6040518082815260200191505060405180910390f35b6079600480359060200150609a565b005b608460045060a8565b6040518082815260200191505060405180910390f35b806000600050819055505b50565b6000600060005054905060b6565b90565b6000600050548156'); + + assert.equal(JSON.stringify(compiledFile.SimpleStorage.info.abiDefinition), '[{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"type":"constructor"}]'); + }); + + }); + + describe('compile a file with an error', function() { + var files = [ + 'test/support/contracts/error.sol' + ]; + + it("throw an error", function() { + var compiler = new Compiler(); + + assert.throws(function() { compiler.compile(files[0]) }, Error); + }); + + }); + +}); + diff --git a/test/support/contracts/error.sol b/test/support/contracts/error.sol new file mode 100644 index 000000000..c15c51587 --- /dev/null +++ b/test/support/contracts/error.sol @@ -0,0 +1,14 @@ +contract SimpleStorage { + uint public storedData; + + function SimpleStorage(uint initialValue) { + storedData2 = initialValue; + } + + function set(uint x) { + storedData = x; + } + function get() constant returns (uint retVal) { + return storedData; + } +}