update old solc process to new process wrapper

This commit is contained in:
Iuri Matias 2018-05-16 18:09:56 -04:00
parent f2e52d1dbc
commit c94d8e9f91
2 changed files with 58 additions and 40 deletions

View File

@ -1,41 +1,58 @@
let solc;
const fs = require('fs-extra');
const path = require('path');
const constants = require('../../constants');
const Utils = require('../../utils/utils');
function findImports(filename) {
if (filename.startsWith('http') || filename.startsWith('git')) {
const fileObj = Utils.getExternalContractUrl(filename);
filename = fileObj.filePath;
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'};
}
if (fs.existsSync(filename)) {
return {contents: fs.readFileSync(filename).toString()};
loadCompiler(solcLocation) {
this.solc = require(solcLocation);
}
if (fs.existsSync(path.join('./node_modules/', filename))) {
return {contents: fs.readFileSync(path.join('./node_modules/', filename)).toString()};
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);
}
if (fs.existsSync(path.join(constants.httpContractsDirectory, filename))) {
return {contents: fs.readFileSync(path.join('./.embark/contracts', filename)).toString()};
}
return {error: 'File not found'};
}
let solcProcess;
process.on('message', function (msg) {
if (msg.action === "init") {
solcProcess = new SolcProcess(msg.options);
return process.send({result: "initiated"});
}
if (msg.action === 'loadCompiler') {
solc = require(msg.solcLocation);
solcProcess.loadCompiler(msg.requirePath);
process.send({result: "loadedCompiler"});
}
if (msg.action === 'compile') {
// TODO: only available in 0.4.11; need to make versions warn about this
let output = solc.compileStandardWrapper(JSON.stringify(msg.jsonObj), findImports);
process.send({result: "compilation", output: output});
solcProcess.compile(msg.jsonObj, (output) => {
process.send({result: "compilation", output: output});
});
}
});
process.on('exit', function () {
process.exit(0);
});

View File

@ -1,58 +1,59 @@
let utils = require('../../utils/utils.js');
let fs = require('../../core/fs.js');
let solcProcess;
let compilerLoaded = false;
let currentSolcVersion = require('../../../package.json').dependencies.solc;
const ProcessLauncher = require('../../process/processLauncher');
class SolcW {
constructor(options) {
this.logger = options.logger;
this.events = options.events;
this.compilerLoaded = false;
this.solcProcess = null;
}
load_compiler(done) {
const self = this;
if (compilerLoaded) {
if (this.compilerLoaded) {
done();
}
solcProcess = require('child_process').fork(utils.joinPath(__dirname, '/solcP.js'));
solcProcess.once('message', function (msg) {
if (msg.result !== 'loadedCompiler') {
return;
}
compilerLoaded = true;
this.solcProcess = new ProcessLauncher({
modulePath: utils.joinPath(__dirname, 'solcP.js'),
logger: self.logger,
events: self.events
});
this.solcProcess.send({action: "init", options: {}});
this.solcProcess.subscribeTo('result', 'loadedCompiler', () => {
self.compilerLoaded = true;
done();
});
this.events.request("version:get:solc", function(solcVersion) {
if (solcVersion === currentSolcVersion) {
solcProcess.send({action: 'loadCompiler', solcLocation: 'solc'});
self.solcProcess.send({action: 'loadCompiler', requirePath: 'solc'});
} else {
self.events.request("version:getPackageLocation", "solc", solcVersion, function(err, location) {
if (err) {
return done(err);
}
let requirePath = fs.dappPath(location);
solcProcess.send({action: 'loadCompiler', solcLocation: requirePath});
self.solcProcess.send({action: 'loadCompiler', requirePath: requirePath});
});
}
});
}
isCompilerLoaded() {
return (compilerLoaded === true);
return (this.compilerLoaded === true);
}
compile(jsonObj, done) {
solcProcess.once('message', function (msg) {
if (msg.result !== 'compilation') {
return;
}
this.solcProcess.subscribeTo('result', 'compilation', (msg) => {
done(JSON.parse(msg.output));
});
solcProcess.send({action: 'compile', jsonObj: jsonObj});
this.solcProcess.send({action: 'compile', jsonObj: jsonObj});
}
}