From b7bdd841c955e494b9eb9d6b87fe9e9b2c3535e5 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 3 Jul 2015 22:27:17 -0400 Subject: [PATCH] refactor modules; update grunt tasks --- demo/spec/contracts/simple_storage_spec.js | 10 +++++++- lib/config/contracts.js | 4 ++++ lib/index.js | 24 ++++++++++++++++--- lib/ipfs.js | 2 +- lib/test.js | 25 +++++++------------- tasks/blockchain.coffee | 4 +++- tasks/deploy.coffee | 6 ++++- tasks/ipfs.coffee | 3 ++- test/test.js | 27 +++++++++++++++++++++- 9 files changed, 79 insertions(+), 26 deletions(-) diff --git a/demo/spec/contracts/simple_storage_spec.js b/demo/spec/contracts/simple_storage_spec.js index c8a6692e..9c545f27 100644 --- a/demo/spec/contracts/simple_storage_spec.js +++ b/demo/spec/contracts/simple_storage_spec.js @@ -1,4 +1,12 @@ -EmbarkSpec = require('embark-framework').Tests; +var Embark = require('embark-framework'); +Embark.init(); +Embark.blockchainConfig.loadConfigFile('config/blockchain.yml'); +Embark.contractsConfig.loadConfigFile('config/contracts.yml'); + +var files = ["app/contracts/simple_storage.sol"]; + +Embark.contractsConfig.init(files); +var EmbarkSpec = Embark.tests(files); describe("SimpleStorage", function() { beforeAll(function() { diff --git a/lib/config/contracts.js b/lib/config/contracts.js index 4416ae78..db7a76a5 100644 --- a/lib/config/contracts.js +++ b/lib/config/contracts.js @@ -16,6 +16,10 @@ ContractsConfig.prototype.init = function(files) { this.contractFiles = files; this.contractDependencies = {}; + if (this.blockchainConfig.config != undefined) { + this.blockchainConfig = this.blockchainConfig.config('development'); + } + try { this.web3.setProvider(new this.web3.providers.HttpProvider("http://" + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort)); primaryAddress = this.web3.eth.coinbase; diff --git a/lib/index.js b/lib/index.js index c28bbc9a..fc584b38 100644 --- a/lib/index.js +++ b/lib/index.js @@ -20,9 +20,27 @@ var Config = require('./config/config.js'); Embark = { init: function() { - this.blockchainConfig = (new Config.BlockchainConfig()); - this.contractsConfig = (new Config.ContractsConfig(this.blockchainConfig, web3)); - } + this.blockchainConfig = (new Config.Blockchain()); + this.contractsConfig = (new Config.Contracts(this.blockchainConfig, web3)); + }, + + tests: function(contractFiles) { + return new Tests(this.contractsConfig, contractFiles); + }, + + startBlockchain: function(env) { + var chain = new Blockchain(this.blockchainConfig.config(env)); + chain.startChain(); + }, + + deployContracts: function(env, contractFiles) { + this.contractsConfig.init(env, contractFiles); + var deploy = new Deploy(env, contractFiles, this.blockchainConfig.config(env), this.contractsConfig); + deploy.deploy_contracts(env); + return deploy.generate_abi_file(); + }, + + release: Release } module.exports = Embark; diff --git a/lib/ipfs.js b/lib/ipfs.js index 725d1240..35c91c0f 100644 --- a/lib/ipfs.js +++ b/lib/ipfs.js @@ -7,7 +7,7 @@ ipfs = function() { cmd = ipfs_path + "/ipfs add -r " + build_dir; - grunt.log.writeln("=== adding " + cmd + " to ipfs"); + console.log("=== adding " + cmd + " to ipfs"); result = exec(cmd); diff --git a/lib/test.js b/lib/test.js index ee735936..36df868f 100644 --- a/lib/test.js +++ b/lib/test.js @@ -59,27 +59,18 @@ TestContract = function(contract, className, args) { return Obj; } -request = function(className, args) { - py_exec("from ethertdd import EvmContract") - - web3.setProvider(new web3.providers.HttpProvider('http://localhost:8101')); - web3.eth.defaultAccount = web3.eth.accounts[0]; - - //TODO: get the files from the config - contractFiles = grunt.file.expand("./app/contracts/**/*.sol") - blockchainConfig = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env); - contractsConfig = new Config.Contracts(contractFiles, blockchainConfig); +test = function(contractsConfig, contractFiles) { + contractsConfig.init(contractFiles); contractsConfig.compileContracts(); - contractDB = contractsConfig.contractDB; + this.contractDB = contractsConfig.contractDB; +} - var contract = contractDB[className]; +test.prototype.request = function(className, args) { + var contract = this.contractDB[className]; + py_exec("from ethertdd import EvmContract") return TestContract(contract, className, args) } -Test = { - request: request -} - -module.exports = Test; +module.exports = test; diff --git a/tasks/blockchain.coffee b/tasks/blockchain.coffee index b5bcde1f..4cc574cc 100644 --- a/tasks/blockchain.coffee +++ b/tasks/blockchain.coffee @@ -3,5 +3,7 @@ module.exports = (grunt) -> grunt.registerTask "blockchain", "deploy ethereum node", (env_) => env = env_ || "development" Embark = require('embark-framework') - Embark.Blockchain.startChain(env) + Embark.init() + Embark.blockchainConfig.loadConfigFile('config/blockchain.yml') + Embark.startBlockchain(env) diff --git a/tasks/deploy.coffee b/tasks/deploy.coffee index c926588d..c032775e 100644 --- a/tasks/deploy.coffee +++ b/tasks/deploy.coffee @@ -8,5 +8,9 @@ module.exports = (grunt) -> destFile = grunt.config.get("deploy.dest"); Embark = require('embark-framework') - Embark.Deploy.deployContracts(env, contractFiles, destFile) + Embark.init() + Embark.blockchainConfig.loadConfigFile('config/blockchain.yml') + Embark.contractsConfig.loadConfigFile('config/contracts.yml') + abi = Embark.deployContracts(env, contractFiles, destFile) + grunt.file.write(destFile, abi); diff --git a/tasks/ipfs.coffee b/tasks/ipfs.coffee index fe11bed1..85b39916 100644 --- a/tasks/ipfs.coffee +++ b/tasks/ipfs.coffee @@ -1,6 +1,7 @@ module.exports = (grunt) -> grunt.registerTask "ipfs", "distribute into ipfs", (env_) => + env = env_ || "development" Embark = require('embark-framework') - Embark.Release.ipfs() + Embark.release.ipfs() diff --git a/test/test.js b/test/test.js index 6cb53b31..1682c1c4 100644 --- a/test/test.js +++ b/test/test.js @@ -1,2 +1,27 @@ -EmbarkSpec = require('../lib/test.js').Tests; +var Config = require('../lib/config/config.js'); +var Test = require('../lib/test.js'); +var assert = require('assert'); +var web3 = require('web3'); +//var contractFiles = grunt.file.expand("./app/contracts/**/*.sol") + +describe('embark.test', function() { + var files = [ + 'test/support/contracts/simple_storage.sol' + ] + var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config("development"); + var contractsConfig = new Config.Contracts(blockchainConfig, web3); + contractsConfig.loadConfigFile('test/support/contracts.yml'); + contractsConfig.init(files); + + describe('simple test', function() { + var embarkSpec = new Test(contractsConfig, files); + + it('execute simple test', function() { + var SimpleStorage = embarkSpec.request('SimpleStorage', [100]) + + assert.equal(SimpleStorage.storedData(), '100'); + }); + }); + +});