Merge pull request #426 from embark-framework/features/update_solc_wrapper

update solc wrapper
This commit is contained in:
Iuri Matias 2018-05-18 13:17:01 -04:00 committed by GitHub
commit c6593f6168
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 43 deletions

View File

@ -109,5 +109,6 @@
"{{className}} has code associated to it but it's configured as an instanceOf {{parentContractName}}": "{{className}} has code associated to it but it's configured as an instanceOf {{parentContractName}}",
"downloading {{packageName}} {{version}}....": "downloading {{packageName}} {{version}}....",
"Swarm node is offline...": "Swarm node is offline...",
"Swarm node detected...": "Swarm node detected..."
"Swarm node detected...": "Swarm node detected...",
"file not found, creating it...": "file not found, creating it..."
}

View File

@ -1,41 +1,58 @@
let solc;
const fs = require('fs-extra');
const path = require('path');
const constants = require('../../constants');
const Utils = require('../../utils/utils');
function findImports(filename) {
if (filename.startsWith('http') || filename.startsWith('git')) {
const fileObj = Utils.getExternalContractUrl(filename);
filename = fileObj.filePath;
const ProcessWrapper = require('../../process/processWrapper');
class SolcProcess extends ProcessWrapper {
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))) {
return {contents: fs.readFileSync(path.join('./.embark/contracts', filename)).toString()};
}
return {error: 'File not found'};
}
if (fs.existsSync(filename)) {
return {contents: fs.readFileSync(filename).toString()};
loadCompiler(solcLocation) {
this.solc = require(solcLocation);
}
if (fs.existsSync(path.join('./node_modules/', filename))) {
return {contents: fs.readFileSync(path.join('./node_modules/', filename)).toString()};
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);
}
if (fs.existsSync(path.join(constants.httpContractsDirectory, filename))) {
return {contents: fs.readFileSync(path.join('./.embark/contracts', filename)).toString()};
}
return {error: 'File not found'};
}
let solcProcess;
process.on('message', function (msg) {
if (msg.action === "init") {
solcProcess = new SolcProcess(msg.options);
return process.send({result: "initiated"});
}
if (msg.action === 'loadCompiler') {
solc = require(msg.solcLocation);
solcProcess.loadCompiler(msg.requirePath);
process.send({result: "loadedCompiler"});
}
if (msg.action === 'compile') {
// TODO: only available in 0.4.11; need to make versions warn about this
let output = solc.compileStandardWrapper(JSON.stringify(msg.jsonObj), findImports);
process.send({result: "compilation", output: output});
solcProcess.compile(msg.jsonObj, (output) => {
process.send({result: "compilation", output: output});
});
}
});
process.on('exit', function () {
process.exit(0);
});

View File

@ -1,58 +1,59 @@
let utils = require('../../utils/utils.js');
let fs = require('../../core/fs.js');
let solcProcess;
let compilerLoaded = false;
let currentSolcVersion = require('../../../package.json').dependencies.solc;
const ProcessLauncher = require('../../process/processLauncher');
class SolcW {
constructor(options) {
this.logger = options.logger;
this.events = options.events;
this.compilerLoaded = false;
this.solcProcess = null;
}
load_compiler(done) {
const self = this;
if (compilerLoaded) {
if (this.compilerLoaded) {
done();
}
solcProcess = require('child_process').fork(utils.joinPath(__dirname, '/solcP.js'));
solcProcess.once('message', function (msg) {
if (msg.result !== 'loadedCompiler') {
return;
}
compilerLoaded = true;
this.solcProcess = new ProcessLauncher({
modulePath: utils.joinPath(__dirname, 'solcP.js'),
logger: self.logger,
events: self.events
});
this.solcProcess.send({action: "init", options: {}});
this.solcProcess.subscribeTo('result', 'loadedCompiler', () => {
self.compilerLoaded = true;
done();
});
this.events.request("version:get:solc", function(solcVersion) {
if (solcVersion === currentSolcVersion) {
solcProcess.send({action: 'loadCompiler', solcLocation: 'solc'});
self.solcProcess.send({action: 'loadCompiler', requirePath: 'solc'});
} else {
self.events.request("version:getPackageLocation", "solc", solcVersion, function(err, location) {
if (err) {
return done(err);
}
let requirePath = fs.dappPath(location);
solcProcess.send({action: 'loadCompiler', solcLocation: requirePath});
self.solcProcess.send({action: 'loadCompiler', requirePath: requirePath});
});
}
});
}
isCompilerLoaded() {
return (compilerLoaded === true);
return (this.compilerLoaded === true);
}
compile(jsonObj, done) {
solcProcess.once('message', function (msg) {
if (msg.result !== 'compilation') {
return;
}
this.solcProcess.subscribeTo('result', 'compilation', (msg) => {
done(JSON.parse(msg.output));
});
solcProcess.send({action: 'compile', jsonObj: jsonObj});
this.solcProcess.send({action: 'compile', jsonObj: jsonObj});
}
}

View File

@ -13,11 +13,19 @@ let readFile = function(file) {
return new File({filename: file, type: File.types.dapp_file, path: file});
};
const currentSolcVersion = require('../package.json').dependencies.solc;
const TestEvents = {
request: (cmd, cb) => {
cb(currentSolcVersion);
}
};
describe('embark.Contracts', function() {
this.timeout(0);
describe('simple', function() {
let plugins = new Plugins({
logger: new TestLogger({})
logger: new TestLogger({}),
events: TestEvents
});
plugins.loadInternalPlugin('solidity', {solcVersion: '0.4.17', contractDirectories: ['app/contracts/']});
@ -99,7 +107,8 @@ describe('embark.Contracts', function() {
describe('config with contract instances', function() {
let plugins = new Plugins({
logger: new TestLogger({})
logger: new TestLogger({}),
events: TestEvents
});
plugins.loadInternalPlugin('solidity', {solcVersion: '0.4.17', contractDirectories: ['app/contracts/']});