From b72a42cfb375906164296dcc6429fdcc83f2b4c2 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 15 Jan 2017 14:30:41 -0500 Subject: [PATCH] add plugin config options; update pipeline plugin implementation --- lib/index.js | 4 ++-- lib/pipeline.js | 20 ++++++++++++-------- lib/plugin.js | 31 ++++++++++++++++++++++++++++--- lib/plugins.js | 22 ++++++++++++++++------ 4 files changed, 58 insertions(+), 19 deletions(-) diff --git a/lib/index.js b/lib/index.js index 32c315582..38b7a9fe9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -45,7 +45,7 @@ var Embark = { this.config = new Config({env: env}); this.config.loadConfigFiles(options); this.logger = new Logger({logLevel: 'debug'}); - this.plugins = new Plugins({plugins: this.config.embarkConfig.plugins}); + this.plugins = new Plugins({plugins: this.config.embarkConfig.plugins, logger: this.logger}); this.plugins.loadPlugins(); }, @@ -138,7 +138,7 @@ var Embark = { callback(); }, function displayLoadedPlugins(callback) { - var pluginList = self.plugins.pluginList; + var pluginList = self.plugins.listPlugins(); if (pluginList.length > 0) { self.logger.info("loaded plugins: " + pluginList.join(", ")); } diff --git a/lib/pipeline.js b/lib/pipeline.js index 9dbfbb465..a5a4fc910 100644 --- a/lib/pipeline.js +++ b/lib/pipeline.js @@ -18,21 +18,25 @@ Pipeline.prototype.build = function(abi) { var content = this.assetFiles[targetFile].map(file => { self.logger.info("reading " + file.filename); + + var pipelinePlugins = this.plugins.getPluginsFor('pipeline'); + if (file.filename === 'embark.js') { return file.content + "\n" + abi; + } else if (['web3.js', 'ipfs.js', 'ipfs-api.js', 'orbit.js'].indexOf(file.filename) >= 0) { + return file.content; } else { + + if (pipelinePlugins.length > 0) { + pipelinePlugins.forEach(function(plugin) { + file.content = plugin.runPipeline({targetFile: file.filename, source: file.content}); + }); + } + return file.content; } }).join("\n"); - var pipelinePlugins = this.plugins.getPluginsFor('pipeline'); - - if (pipelinePlugins.length > 0) { - pipelinePlugins.forEach(function(plugin) { - content = plugin.runPipeline({targetFile: targetFile, source: content}); - }); - } - var dir = targetFile.split('/').slice(0, -1).join('/'); self.logger.info("creating dir " + this.buildDir + dir); mkdirp.sync(this.buildDir + dir); diff --git a/lib/plugin.js b/lib/plugin.js index cfda13937..ccc42caac 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -1,18 +1,33 @@ +/*jshint esversion: 6, loopfunc: true */ +var grunt = require('grunt'); // TODO: pass other params like blockchainConfig, contract files, etc.. var Plugin = function(options) { this.name = options.name; this.pluginModule = options.pluginModule; + this.pluginConfig = options.pluginConfig; this.clientWeb3Providers = []; this.contractsGenerators = []; this.pipeline = []; this.pluginTypes = []; + this.logger = options.logger; }; Plugin.prototype.loadPlugin = function() { + this.interceptLogs(this.pluginModule); (this.pluginModule.call(this, this)); }; +Plugin.prototype.interceptLogs = function(context) { + var self = this; + context.console = console; + context.console.error = function(txt) { + // TODO: logger should support an array instead of a single argument + //self.logger.error.apply(self.logger, arguments); + self.logger.error(self.name + " > " + txt); + }; +}; + // TODO: add deploy provider Plugin.prototype.registerClientWeb3Provider = function(cb) { this.clientWeb3Providers.push(cb); @@ -24,9 +39,9 @@ Plugin.prototype.registerContractsGeneration = function(cb) { this.pluginTypes.push('contractGeneration'); }; -Plugin.prototype.registerPipeline = function(cb) { +Plugin.prototype.registerPipeline = function(matcthingFiles, cb) { // TODO: generate error for more than one pipeline per plugin - this.pipeline.push(cb); + this.pipeline.push({matcthingFiles: matcthingFiles, cb: cb}); this.pluginTypes.push('pipeline'); }; @@ -47,7 +62,17 @@ Plugin.prototype.generateContracts = function(args) { }; Plugin.prototype.runPipeline = function(args) { - return this.pipeline[0].call(this, args); + // TODO: should iterate the pipeliens + var pipeline = this.pipeline[0]; + //var shouldRunPipeline = pipeline.matcthingFiles.some(match => { + //}); + var shouldRunPipeline = grunt.file.isMatch(pipeline.matcthingFiles, args.targetFile); + if (shouldRunPipeline) { + console.log('running plugin'); + return pipeline.cb.call(this, args); + } else { + return args.source; + } }; module.exports = Plugin; diff --git a/lib/plugins.js b/lib/plugins.js index 132a19b28..c41ae24d4 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -4,20 +4,30 @@ var path = require('path'); var Plugins = function(options) { this.pluginList = options.plugins || []; this.plugins = []; + // TODO: need backup 'NullLogger' + this.logger = options.logger; }; Plugins.prototype.loadPlugins = function() { - var i, plugin; - for (i = 0; i < this.pluginList.length; i++) { - plugin = this.pluginList[i]; - this.loadPlugin(plugin); + var pluginConfig; + for (var pluginName in this.pluginList) { + pluginConfig = this.pluginList[pluginName]; + this.loadPlugin(pluginName, pluginConfig); } }; -Plugins.prototype.loadPlugin = function(pluginName) { +Plugins.prototype.listPlugins = function() { + var list = []; + for (var className in this.pluginList) { + list.push(className); + } + return list; +}; + +Plugins.prototype.loadPlugin = function(pluginName, pluginConfig) { var plugin = require(path.join(process.env.PWD, 'node_modules', pluginName)); - var pluginWrapper = new Plugin({name: pluginName, pluginModule: plugin}); + var pluginWrapper = new Plugin({name: pluginName, pluginModule: plugin, pluginConfig: pluginConfig, logger: this.logger}); pluginWrapper.loadPlugin(); this.plugins.push(pluginWrapper); };