/
pluginlangs.go
122 lines (108 loc) · 2.62 KB
/
pluginlangs.go
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
package common
import (
"encoding/json"
"errors"
"io/ioutil"
"path/filepath"
)
var pluginLangs = make(map[string]PluginLang)
// For non-native plugins to bind JSON files to. E.g. JS and Lua
type PluginMeta struct {
UName string
Name string
Author string
URL string
Settings string
Tag string
Skip bool // Skip this folder?
Main string // The main file
Hooks map[string]string // Hooks mapped to functions
}
type PluginLang interface {
GetName() string
GetExts() []string
Init() error
AddPlugin(meta PluginMeta) (*Plugin, error)
//AddHook(name string, handler interface{}) error
//RemoveHook(name string, handler interface{})
//RunHook(name string, data interface{}) interface{}
//RunVHook(name string data ...interface{}) interface{}
}
/*
var ext = filepath.Ext(pluginFile.Name())
if ext == ".txt" || ext == ".go" {
continue
}
*/
func InitPluginLangs() error {
for _, pluginLang := range pluginLangs {
pluginLang.Init()
}
pluginList, err := GetPluginFiles()
if err != nil {
return err
}
for _, pluginItem := range pluginList {
pluginFile, err := ioutil.ReadFile("./extend/" + pluginItem + "/plugin.json")
if err != nil {
return err
}
var plugin PluginMeta
err = json.Unmarshal(pluginFile, &plugin)
if err != nil {
return err
}
if plugin.Skip {
continue
}
e := func(field, name string) error {
return errors.New("The " + field + " field must not be blank on plugin '" + name + "'")
}
if plugin.UName == "" {
return e("UName", pluginItem)
}
if plugin.Name == "" {
return e("Name", pluginItem)
}
if plugin.Author == "" {
return e("Author", pluginItem)
}
if plugin.Main == "" {
return errors.New("Couldn't find a main file for plugin '" + pluginItem + "'")
}
ext := filepath.Ext(plugin.Main)
pluginLang, err := ExtToPluginLang(ext)
if err != nil {
return err
}
pplugin, err := pluginLang.AddPlugin(plugin)
if err != nil {
return err
}
Plugins[plugin.UName] = pplugin
}
return nil
}
func GetPluginFiles() (pluginList []string, err error) {
pluginFiles, err := ioutil.ReadDir("./extend")
if err != nil {
return nil, err
}
for _, pluginFile := range pluginFiles {
if !pluginFile.IsDir() {
continue
}
pluginList = append(pluginList, pluginFile.Name())
}
return pluginList, nil
}
func ExtToPluginLang(ext string) (PluginLang, error) {
for _, pluginLang := range pluginLangs {
for _, registeredExt := range pluginLang.GetExts() {
if registeredExt == ext {
return pluginLang, nil
}
}
}
return nil, errors.New("No plugin lang handlers are capable of handling extension '" + ext + "'")
}