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 fs = require('fs-extra');
const path = require('path'); const path = require('path');
const constants = require('../../constants'); const constants = require('../../constants');
const Utils = require('../../utils/utils'); const Utils = require('../../utils/utils');
function findImports(filename) { const ProcessWrapper = require('../../process/processWrapper');
if (filename.startsWith('http') || filename.startsWith('git')) {
const fileObj = Utils.getExternalContractUrl(filename); class SolcProcess extends ProcessWrapper {
filename = fileObj.filePath;
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) { process.on('message', function (msg) {
if (msg.action === "init") {
solcProcess = new SolcProcess(msg.options);
return process.send({result: "initiated"});
}
if (msg.action === 'loadCompiler') { if (msg.action === 'loadCompiler') {
solc = require(msg.solcLocation); solcProcess.loadCompiler(msg.requirePath);
process.send({result: "loadedCompiler"}); process.send({result: "loadedCompiler"});
} }
if (msg.action === 'compile') { if (msg.action === 'compile') {
// TODO: only available in 0.4.11; need to make versions warn about this solcProcess.compile(msg.jsonObj, (output) => {
let output = solc.compileStandardWrapper(JSON.stringify(msg.jsonObj), findImports); process.send({result: "compilation", output: 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 utils = require('../../utils/utils.js');
let fs = require('../../core/fs.js'); let fs = require('../../core/fs.js');
let solcProcess;
let compilerLoaded = false;
let currentSolcVersion = require('../../../package.json').dependencies.solc; let currentSolcVersion = require('../../../package.json').dependencies.solc;
const ProcessLauncher = require('../../process/processLauncher');
class SolcW { class SolcW {
constructor(options) { constructor(options) {
this.logger = options.logger; this.logger = options.logger;
this.events = options.events; this.events = options.events;
this.compilerLoaded = false;
this.solcProcess = null;
} }
load_compiler(done) { load_compiler(done) {
const self = this; const self = this;
if (compilerLoaded) { if (this.compilerLoaded) {
done(); done();
} }
solcProcess = require('child_process').fork(utils.joinPath(__dirname, '/solcP.js')); this.solcProcess = new ProcessLauncher({
solcProcess.once('message', function (msg) { modulePath: utils.joinPath(__dirname, 'solcP.js'),
if (msg.result !== 'loadedCompiler') { logger: self.logger,
return; events: self.events
} });
compilerLoaded = true; this.solcProcess.send({action: "init", options: {}});
this.solcProcess.subscribeTo('result', 'loadedCompiler', () => {
self.compilerLoaded = true;
done(); done();
}); });
this.events.request("version:get:solc", function(solcVersion) { this.events.request("version:get:solc", function(solcVersion) {
if (solcVersion === currentSolcVersion) { if (solcVersion === currentSolcVersion) {
solcProcess.send({action: 'loadCompiler', solcLocation: 'solc'}); self.solcProcess.send({action: 'loadCompiler', requirePath: 'solc'});
} else { } else {
self.events.request("version:getPackageLocation", "solc", solcVersion, function(err, location) { self.events.request("version:getPackageLocation", "solc", solcVersion, function(err, location) {
if (err) { if (err) {
return done(err); return done(err);
} }
let requirePath = fs.dappPath(location); let requirePath = fs.dappPath(location);
solcProcess.send({action: 'loadCompiler', solcLocation: requirePath}); self.solcProcess.send({action: 'loadCompiler', requirePath: requirePath});
}); });
} }
}); });
} }
isCompilerLoaded() { isCompilerLoaded() {
return (compilerLoaded === true); return (this.compilerLoaded === true);
} }
compile(jsonObj, done) { compile(jsonObj, done) {
solcProcess.once('message', function (msg) { this.solcProcess.subscribeTo('result', 'compilation', (msg) => {
if (msg.result !== 'compilation') {
return;
}
done(JSON.parse(msg.output)); done(JSON.parse(msg.output));
}); });
solcProcess.send({action: 'compile', jsonObj: jsonObj});
this.solcProcess.send({action: 'compile', jsonObj: jsonObj});
} }
} }