2018-04-12 17:24:54 +00:00
|
|
|
let async = require('../../utils/async_extend.js');
|
|
|
|
const shelljs = require('shelljs');
|
2018-04-12 20:33:01 +00:00
|
|
|
const path = require('path');
|
2018-04-12 17:24:54 +00:00
|
|
|
|
|
|
|
class Vyper {
|
|
|
|
|
2018-05-30 16:26:49 +00:00
|
|
|
constructor(embark, _options) {
|
2018-04-12 17:24:54 +00:00
|
|
|
this.logger = embark.logger;
|
|
|
|
this.events = embark.events;
|
2018-05-30 16:26:49 +00:00
|
|
|
this.contractDirectories = embark.config.contractDirectories;
|
2018-04-12 17:24:54 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-14 15:18:41 +00:00
|
|
|
compileVyperContract(filename, compileABI, callback) {
|
|
|
|
const self = this;
|
|
|
|
const params = compileABI ? '-f=json ' : '';
|
2018-04-17 15:25:46 +00:00
|
|
|
shelljs.exec(`vyper ${params}${filename}`, {silent: true}, (code, stdout, stderr) => {
|
|
|
|
if (stderr) {
|
|
|
|
return callback(stderr);
|
|
|
|
}
|
|
|
|
if (code !== 0) {
|
2018-08-14 15:18:41 +00:00
|
|
|
self.logger.error(stdout);
|
2018-05-08 21:49:46 +00:00
|
|
|
return callback(__('Vyper exited with error code ') + code);
|
2018-04-17 15:25:46 +00:00
|
|
|
}
|
|
|
|
if (!stdout) {
|
2018-05-08 21:49:46 +00:00
|
|
|
return callback(__('Execution returned no result'));
|
2018-04-17 15:25:46 +00:00
|
|
|
}
|
|
|
|
callback(null, stdout.replace(/\n/g, ''));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-30 17:27:18 +00:00
|
|
|
compile_vyper(contractFiles, _options, cb) {
|
2018-08-14 15:18:41 +00:00
|
|
|
const self = this;
|
2018-04-16 17:10:55 +00:00
|
|
|
if (!contractFiles || !contractFiles.length) {
|
|
|
|
return cb();
|
|
|
|
}
|
2018-05-27 12:00:18 +00:00
|
|
|
|
2018-05-25 19:44:07 +00:00
|
|
|
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();
|
|
|
|
}
|
2018-05-27 12:00:18 +00:00
|
|
|
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) {
|
2018-08-14 15:18:41 +00:00
|
|
|
self.compileVyperContract(file.filename, false, (err, byteCode) => {
|
2018-04-17 15:25:46 +00:00
|
|
|
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-12 20:33:01 +00:00
|
|
|
},
|
2018-04-16 17:10:55 +00:00
|
|
|
function getABI(paraCb) {
|
2018-08-14 15:18:41 +00:00
|
|
|
self.compileVyperContract(file.filename, true, (err, ABIString) => {
|
2018-04-17 15:25:46 +00:00
|
|
|
if (err) {
|
|
|
|
return paraCb(err);
|
2018-04-16 17:10:55 +00:00
|
|
|
}
|
|
|
|
let ABI = [];
|
|
|
|
try {
|
2018-04-17 15:25:46 +00:00
|
|
|
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;
|