diff --git a/bin/embark b/bin/embark index a99cd29b..a4acc80b 100755 --- a/bin/embark +++ b/bin/embark @@ -1,193 +1,4 @@ #!/usr/bin/env node -var program = require('commander'); -var path = require('path'); -var wrench = require('wrench'); -var grunt = require('grunt'); -require('shelljs/global'); -var readYaml = require('read-yaml'); var Embark = require('..'); - -var run = function(cmd) { - if (exec(cmd).code != 0) { - exit(); - } -} - -var deploy = function(env, embarkConfig, cb) { - var contractFiles = grunt.file.expand(embarkConfig.contracts); - var destFile = embarkConfig.output; - - Embark.init(); - Embark.blockchainConfig.loadConfigFile(embarkConfig.blockchainConfig); - Embark.contractsConfig.loadConfigFile(embarkConfig.contractsConfig); - - var chainFile = Embark.blockchainConfig.blockchainConfig[env].chains || embarkConfig.chains || './chains.json'; - - abi = Embark.deployContracts(env, contractFiles, destFile, chainFile, true, true, function(abi) { - grunt.file.write(destFile, abi); - cb(); - }); -} - -program - .version('1.2.0'); - -program.command('new [name]').description('New application').action(function(name) { - if (name === undefined) { - console.log("please specify the app name"); - exit; - } - var prefPath = path.join(__dirname + '/../boilerplate'); - - var targetDir = "./" + name; - wrench.copyDirSyncRecursive(prefPath, targetDir); - cd(targetDir); - run('npm install'); - console.log('\n\ninit complete'); -}); - -program.command('deploy [env]').description('deploy contracts').action(function(env_) { - var env = env_ || 'development'; - var embarkConfig = readYaml.sync("./embark.yml"); - - if (embarkConfig.type === "grunt") { - run("grunt deploy_contracts:" + env); - } - else { - deploy(env, embarkConfig, function() { exit(); }); - } -}); - -program.command('build [env]').description('build dapp').action(function(env_) { - var env = env_ || 'development'; - var embarkConfig = readYaml.sync("./embark.yml"); - - if (embarkConfig.type === "grunt") { - run("grunt clean"); - run("grunt deploy_contracts:" + env); - run('grunt build --env=' + env); - } - else if (embarkConfig.type === "meteor") { - deploy(env, embarkConfig); - run("meteor-build-client ./build -p ''"); - } -}); - -program.command('ipfs [env]').description('build dapp and make it available in ipfs').action(function(env_) { - var env = env_ || 'development'; - var embarkConfig = readYaml.sync("./embark.yml"); - - if (embarkConfig.type === "grunt") { - run("grunt clean") - run("grunt deploy_contracts:" + env) - run('grunt build --env=' + env) - run('grunt ipfs --env=' + env) - } - else if (embarkConfig.type === "meteor") { - deploy(env, embarkConfig); - run("meteor-build-client ./build -p ''"); - Embark.release.ipfs("build/") - } - else { - console.log("command not available in manual mode yet"); - } -}); - -program.command('run [env]').description('run dapp').action(function(env_) { - var env = env_ || 'development'; - var embarkConfig = readYaml.sync("./embark.yml"); - - if (embarkConfig.type === "grunt") { - run('grunt deploy --env=' + env); - } - else { - console.log("command not available in meteor or manual mode yet"); - console.log("try instead embark deploy; if using meteor then follow that with 'meteor'"); - } -}); - -program.command('spec').description('run tests').action(function() { - var embarkConfig = readYaml.sync("./embark.yml"); - - if (embarkConfig.type === "grunt") { - run('mocha test/ --no-timeouts'); - } - else { - console.log("command not available in meteor or manual mode yet"); - console.log("note: you can use embark tests with any framework"); - console.log("try running mocha test/"); - } -}); - -program.command('blockchain [env]').description('run blockchain').action(function(env_) { - var env = env_ || 'development'; - var embarkConfig = readYaml.sync("./embark.yml"); - - if (embarkConfig.type === "grunt") { - run('grunt blockchain:' + env); - } - else { - Embark.init() - Embark.blockchainConfig.loadConfigFile(embarkConfig.blockchainConfig) - Embark.contractsConfig.loadConfigFile(embarkConfig.contractsConfig) - - Embark.copyMinerJavascriptToTemp(); - - Embark.startBlockchain(env, true); - } -}); - -program.command('geth [args...]').description('run geth with specified arguments').action(function(env_, args_) { - var env = env_ || 'development'; - var embarkConfig = readYaml.sync("./embark.yml"); - var args = args_.join(' '); - - Embark.init() - Embark.blockchainConfig.loadConfigFile(embarkConfig.blockchainConfig) - Embark.contractsConfig.loadConfigFile(embarkConfig.contractsConfig) - - Embark.geth(env, args); -}); - -program.command('demo').description('create a working dapp with a SimpleStorage contract').action(function() { - var boilerPath = path.join(__dirname + '/../boilerplate'); - var demoPath = path.join(__dirname + '/../demo'); - - var targetDir = "./embark_demo"; - wrench.copyDirSyncRecursive(boilerPath, targetDir); - wrench.copyDirSyncRecursive(demoPath + "/app", targetDir + "/app", {forceDelete: true}); - wrench.copyDirSyncRecursive(demoPath + "/config", targetDir + "/config", {forceDelete: true}); - wrench.copyDirSyncRecursive(demoPath + "/test", targetDir + "/test", {forceDelete: true}); - - cd(targetDir); - run('npm install'); - console.log('\n\ninit complete'); -}); - -program.command('meteor_demo').description('create a working meteor dapp with a SimpleStorage contract').action(function() { - var boilerPath = path.join(__dirname + '/../demo_meteor'); - - var targetDir = "./embark_demo"; - wrench.copyDirSyncRecursive(boilerPath, targetDir); - console.log('\n\ninit complete'); -}); - -program.command('simulator').description('run a fast ethereum rpc simulator').action(function() { - try { - var EtherSim = require('ethersim'); - } catch(e) { - console.log('EtherSim not found; Please install it with "npm install ethersim --save"'); - console.log('For more information see https://github.com/iurimatias/ethersim'); - exit(); - } - EtherSim.startServer(); -}); - -program.parse(process.argv) - -if (!process.argv.slice(2).length) { - program.outputHelp(); -} - -//exit(); +Embark.process(process.argv) diff --git a/bin/old_embark b/bin/old_embark new file mode 100755 index 00000000..a99cd29b --- /dev/null +++ b/bin/old_embark @@ -0,0 +1,193 @@ +#!/usr/bin/env node + +var program = require('commander'); +var path = require('path'); +var wrench = require('wrench'); +var grunt = require('grunt'); +require('shelljs/global'); +var readYaml = require('read-yaml'); +var Embark = require('..'); + +var run = function(cmd) { + if (exec(cmd).code != 0) { + exit(); + } +} + +var deploy = function(env, embarkConfig, cb) { + var contractFiles = grunt.file.expand(embarkConfig.contracts); + var destFile = embarkConfig.output; + + Embark.init(); + Embark.blockchainConfig.loadConfigFile(embarkConfig.blockchainConfig); + Embark.contractsConfig.loadConfigFile(embarkConfig.contractsConfig); + + var chainFile = Embark.blockchainConfig.blockchainConfig[env].chains || embarkConfig.chains || './chains.json'; + + abi = Embark.deployContracts(env, contractFiles, destFile, chainFile, true, true, function(abi) { + grunt.file.write(destFile, abi); + cb(); + }); +} + +program + .version('1.2.0'); + +program.command('new [name]').description('New application').action(function(name) { + if (name === undefined) { + console.log("please specify the app name"); + exit; + } + var prefPath = path.join(__dirname + '/../boilerplate'); + + var targetDir = "./" + name; + wrench.copyDirSyncRecursive(prefPath, targetDir); + cd(targetDir); + run('npm install'); + console.log('\n\ninit complete'); +}); + +program.command('deploy [env]').description('deploy contracts').action(function(env_) { + var env = env_ || 'development'; + var embarkConfig = readYaml.sync("./embark.yml"); + + if (embarkConfig.type === "grunt") { + run("grunt deploy_contracts:" + env); + } + else { + deploy(env, embarkConfig, function() { exit(); }); + } +}); + +program.command('build [env]').description('build dapp').action(function(env_) { + var env = env_ || 'development'; + var embarkConfig = readYaml.sync("./embark.yml"); + + if (embarkConfig.type === "grunt") { + run("grunt clean"); + run("grunt deploy_contracts:" + env); + run('grunt build --env=' + env); + } + else if (embarkConfig.type === "meteor") { + deploy(env, embarkConfig); + run("meteor-build-client ./build -p ''"); + } +}); + +program.command('ipfs [env]').description('build dapp and make it available in ipfs').action(function(env_) { + var env = env_ || 'development'; + var embarkConfig = readYaml.sync("./embark.yml"); + + if (embarkConfig.type === "grunt") { + run("grunt clean") + run("grunt deploy_contracts:" + env) + run('grunt build --env=' + env) + run('grunt ipfs --env=' + env) + } + else if (embarkConfig.type === "meteor") { + deploy(env, embarkConfig); + run("meteor-build-client ./build -p ''"); + Embark.release.ipfs("build/") + } + else { + console.log("command not available in manual mode yet"); + } +}); + +program.command('run [env]').description('run dapp').action(function(env_) { + var env = env_ || 'development'; + var embarkConfig = readYaml.sync("./embark.yml"); + + if (embarkConfig.type === "grunt") { + run('grunt deploy --env=' + env); + } + else { + console.log("command not available in meteor or manual mode yet"); + console.log("try instead embark deploy; if using meteor then follow that with 'meteor'"); + } +}); + +program.command('spec').description('run tests').action(function() { + var embarkConfig = readYaml.sync("./embark.yml"); + + if (embarkConfig.type === "grunt") { + run('mocha test/ --no-timeouts'); + } + else { + console.log("command not available in meteor or manual mode yet"); + console.log("note: you can use embark tests with any framework"); + console.log("try running mocha test/"); + } +}); + +program.command('blockchain [env]').description('run blockchain').action(function(env_) { + var env = env_ || 'development'; + var embarkConfig = readYaml.sync("./embark.yml"); + + if (embarkConfig.type === "grunt") { + run('grunt blockchain:' + env); + } + else { + Embark.init() + Embark.blockchainConfig.loadConfigFile(embarkConfig.blockchainConfig) + Embark.contractsConfig.loadConfigFile(embarkConfig.contractsConfig) + + Embark.copyMinerJavascriptToTemp(); + + Embark.startBlockchain(env, true); + } +}); + +program.command('geth [args...]').description('run geth with specified arguments').action(function(env_, args_) { + var env = env_ || 'development'; + var embarkConfig = readYaml.sync("./embark.yml"); + var args = args_.join(' '); + + Embark.init() + Embark.blockchainConfig.loadConfigFile(embarkConfig.blockchainConfig) + Embark.contractsConfig.loadConfigFile(embarkConfig.contractsConfig) + + Embark.geth(env, args); +}); + +program.command('demo').description('create a working dapp with a SimpleStorage contract').action(function() { + var boilerPath = path.join(__dirname + '/../boilerplate'); + var demoPath = path.join(__dirname + '/../demo'); + + var targetDir = "./embark_demo"; + wrench.copyDirSyncRecursive(boilerPath, targetDir); + wrench.copyDirSyncRecursive(demoPath + "/app", targetDir + "/app", {forceDelete: true}); + wrench.copyDirSyncRecursive(demoPath + "/config", targetDir + "/config", {forceDelete: true}); + wrench.copyDirSyncRecursive(demoPath + "/test", targetDir + "/test", {forceDelete: true}); + + cd(targetDir); + run('npm install'); + console.log('\n\ninit complete'); +}); + +program.command('meteor_demo').description('create a working meteor dapp with a SimpleStorage contract').action(function() { + var boilerPath = path.join(__dirname + '/../demo_meteor'); + + var targetDir = "./embark_demo"; + wrench.copyDirSyncRecursive(boilerPath, targetDir); + console.log('\n\ninit complete'); +}); + +program.command('simulator').description('run a fast ethereum rpc simulator').action(function() { + try { + var EtherSim = require('ethersim'); + } catch(e) { + console.log('EtherSim not found; Please install it with "npm install ethersim --save"'); + console.log('For more information see https://github.com/iurimatias/ethersim'); + exit(); + } + EtherSim.startServer(); +}); + +program.parse(process.argv) + +if (!process.argv.slice(2).length) { + program.outputHelp(); +} + +//exit(); diff --git a/boilerplate/embark.json b/boilerplate/embark.json index 7cb537b5..ee0531a6 100644 --- a/boilerplate/embark.json +++ b/boilerplate/embark.json @@ -2,7 +2,7 @@ "contracts": ["app/contracts/**"], "app": { "css/app.css": ["app/css/**"], - "js/app.js": ["app/js/**"], + "js/app.js": ["embark.js", "app/js/**"], "index.html": "app/index.html" }, "config": "config/" diff --git a/demo/app/contracts/simple_storage.sol b/demo/app/contracts/simple_storage.sol index 5203fb63..b07e25a8 100644 --- a/demo/app/contracts/simple_storage.sol +++ b/demo/app/contracts/simple_storage.sol @@ -6,11 +6,12 @@ contract SimpleStorage { } function set(uint x) { - for (uint same3=0; same3 < 3; same3++) { - storedData = same3; - } - //storedData = x; + //for (uint same3=0; same3 < 3; same3++) { + // storedData = same3; + //} + storedData = x; } + function get() constant returns (uint retVal) { return storedData; } diff --git a/demo/config/development/blockchain.json b/demo/config/development/blockchain.json index c92f43aa..ce15515a 100644 --- a/demo/config/development/blockchain.json +++ b/demo/config/development/blockchain.json @@ -1,7 +1,7 @@ { "development": { "rpcHost": "localhost", - "rpcPort": 8101, + "rpcPort": 8545, "account": { "password": "config/development/password" } diff --git a/demo/embark.json b/demo/embark.json index 7cb537b5..ee0531a6 100644 --- a/demo/embark.json +++ b/demo/embark.json @@ -2,7 +2,7 @@ "contracts": ["app/contracts/**"], "app": { "css/app.css": ["app/css/**"], - "js/app.js": ["app/js/**"], + "js/app.js": ["embark.js", "app/js/**"], "index.html": "app/index.html" }, "config": "config/" diff --git a/lib/abi.js b/lib/abi.js index 58df204f..16fd15ec 100644 --- a/lib/abi.js +++ b/lib/abi.js @@ -2,7 +2,7 @@ var ABIGenerator = function(contractsManager) { this.contractsManager = contractsManager; this.rpcHost = 'localhost'; - this.rpcPort = '8101'; + this.rpcPort = '8545'; }; ABIGenerator.prototype.generateProvider = function() { @@ -36,4 +36,13 @@ ABIGenerator.prototype.generateContracts = function() { return result; }; +ABIGenerator.prototype.generateABI = function() { + var result = ""; + + result += this.generateProvider(); + result += this.generateContracts(); + + return result; +}; + module.exports = ABIGenerator; diff --git a/lib/cmd.js b/lib/cmd.js new file mode 100644 index 00000000..698e1197 --- /dev/null +++ b/lib/cmd.js @@ -0,0 +1,54 @@ +var program = require('commander'); +var colors = require('colors'); + +var Cmd = function(Embark) { + this.Embark = Embark; +}; + +Cmd.prototype.process = function(args) { + this.run(); + this.simulator(); + this.otherCommands(); + program.parse(args); +}; + +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.simulator = function() { + program + .command('simulator') + .description('run a fast ethereum rpc simulator') + .option('--ethersim', 'use ethersim as the rpc simulator [default]') + .action(function() { + var EtherSim; + try { + EtherSim = require('ethersim'); + } catch(e) { + console.log('EtherSim not found; Please install it with "npm install ethersim --save"'); + console.log('For more information see https://github.com/iurimatias/ethersim'); + process.exit(1); + } + EtherSim.startServer(); + }); +}; + +Cmd.prototype.otherCommands = function() { + program + .command('*') + .action(function(env){ + console.log('unknown command "%s"'.red, env); + }); +}; + +module.exports = Cmd; diff --git a/lib/index.js b/lib/index.js index 46bd4d96..fd1d51e5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -7,18 +7,25 @@ var mkdirp = require('mkdirp'); var colors = require('colors'); var chokidar = require('chokidar'); +var Cmd = require('./cmd.js'); var Deploy = require('./deploy.js'); var ContractsManager = require('./contracts.js'); var ABIGenerator = require('./abi.js'); var Embark = { + + process: function(args) { + var cmd = new Cmd(Embark); + cmd.process(args); + }, + initConfig: function(configDir, files, env) { this.contractsManager = new ContractsManager(configDir, files, env); this.contractsManager.init(); return this.contractsManager; }, - deploy: function() { + deploy: function(done) { async.waterfall([ function loadConfig(callback) { var contractsManager = Embark.initConfig('config/', 'app/contracts/**/*.sol', 'development'); @@ -30,7 +37,7 @@ var Embark = { }, function deployContracts(contractsManager, callback) { var web3 = new Web3(); - web3.setProvider(new web3.providers.HttpProvider('http://localhost:8101')); + web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545')); var deploy = new Deploy(web3, contractsManager); deploy.deployAll(function() { callback(null, contractsManager); @@ -38,22 +45,20 @@ var Embark = { }, function generateABI(contractsManager, callback) { var abiGenerator = new ABIGenerator(contractsManager); - console.log(abiGenerator.generateProvider()); - console.log(abiGenerator.generateContracts()); - callback(null, 'done'); + callback(null, abiGenerator.generateABI()); }, ], function(err, result) { - console.log(arguments); + done(result); }); }, - buildAssets: function() { + buildAssets: function(abi) { var embarkConfig = JSON.parse(fs.readFileSync("embark.json")); var appConfig = embarkConfig.app; for(var targetFile in appConfig) { - var originalFiles = grunt.file.expand(appConfig[targetFile]); + var originalFiles = grunt.file.expand({nonull: true}, appConfig[targetFile]); console.log(originalFiles); // remove duplicates @@ -61,7 +66,11 @@ var Embark = { return file.indexOf('.') >= 0; }).map(function(file) { console.log("reading " + file); - return fs.readFileSync(file); + if (file === 'embark.js') { + return fs.readFileSync("../js/web3.js") + "\n" + abi; + } else { + return fs.readFileSync(file); + } }).join("\n"); var dir = targetFile.split('/').slice(0, -1).join('/'); @@ -92,7 +101,7 @@ var Embark = { callback(); }, - watch: function() { + watch: function() { var embarkConfig = JSON.parse(fs.readFileSync("embark.json")); var appConfig = embarkConfig.app; @@ -115,13 +124,18 @@ var Embark = { .on('unlink', path => console.log(`File ${path} has been removed`)) .on('ready', () => console.log('ready to watch changes')); console.log("done!"); + }, + + run: function(env) { + Embark.deploy(function(abi) { + Embark.buildAssets(abi); + Embark.server(function() { + Embark.watch(); + }); + }); } + }; -//module.exports = Embark; - -Embark.buildAssets(); -Embark.server(function() { - Embark.watch(); -}); +module.exports = Embark;