From 9e8ebe9cb3c46317732b28e0f01c16df55b8a46b Mon Sep 17 00:00:00 2001 From: VoR0220 Date: Sun, 25 Oct 2015 13:36:07 -0500 Subject: [PATCH] Update compiler.js --- lib/compiler.js | 68 +++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index f274ebb1..1bcdc041 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -1,7 +1,8 @@ var shelljs = require('shelljs'); var shelljs_global = require('shelljs/global'); var web3 = require('web3'); - +var fs = require('fs'); +var solc = require('solc'); Compiler = function(blockchainConfig) { this.blockchainConfig = blockchainConfig; @@ -11,26 +12,31 @@ Compiler.prototype.init = function(env) { var config = this.blockchainConfig.config(env); }; -Compiler.prototype.compile_solidity = function(contractFile) { - var cmd, result, output, version, json, compiled_object; +Compiler.prototype.compile_solidity = function(contractFiles) { + console.log("I HAVE REACHED HERE THROUGH GRUNT-EMBARK") + var source = '{ '; + for (var contractFile in contractFiles){ + source += '\'' + contractFile + '\': \'' + var file = fs.readFileSync(contractFile).toString() + '\''; - cmd = "solc " + contractFile + " --combined-json bin,abi"; + if (!contractFiles.length) source += ', ' + } + source += '}'; + + var output = solc.compile(source, 1); - result = exec(cmd, {silent: true}); - output = result.output; + var json = output.contracts; - json = JSON.parse(output).contracts; + compiled_object = {} - compiled_object = {} - - for (var className in json) { - var contract = json[className]; - - compiled_object[className] = {}; - compiled_object[className].code = contract.binary || contract.bin; - compiled_object[className].info = {}; - compiled_object[className].info.abiDefinition = JSON.parse(contract["abi"] || contract["json-abi"]); - } + for (var className in json) { + var contract = json[className]; + + compiled_object[className] = {}; + compiled_object[className].code = contract.bytecode; + compiled_object[className].info = {}; + compiled_object[className].info.abiDefinition = JSON.parse(contract.interface); + } return compiled_object; }; @@ -75,18 +81,24 @@ Compiler.prototype.compile_serpent = function(contractFile) { } -Compiler.prototype.compile = function(contractFile) { - var extension = contractFile.split('.')[1]; +Compiler.prototype.compile = function(contractFiles) { + var solidity = [], serpent = []; + + for (contract in contractFiles) { + var extension = contractFile.split('.')[1]; + if (extension === 'sol') { + solidity.push(contractFile); + } + else if (extension === 'se') { + serpent.push(contractFile); + } + else { + throw new Error("extension not known"); + } + } - if (extension === 'sol') { - return this.compile_solidity(contractFile); - } - else if (extension === 'se') { - return this.compile_serpent(contractFile); - } - else { - throw new Error("extension not known"); - } + if (solidity.length > 0) return compile_solidity(solidity); + if (serpent.length > 0) return compile_serpent(serpent); }; module.exports = Compiler;