embark-area-51/lib/modules/solidity/solcW.js

62 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-12-16 20:39:30 +00:00
let utils = require('../../utils/utils.js');
2018-04-02 19:06:56 +00:00
let fs = require('../../core/fs.js');
2017-12-16 20:39:30 +00:00
let currentSolcVersion = require('../../../package.json').dependencies.solc;
const ProcessLauncher = require('../../process/processLauncher');
2017-02-25 00:27:27 +00:00
2017-03-30 11:12:39 +00:00
class SolcW {
2017-02-25 00:27:27 +00:00
constructor(options) {
this.logger = options.logger;
this.events = options.events;
this.compilerLoaded = false;
this.solcProcess = null;
}
2017-03-30 11:12:39 +00:00
load_compiler(done) {
const self = this;
if (this.compilerLoaded) {
2018-05-18 18:22:58 +00:00
return done();
2017-02-25 03:49:34 +00: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 18:22:58 +00:00
this.solcProcess.once('result', 'loadedCompiler', () => {
self.compilerLoaded = true;
2017-03-30 11:12:39 +00:00
done();
});
this.events.request("version:get:solc", function(solcVersion) {
2017-12-30 23:12:16 +00:00
if (solcVersion === currentSolcVersion) {
self.solcProcess.send({action: 'loadCompiler', requirePath: 'solc'});
} else {
2017-12-31 00:34:15 +00:00
self.events.request("version:getPackageLocation", "solc", solcVersion, function(err, location) {
if (err) {
return done(err);
}
2018-04-02 19:06:56 +00:00
let requirePath = fs.dappPath(location);
self.solcProcess.send({action: 'loadCompiler', requirePath: requirePath});
});
}
});
2017-03-30 11:12:39 +00:00
}
2017-02-25 03:49:34 +00:00
2017-03-30 11:12:39 +00:00
isCompilerLoaded() {
return (this.compilerLoaded === true);
2017-03-30 11:12:39 +00:00
}
compile(jsonObj, done) {
2018-05-18 18:22:58 +00:00
this.solcProcess.once('result', 'compilation', (msg) => {
done(JSON.parse(msg.output));
2017-03-30 11:12:39 +00:00
});
this.solcProcess.send({action: 'compile', jsonObj: jsonObj});
2017-03-30 11:12:39 +00:00
}
}
2017-02-25 00:27:27 +00:00
module.exports = SolcW;