summaryrefslogtreecommitdiff
path: root/scripts/lib/extensions.js
blob: a1f40d7aa70a66c52b9daed43d8313f26725e560 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
(function () {
  var _config = undefined;
  var _debug = false;
  function getPlugin(name, filename) {
    var ret = null;
    try {
      if (system.fileTest(filename, FileTest.exists)) {
        ret = include(filename);
      }
    }
    catch(e) {
      extensions.error(name, "Error in line " + e.line + " parsing " + filename);
    }
    return ret;
  }
  function getStack(offset) {
    if (arguments.length === 0) {
      offset = 0;
    }
    try {
      throw Error (message);
    }
    catch (e) {
      var stack = e.stack.match(/[^\n]+/g);
      return "STACK: [" + stack.slice(offset+1).join("] [")+"]";
    }
  }
  Object.defineProperties(extensions, { 
    "warning" : {
      value : function (name, message) {
        io.print("\033[1mDWB EXTENSION WARNING: \033[0mextension  \033[1m" + name + "\033[0m: " + message, "stderr");
      }
    }, 
    "enableDebugging" : {
      set : function (value) {
        if (typeof value == "boolean")
          _debug = value;
      }
    },
    "debug" : {
      value : function (name, message) {
        if (_debug) {
          io.print("\033[1mDWB EXTENSION DEBUG: \033[0mextension  \033[1m" + name + "\033[0m\n" + getStack(1), "stderr");
        }
      }
    }, 
    "error" : {
      value : function (name, a, b) {
        var message = "";
        if (a instanceof Error) {
          if (a.message) {
            message = a.message;
          }
          else if (arguments.length > 2)
            message = b;
          else 
            b = "";
          io.print("\033[31mDWB EXTENSION ERROR: \033[0mextension \033[1m" + name + "\033[0m in line " + a.line + ": " + 
              message + "\nSTACK: [" + a.stack.match(/[^\n]+/g).join("] [") + "]", "stderr");
        }
        else {
          io.print("\033[31mDWB EXTENSION ERROR: \033[0mextension \033[1m" + name + "\033[0m: " + a + "\n" + getStack(1), "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);