1 | /**
|
2 | Licensed to the Apache Software Foundation (ASF) under one
|
3 | or more contributor license agreements. See the NOTICE file
|
4 | distributed with this work for additional information
|
5 | regarding copyright ownership. The ASF licenses this file
|
6 | to you under the Apache License, Version 2.0 (the
|
7 | "License"); you may not use this file except in compliance
|
8 | with the License. You may obtain a copy of the License at
|
9 |
|
10 | http://www.apache.org/licenses/LICENSE-2.0
|
11 |
|
12 | Unless required by applicable law or agreed to in writing,
|
13 | software distributed under the License is distributed on an
|
14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15 | KIND, either express or implied. See the License for the
|
16 | specific language governing permissions and limitations
|
17 | under the License.
|
18 | */
|
19 |
|
20 | /* jshint sub:true, laxcomma:true, laxbreak:true */
|
21 |
|
22 | var fs = require('fs');
|
23 | var path = require('path');
|
24 | var PluginInfo = require('./PluginInfo');
|
25 | var events = require('../events');
|
26 |
|
27 | function PluginInfoProvider () {
|
28 | this._cache = {};
|
29 | this._getAllCache = {};
|
30 | }
|
31 |
|
32 | PluginInfoProvider.prototype.get = function (dirName) {
|
33 | var absPath = path.resolve(dirName);
|
34 | if (!this._cache[absPath]) {
|
35 | this._cache[absPath] = new PluginInfo(dirName);
|
36 | }
|
37 | return this._cache[absPath];
|
38 | };
|
39 |
|
40 | // Normally you don't need to put() entries, but it's used
|
41 | // when copying plugins, and in unit tests.
|
42 | PluginInfoProvider.prototype.put = function (pluginInfo) {
|
43 | var absPath = path.resolve(pluginInfo.dir);
|
44 | this._cache[absPath] = pluginInfo;
|
45 | };
|
46 |
|
47 | // Used for plugin search path processing.
|
48 | // Given a dir containing multiple plugins, create a PluginInfo object for
|
49 | // each of them and return as array.
|
50 | // Should load them all in parallel and return a promise, but not yet.
|
51 | PluginInfoProvider.prototype.getAllWithinSearchPath = function (dirName) {
|
52 | var absPath = path.resolve(dirName);
|
53 | if (!this._getAllCache[absPath]) {
|
54 | this._getAllCache[absPath] = getAllHelper(absPath, this);
|
55 | }
|
56 | return this._getAllCache[absPath];
|
57 | };
|
58 |
|
59 | function getAllHelper (absPath, provider) {
|
60 | if (!fs.existsSync(absPath)) {
|
61 | return [];
|
62 | }
|
63 | // If dir itself is a plugin, return it in an array with one element.
|
64 | if (fs.existsSync(path.join(absPath, 'plugin.xml'))) {
|
65 | return [provider.get(absPath)];
|
66 | }
|
67 | var subdirs = fs.readdirSync(absPath);
|
68 | var plugins = [];
|
69 | subdirs.forEach(function (subdir) {
|
70 | var d = path.join(absPath, subdir);
|
71 | if (fs.existsSync(path.join(d, 'plugin.xml'))) {
|
72 | try {
|
73 | plugins.push(provider.get(d));
|
74 | } catch (e) {
|
75 | events.emit('warn', 'Error parsing ' + path.join(d, 'plugin.xml.\n' + e.stack));
|
76 | }
|
77 | }
|
78 | });
|
79 | return plugins;
|
80 | }
|
81 |
|
82 | module.exports = PluginInfoProvider;
|