|
| 1 | +let async = require('../../utils/async_extend.js'); |
| 2 | +const shelljs = require('shelljs'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +class Vyper { |
| 6 | + |
| 7 | + constructor(embark, options) { |
| 8 | + this.logger = embark.logger; |
| 9 | + this.events = embark.events; |
| 10 | + this.contractDirectories = options.contractDirectories; |
| 11 | + |
| 12 | + embark.registerCompiler(".py", this.compile_vyper.bind(this)); |
| 13 | + } |
| 14 | + |
| 15 | + compile_vyper(contractFiles, cb) { |
| 16 | + let self = this; |
| 17 | + async.waterfall([ |
| 18 | + function compileContracts(callback) { |
| 19 | + self.logger.info("compiling vyper contracts..."); |
| 20 | + const compiled_object = {}; |
| 21 | + async.each(contractFiles, |
| 22 | + function (file, fileCb) { |
| 23 | + const className = path.basename(file.filename).split('.')[0]; |
| 24 | + compiled_object[className] = {}; |
| 25 | + async.parallel([ |
| 26 | + function getByteCode(paraCb) { |
| 27 | + shelljs.exec(`vyper ${file.filename}`, {silent: true}, (code, stdout, stderr) => { |
| 28 | + if (stderr) { |
| 29 | + return paraCb(stderr); |
| 30 | + } |
| 31 | + if (code !== 0) { |
| 32 | + return paraCb(`Vyper exited with error code ${code}`); |
| 33 | + } |
| 34 | + if (!stdout) { |
| 35 | + return paraCb('Execution returned no bytecode'); |
| 36 | + } |
| 37 | + const byteCode = stdout.replace(/\n/g, ''); |
| 38 | + compiled_object[className].runtimeBytecode = byteCode; |
| 39 | + compiled_object[className].realRuntimeBytecode = byteCode; |
| 40 | + compiled_object[className].code = byteCode; |
| 41 | + paraCb(); |
| 42 | + }); |
| 43 | + }, |
| 44 | + function getABI(paraCb) { |
| 45 | + shelljs.exec(`vyper -f json ${file.filename}`, {silent: true}, (code, stdout, stderr) => { |
| 46 | + if (stderr) { |
| 47 | + return paraCb(stderr); |
| 48 | + } |
| 49 | + if (code !== 0) { |
| 50 | + return paraCb(`Vyper exited with error code ${code}`); |
| 51 | + } |
| 52 | + if (!stdout) { |
| 53 | + return paraCb('Execution returned no ABI'); |
| 54 | + } |
| 55 | + let ABI = []; |
| 56 | + try { |
| 57 | + ABI = JSON.parse(stdout.replace(/\n/g, '')); |
| 58 | + } catch (e) { |
| 59 | + return paraCb('ABI is not valid JSON'); |
| 60 | + } |
| 61 | + compiled_object[className].abiDefinition = ABI; |
| 62 | + paraCb(); |
| 63 | + }); |
| 64 | + } |
| 65 | + ], fileCb); |
| 66 | + }, |
| 67 | + function (err) { |
| 68 | + callback(err, compiled_object); |
| 69 | + }); |
| 70 | + } |
| 71 | + ], function (err, result) { |
| 72 | + cb(err, result); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +module.exports = Vyper; |
0 commit comments