implement templates, build, and blockchain

This commit is contained in:
Iuri Matias 2016-08-21 10:42:42 -04:00
parent d1dae1bade
commit 6f5c5fae36
8 changed files with 319 additions and 7 deletions

View File

@ -10,7 +10,7 @@
"license": "ISC", "license": "ISC",
"homepage": "", "homepage": "",
"devDependencies": { "devDependencies": {
"embark-framework": "~/Projects/embark-framework", "embark-framework": "/Users/iurimatias/Projects/embark-framework",
"mocha": "^2.2.5" "mocha": "^2.2.5"
} }
} }

View File

@ -1 +0,0 @@
{}

16
demo/package.json Normal file
View File

@ -0,0 +1,16 @@
{
"name": "app_name",
"version": "0.0.1",
"description": "",
"main": "Gruntfile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"homepage": "",
"devDependencies": {
"embark-framework": "/Users/iurimatias/Projects/embark-framework",
"mocha": "^2.2.5"
}
}

View File

@ -1,5 +1,5 @@
EmbarkJS = { var EmbarkJS = {
}; };
options = { options = {
@ -75,3 +75,26 @@ EmbarkJS.Contract.prototype.deploy = function(args) {
return promise; return promise;
}; };
EmbarkJS.Messages = {
};
EmbarkJS.Messages.setProvider = function(msgProvider) {
};
EmbarkJS.Messages.sendMessage = function(options) {
};
EmbarkJS.Messages.listenTo = function(options) {
};
EmbarkJS.Messages.Whisper = {
};
EmbarkJS.Messages.Whisper.sendMessage = function(options) {
};
EmbarkJS.Messages.Whisper.listenTo = function(options) {
};

182
lib/blockchain.js Normal file
View File

@ -0,0 +1,182 @@
var mkdirp = require('mkdirp');
var wrench = require('wrench');
var Blockchain = function() {
};
Blockchain.prototype.run = function(options) {
this.env = env;
//var address = this.get_address();
var address = this.initChainAndGetAddress(options);
console.log("running: " + this.run_command(address, false));
exec(this.run_command(address, false));
//this.runChain(address, options);
};
Blockchain.prototype.initChainAndGetAddress = function(options) {
var address = null, result;
// ensure datadir exists, bypassing the interactive liabilities prompt.
this.datadir = '.embark/development/datadir';
mkdirp.sync(this.datadir);
wrench.copyDirSyncRecursive(__dirname + "/../js", ".embark/development/js", {forceDelete: true});
console.log("running: " + this.list_command());
result = exec(this.list_command());
if (result.output === undefined || result.output === '' || result.output.indexOf("Fatal") >= 0) {
//if (config.genesisBlock !== void 0) {
console.log("initializing genesis block");
console.log("running: " + this.generate_genesis_init_command());
result = exec(this.generate_genesis_init_command());
//}
console.log("running: " + this.init_command());
result = exec(this.init_command());
address = result.output.match(/{(\w+)}/)[1];
} else {
console.log("=== already initialized");
address = result.output.match(/{(\w+)}/)[1];
}
return address;
};
Blockchain.prototype.generate_genesis_init_command = function() {
//var config = this.config;
var cmd = "geth ";
//if (config.datadir !== "default") {
cmd += "--datadir=\"" + this.datadir + "\" ";
//}
//cmd += "init \"" + config.genesisBlock + "\" ";
cmd += "init \"" + "./config/development/genesis.json" + "\" ";
return cmd;
};
Blockchain.prototype.init_command = function() {
return this.generate_init_command() + "account new ";
};
Blockchain.prototype.generate_init_command = function() {
//var config = this.config;
//var address = config.account.address;
var cmd = "geth ";
//if (config.datadir !== "default") {
cmd += "--datadir=\"" + this.datadir + "\" ";
//}
//if (config.account.password !== void 0) {
cmd += "--password " + './config/development/password' + " ";
//}
return cmd;
};
Blockchain.prototype.generate_basic_command = function() {
var config = this.config;
//var address = config.account.address;
var cmd = "geth ";
var rpc_api = ['eth', 'web3'];
//if (config.datadir !== "default") {
cmd += "--datadir=\"" + this.datadir + "\" ";
//}
//if (config.testnet) {
// cmd += "--testnet ";
//}
//if (config.account.password !== void 0) {
//cmd += "--password " + config.account.password + " ";
cmd += "--password " + "./config/development/password" + " ";
//}
cmd += "--port " + "30303" + " ";
cmd += "--rpc ";
cmd += "--rpcport " + 8545 + " ";
cmd += "--rpcaddr " + "localhost" + " ";
cmd += "--networkid " + "12301" + " ";
cmd += "--rpccorsdomain=\"" + "*" + "\" ";
//cmd += "--port " + config.port + " ";
//cmd += "--rpc ";
//cmd += "--rpcport " + config.rpcPort + " ";
//cmd += "--rpcaddr " + config.rpcHost + " ";
//cmd += "--networkid " + config.networkId + " ";
//cmd += "--rpccorsdomain=\"" + config.rpcWhitelist + "\" ";
//if (config.minerthreads !== void 0) {
// cmd += "--minerthreads \"" + config.minerthreads + "\" ";
//}
//if(config.mine_when_needed || config.mine)
cmd += "--mine ";
//if (config.whisper) {
cmd += "--shh ";
rpc_api.push('shh');
//}
cmd += '--rpcapi "' + rpc_api.join(',') + '" ';
//TODO: this should be configurable
//cmd += "--maxpeers " + config.maxPeers + " ";
return cmd;
};
Blockchain.prototype.list_command = function() {
return this.generate_init_command() + "account list ";
};
Blockchain.prototype.run_command = function(address, use_tmp) {
var cmd = this.generate_basic_command();
//var config = this.config;
//if (address !== void 0) {
cmd += "--unlock=" + address + " ";
//}
cmd += "js .embark/development/js/mine.js";
//if (config.mine_when_needed) {
// if (use_tmp) {
// cmd += "js /tmp/js/mine.js";
// }
// else {
// cmd += "js node_modules/embark-framework/js/mine.js";
// }
//}
return cmd;
};
Blockchain.prototype.startChain = function(use_tmp) {
var address = this.get_address();
console.log("running: " + this.run_command(address, use_tmp));
exec(this.run_command(address, use_tmp));
};
Blockchain.prototype.getStartChainCommand = function(use_tmp) {
var address = this.get_address();
return this.run_command(address, use_tmp);
};
var BlockchainClient = function(client) {
if (client === 'geth') {
return new Blockchain();
} else {
throw new Error('unknown client');
}
};
module.exports = BlockchainClient;

View File

@ -6,25 +6,73 @@ var Cmd = function(Embark) {
}; };
Cmd.prototype.process = function(args) { Cmd.prototype.process = function(args) {
this.newApp();
this.demo();
this.build();
this.run(); this.run();
this.blockchain();
this.simulator(); this.simulator();
this.otherCommands(); this.otherCommands();
program.parse(args); program.parse(args);
}; };
Cmd.prototype.newApp = function() {
var self = this;
program
.command('new [name]')
.description('new application')
.action(function(name, options) {
if (name === undefined) {
console.log("please specify your app Name".red);
console.log("e.g embark new MyApp".green);
console.log("e.g embark new --help for more information".green);
exit();
}
self.Embark.generateTemplate('boilerplate', './', name);
});
};
Cmd.prototype.demo = function() {
var self = this;
program
.command('demo')
.description('create a working dapp with a SimpleStorage contract')
.action(function() {
self.Embark.generateTemplate('demo', './', 'embark_demo');
});
};
Cmd.prototype.build = function() {
var self = this;
program
.command('build [environment]')
.description('deploy and build dapp at dist/ (default: development)')
.action(function(env, options) {
self.Embark.build(env || 'development');
});
};
Cmd.prototype.run = function() { Cmd.prototype.run = function() {
var self = this; var self = this;
program program
.command('run [environment]') .command('run [environment]')
.description('run dapp (default: development)') .description('run dapp (default: development)')
//.option('-e', '--environment', 'choose environment to run (default: development)')
.action(function(env, options) { .action(function(env, options) {
//var EtherSim = require('ethersim');
//EtherSim.startServer();
self.Embark.run(env || 'development'); self.Embark.run(env || 'development');
}); });
}; };
Cmd.prototype.blockchain = function() {
var self = this;
program
.command('blockchain [environment]')
.option('-c, --client [client]', 'Use a specific ethereum client or simulator (supported: geth, parity, ethersim, testrpc')
.description('run blockchain server (default: development)')
.action(function(env ,options) {
self.Embark.blockchain(env || 'development', options.client || 'geth');
});
};
Cmd.prototype.simulator = function() { Cmd.prototype.simulator = function() {
program program
.command('simulator') .command('simulator')
@ -45,7 +93,6 @@ Cmd.prototype.simulator = function() {
Cmd.prototype.otherCommands = function() { Cmd.prototype.otherCommands = function() {
program program
.command('*')
.action(function(env){ .action(function(env){
console.log('unknown command "%s"'.red, env); console.log('unknown command "%s"'.red, env);
}); });

View File

@ -11,6 +11,8 @@ var Cmd = require('./cmd.js');
var Deploy = require('./deploy.js'); var Deploy = require('./deploy.js');
var ContractsManager = require('./contracts.js'); var ContractsManager = require('./contracts.js');
var ABIGenerator = require('./abi.js'); var ABIGenerator = require('./abi.js');
var TemplateGenerator = require('./template_generator.js');
var Blockchain = require('./blockchain.js');
var Embark = { var Embark = {
@ -19,6 +21,11 @@ var Embark = {
cmd.process(args); cmd.process(args);
}, },
generateTemplate: function(templateName, destinationFolder, name) {
var templateGenerator = new TemplateGenerator(templateName);
templateGenerator.generate(destinationFolder, name);
},
initConfig: function(configDir, files, env) { initConfig: function(configDir, files, env) {
this.contractsManager = new ContractsManager(configDir, files, env); this.contractsManager = new ContractsManager(configDir, files, env);
this.contractsManager.init(); this.contractsManager.init();
@ -133,6 +140,17 @@ var Embark = {
Embark.watch(); Embark.watch();
}); });
}); });
},
build: function(env) {
Embark.deploy(function(abi) {
Embark.buildAssets(abi);
});
},
blockchain: function(env, client) {
var blockchain = Blockchain(client);
blockchain.run({env: env});
} }
}; };

27
lib/template_generator.js Normal file
View File

@ -0,0 +1,27 @@
// TODO: replace with something else more native to node
require('shelljs/global');
var path = require('path');
var wrench = require('wrench');
var run = function(cmd) {
if (exec(cmd).code !== 0) {
exit();
}
};
var TemplateGenerator = function(templateName) {
this.templateName = templateName;
};
TemplateGenerator.prototype.generate = function(destinationFolder, name) {
var templatePath = path.join(__dirname + '/../' + this.templateName);
wrench.copyDirSyncRecursive(templatePath, destinationFolder + name);
cd(destinationFolder + name);
run('npm install');
console.log('\n\ninit complete'.green);
console.log('\n\app ready at '.green + destinationFolder + name);
};
module.exports = TemplateGenerator;