114 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-12-16 15:39:30 -05:00
let utils = require('../../utils/utils.js');
2018-04-02 15:06:56 -04:00
let fs = require('../../core/fs.js');
2017-12-16 15:39:30 -05:00
let currentSolcVersion = require('../../../package.json').dependencies.solc;
const ProcessLauncher = require('../../process/processLauncher');
const uuid = require('uuid/v1');
2018-06-04 13:12:51 -04:00
let ipc = require('node-ipc')
2017-02-24 19:27:27 -05:00
2017-03-30 20:12:39 +09:00
class SolcW {
2017-02-24 19:27:27 -05:00
constructor(options) {
this.logger = options.logger;
this.events = options.events;
2018-06-04 13:12:51 -04:00
this.useIpc = options.useIpc;
this.compilerLoaded = false;
this.solcProcess = null;
}
2017-03-30 20:12:39 +09:00
load_compiler(done) {
const self = this;
2018-06-04 13:12:51 -04:00
if (self.useIpc) {
// try to connect, if it can't then continue
self.compilerLoaded = true;
ipc.config.silent = true;
function connecting(socket) {
ipc.of['embark'].on('connect',function() {
done();
});
}
2018-06-04 13:26:25 -04:00
ipc.connectTo('embark', fs.dappPath(".embark/embark.ipc"), connecting);
2018-06-04 13:12:51 -04:00
return;
}
if (this.compilerLoaded) {
2018-05-18 14:22:58 -04:00
return done();
2017-02-24 22:49:34 -05:00
}
this.solcProcess = new ProcessLauncher({
modulePath: utils.joinPath(__dirname, 'solcP.js'),
logger: self.logger,
events: self.events
});
this.solcProcess.send({action: "init", options: {}});
2018-05-18 14:22:58 -04:00
this.solcProcess.once('result', 'loadedCompiler', () => {
self.compilerLoaded = true;
2017-03-30 20:12:39 +09:00
done();
});
2018-06-04 13:12:51 -04:00
ipc.config.silent = true;
function _connecting() {
ipc.server.on(
'message',
function(data, socket) {
2018-06-04 13:33:07 -04:00
if (data.action != 'compile') {
return;
}
self.compile(data.message, (result) => {
ipc.server.emit(socket, 'message', {action: 'compilation', message: result});
});
2018-06-04 13:12:51 -04:00
}
);
}
2018-06-04 13:26:25 -04:00
ipc.serve(fs.dappPath(".embark/embark.ipc"), _connecting)
2018-06-04 13:12:51 -04:00
ipc.server.start()
2018-06-04 13:26:25 -04:00
this.logger.info(`pid ${process.pid} listening on ${fs.dappPath(".embark/embark.ipc")}`);
2018-06-04 13:12:51 -04:00
this.events.request("version:get:solc", function(solcVersion) {
2017-12-30 18:12:16 -05:00
if (solcVersion === currentSolcVersion) {
self.solcProcess.send({action: 'loadCompiler', requirePath: 'solc'});
} else {
2017-12-30 19:34:15 -05:00
self.events.request("version:getPackageLocation", "solc", solcVersion, function(err, location) {
if (err) {
return done(err);
}
2018-04-02 15:06:56 -04:00
let requirePath = fs.dappPath(location);
self.solcProcess.send({action: 'loadCompiler', requirePath: requirePath});
});
}
});
2017-03-30 20:12:39 +09:00
}
2017-02-24 22:49:34 -05:00
2017-03-30 20:12:39 +09:00
isCompilerLoaded() {
return (this.compilerLoaded === true);
2017-03-30 20:12:39 +09:00
}
compile(jsonObj, done) {
const id = uuid();
2018-06-04 13:12:51 -04:00
let a = new Date();
if (this.useIpc) {
ipc.of['embark'].once('message', function(msg) {
done(msg.message);
});
ipc.of['embark'].emit('message', {action: 'compile', message: jsonObj});
return;
}
this.solcProcess.once('result', 'compilation-' + id, (msg) => {
done(JSON.parse(msg.output));
2017-03-30 20:12:39 +09:00
});
this.solcProcess.send({action: 'compile', jsonObj: jsonObj, id});
2017-03-30 20:12:39 +09:00
}
}
2017-02-24 19:27:27 -05:00
module.exports = SolcW;