99 lines
2.6 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;
2018-07-27 17:33:50 -04:00
const ProcessLauncher = require('../../core/processes/processLauncher.js');
const uuid = require('uuid/v1');
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 15:36:43 -04:00
this.ipc = options.ipc;
this.compilerLoaded = false;
this.solcProcess = null;
this.useDashboard = options.useDashboard;
}
2017-03-30 20:12:39 +09:00
load_compiler(done) {
const self = this;
2018-06-05 16:13:17 -04:00
if (!self.ipc.isClient()) {
return self.load_compiler_internally(done);
2018-06-04 13:12:51 -04:00
}
2018-08-23 12:54:43 -04:00
if (self.ipc.connected) {
self.compilerLoaded = true;
return done();
}
2018-06-05 16:13:17 -04:00
self.ipc.connect((err) => {
if (err) {
return self.load_compiler_internally(done);
}
self.compilerLoaded = true;
done();
});
}
load_compiler_internally(done) {
const self = this;
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,
2018-07-20 21:28:46 +03:00
events: self.events,
silent: false
});
this.solcProcess.once("result", "initiated", () => {
this.events.request("version:get:solc", function(solcVersion) {
if (solcVersion === currentSolcVersion) {
return self.solcProcess.send({action: 'loadCompiler', requirePath: 'solc'});
}
self.events.request("version:getPackagePath", "solc", solcVersion, function(err, path) {
if (err) {
return done(err);
}
let requirePath = fs.dappPath(path);
self.solcProcess.send({action: 'installAndLoadCompiler', solcVersion: solcVersion, packagePath: requirePath});
});
});
});
this.solcProcess.once("result", "loadedCompiler", () => {
self.compilerLoaded = true;
2017-03-30 20:12:39 +09:00
done();
});
this.solcProcess.send({action: "init", options: {logger: self.logger, showSpinner: !self.useDashboard}});
if (this.ipc.isServer()) {
this.ipc.on('compile', self.compile.bind(this));
}
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
if (this.ipc.isClient() && this.ipc.connected) {
return this.ipc.request('compile', jsonObj, done);
2018-06-04 13:12:51 -04:00
}
this.solcProcess.once('result', 'compilation-' + id, (msg) => {
if(msg.err) {
return done(msg.err);
}
done(null, 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;