[Pkg-javascript-commits] [less.js] 162/285: Add file manager to plugin system

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:23:50 UTC 2015


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag v2.0.0
in repository less.js.

commit 9869b29c3d9fd8f902299e8796d62387c7d7cec0
Author: Luke Page <luke.a.page at gmail.com>
Date:   Thu Oct 9 17:45:30 2014 +0100

    Add file manager to plugin system
---
 bin/lessc                                          | 21 ++++++++-----
 lib/less-node/index.js                             |  2 +-
 .../{node-plugin-manager.js => plugin-loader.js}   | 34 ++++++++++----------
 lib/less/contexts.js                               |  6 ++--
 lib/less/environment/environment.js                |  8 +++--
 lib/less/parse-tree.js                             |  5 ++-
 lib/less/plugin-manager.js                         | 36 ++++++++++++++++++----
 lib/less/render.js                                 | 13 ++++++--
 lib/less/transform-tree.js                         |  4 +--
 9 files changed, 86 insertions(+), 43 deletions(-)

diff --git a/bin/lessc b/bin/lessc
index 9a497bd..c87eac0 100755
--- a/bin/lessc
+++ b/bin/lessc
@@ -6,7 +6,9 @@ var path = require('path'),
     mkdirp;
 
 var less = require('../lib/less-node'),
-    pluginManager = new less.PluginManager(less);
+    pluginLoader = new less.PluginLoader(less),
+    plugin,
+    plugins = [];
 
 var args = process.argv.slice(1);
 var silent = false,
@@ -27,7 +29,8 @@ var silent = false,
     strictUnits: false,
     globalVariables: '',
     modifyVariables: '',
-    urlArgs: ''
+    urlArgs: '',
+    plugins: plugins
 };
 var sourceMapOptions = {};
 var continueProcessing = true,
@@ -66,7 +69,7 @@ var sourceMapFileInline = false;
 
 function printUsage() {
     less.lesscHelper.printUsage();
-    pluginManager.printUsage();
+    pluginLoader.printUsage(plugins);
     continueProcessing = false;
 }
 
