embark-area-51/lib/modules/vyper/index.js

88 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-04-12 17:24:54 +00:00
let async = require('../../utils/async_extend.js');
const shelljs = require('shelljs');
const path = require('path');
2018-04-12 17:24:54 +00:00
class Vyper {
constructor(embark, options) {
this.logger = embark.logger;
this.events = embark.events;
this.contractDirectories = options.contractDirectories;
2018-05-17 13:10:40 +00:00
// FIXME: Use array of extensions instead of duplicatiing
2018-04-12 17:24:54 +00:00
embark.registerCompiler(".py", this.compile_vyper.bind(this));
2018-05-17 13:10:40 +00:00
embark.registerCompiler(".vy", this.compile_vyper.bind(this));
2018-04-12 17:24:54 +00:00
}
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) {
2018-05-08 21:49:46 +00:00
return callback(__('Vyper exited with error code ') + code);
}
if (!stdout) {
2018-05-08 21:49:46 +00:00
return callback(__('Execution returned no result'));
}
callback(null, stdout.replace(/\n/g, ''));
});
}
2018-04-12 17:24:54 +00:00
compile_vyper(contractFiles, cb) {
let self = this;
2018-04-16 17:10:55 +00:00
if (!contractFiles || !contractFiles.length) {
return cb();
}
const vyper = shelljs.which('vyper');
if (!vyper) {
self.logger.warn(__('%s is not installed on your machine', 'Vyper'));
self.logger.info(__('You can install it by visiting: %s', 'https://vyper.readthedocs.io/en/latest/installing-vyper.html'));
return cb();
}
self.logger.info(__("compiling Vyper contracts") + "...");
2018-04-16 17:10:55 +00:00
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);
2018-04-16 17:10:55 +00:00
}
compiled_object[className].runtimeBytecode = byteCode;
compiled_object[className].realRuntimeBytecode = byteCode;
compiled_object[className].code = byteCode;
paraCb();
});
},
2018-04-16 17:10:55 +00:00
function getABI(paraCb) {
Vyper.compileVyperContract(file.filename, true, (err, ABIString) => {
if (err) {
return paraCb(err);
2018-04-16 17:10:55 +00:00
}
let ABI = [];
try {
ABI = JSON.parse(ABIString);
2018-04-16 17:10:55 +00:00
} catch (e) {
return paraCb('ABI is not valid JSON');
}
compiled_object[className].abiDefinition = ABI;
paraCb();
});
}
], fileCb);
},
function (err) {
cb(err, compiled_object);
});
2018-04-12 17:24:54 +00:00
}
}
module.exports = Vyper;