move vyper contract compile to static util function

This commit is contained in:
Jonathan Rainville 2018-04-17 11:25:46 -04:00
parent dd1c10fe85
commit 6f119eeb1b
1 changed files with 23 additions and 21 deletions

View File

@ -12,6 +12,22 @@ class Vyper {
embark.registerCompiler(".py", this.compile_vyper.bind(this)); embark.registerCompiler(".py", this.compile_vyper.bind(this));
} }
static compileVyperContract(filename, compileABI, callback) {
const params = compileABI ? '-f json ' : '';
shelljs.exec(`vyper ${params}${filename}`, {silent: true}, (code, stdout, stderr) => {
if (stderr) {
return callback(stderr);
}
if (code !== 0) {
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) { compile_vyper(contractFiles, cb) {
let self = this; let self = this;
if (!contractFiles || !contractFiles.length) { if (!contractFiles || !contractFiles.length) {
@ -25,17 +41,10 @@ class Vyper {
compiled_object[className] = {}; compiled_object[className] = {};
async.parallel([ async.parallel([
function getByteCode(paraCb) { function getByteCode(paraCb) {
shelljs.exec(`vyper ${file.filename}`, {silent: true}, (code, stdout, stderr) => { Vyper.compileVyperContract(file.filename, false, (err, byteCode) => {
if (stderr) { if (err) {
return paraCb(stderr); return paraCb(err);
} }
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].runtimeBytecode = byteCode;
compiled_object[className].realRuntimeBytecode = byteCode; compiled_object[className].realRuntimeBytecode = byteCode;
compiled_object[className].code = byteCode; compiled_object[className].code = byteCode;
@ -43,19 +52,13 @@ class Vyper {
}); });
}, },
function getABI(paraCb) { function getABI(paraCb) {
shelljs.exec(`vyper -f json ${file.filename}`, {silent: true}, (code, stdout, stderr) => { Vyper.compileVyperContract(file.filename, true, (err, ABIString) => {
if (stderr) { if (err) {
return paraCb(stderr); return paraCb(err);
}
if (code !== 0) {
return paraCb(`Vyper exited with error code ${code}`);
}
if (!stdout) {
return paraCb('Execution returned no ABI');
} }
let ABI = []; let ABI = [];
try { try {
ABI = JSON.parse(stdout.replace(/\n/g, '')); ABI = JSON.parse(ABIString);
} catch (e) { } catch (e) {
return paraCb('ABI is not valid JSON'); return paraCb('ABI is not valid JSON');
} }
@ -69,7 +72,6 @@ class Vyper {
cb(err, compiled_object); cb(err, compiled_object);
}); });
} }
} }
module.exports = Vyper; module.exports = Vyper;