Merge pull request #367 from embark-framework/refactor/vyper-utils

Refactor Vyper compiler
This commit is contained in:
Iuri Matias 2018-04-17 11:35:38 -04:00 committed by GitHub
commit e5ae6bc86e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,67 +12,66 @@ class Vyper {
embark.registerCompiler(".py", this.compile_vyper.bind(this)); embark.registerCompiler(".py", this.compile_vyper.bind(this));
} }
compile_vyper(contractFiles, cb) { static compileVyperContract(filename, compileABI, callback) {
let self = this; const params = compileABI ? '-f json ' : '';
async.waterfall([ shelljs.exec(`vyper ${params}${filename}`, {silent: true}, (code, stdout, stderr) => {
function compileContracts(callback) { if (stderr) {
self.logger.info("compiling vyper contracts..."); return callback(stderr);
const compiled_object = {};
async.each(contractFiles,
function (file, fileCb) {
const className = path.basename(file.filename).split('.')[0];
compiled_object[className] = {};
async.parallel([
function getByteCode(paraCb) {
shelljs.exec(`vyper ${file.filename}`, {silent: true}, (code, stdout, stderr) => {
if (stderr) {
return paraCb(stderr);
}
if (code !== 0) {
return paraCb(`Vyper exited with error code ${code}`);
}
if (!stdout) {
return paraCb('Execution returned no bytecode');
}
const byteCode = stdout.replace(/\n/g, '');
compiled_object[className].runtimeBytecode = byteCode;
compiled_object[className].realRuntimeBytecode = byteCode;
compiled_object[className].code = byteCode;
paraCb();
});
},
function getABI(paraCb) {
shelljs.exec(`vyper -f json ${file.filename}`, {silent: true}, (code, stdout, stderr) => {
if (stderr) {
return paraCb(stderr);
}
if (code !== 0) {
return paraCb(`Vyper exited with error code ${code}`);
}
if (!stdout) {
return paraCb('Execution returned no ABI');
}
let ABI = [];
try {
ABI = JSON.parse(stdout.replace(/\n/g, ''));
} catch (e) {
return paraCb('ABI is not valid JSON');
}
compiled_object[className].abiDefinition = ABI;
paraCb();
});
}
], fileCb);
},
function (err) {
callback(err, compiled_object);
});
} }
], function (err, result) { if (code !== 0) {
cb(err, result); return callback(`Vyper exited with error code ${code}`);
}
if (!stdout) {
return callback('Execution returned no result');
}
callback(null, stdout.replace(/\n/g, ''));
}); });
} }
compile_vyper(contractFiles, cb) {
let self = this;
if (!contractFiles || !contractFiles.length) {
return cb();
}
self.logger.info("compiling Vyper contracts...");
const compiled_object = {};
async.each(contractFiles,
function (file, fileCb) {
const className = path.basename(file.filename).split('.')[0];
compiled_object[className] = {};
async.parallel([
function getByteCode(paraCb) {
Vyper.compileVyperContract(file.filename, false, (err, byteCode) => {
if (err) {
return paraCb(err);
}
compiled_object[className].runtimeBytecode = byteCode;
compiled_object[className].realRuntimeBytecode = byteCode;
compiled_object[className].code = byteCode;
paraCb();
});
},
function getABI(paraCb) {
Vyper.compileVyperContract(file.filename, true, (err, ABIString) => {
if (err) {
return paraCb(err);
}
let ABI = [];
try {
ABI = JSON.parse(ABIString);
} catch (e) {
return paraCb('ABI is not valid JSON');
}
compiled_object[className].abiDefinition = ABI;
paraCb();
});
}
], fileCb);
},
function (err) {
cb(err, compiled_object);
});
}
} }
module.exports = Vyper; module.exports = Vyper;