summaryrefslogtreecommitdiff
path: root/scripts/lib/extensions.js
blob: 2e6d163e5d945e740fb064dd383ff69d3d2c4be8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
(function () {
  var _config = undefined;
  function getPlugin(name, filename) {
    var ret = null;
    try {
      if (io.fileTest(filename, FileTest.exists)) {
        ret = include(filename);
      }
    }
    catch(e) {
      extensions.error(name, "Error in line " + e.line + " parsing " + filename);
    }
    return ret;
  }
  Object.defineProperties(extensions, { 
    "warning" : {
      value : function (name, message) {
        io.print("\033[1mDWB EXTENSION WARNING: \033[0mextension  \033[1m" + name + "\033[0m: " + message, "stderr");
      }
    }, 
    "error" : {
      value : function (name, message) {
        io.print("\033[31mDWB EXTENSION ERROR: \033[0mextension \033[1m" + name + "\033[0m: " + message, "stderr");
      }
    },
    "message" : {
      value : function (name, message) {
        io.print("\033[1mDWB EXTENSION: \033[0mextension \033[1m" + name + "\033[0m: " + message, "stderr");
      }
    },
    "load" : {
      value : function(name, c) {
        var boldname = "\033[1m" + name + "\033[0m";

        var config, dataBase, pluginPath, plugin = null;
        var extConfig = null;

        /* Get default config if the config hasn't been read yet */
        if (arguments.length == 2) {
          extConfig = c;
        }
        else {
          if (_config === undefined) {
            try {
              config = include(data.configDir + "/extensionrc");
            }
            catch (e) {
              extensions.error(name, "loading config failed : " + e);
            }
            if (config === null) {
              extensions.warning(name, "Could not load config.");
            }
            else {
              _config = config;
            }
          }
          if (_config) {
            extConfig = _config[name] || null;
          }
        }

        /* Load extension */
        var filename = data.userDataDir + "/extensions/" + name;
        plugin = getPlugin(name, data.userDataDir + "/extensions/" + name);
        if (plugin === null) {
          plugin = getPlugin(name, data.systemDataDir + "/extensions/" + name);
          if (plugin === null) {
            extensions.error(name, "Couldn't find extension.");
            return false;
          }
        }
        try {
          if (plugin.init(extConfig)) {
            extensions.message(name, "Successfully loaded and initialized.");
            return true;
          }
          else {
            extensions.error(name, "Initialization failed.");
            return false;
          }
        }
        catch (e) {
          extensions.error(name, "Initialization failed: " + e);
          return false;
        }
      }
    }
  });
})();
Object.freeze(extensions);