@@ -220,7 +223,10 @@ args = args.filter(function (arg) {
                 name = splitupArg[1],
                 pluginOptions = splitupArg[3];
 
-            if (!pluginManager.tryLoadPlugin(name, pluginOptions)) {
+            plugin = pluginLoader.tryLoadPlugin(name, pluginOptions);
+            if (plugin) {
+                plugins.push(plugin);
+            } else {
                 console.log("Unable to load plugin " + name + " please make sure that it is installed under at the same level as less");
                 console.log();
                 printUsage();
@@ -228,7 +234,10 @@ args = args.filter(function (arg) {
             }
             break;
         default:
-            if (!pluginManager.tryLoadPlugin("less-plugin-" + arg, match[2])) {
+            plugin = pluginLoader.tryLoadPlugin("less-plugin-" + arg, match[2]);
+            if (plugin) {
+                plugins.push(plugin);
+            } else {
                 console.log("Unable to interpret argument " + arg + " - if it is a plugin (less-plugin-" + arg + "), make sure that it is installed under at the same level as less");
                 console.log();
                 printUsage();
@@ -362,8 +371,6 @@ var parseLessFile = function (e, data) {
         options.sourceMap = sourceMapOptions;
     }
 
-    options.plugins = pluginManager;
-
     less.logger.addListener({
         info: function(msg) {
             if (verbose) {
diff --git a/lib/less-node/index.js b/lib/less-node/index.js
index 8392690..ce94a18 100644
--- a/lib/less-node/index.js
+++ b/lib/less-node/index.js
@@ -8,7 +8,7 @@ var environment = require("./environment"),
 // allow people to create less with their own environment
 less.createFromEnvironment = createFromEnvironment;
 less.lesscHelper = lesscHelper;
-less.PluginManager = require("./node-plugin-manager");
+less.PluginLoader = require("./plugin-loader");
 less.fs = require("./fs");
 less.fileImport = require("./file-import.js");
 less.urlImport = require("./url-import.js");
diff --git a/lib/less-node/node-plugin-manager.js b/lib/less-node/plugin-loader.js
similarity index 57%
rename from lib/less-node/node-plugin-manager.js
rename to lib/less-node/plugin-loader.js
index a013fcc..428b1ef 100644
--- a/lib/less-node/node-plugin-manager.js
+++ b/lib/less-node/plugin-loader.js
@@ -1,28 +1,28 @@
-var PluginManager = require("../less/plugin-manager");
 /**
- * Node Plugin Manager
+ * Node Plugin Loader
  */
-var NodePluginManager = function(less) {
-    PluginManager.call(this, less);
+var PluginLoader = function(less) {
+    this.less = less;
 };
-NodePluginManager.prototype = new PluginManager();
-NodePluginManager.prototype.tryLoadPlugin = function(name, argument) {
+PluginLoader.prototype.tryLoadPlugin = function(name, argument) {
     var plugin = this.tryRequirePlugin(name);
     if (plugin) {
         if (plugin.minVersion) {
             if (this.compareVersion(plugin.minVersion, this.less.version) < 0) {
                 console.log("plugin " + name + " requires version " + this.versionToString(plugin.minVersion));
-                return false;
+                return null;
             }
         } else {
             console.log("plugin has no min version");
         }
-        this.addPlugin(plugin, argument);
-        return true;
+        if (argument) {
+            plugin.setOptions(argument);
+        }
+        return plugin;
     }
-    return false;
+    return null;
 };
-NodePluginManager.prototype.compareVersion = function(aVersion, bVersion) {
+PluginLoader.prototype.compareVersion = function(aVersion, bVersion) {
     for(var i = 0; i < aVersion.length; i++) {
         if (aVersion[i] !== bVersion[i]) {
             return aVersion[i] > bVersion[i] ? -1 : 1;
@@ -30,14 +30,14 @@ NodePluginManager.prototype.compareVersion = function(aVersion, bVersion) {
     }
     return 0;
 };
-NodePluginManager.prototype.versionToString = function(version) {
+PluginLoader.prototype.versionToString = function(version) {
     var versionString = "";
     for(var i = 0; i < version.length; i++) {
         versionString += (versionString ? "." : "") + version[i];
     }
     return versionString;
 };
-NodePluginManager.prototype.tryRequirePlugin = function(name) {
+PluginLoader.prototype.tryRequirePlugin = function(name) {
     if (name[0] !== '.') {
         try {
             return require(name);
@@ -51,12 +51,12 @@ NodePluginManager.prototype.tryRequirePlugin = function(name) {
     catch(e) {
     }
 };
-NodePluginManager.prototype.printUsage = function() {
-    for(var i = 0; i < this.installedPlugins.length; i++) {
-        var plugin = this.installedPlugins[i];
+PluginLoader.prototype.printUsage = function(plugins) {
+    for(var i = 0; i < plugins.length; i++) {
+        var plugin = plugins[i];
         if (plugin.printUsage) {
             plugin.printUsage();
         }
     }
 };
-module.exports = NodePluginManager;
+module.exports = PluginLoader;
diff --git a/lib/less/contexts.js b/lib/less/contexts.js
index 96335a7..e3de368 100644
--- a/lib/less/contexts.js
+++ b/lib/less/contexts.js
@@ -30,7 +30,8 @@ var parseCopyProperties = [
     // context
     'processImports',   // option & context - whether to process imports. if false then imports will not be imported.
                         // Used by the import manager to stop multiple import visitors being created.
-    'reference'         // Used to indicate that the contents are imported by reference
+    'reference',        // Used to indicate that the contents are imported by reference
+    'pluginManager'     // Used as the plugin manager for the session
 ];
 
 contexts.Parse = function(options) {
@@ -47,7 +48,8 @@ var evalCopyProperties = [
     'sourceMap',      // whether to output a source map
     'importMultiple', // whether we are currently importing multiple copies
     'urlArgs',        // whether to add args into url tokens
-    'javascriptEnabled'// option - whether JavaScript is enabled. if undefined, defaults to true
+    'javascriptEnabled',// option - whether JavaScript is enabled. if undefined, defaults to true
+    'pluginManager'     // Used as the plugin manager for the session
     ];
 
 contexts.Eval = function(options, frames) {
diff --git a/lib/less/environment/environment.js b/lib/less/environment/environment.js
index 84b4160..c0e3f50 100644
--- a/lib/less/environment/environment.js
+++ b/lib/less/environment/environment.js
@@ -19,8 +19,12 @@ environment.prototype.warn = function (msg) {
 };
 
 environment.prototype.getFileManager = function (filename, currentDirectory, options, environment, isSync) {
-    for(var i = this.fileManagers.length - 1; i >= 0 ; i--) {
-        var fileManager = this.fileManagers[i];
+    var fileManagers = this.fileManagers;
+    if (options.pluginManager) {
+        fileManagers = [].concat(fileManagers).concat(options.pluginManager.getFileManagers());
+    }
+    for(var i = fileManagers.length - 1; i >= 0 ; i--) {
+        var fileManager = fileManagers[i];
         if (fileManager[isSync ? "supportsSync" : "supports"](filename, currentDirectory, options, environment)) {
             return fileManager;
         }
diff --git a/lib/less/parse-tree.js b/lib/less/parse-tree.js
index f04749f..6b64709 100644
--- a/lib/less/parse-tree.js
+++ b/lib/less/parse-tree.js
@@ -32,10 +32,9 @@ ParseTree.prototype.toCSS = function(options) {
         throw new LessError(e, this.imports);
     }
 
-    if (options.plugins) {
-        var postProcessors = options.plugins.getPostProcessors();
+    if (options.pluginManager) {
+        var postProcessors = options.pluginManager.getPostProcessors();
         for(var i = 0; i < postProcessors.length; i++) {
-            // TODO - async
             result.css = postProcessors[i].process(result.css, { sourceMap: sourceMapBuilder, options: options, imports: this.imports });
         }
     }
diff --git a/lib/less/plugin-manager.js b/lib/less/plugin-manager.js
index 922885c..f2051a0 100644
--- a/lib/less/plugin-manager.js
+++ b/lib/less/plugin-manager.js
@@ -6,10 +6,26 @@ var PluginManager = function(less) {
     this.visitors = [];
     this.postProcessors = [];
     this.installedPlugins = [];
+    this.fileManagers = [];
 };
-PluginManager.prototype.addPlugin = function(plugin, options) {
+/**
+ * Adds all the plugins in the array
+ * @param {Array} plugins
+ */
+PluginManager.prototype.addPlugins = function(plugins) {
+    if (plugins) {
+        for(var i = 0;i < plugins.length; i++) {
+            this.addPlugin(plugins[i]);
+        }
+    }
+};
+/**
+ *
+ * @param plugin
+ */
+PluginManager.prototype.addPlugin = function(plugin) {
     this.installedPlugins.push(plugin);
-    plugin.install(this.less, this, options);
+    plugin.install(this.less, this);
 };
 /**
  * Adds a visitor. The visitor object has options on itself to determine
@@ -34,14 +50,14 @@ PluginManager.prototype.addPostProcessor = function(postProcessor, priority) {
     this.postProcessors.splice(indexToInsertAt, 0, {postProcessor: postProcessor, priority: priority});
 };
 /**
- * Adds a file manager
+ *
  * @param manager
  */
 PluginManager.prototype.addFileManager = function(manager) {
-    // TODO
+    this.fileManagers.push(manager);
 };
 /**
- * Gets all post processors
+ *
  * @returns {Array}
  * @private
  */
@@ -53,11 +69,19 @@ PluginManager.prototype.getPostProcessors = function() {
     return postProcessors;
 };
 /**
- * Get all visitors
+ *
  * @returns {Array}
  * @private
  */
 PluginManager.prototype.getVisitors = function() {
     return this.visitors;
 };
+/**
+ *
+ * @returns {Array}
+ * @private
+ */
+PluginManager.prototype.getFileManagers = function() {
+    return this.fileManagers;
+};
 module.exports = PluginManager;
diff --git a/lib/less/render.js b/lib/less/render.js
index 6159df2..5013b77 100644
--- a/lib/less/render.js
+++ b/lib/less/render.js
@@ -1,6 +1,7 @@
 var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise,
     contexts = require("./contexts"),
-    Parser = require('./parser/parser');
+    Parser = require('./parser/parser'),
+    PluginManager = require('./plugin-manager');
 
 module.exports = function(environment, ParseTree, ImportManager) {
     var render = function (input, options, callback) {
@@ -20,8 +21,14 @@ module.exports = function(environment, ParseTree, ImportManager) {
                     callback(error);
                 });
         } else {
-            var context = new contexts.Parse(options),
-                rootFileInfo;
+            var context,
+                rootFileInfo,
+                pluginManager = new PluginManager();
+
+            pluginManager.addPlugins(options.plugins);
+            options.pluginManager = pluginManager;
+
+            context = new contexts.Parse(options);
 
             if (options.rootFileInfo) {
                 rootFileInfo = options.rootFileInfo;
diff --git a/lib/less/transform-tree.js b/lib/less/transform-tree.js
index 2a0c69f..5a456de 100644
--- a/lib/less/transform-tree.js
+++ b/lib/less/transform-tree.js
@@ -43,8 +43,8 @@ module.exports = function(root, options) {
             new(visitor.ToCSSVisitor)({compress: Boolean(options.compress)})
         ], i;
 
-    if (options.plugins) {
-        var pluginVisitors = options.plugins.getVisitors();
+    if (options.pluginManager) {
+        var pluginVisitors = options.pluginManager.getVisitors();
         for(i =0; i < pluginVisitors.length; i++) {
             var pluginVisitor = pluginVisitors[i];
             if (pluginVisitor.isPreEvalVisitor) {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/less.js.git



More information about the Pkg-javascript-commits mailing list