mirror of https://github.com/embarklabs/embark.git
update old solc process to new process wrapper
This commit is contained in:
parent
f2e52d1dbc
commit
c94d8e9f91
|
@ -1,11 +1,13 @@
|
||||||
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');
|
||||||
|
|
||||||
|
class SolcProcess extends ProcessWrapper {
|
||||||
|
|
||||||
|
findImports(filename) {
|
||||||
if (filename.startsWith('http') || filename.startsWith('git')) {
|
if (filename.startsWith('http') || filename.startsWith('git')) {
|
||||||
const fileObj = Utils.getExternalContractUrl(filename);
|
const fileObj = Utils.getExternalContractUrl(filename);
|
||||||
filename = fileObj.filePath;
|
filename = fileObj.filePath;
|
||||||
|
@ -22,20 +24,35 @@ function findImports(filename) {
|
||||||
return {error: 'File not found'};
|
return {error: 'File not found'};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
loadCompiler(solcLocation) {
|
||||||
|
this.solc = require(solcLocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
|
@ -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});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue