From 0c9dc4836212d4609547456171842069cdffa10d Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 27 Jul 2018 14:54:25 -0400 Subject: [PATCH] move code generator to a module --- lib/core/engine.js | 31 +++++++++++-------- .../code_templates/define-web3-simple.js.ejs | 0 .../define-when-env-loaded.js.ejs | 0 .../code_templates/do-when-loaded.js.ejs | 0 .../embark-building-placeholder.html.ejs | 0 .../code_templates/embarkjs-contract.js.ejs | 0 .../exec-when-env-loaded.js.ejs | 0 .../code_templates/exec-when-ready.js.ejs | 0 .../code_templates/load-manager.js.ejs | 0 .../code_templates/main-context.js.ejs | 0 .../code_templates/utils.js.ejs | 0 .../code_templates/vanilla-contract.js.ejs | 0 .../code_templates/web3-connector.js.ejs | 0 .../code_generator/index.js} | 25 +++++++++------ 14 files changed, 34 insertions(+), 22 deletions(-) rename lib/{contracts => modules/code_generator}/code_templates/define-web3-simple.js.ejs (100%) rename lib/{contracts => modules/code_generator}/code_templates/define-when-env-loaded.js.ejs (100%) rename lib/{contracts => modules/code_generator}/code_templates/do-when-loaded.js.ejs (100%) rename lib/{contracts => modules/code_generator}/code_templates/embark-building-placeholder.html.ejs (100%) rename lib/{contracts => modules/code_generator}/code_templates/embarkjs-contract.js.ejs (100%) rename lib/{contracts => modules/code_generator}/code_templates/exec-when-env-loaded.js.ejs (100%) rename lib/{contracts => modules/code_generator}/code_templates/exec-when-ready.js.ejs (100%) rename lib/{contracts => modules/code_generator}/code_templates/load-manager.js.ejs (100%) rename lib/{contracts => modules/code_generator}/code_templates/main-context.js.ejs (100%) rename lib/{contracts => modules/code_generator}/code_templates/utils.js.ejs (100%) rename lib/{contracts => modules/code_generator}/code_templates/vanilla-contract.js.ejs (100%) rename lib/{contracts => modules/code_generator}/code_templates/web3-connector.js.ejs (100%) rename lib/{contracts/code_generator.js => modules/code_generator/index.js} (95%) diff --git a/lib/core/engine.js b/lib/core/engine.js index 3f0acb477..293934125 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -158,23 +158,28 @@ class Engine { codeGeneratorService(_options) { let self = this; - const CodeGenerator = require('../contracts/code_generator.js'); - this.codeGenerator = new CodeGenerator({ - blockchainConfig: self.config.blockchainConfig, - contractsConfig: self.config.contractsConfig, - plugins: self.plugins, - storageConfig: self.config.storageConfig, - namesystemConfig: self.config.namesystemConfig, - communicationConfig: self.config.communicationConfig, - events: self.events, - env: self.env - }); - this.codeGenerator.listenToCommands(); + this.registerModule('code_generator', {plugins: self.plugins, env: self.env}); + //const CodeGenerator = require('../contracts/code_generator.js'); + //this.codeGenerator = new CodeGenerator({ + // blockchainConfig: self.config.blockchainConfig, + // contractsConfig: self.config.contractsConfig, + // plugins: self.plugins, + // storageConfig: self.config.storageConfig, + // namesystemConfig: self.config.namesystemConfig, + // communicationConfig: self.config.communicationConfig, + // events: self.events, + // env: self.env + //}); + //this.codeGenerator.listenToCommands(); const generateCode = function () { - self.codeGenerator.buildEmbarkJS(function() { + self.events.request("code-generator:embarkjs:build", () => { self.events.emit('code-generator-ready'); }); + + //self.codeGenerator.buildEmbarkJS(function() { + // self.events.emit('code-generator-ready'); + //}); }; const cargo = async.cargo((_tasks, callback) => { generateCode(); diff --git a/lib/contracts/code_templates/define-web3-simple.js.ejs b/lib/modules/code_generator/code_templates/define-web3-simple.js.ejs similarity index 100% rename from lib/contracts/code_templates/define-web3-simple.js.ejs rename to lib/modules/code_generator/code_templates/define-web3-simple.js.ejs diff --git a/lib/contracts/code_templates/define-when-env-loaded.js.ejs b/lib/modules/code_generator/code_templates/define-when-env-loaded.js.ejs similarity index 100% rename from lib/contracts/code_templates/define-when-env-loaded.js.ejs rename to lib/modules/code_generator/code_templates/define-when-env-loaded.js.ejs diff --git a/lib/contracts/code_templates/do-when-loaded.js.ejs b/lib/modules/code_generator/code_templates/do-when-loaded.js.ejs similarity index 100% rename from lib/contracts/code_templates/do-when-loaded.js.ejs rename to lib/modules/code_generator/code_templates/do-when-loaded.js.ejs diff --git a/lib/contracts/code_templates/embark-building-placeholder.html.ejs b/lib/modules/code_generator/code_templates/embark-building-placeholder.html.ejs similarity index 100% rename from lib/contracts/code_templates/embark-building-placeholder.html.ejs rename to lib/modules/code_generator/code_templates/embark-building-placeholder.html.ejs diff --git a/lib/contracts/code_templates/embarkjs-contract.js.ejs b/lib/modules/code_generator/code_templates/embarkjs-contract.js.ejs similarity index 100% rename from lib/contracts/code_templates/embarkjs-contract.js.ejs rename to lib/modules/code_generator/code_templates/embarkjs-contract.js.ejs diff --git a/lib/contracts/code_templates/exec-when-env-loaded.js.ejs b/lib/modules/code_generator/code_templates/exec-when-env-loaded.js.ejs similarity index 100% rename from lib/contracts/code_templates/exec-when-env-loaded.js.ejs rename to lib/modules/code_generator/code_templates/exec-when-env-loaded.js.ejs diff --git a/lib/contracts/code_templates/exec-when-ready.js.ejs b/lib/modules/code_generator/code_templates/exec-when-ready.js.ejs similarity index 100% rename from lib/contracts/code_templates/exec-when-ready.js.ejs rename to lib/modules/code_generator/code_templates/exec-when-ready.js.ejs diff --git a/lib/contracts/code_templates/load-manager.js.ejs b/lib/modules/code_generator/code_templates/load-manager.js.ejs similarity index 100% rename from lib/contracts/code_templates/load-manager.js.ejs rename to lib/modules/code_generator/code_templates/load-manager.js.ejs diff --git a/lib/contracts/code_templates/main-context.js.ejs b/lib/modules/code_generator/code_templates/main-context.js.ejs similarity index 100% rename from lib/contracts/code_templates/main-context.js.ejs rename to lib/modules/code_generator/code_templates/main-context.js.ejs diff --git a/lib/contracts/code_templates/utils.js.ejs b/lib/modules/code_generator/code_templates/utils.js.ejs similarity index 100% rename from lib/contracts/code_templates/utils.js.ejs rename to lib/modules/code_generator/code_templates/utils.js.ejs diff --git a/lib/contracts/code_templates/vanilla-contract.js.ejs b/lib/modules/code_generator/code_templates/vanilla-contract.js.ejs similarity index 100% rename from lib/contracts/code_templates/vanilla-contract.js.ejs rename to lib/modules/code_generator/code_templates/vanilla-contract.js.ejs diff --git a/lib/contracts/code_templates/web3-connector.js.ejs b/lib/modules/code_generator/code_templates/web3-connector.js.ejs similarity index 100% rename from lib/contracts/code_templates/web3-connector.js.ejs rename to lib/modules/code_generator/code_templates/web3-connector.js.ejs diff --git a/lib/contracts/code_generator.js b/lib/modules/code_generator/index.js similarity index 95% rename from lib/contracts/code_generator.js rename to lib/modules/code_generator/index.js index 9cae038c4..8c13819b0 100644 --- a/lib/contracts/code_generator.js +++ b/lib/modules/code_generator/index.js @@ -1,6 +1,6 @@ let async = require('async'); -let fs = require('../core/fs.js'); -const utils = require('../utils/utils.js'); +let fs = require('../../core/fs.js'); +const utils = require('../../utils/utils.js'); require('ejs'); const Templates = { @@ -19,17 +19,24 @@ const Templates = { }; class CodeGenerator { - constructor(options) { - this.blockchainConfig = options.blockchainConfig || {}; + constructor(embark, options) { + this.blockchainConfig = embark.config.blockchainConfig || {}; this.rpcHost = this.blockchainConfig.rpcHost || ''; this.rpcPort = this.blockchainConfig.rpcPort || ''; - this.contractsConfig = options.contractsConfig || {}; - this.storageConfig = options.storageConfig || {}; - this.communicationConfig = options.communicationConfig || {}; - this.namesystemConfig = options.namesystemConfig || {}; + this.contractsConfig = embark.config.contractsConfig || {}; + this.storageConfig = embark.config.storageConfig || {}; + this.communicationConfig = embark.config.communicationConfig || {}; + this.namesystemConfig = embark.config.namesystemConfig || {}; this.env = options.env || 'development'; this.plugins = options.plugins; - this.events = options.events; + this.events = embark.events; + + this.listenToCommands(); + + const self = this; + this.events.setCommandHandler("code-generator:embarkjs:build", (cb) => { + self.buildEmbarkJS(cb); + }); } listenToCommands() {