Просмотр исходного кода

impl bin/generate-plugin-definitions-source.js

Yuki Takei 9 лет назад
Родитель
Сommit
504f1faac0

+ 2 - 0
.gitignore

@@ -24,6 +24,8 @@ npm-debug.log
 /.awcache
 /.awcache
 .webpack.json
 .webpack.json
 /compiled/
 /compiled/
+# Crowi Plugin Detection #
+/resource/js/plugins/*
 
 
 # Doc #
 # Doc #
 /doc/
 /doc/

+ 26 - 0
bin/generate-plugin-definitions-source.js

@@ -0,0 +1,26 @@
+const fs = require('graceful-fs');
+const slash = require('slash');
+const swig = require('swig');
+const helpers = require('../config/helpers');
+
+const PluginUtils = require('../lib/plugins/plugin-utils');
+const pluginUtils = new PluginUtils();
+
+const definitions = pluginUtils.listPluginNames(helpers.root())
+  .map((name) => {
+    return pluginUtils.generatePluginDefinition(name, true);
+  })
+  .map((definition) => {
+    // convert backslash to slash
+    definition.entries = definition.entries.map((entryPath) => {
+      return slash(entryPath);
+    });
+    return definition;
+  });
+
+var template = swig.compileFile(helpers.root('bin/templates/plugin-definitions.js.swig'));
+var output = template({definitions});
+
+fs.writeFileSync(
+  helpers.root('resource/js/plugins/plugin-definitions.js'),
+  output);

+ 13 - 0
bin/templates/plugin-definitions.js.swig

@@ -0,0 +1,13 @@
+export const definitions = [
+  {% for definition in definitions %}
+  {
+    name: '{{ definition.name }}',
+    meta: require('{{ definition.name }}'),
+    entries: [
+      {% for entryPath in definition.entries %}
+      require('{{ entryPath }}'),
+      {% endfor %}
+    ]
+  },
+  {% endfor %}
+]

+ 7 - 8
lib/plugins/plugin-utils.js

@@ -10,24 +10,23 @@ class PluginUtils {
    *   name: 'crowi-plugin-X',
    *   name: 'crowi-plugin-X',
    *   meta: require('crowi-plugin-X'),
    *   meta: require('crowi-plugin-X'),
    *   entries: [
    *   entries: [
-   *     require('crowi-plugin-X/lib/client-entry')
+   *     'crowi-plugin-X/lib/client-entry'
    *   ]
    *   ]
    * }
    * }
    *
    *
+   *
    * @param {string} pluginName
    * @param {string} pluginName
    * @return
    * @return
    * @memberOf PluginService
    * @memberOf PluginService
    */
    */
-  generatePluginDefinition(pluginName, isForClient = false) {
-    const meta = require(pluginName);
+  generatePluginDefinition(name, isForClient = false) {
+    const meta = require(name);
     const entries = (isForClient) ? meta.clientEntries : meta.serverEntries;
     const entries = (isForClient) ? meta.clientEntries : meta.serverEntries;
 
 
     return {
     return {
-      name: pluginName,
-      meta: meta,
-      entries: entries.map((entryPath) => {
-        return require(entryPath);
-      })
+      name,
+      meta,
+      entries,
     }
     }
   }
   }
 
 

+ 2 - 1
lib/plugins/plugin.service.js

@@ -37,7 +37,8 @@ class PluginService {
     if (2 === meta.pluginSchemaVersion) {
     if (2 === meta.pluginSchemaVersion) {
       debug(`load plugin '${definition.name}'`);
       debug(`load plugin '${definition.name}'`);
 
 
-      definition.entries.forEach((entry) => {
+      definition.entries.forEach((entryPath) => {
+        const entry = require(entryPath);
         entry(this.crowi, this.app);
         entry(this.crowi, this.app);
       });
       });
     }
     }

+ 1 - 0
package.json

@@ -116,6 +116,7 @@
     "sass-loader": "^6.0.3",
     "sass-loader": "^6.0.3",
     "sinon": "~1.14.0",
     "sinon": "~1.14.0",
     "sinon-chai": "~2.7.0",
     "sinon-chai": "~2.7.0",
+    "slash": "^1.0.0",
     "style-loader": "^0.13.2",
     "style-loader": "^0.13.2",
     "to-string-loader": "^1.1.5",
     "to-string-loader": "^1.1.5",
     "webpack": "2.2.0",
     "webpack": "2.2.0",

+ 5 - 12
resource/js/plugin.js

@@ -1,11 +1,4 @@
-const plugins = {
-  // 'crowi-plugin-X': {
-  //   meta: require('crowi-plugin-X'),
-  //   entries: [
-  //     require('crowi-plugin-X/lib/client-entry')
-  //   ]
-  // },
-}
+import { definitions } from './plugins/plugin-definitions';
 
 
 export default class CrowiPlugin {
 export default class CrowiPlugin {
 
 
@@ -18,9 +11,9 @@ export default class CrowiPlugin {
    * @memberof CrowiPlugin
    * @memberof CrowiPlugin
    */
    */
   installAll(crowi, crowiRenderer) {
   installAll(crowi, crowiRenderer) {
-    for (let pluginName of Object.keys(plugins)) {
-      let meta = plugins[pluginName].meta;
-      let entries = plugins[pluginName].entries;
+    definitions.forEach((definition) => {
+      const meta = definition.meta;
+      const entries = definition.entries;
 
 
       // v1 is deprecated
       // v1 is deprecated
 
 
@@ -30,7 +23,7 @@ export default class CrowiPlugin {
           entry(crowi, crowiRenderer);
           entry(crowi, crowiRenderer);
         });
         });
       }
       }
-    }
+    });
   }
   }
 
 
 }
 }

+ 2 - 0
resource/js/plugins/plugin-definitions.js

@@ -0,0 +1,2 @@
+export const definitions = [
+]