embark/lib/compiler.js

109 lines
2.9 KiB
JavaScript
Raw Normal View History

var shelljs = require('shelljs');
var shelljs_global = require('shelljs/global');
2015-07-04 03:23:21 +00:00
var web3 = require('web3');
2015-10-25 18:36:07 +00:00
var fs = require('fs');
var solc = require('solc');
2015-07-04 03:23:21 +00:00
Compiler = function(blockchainConfig) {
this.blockchainConfig = blockchainConfig;
};
Compiler.prototype.init = function(env) {
var config = this.blockchainConfig.config(env);
};
2015-10-25 18:36:07 +00:00
Compiler.prototype.compile_solidity = function(contractFiles) {
2015-10-26 23:42:56 +00:00
var input = {}
2016-02-09 19:44:40 +00:00
for (var i = 0; i < contractFiles.length; i++){
2016-02-09 19:44:40 +00:00
//console.log(contractFiles[i]);
//input[contractFiles[i].substring(14)] = fs.readFileSync(contractFiles[i]).toString();
input[contractFiles[i].split('/')[3]] = fs.readFileSync(contractFiles[i]).toString();
2015-10-25 18:36:07 +00:00
}
2015-10-26 23:42:56 +00:00
var output = solc.compile({sources: input}, 1);
2016-02-09 19:44:40 +00:00
2015-10-28 19:20:14 +00:00
if (output.errors)
throw new Error ("Solidity errors: " + output.errors)
2015-10-14 19:37:45 +00:00
2015-10-25 18:36:07 +00:00
var json = output.contracts;
2015-10-18 01:39:15 +00:00
2015-10-25 18:36:07 +00:00
compiled_object = {}
2015-10-18 01:39:15 +00:00
2015-10-25 18:36:07 +00:00
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;
2015-10-02 03:04:15 +00:00
};
2015-08-31 02:10:45 +00:00
2016-02-09 20:25:23 +00:00
Compiler.prototype.compile_serpent = function(contractFiles) {
2015-08-31 02:10:45 +00:00
var cmd, result, output, json, compiled_object;
2016-02-09 20:25:23 +00:00
//TODO: figure out how to compile multiple files and get the correct json
var contractFile = contractFiles[0];
2015-08-31 02:10:45 +00:00
cmd = "serpent compile " + contractFile;
result = exec(cmd, {silent: true});
code = result.output;
if (result.code === 1) {
throw new Error(result.output);
}
cmd = "serpent mk_full_signature " + contractFile;
result = exec(cmd, {silent: true});
if (result.code === 1) {
throw new Error(result.output);
}
json = JSON.parse(result.output.trim());
className = contractFile.split('.')[0].split("/").pop();
2015-08-31 12:24:09 +00:00
for (var i=0; i < json.length; i++) {
var elem = json[i];
if (elem.outputs.length > 0) {
elem.constant = true;
}
}
2015-08-31 02:10:45 +00:00
compiled_object = {}
compiled_object[className] = {};
compiled_object[className].code = code.trim();
compiled_object[className].info = {};
compiled_object[className].info.abiDefinition = json;
return compiled_object;
}
2015-10-25 18:36:07 +00:00
Compiler.prototype.compile = function(contractFiles) {
var solidity = [], serpent = [];
2016-02-09 20:25:23 +00:00
for (var i = 0; i < contractFiles.length; i++) {
var extension = contractFiles[i].split('.')[1];
2015-10-25 18:36:07 +00:00
if (extension === 'sol') {
solidity.push(contractFiles[i]);
2015-10-25 18:36:07 +00:00
}
else if (extension === 'se') {
serpent.push(contractFiles[i]);
2015-10-25 18:36:07 +00:00
}
else {
throw new Error("extension not known, got " + extension);
2015-10-25 18:36:07 +00:00
}
2015-08-31 02:10:45 +00:00
}
2015-10-27 16:54:43 +00:00
//TODO: Get these compiling and returning together...problem might come with the JSON objects
if (solidity.length > 0) return this.compile_solidity(solidity);
if (serpent.length > 0) return this.compile_serpent(serpent);
2015-07-04 03:23:21 +00:00
};
module.exports = Compiler;