This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplugin.go
136 lines (115 loc) · 2.86 KB
/
plugin.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package app
import (
"errors"
"html/template"
"os"
"path/filepath"
"reflect"
"strings"
)
type ConfigureQorPluginInterface interface {
ConfigureQorPlugin(PluginInterface)
}
type PluginInterface interface {
Initialize(PluginInterface)
GetName() string
EnableOption(name string) error
DisableOption(name string) error
EnabledOptions() []string
EnabledOption(name string) bool
GetTemplatesPath() string
CopyFiles(PluginInterface) error // copy files for web, android, ios
GetTheme() ThemeInterface
SetTheme(theme ThemeInterface)
FuncMap() template.FuncMap
}
type Plugin struct {
Name string
TemplatesPath string
Options []string
Theme ThemeInterface
}
func (plugin *Plugin) Initialize(p PluginInterface) {
if p.GetName() == "" {
plugin.Name = reflect.ValueOf(p).Elem().Type().Name()
}
if p.GetTemplatesPath() == "" {
plugin.TemplatesPath = filepath.Join(reflect.ValueOf(p).Elem().Type().PkgPath(), "templates")
}
}
func (plugin *Plugin) GetName() string {
return plugin.Name
}
func (plugin *Plugin) ConfigureQorPlugin(p PluginInterface) {
if plugin.GetName() == "" {
plugin.Name = reflect.ValueOf(p).Elem().Type().Name()
}
}
func (plugin *Plugin) GetTemplatesPath() string {
if plugin.TemplatesPath != "" {
if pth, ok := isExistingDir(filepath.Join(root, "vendor", plugin.TemplatesPath)); ok {
return pth
}
for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") {
if pth, ok := isExistingDir(filepath.Join(gopath, "src", plugin.TemplatesPath)); ok {
return pth
}
}
}
return plugin.TemplatesPath
}
// Options
func (plugin *Plugin) EnabledOptions() []string {
return plugin.Options
}
func (plugin *Plugin) EnabledOption(name string) bool {
for _, option := range plugin.EnabledOptions() {
if option == name {
return true
}
}
return false
}
func (plugin *Plugin) EnableOption(name string) error {
plugin.Options = append(plugin.Options, name)
return nil
}
func (plugin *Plugin) DisableOption(name string) (err error) {
var found bool
var options []string
for _, option := range plugin.Options {
if name != option {
options = append(options, option)
} else {
found = true
}
}
plugin.Options = options
if found {
return nil
}
return errors.New("option not enabled")
}
func (*Plugin) CopyFiles(plugin PluginInterface) error {
return copyFiles(plugin.GetTemplatesPath(), plugin.GetTheme().GetPath(), plugin.FuncMap(), plugin)
}
// Plugin Application
func (plugin *Plugin) GetTheme() ThemeInterface {
return plugin.Theme
}
func (plugin *Plugin) SetTheme(theme ThemeInterface) {
plugin.Theme = theme
}
// FuncMap
func (plugin *Plugin) FuncMap() template.FuncMap {
funcMap := plugin.GetTheme().FuncMap()
funcMap["has_option"] = func(name string) bool {
for _, option := range plugin.EnabledOptions() {
if option == name {
return true
}
}
return false
}
return funcMap
}