abstract compiler into its own module

This commit is contained in:
Iuri Matias 2015-07-03 23:23:21 -04:00
parent 3f86244fbd
commit c2416389fc
6 changed files with 52 additions and 27 deletions

25
lib/compiler.js Normal file
View File

@ -0,0 +1,25 @@
var web3 = require('web3');
Compiler = function(blockchainConfig) {
this.blockchainConfig = blockchainConfig;
};
Compiler.prototype.init = function(env) {
var config = this.blockchainConfig.config(env);
try {
web3.setProvider(new web3.providers.HttpProvider("http://" + config.rpcHost + ":" + config.rpcPort));
primaryAddress = web3.eth.coinbase;
web3.eth.defaultAccount = primaryAddress;
} catch (e) {
throw new Error("can't connect to " + config.rpcHost + ":" + config.rpcPort + " check if an ethereum node is running");
}
console.log("address is : " + primaryAddress);
};
Compiler.prototype.compile = function(source) {
return web3.eth.compile.solidity(source);
};
module.exports = Compiler;

View File

@ -2,9 +2,9 @@ var readYaml = require('read-yaml');
var fs = require('fs');
var toposort = require('toposort');
ContractsConfig = function(blockchainConfig, web3) {
ContractsConfig = function(blockchainConfig, compiler) {
this.blockchainConfig = blockchainConfig;
this.web3 = web3;
this.compiler = compiler;
this.contractFiles = [];
}
@ -17,17 +17,6 @@ ContractsConfig.prototype.init = function(files) {
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;
this.web3.eth.defaultAccount = primaryAddress;
} catch (_error) {
e = _error;
throw new Error("can't connect to " + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort + " check if an ethereum node is running");
}
console.log("address is : " + primaryAddress);
};
ContractsConfig.prototype.loadConfigFile = function(filename) {
@ -51,6 +40,7 @@ ContractsConfig.prototype.config = function(env) {
ContractsConfig.prototype.compileContracts = function(env) {
var contractFile, source, j;
var contractsConfig = this.config(env);
this.compiler.init(env);
if (contractsConfig != null) {
for (className in contractsConfig) {
@ -75,7 +65,7 @@ ContractsConfig.prototype.compileContracts = function(env) {
source = fs.readFileSync(contractFile).toString()
console.log("compiling " + contractFile);
compiled_contracts = this.web3.eth.compile.solidity(source);
compiled_contracts = this.compiler.compile(source);
for (className in compiled_contracts) {
var contract = compiled_contracts[className];
this.all_contracts.push(className);

View File

@ -17,11 +17,13 @@ var Blockchain = require('./blockchain.js');
var Deploy = require('./deploy.js');
var Release = require('./ipfs.js');
var Config = require('./config/config.js');
var Compiler = require('./config/compiler.js');
Embark = {
init: function() {
this.blockchainConfig = (new Config.Blockchain());
this.contractsConfig = (new Config.Contracts(this.blockchainConfig, web3));
this.compiler = (new Compiler(this.blockchainConfig()));
this.contractsConfig = (new Config.Contracts(this.blockchainConfig, this.compiler));
},
tests: function(contractFiles) {

View File

@ -1,15 +1,17 @@
var Config = require('../lib/config/config.js');
var Compiler = require('../lib/compiler.js');
var assert = require('assert');
var sinon = require('sinon');
var web3 = require('web3');
require('mocha-sinon');
describe('embark.config.contracts', function() {
var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config("development");
var _blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml');
var blockchainConfig = _blockchainConfig.config("development");
var compiler = new Compiler(_blockchainConfig);
describe('#loadConfigFile', function() {
it('should read and load yml file', function() {
var contractsConfig = new Config.Contracts(blockchainConfig, web3);
var contractsConfig = new Config.Contracts(blockchainConfig, compiler);
contractsConfig.loadConfigFile('test/support/contracts.yml');
assert.equal(contractsConfig.contractConfig.hasOwnProperty('development'), true)
@ -23,7 +25,7 @@ describe('embark.config.contracts', function() {
describe('#loadConfig', function() {
it('should load config', function() {
var contractsConfig = new Config.Contracts([], blockchainConfig, web3);
var contractsConfig = new Config.Contracts([], blockchainConfig, compiler);
var hsh = {
development: {},
staging: {}
@ -43,7 +45,7 @@ describe('embark.config.contracts', function() {
'test/support/contracts/simple_storage.sol',
'test/support/contracts/another_storage.sol'
]
contractsConfig = new Config.Contracts(blockchainConfig, web3);
contractsConfig = new Config.Contracts(blockchainConfig, compiler);
contractsConfig.loadConfigFile('test/support/contracts.yml');
contractsConfig.init(files);
contractsConfig.compileContracts();
@ -62,7 +64,7 @@ describe('embark.config.contracts', function() {
'test/support/contracts/another_storage.sol',
'test/support/contracts/wallets.sol'
]
contractsConfig = new Config.Contracts(blockchainConfig, web3);
contractsConfig = new Config.Contracts(blockchainConfig, compiler);
contractsConfig.loadConfigFile('test/support/arguments.yml');
contractsConfig.init(files);
contractsConfig.compileContracts('development');

View File

@ -1,7 +1,7 @@
var Config = require('../lib/config/config.js');
var Deploy = require('../lib/deploy.js');
var Compiler = require('../lib/compiler.js');
var assert = require('assert');
var web3 = require('web3');
describe('embark.deploy', function() {
var files = [
@ -10,13 +10,16 @@ describe('embark.deploy', function() {
'test/support/contracts/another_storage.sol',
'test/support/contracts/wallets.sol'
];
var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config("development");
var contractsConfig = new Config.Contracts(blockchainConfig, web3);
var _blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml');
var blockchainConfig = _blockchainConfig.config("development");
var compiler = new Compiler(_blockchainConfig);
var contractsConfig = new Config.Contracts(blockchainConfig, compiler);
contractsConfig.loadConfigFile('test/support/arguments.yml');
contractsConfig.init(files);
var deploy = new Deploy('development', files, blockchainConfig, contractsConfig);
describe('#deploy_contracts', function() {
compiler.init('development');
deploy.deploy_contracts("development");
it("should deploy contracts", function() {
@ -41,6 +44,7 @@ describe('embark.deploy', function() {
}
it("should deploy contracts", function() {
compiler.init('development');
var result = deploy.generate_abi_file();
assert.strictEqual(result, "web3.setProvider(new web3.providers.HttpProvider('http://localhost:8101'));web3.eth.defaultAccount = web3.eth.accounts[0];var SimpleStorageAbi = 123;var SimpleStorageContract = web3.eth.contract(SimpleStorageAbi);var SimpleStorage = SimpleStorageContract.at('0x123');var AnotherStorageAbi = 234;var AnotherStorageContract = web3.eth.contract(AnotherStorageAbi);var AnotherStorage = AnotherStorageContract.at('0x234');");

View File

@ -1,7 +1,7 @@
var Config = require('../lib/config/config.js');
var Test = require('../lib/test.js');
var Compiler = require('../lib/compiler.js');
var assert = require('assert');
var web3 = require('web3');
//var contractFiles = grunt.file.expand("./app/contracts/**/*.sol")
@ -9,8 +9,10 @@ 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);
var _blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml');
var blockchainConfig = _blockchainConfig.config("development");
var compiler = new Compiler(_blockchainConfig);
var contractsConfig = new Config.Contracts(blockchainConfig, compiler);
contractsConfig.loadConfigFile('test/support/contracts.yml');
contractsConfig.init(files);