From 6f5c5fae366b54a5b0792dec13a523390e8e183b Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 21 Aug 2016 10:42:42 -0400 Subject: [PATCH] implement templates, build, and blockchain --- boilerplate/package.json | 2 +- demo/.embark/chains.json | 1 - demo/package.json | 16 ++++ js/embark.js | 25 +++++- lib/blockchain.js | 182 ++++++++++++++++++++++++++++++++++++++ lib/cmd.js | 55 +++++++++++- lib/index.js | 18 ++++ lib/template_generator.js | 27 ++++++ 8 files changed, 319 insertions(+), 7 deletions(-) delete mode 100644 demo/.embark/chains.json create mode 100644 demo/package.json create mode 100644 lib/blockchain.js create mode 100644 lib/template_generator.js diff --git a/boilerplate/package.json b/boilerplate/package.json index 3a99f6f88..7527b2efa 100644 --- a/boilerplate/package.json +++ b/boilerplate/package.json @@ -10,7 +10,7 @@ "license": "ISC", "homepage": "", "devDependencies": { - "embark-framework": "~/Projects/embark-framework", + "embark-framework": "/Users/iurimatias/Projects/embark-framework", "mocha": "^2.2.5" } } diff --git a/demo/.embark/chains.json b/demo/.embark/chains.json deleted file mode 100644 index 0967ef424..000000000 --- a/demo/.embark/chains.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/demo/package.json b/demo/package.json new file mode 100644 index 000000000..7527b2efa --- /dev/null +++ b/demo/package.json @@ -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" + } +} diff --git a/js/embark.js b/js/embark.js index 0c1695540..801decd81 100644 --- a/js/embark.js +++ b/js/embark.js @@ -1,5 +1,5 @@ -EmbarkJS = { +var EmbarkJS = { }; options = { @@ -75,3 +75,26 @@ EmbarkJS.Contract.prototype.deploy = function(args) { 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) { +}; + + + diff --git a/lib/blockchain.js b/lib/blockchain.js new file mode 100644 index 000000000..7def6c252 --- /dev/null +++ b/lib/blockchain.js @@ -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; + diff --git a/lib/cmd.js b/lib/cmd.js index 698e11978..ced1ac338 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -6,25 +6,73 @@ var Cmd = function(Embark) { }; Cmd.prototype.process = function(args) { + this.newApp(); + this.demo(); + this.build(); this.run(); + this.blockchain(); this.simulator(); this.otherCommands(); 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() { var self = this; program .command('run [environment]') .description('run dapp (default: development)') - //.option('-e', '--environment', 'choose environment to run (default: development)') .action(function(env, options) { - //var EtherSim = require('ethersim'); - //EtherSim.startServer(); 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() { program .command('simulator') @@ -45,7 +93,6 @@ Cmd.prototype.simulator = function() { Cmd.prototype.otherCommands = function() { program - .command('*') .action(function(env){ console.log('unknown command "%s"'.red, env); }); diff --git a/lib/index.js b/lib/index.js index 0e2872af5..5966c9d02 100644 --- a/lib/index.js +++ b/lib/index.js @@ -11,6 +11,8 @@ var Cmd = require('./cmd.js'); var Deploy = require('./deploy.js'); var ContractsManager = require('./contracts.js'); var ABIGenerator = require('./abi.js'); +var TemplateGenerator = require('./template_generator.js'); +var Blockchain = require('./blockchain.js'); var Embark = { @@ -19,6 +21,11 @@ var Embark = { cmd.process(args); }, + generateTemplate: function(templateName, destinationFolder, name) { + var templateGenerator = new TemplateGenerator(templateName); + templateGenerator.generate(destinationFolder, name); + }, + initConfig: function(configDir, files, env) { this.contractsManager = new ContractsManager(configDir, files, env); this.contractsManager.init(); @@ -133,6 +140,17 @@ var Embark = { Embark.watch(); }); }); + }, + + build: function(env) { + Embark.deploy(function(abi) { + Embark.buildAssets(abi); + }); + }, + + blockchain: function(env, client) { + var blockchain = Blockchain(client); + blockchain.run({env: env}); } }; diff --git a/lib/template_generator.js b/lib/template_generator.js new file mode 100644 index 000000000..f140b1041 --- /dev/null +++ b/lib/template_generator.js @@ -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;