Changed scaffolding to service

This commit is contained in:
Richard Ramos 2018-10-05 19:13:00 -04:00 committed by Pascal Precht
parent e5e8d21765
commit 2c0644b5cb
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
6 changed files with 83 additions and 122 deletions

View File

@ -435,9 +435,7 @@ class EmbarkController {
this.context = options.context || [constants.contexts.scaffold];
options.onlyCompile = true;
const Scaffolding = require('../lib/cmds/scaffolding.js');
const Engine = require('../lib/core/engine.js');
const engine = new Engine({
env: options.env,
version: this.version,
@ -463,6 +461,7 @@ class EmbarkController {
engine.startService("pipeline");
engine.startService("deployment", {onlyCompile: true});
engine.startService("web3");
engine.startService("scaffolding");
engine.events.request('deploy:contracts', callback);
}
@ -471,8 +470,10 @@ class EmbarkController {
engine.logger.error(err.message);
engine.logger.info(err.stack);
} else {
let scaffold = new Scaffolding(engine, options);
scaffold.generate(options.contract, options.overwrite);
engine.events.request("scaffolding:generate", options, () => {
engine.logger.info(__("finished generating the UI").underline);
process.exit();
});
}
});
}

View File

@ -1,54 +0,0 @@
class Scaffolding {
constructor(engine, options){
this.engine = engine;
this.options = options;
this.framework = options.framework;
this.frameworkPlugin = null;
}
isContract(contractName){
return this.engine.config.contractsConfig.contracts[contractName] !== undefined;
}
generate(contractName, overwrite){
if(this.framework === 'react'){
this.engine.plugins.loadInternalPlugin('scaffolding-react', this.options);
}
let dappGenerators = this.engine.plugins.getPluginsFor('dappGenerator');
let build = null;
dappGenerators.forEach((plugin) => {
plugin.dappGenerators.forEach((d) => {
if(d.framework === this.framework){
build = d.cb;
}
});
});
if(build === null){
throw new Error("Could not find plugin for framework '" + this.framework + "'");
}
if(!this.isContract(contractName)){
throw new Error("contract '" + contractName + "' does not exist");
}
this.engine.events.request("contracts:list", (_err, contractsList) => {
if(_err) throw new Error(_err);
const contract = contractsList.find(x => x.className === contractName);
try {
build(contract, overwrite, () => {
this.engine.logger.info(__("finished generating the UI").underline);
process.exit();
});
} catch(err){
this.engine.logger.error(err.message);
}
});
}
}
module.exports = Scaffolding;

View File

@ -9,6 +9,7 @@
"build": "build",
"console": "console",
"graph": "graph",
"scaffold": "scaffold",
"test": "test",
"reset": "reset",
"any": "any"

View File

@ -75,8 +75,10 @@ class Engine {
"storage": this.storageService,
"pluginCommand": this.pluginCommandService
"graph": this.graphService,
"pluginCommand": this.pluginCommandService,
"testRunner": this.testRunnerService,
"codeCoverage": this.codeCoverageService,
"testRunner": this.testRunnerService
"scaffolding": this.scaffoldingService
};
let service = services[serviceName];
@ -103,6 +105,10 @@ class Engine {
this.registerModule('graph');
}
scaffoldingService(_options) {
this.registerModule('scaffolding', {plugins: this.plugins});
}
pipelineService(_options) {
const self = this;
this.registerModule('pipeline', {

View File

@ -74,32 +74,37 @@ class ScaffoldingReact {
process.exit(1);
}
const filename = contract.className.toLowerCase();
try {
const filename = contract.className.toLowerCase();
this._generateFile(contract, 'index.html.tpl', 'html',
{
'title': contract.className,
filename
}, overwrite);
this._generateFile(contract, 'index.html.tpl', 'html',
{
'title': contract.className,
filename
}, overwrite);
this._generateFile(contract, 'dapp.js.tpl', 'js',
{
'title': contract.className,
'contractName': contract.className,
'functions': contract.abiDefinition.filter(x => x.type === 'function')
}, overwrite);
this._generateFile(contract, 'dapp.js.tpl', 'js',
{
'title': contract.className,
'contractName': contract.className,
'functions': contract.abiDefinition.filter(x => x.type === 'function')
}, overwrite);
// Update config
const contents = fs.readFileSync("./embark.json");
let embarkJson = JSON.parse(contents);
embarkJson.app["js/" + filename + ".js"] = "app/" + filename + '.js';
embarkJson.app[filename + ".html"] = "app/" + filename + '.html';
// Update config
const contents = fs.readFileSync("./embark.json");
let embarkJson = JSON.parse(contents);
embarkJson.app["js/" + filename + ".js"] = "app/" + filename + '.js';
embarkJson.app[filename + ".html"] = "app/" + filename + '.html';
fs.writeFileSync("./embark.json", JSON.stringify(embarkJson, null, 4));
fs.writeFileSync("./embark.json", JSON.stringify(embarkJson, null, 4));
this.embark.logger.info(filename + ".html generated");
this.embark.logger.info(filename + ".js generated");
this.embark.logger.info(filename + ".html generated");
this.embark.logger.info(filename + ".js generated");
} catch(error){
this.embark.logger.error(error.message);
process.exit(1);
}
cb();
});
}

View File

@ -1,50 +1,52 @@
const fs = require('fs');
const commandName = "generate-ui";
//const errorMessage = (message) => new Error(commandName + ": " + message);
const formatReplyMsg = (message) => commandName + ": " + message;
class Scaffolding {
constructor(embark, options){
this.embark = embark;
this.options = options;
this.framework = options.framework;
this.frameworkPlugin = null;
constructor(engine, _options){
this.engine = engine;
this.options = _options;
this.plugins = _options.plugins;
engine.events.setCommandHandler("scaffolding:generate", (options, cb) => {
this.framework = options.framework;
this.generate(options.contract, options.overwrite, cb);
});
}
isContract(contractName){
return this.embark.config.contractsConfig.contracts[contractName] !== undefined;
return this.engine.config.contractsConfig.contracts[contractName] !== undefined;
}
generate(contractName){
let frameworkPlugin;
generate(contractName, overwrite, cb){
if(this.framework === 'react'){
this.plugins.loadInternalPlugin('scaffolding-react', this.options);
}
if(this.framework == 'react'){
this.embark.plugins.loadInternalPlugin('scaffolding-react', this.options);
frameworkPlugin = this.embark.plugins.plugins.filter(x => x.name == "scaffolding-react")[0].pluginModule;
let dappGenerators = this.plugins.getPluginsFor('dappGenerator');
let build = null;
dappGenerators.forEach((plugin) => {
plugin.dappGenerators.forEach((d) => {
if(d.framework === this.framework){
build = d.cb;
}
});
});
if(build === null){
this.engine.logger.error("Could not find plugin for framework '" + this.framework + "'");
cb();
} else if(!this.isContract(contractName)){
this.engine.logger.error("contract '" + contractName + "' does not exist");
cb();
} else {
let plugins = this.embark.plugins.getPluginsFor(this.framework);
if(plugins.length !== 1){
//throw errorMessage("Could not find plugin for framework '" + this.framework + "'");
return formatReplyMsg("Could not find plugin for framework '" + this.framework + "'");
}
frameworkPlugin = plugins[0].pluginModule;
}
this.engine.events.request("contracts:list", (_err, contractsList) => {
if(_err) throw new Error(_err);
if(!this.isContract(contractName)){
return errorMessage("contract '" + contractName + "' does not exist");
}
const contract = this.embark.config.contractsConfig.contracts[contractName];
try {
let uiFramework = new frameworkPlugin(this.embark, this.options);
let result = uiFramework.build(contract);
this.embark.logger.info(result);
} catch(err){
throw errorMessage(err);
const contract = contractsList.find(x => x.className === contractName);
try {
build(contract, overwrite, cb);
} catch(err){
this.engine.logger.error(err.message);
}
});
}
}
}