From 16499e0bb0f596018d6adad7dd98369b83956569 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Fri, 12 Oct 2018 15:07:59 -0400 Subject: [PATCH] Array handling --- lib/modules/scaffolding-react/index.js | 12 +++++++++ .../scaffolding-react/templates/dapp.js.tpl | 18 ++++++++----- lib/modules/scaffolding-solidity/index.js | 6 ++++- lib/modules/scaffolding/index.js | 26 +++++++++++-------- 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/lib/modules/scaffolding-react/index.js b/lib/modules/scaffolding-react/index.js index 577a24c4..649b0ce2 100644 --- a/lib/modules/scaffolding-react/index.js +++ b/lib/modules/scaffolding-react/index.js @@ -23,6 +23,14 @@ Handlebars.registerHelper('ifeq', function(elem, value, options){ return options.inverse(this); }); +Handlebars.registerHelper('ifarr', function(elem, options){ + if(elem.indexOf('[]') > -1){ + return options.fn(this); + } + return options.inverse(this); +}); + + Handlebars.registerHelper('iflengthgt', function(arr, val, options) { if (arr.length > val) { return options.fn(this); @@ -34,6 +42,10 @@ Handlebars.registerHelper('emptyname', function(name, index) { return name ? name : 'output' + index; }); +Handlebars.registerHelper('trim', function(name) { + return name.replace('[]', ''); +}); + Handlebars.registerHelper('methodname', function(abiDefinition, functionName, inputs){ let funCount = abiDefinition.filter(x => x.name === functionName).length; diff --git a/lib/modules/scaffolding-react/templates/dapp.js.tpl b/lib/modules/scaffolding-react/templates/dapp.js.tpl index 66df2ff3..4cc141e2 100644 --- a/lib/modules/scaffolding-react/templates/dapp.js.tpl +++ b/lib/modules/scaffolding-react/templates/dapp.js.tpl @@ -14,7 +14,7 @@ class {{capitalize name}}Form{{@index}} extends Component { {{#if inputs.length}} input: { {{#each inputs}} - {{#ifeq name ''}}field{{else}}{{name}}{{/ifeq}}: {{#ifeq type 'bool'}}false{{else}}''{{/ifeq}}{{#unless @last}},{{/unless}} + {{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}: {{#ifeq type 'bool'}}false{{else}}''{{/ifeq}}{{#unless @last}},{{/unless}} {{/each}} }, {{/if}} @@ -50,7 +50,7 @@ class {{capitalize name}}Form{{@index}} extends Component { try { {{#ifview stateMutability}} - const result = await {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}input.{{#ifeq name ''}}field{{else}}{{name}}{{/ifeq}}{{#unless @last}}, {{/unless}}{{/each}}).call() + const result = await {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}input.{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}{{#unless @last}}, {{/unless}}{{/each}}).call() {{#iflengthgt outputs 1}} this.setState({output: { {{#each outputs}} @@ -61,7 +61,13 @@ class {{capitalize name}}Form{{@index}} extends Component { this.setState({output: result}); {{/iflengthgt}} {{else}} - const toSend = {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}input.{{#ifeq name ''}}field{{else}}{{name}}{{/ifeq}}{{#unless @last}}, {{/unless}}{{/each}}); + {{#each inputs}} + {{#ifarr type}} + input.{{trim name}} = input.{{trim name}}.split(',').map(x => trim(x.toString())); + {{/ifarr}} + {{/each}} + + const toSend = {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}input.{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}{{#unless @last}}, {{/unless}}{{/each}}); const estimatedGas = await toSend.estimateGas({from: web3.eth.defaultAccount}); @@ -95,14 +101,14 @@ class {{capitalize name}}Form{{@index}} extends Component { {{#ifeq name ''}}field{{else}}{{name}}{{/ifeq}} {{#ifeq type 'bool'}} this.handleCheckbox(e, '{{#ifeq name ''}}field{{else}}{{name}}{{/ifeq}}')} + onClick={(e) => this.handleCheckbox(e, '{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}')} /> {{else}} this.handleChange(e, '{{#ifeq name ''}}field{{else}}{{name}}{{/ifeq}}')} + onChange={(e) => this.handleChange(e, '{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}')} /> {{/ifeq}} diff --git a/lib/modules/scaffolding-solidity/index.js b/lib/modules/scaffolding-solidity/index.js index ce92b911..13d0b421 100644 --- a/lib/modules/scaffolding-solidity/index.js +++ b/lib/modules/scaffolding-solidity/index.js @@ -2,6 +2,8 @@ const Handlebars = require('handlebars'); const fs = require('../../core/fs'); +const capitalize = string => string.charAt(0).toUpperCase() + string.slice(1); + class ScaffoldingSolidity { constructor(embark, options){ this.embark = embark; @@ -27,7 +29,9 @@ class ScaffoldingSolidity { build(contract, overwrite, cb){ try { - const filename = contract.className.toLowerCase(); + contract.className = capitalize(contract.className); + + const filename = contract.className; this._generateFile(contract, 'contract.sol.tpl', 'sol', { 'contractName': contract.className, diff --git a/lib/modules/scaffolding/index.js b/lib/modules/scaffolding/index.js index 0544bbd8..b5958fd1 100644 --- a/lib/modules/scaffolding/index.js +++ b/lib/modules/scaffolding/index.js @@ -16,11 +16,7 @@ class Scaffolding { this.generate(options.contract, options.overwrite, false, cb); }); } - - isContract(contractName){ - return this.engine.config.contractsConfig.contracts[contractName] !== undefined; - } - + getScaffoldPlugin(framework){ let dappGenerators = this.plugins.getPluginsFor('dappGenerator'); let builder; @@ -49,21 +45,23 @@ class Scaffolding { generate(contractName, overwrite, isContractGeneration, cb){ this.loadFrameworkModule(); - let build = this.getScaffoldPlugin(this.framework); + const build = this.getScaffoldPlugin(this.framework); if(!build){ this.engine.logger.error("Could not find plugin for framework '" + this.framework + "'"); process.exit(); } - - if(!this.isContract(contractName) && Object.getOwnPropertyNames(this.fields).length === 0){ - this.engine.logger.error("contract '" + contractName + "' does not exist"); + + const hasFields = Object.getOwnPropertyNames(this.fields).length !== 0; + + if(isContractGeneration && !hasFields){ + // This happens when you execute "scaffold ContractName", + // assuming the contract already exists in a .sol file cb(); return; } - let contract; - if(isContractGeneration){ + if(isContractGeneration && hasFields){ contract = {className: contractName, fields: this.fields}; try { build(contract, overwrite, cb); @@ -75,6 +73,12 @@ class Scaffolding { this.engine.events.request("contracts:list", (_err, contractsList) => { if(_err) throw new Error(_err); const contract = contractsList.find(x => x.className === contractName); + if(!contract){ + this.engine.logger.error("contract '" + contractName + "' does not exist"); + cb(); + return; + } + try { build(contract, overwrite, cb); } catch(err){