2018-10-12 17:50:35 +00:00
|
|
|
|
|
|
|
const Handlebars = require('handlebars');
|
|
|
|
const fs = require('../../core/fs');
|
|
|
|
|
2018-10-12 19:07:59 +00:00
|
|
|
const capitalize = string => string.charAt(0).toUpperCase() + string.slice(1);
|
|
|
|
|
2018-10-12 17:50:35 +00:00
|
|
|
class ScaffoldingSolidity {
|
|
|
|
constructor(embark, options){
|
|
|
|
this.embark = embark;
|
|
|
|
this.options = options;
|
|
|
|
this.embark.registerDappGenerator('solidity', this.build.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
_generateFile(contract, templateFilename, extension, data, overwrite){
|
2018-10-15 12:36:59 +00:00
|
|
|
const filename = capitalize(contract.className.toLowerCase()) + '.' + extension;
|
2018-10-12 17:50:35 +00:00
|
|
|
const filePath = './contracts/' + filename;
|
|
|
|
if (!overwrite && fs.existsSync(filePath)){
|
|
|
|
throw new Error("file '" + filePath + "' already exists");
|
|
|
|
}
|
|
|
|
|
|
|
|
const templatePath = fs.embarkPath('lib/modules/scaffolding-solidity/templates/' + templateFilename);
|
|
|
|
const source = fs.readFileSync(templatePath).toString();
|
|
|
|
const template = Handlebars.compile(source);
|
|
|
|
|
|
|
|
// Write template
|
|
|
|
const result = template(data);
|
|
|
|
fs.writeFileSync(filePath, result);
|
2018-10-17 17:46:49 +00:00
|
|
|
return filePath;
|
2018-10-12 17:50:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 18:23:31 +00:00
|
|
|
build(contract, overwrite, cb){
|
2018-10-12 17:50:35 +00:00
|
|
|
try {
|
2018-10-12 19:07:59 +00:00
|
|
|
contract.className = capitalize(contract.className);
|
|
|
|
|
|
|
|
const filename = contract.className;
|
2018-10-12 17:50:35 +00:00
|
|
|
|
2018-10-17 17:46:49 +00:00
|
|
|
const filePath = this._generateFile(contract, 'contract.sol.tpl', 'sol', {
|
2018-10-12 17:50:35 +00:00
|
|
|
'contractName': contract.className,
|
|
|
|
'structName': contract.className + "Struct",
|
2018-10-12 18:23:31 +00:00
|
|
|
'fields': Object.keys(contract.fields).map(f => {
|
|
|
|
return {name:f, type: contract.fields[f]};
|
|
|
|
})
|
|
|
|
}, overwrite);
|
2018-10-12 17:50:35 +00:00
|
|
|
this.embark.logger.info("contracts/" + filename + ".sol generated");
|
|
|
|
|
2018-10-17 17:46:49 +00:00
|
|
|
cb(null, filePath);
|
2018-10-12 18:23:31 +00:00
|
|
|
} catch(error) {
|
2018-10-12 17:50:35 +00:00
|
|
|
this.embark.logger.error(error.message);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ScaffoldingSolidity;
|