2018-04-19 18:26:11 +00:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
|
|
|
const constants = require('../../constants');
|
2018-04-20 15:39:17 +00:00
|
|
|
const Utils = require('../../utils/utils');
|
2018-04-12 21:55:57 +00:00
|
|
|
|
2018-05-16 22:09:56 +00:00
|
|
|
const ProcessWrapper = require('../../process/processWrapper');
|
|
|
|
|
|
|
|
class SolcProcess extends ProcessWrapper {
|
|
|
|
|
|
|
|
findImports(filename) {
|
|
|
|
if (filename.startsWith('http') || filename.startsWith('git')) {
|
|
|
|
const fileObj = Utils.getExternalContractUrl(filename);
|
|
|
|
filename = fileObj.filePath;
|
|
|
|
}
|
|
|
|
if (fs.existsSync(filename)) {
|
|
|
|
return {contents: fs.readFileSync(filename).toString()};
|
|
|
|
}
|
|
|
|
if (fs.existsSync(path.join('./node_modules/', filename))) {
|
|
|
|
return {contents: fs.readFileSync(path.join('./node_modules/', filename)).toString()};
|
|
|
|
}
|
|
|
|
if (fs.existsSync(path.join(constants.httpContractsDirectory, filename))) {
|
|
|
|
return {contents: fs.readFileSync(path.join('./.embark/contracts', filename)).toString()};
|
|
|
|
}
|
|
|
|
return {error: 'File not found'};
|
2018-04-12 21:55:57 +00:00
|
|
|
}
|
2018-05-16 22:09:56 +00:00
|
|
|
|
|
|
|
loadCompiler(solcLocation) {
|
|
|
|
this.solc = require(solcLocation);
|
2018-04-12 21:57:55 +00:00
|
|
|
}
|
2018-05-16 22:09:56 +00:00
|
|
|
|
|
|
|
compile(jsonObj, cb) {
|
|
|
|
// TODO: only available in 0.4.11; need to make versions warn about this
|
|
|
|
let output = this.solc.compileStandardWrapper(JSON.stringify(jsonObj), this.findImports);
|
|
|
|
cb(output);
|
2018-04-19 18:26:11 +00:00
|
|
|
}
|
2018-05-16 22:09:56 +00:00
|
|
|
|
2018-04-12 21:55:57 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 22:09:56 +00:00
|
|
|
let solcProcess;
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
process.on('message', function (msg) {
|
2018-05-16 22:09:56 +00:00
|
|
|
if (msg.action === "init") {
|
|
|
|
solcProcess = new SolcProcess(msg.options);
|
|
|
|
return process.send({result: "initiated"});
|
|
|
|
}
|
|
|
|
|
2017-02-25 03:49:34 +00:00
|
|
|
if (msg.action === 'loadCompiler') {
|
2018-05-16 22:09:56 +00:00
|
|
|
solcProcess.loadCompiler(msg.requirePath);
|
2017-02-25 03:49:34 +00:00
|
|
|
process.send({result: "loadedCompiler"});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.action === 'compile') {
|
2018-05-16 22:09:56 +00:00
|
|
|
solcProcess.compile(msg.jsonObj, (output) => {
|
2018-05-31 17:32:02 +00:00
|
|
|
process.send({result: "compilation-" + msg.id, output: output});
|
2018-05-16 22:09:56 +00:00
|
|
|
});
|
2017-02-25 03:49:34 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|