From 5f64376c846d7057dfdb07b447af82439362eb9f Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 22 May 2018 14:31:21 -0400 Subject: [PATCH] refactor generateContractsJson --- lib/contracts/code_generator.js | 69 +++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index 9b45318a..a6ffaf9d 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -46,68 +46,78 @@ class CodeGenerator { // deprecated events; to remove in embark 2.7.0 this.events.setCommandHandlerOnce('abi-vanila', function(cb) { let vanillaABI = self.generateABI({useEmbarkJS: false}); - let contractsJSON = self.generateContractsJSON(); - cb(vanillaABI, contractsJSON); + this.events.request("contracts:list", (contractsList) => { + let contractsJSON = self.generateContractsJSON(contractsList); + cb(vanillaABI, contractsJSON); + }); }); this.events.setCommandHandlerOnce('abi', function(cb) { let embarkJSABI = self.generateABI({useEmbarkJS: true}); - let contractsJSON = self.generateContractsJSON(); - - cb(embarkJSABI, contractsJSON); + this.events.request("contracts:list", (contractsList) => { + let contractsJSON = self.generateContractsJSON(contractsList); + cb(embarkJSABI, contractsJSON); + }); }); this.events.setCommandHandlerOnce('abi-contracts-vanila', function(cb) { let vanillaContractsABI = self.generateContracts(false, true, false); - let contractsJSON = self.generateContractsJSON(); - - cb(vanillaContractsABI, contractsJSON); + self.events.request("contracts:list", (contractsList) => { + let contractsJSON = self.generateContractsJSON(contractsList); + cb(vanillaContractsABI, contractsJSON); + }); }); this.events.setCommandHandlerOnce('abi-vanila-deployment', function(cb) { let vanillaABI = self.generateABI({useEmbarkJS: false, deployment: true}); - let contractsJSON = self.generateContractsJSON(); - - cb(vanillaABI, contractsJSON); + self.events.request("contracts:list", (contractsList) => { + let contractsJSON = self.generateContractsJSON(contractsList); + cb(vanillaABI, contractsJSON); + }); }); // new events this.events.setCommandHandlerOnce('code-vanila', function(cb) { let vanillaABI = self.generateABI({useEmbarkJS: false}); - let contractsJSON = self.generateContractsJSON(); - - cb(vanillaABI, contractsJSON); + self.events.request("contracts:list", (contractsList) => { + let contractsJSON = self.generateContractsJSON(contractsList); + cb(vanillaABI, contractsJSON); + }); }); this.events.setCommandHandlerOnce('code', function(cb) { let embarkJSABI = self.generateABI({useEmbarkJS: true}); - let contractsJSON = self.generateContractsJSON(); - - cb(embarkJSABI, contractsJSON); + self.events.request("contracts:list", (contractsList) => { + let contractsJSON = self.generateContractsJSON(contractsList); + cb(embarkJSABI, contractsJSON); + }); }); this.events.setCommandHandlerOnce('code-contracts-vanila', function(cb) { let vanillaContractsABI = self.generateContracts(false, true, false); - let contractsJSON = self.generateContractsJSON(); - - cb(vanillaContractsABI, contractsJSON); + self.events.request("contracts:list", (contractsList) => { + let contractsJSON = self.generateContractsJSON(contractsList); + cb(vanillaContractsABI, contractsJSON); + }); }); this.events.setCommandHandlerOnce('code-vanila-deployment', function(cb) { let vanillaABI = self.generateABI({useEmbarkJS: false, deployment: true}); - let contractsJSON = self.generateContractsJSON(); - - cb(vanillaABI, contractsJSON); + self.events.request("contracts:list", (contractsList) => { + let contractsJSON = self.generateContractsJSON(contractsList); + cb(vanillaABI, contractsJSON); + }); }); this.events.setCommandHandlerOnce('code-generator:web3js', function(cb) { self.buildWeb3JS(cb); }); - this.events.setCommandHandler('code-generator:contract', (contractName, cb) => { - let contract = self.contractsManager.contracts[contractName]; - self.buildContractJS(contractName, self.generateContractJSON(contractName, contract), cb); + self.events.setCommandHandler('code-generator:contract', (contractName, cb) => { + self.events.setCommandHandler('contracts:contract', contractName, (contract) => { + self.buildContractJS(contractName, self.generateContractJSON(contract, contract), cb); + }); }); self.events.setCommandHandler('code-generator:contract:vanilla', (contract, gasLimit, cb) => { @@ -304,12 +314,11 @@ class CodeGenerator { return contractJSON; } - generateContractsJSON() { + generateContractsJSON(contractsList) { let contracts = {}; - for (let className in this.contractsManager.contracts) { - let contract = this.contractsManager.contracts[className]; - contracts[className] = this.generateContractJSON(className, contract); + for (let contract of contractsList) { + contracts[contract.className] = this.generateContractJSON(contract.className, contract); } return contracts;