2018-04-19 18:26:11 +00:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
|
|
|
const constants = require('../../constants');
|
2018-04-20 15:39:17 +00:00
|
|
|
const Utils = require('../../utils/utils');
|
2018-07-20 14:22:26 +00:00
|
|
|
const ProcessWrapper = require('../../processes/processWrapper');
|
2018-06-14 08:09:02 +00:00
|
|
|
const PluginManager = require('live-plugin-manager-git-fix').PluginManager;
|
|
|
|
const NpmTimer = require('../../versions/npmTimer');
|
|
|
|
|
2018-05-16 22:09:56 +00:00
|
|
|
|
|
|
|
class SolcProcess extends ProcessWrapper {
|
|
|
|
|
2018-06-14 08:09:02 +00:00
|
|
|
constructor(options){
|
|
|
|
super();
|
|
|
|
this._logger = options.logger;
|
2018-06-14 23:37:52 +00:00
|
|
|
this._showSpinner = options.showSpinner === true;
|
2018-06-14 08:09:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 22:09:56 +00:00
|
|
|
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))) {
|
2018-06-07 14:33:05 +00:00
|
|
|
return {contents: fs.readFileSync(path.join(constants.httpContractsDirectory, filename)).toString()};
|
2018-05-16 22:09:56 +00:00
|
|
|
}
|
|
|
|
return {error: 'File not found'};
|
2018-04-12 21:55:57 +00:00
|
|
|
}
|
2018-05-16 22:09:56 +00:00
|
|
|
|
2018-06-13 13:16:54 +00:00
|
|
|
installAndLoadCompiler(solcVersion, packagePath){
|
|
|
|
let self = this;
|
2018-06-14 23:37:52 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-06-13 13:16:54 +00:00
|
|
|
let manager = new PluginManager({pluginsPath: packagePath});
|
2018-06-14 08:09:02 +00:00
|
|
|
let timer;
|
|
|
|
if (!fs.existsSync(packagePath)) {
|
2018-06-14 23:37:52 +00:00
|
|
|
timer = new NpmTimer({logger: self._logger, packageName: 'solc', version: solcVersion, showSpinner: self._showSpinner});
|
2018-06-14 08:09:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(timer) timer.start();
|
2018-06-13 13:16:54 +00:00
|
|
|
manager.install('solc', solcVersion).then(() => {
|
|
|
|
self.solc = manager.require('solc');
|
2018-06-14 08:09:02 +00:00
|
|
|
if(timer) timer.end();
|
2018-06-13 13:16:54 +00:00
|
|
|
resolve();
|
2018-06-14 23:37:52 +00:00
|
|
|
}).catch(reject);
|
2018-06-14 08:09:02 +00:00
|
|
|
|
2018-06-13 13:16:54 +00:00
|
|
|
});
|
2018-04-12 21:57:55 +00:00
|
|
|
}
|
2018-05-16 22:09:56 +00:00
|
|
|
|
|
|
|
compile(jsonObj, cb) {
|
|
|
|
// TODO: only available in 0.4.11; need to make versions warn about this
|
2018-06-14 08:09:02 +00:00
|
|
|
try {
|
|
|
|
let output = this.solc.compileStandardWrapper(JSON.stringify(jsonObj), this.findImports);
|
|
|
|
cb(null, output);
|
|
|
|
} catch (err) {
|
|
|
|
cb(err.message);
|
|
|
|
}
|
2018-04-19 18:26:11 +00:00
|
|
|
}
|
2018-05-16 22:09:56 +00:00
|
|
|
|
2018-06-13 13:16:54 +00:00
|
|
|
|
2018-04-12 21:55:57 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 22:09:56 +00:00
|
|
|
let solcProcess;
|
2018-06-14 08:09:02 +00:00
|
|
|
process.on('message', (msg) => {
|
2018-05-16 22:09:56 +00:00
|
|
|
if (msg.action === "init") {
|
|
|
|
solcProcess = new SolcProcess(msg.options);
|
2018-06-14 08:09:02 +00:00
|
|
|
return process.send({result: "initiated"});
|
2018-05-16 22:09:56 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 13:16:54 +00:00
|
|
|
else if (msg.action === 'loadCompiler') {
|
2018-06-14 08:09:02 +00:00
|
|
|
solcProcess.solc = require('solc');
|
|
|
|
return process.send({result: "loadedCompiler"});
|
2018-06-13 13:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (msg.action == 'installAndLoadCompiler') {
|
|
|
|
solcProcess.installAndLoadCompiler(msg.solcVersion, msg.packagePath).then(() => {
|
2018-06-14 08:09:02 +00:00
|
|
|
return process.send({result: "loadedCompiler"});
|
2018-06-13 13:16:54 +00:00
|
|
|
});
|
2017-02-25 03:49:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 13:16:54 +00:00
|
|
|
else if (msg.action === 'compile') {
|
2018-06-14 08:09:02 +00:00
|
|
|
solcProcess.compile(msg.jsonObj, (err, output) => {
|
|
|
|
process.send({result: "compilation-" + msg.id, err: err, output: output});
|
2018-05-16 22:09:56 +00:00
|
|
|
});
|
2017-02-25 03:49:34 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|