-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
266 lines (220 loc) · 5.52 KB
/
main.go
File metadata and controls
266 lines (220 loc) · 5.52 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"encoding/json"
"errors"
"io"
"io/fs"
"log"
"os"
"path"
"path/filepath"
"slices"
"strings"
"github.com/fsnotify/fsnotify"
)
// TODO: implement delete handling. I need to handle the delete operation and.
// look for fallback files.
// TODO: add the creation handling. for creation I need to look for the file
// on priority plugins. if there is not items from mor priority items add the
// file to the dynamic
var pluginsData []PluginData
func main() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
pluginsData, err = loadPluginsData()
if err != nil {
log.Fatal("unable to load the plugins data due to: ", err)
}
go watchFiles(watcher)
log.Println("Loading plugins directories")
for _, data := range pluginsData {
if !data.Enabled {
continue
}
err := watchPluginRecursive(watcher, data)
if err != nil {
log.Fatal("unable to watch the plugin "+data.Name+" due to: ", err)
}
}
log.Println("plugins loaded")
<-make(chan struct{})
}
type PluginData struct {
Compatible bool
Description string
Enabled bool
Folder string
Hidden bool
Installed bool
MinVersion float32
MinPHP float32 `json:"min_php"`
Name string
Order int
PostDisable bool
PostEnable bool
Require []string
RequirePHP []string
Version float32
}
func loadPluginsData() ([]PluginData, error) {
pluginsFile := filepath.Join("MyFiles/plugins.json")
if _, err := os.Stat(pluginsFile); err != nil {
return []PluginData{}, err
}
content, err := os.ReadFile(pluginsFile)
if err != nil {
return []PluginData{}, err
}
var pluginsData []PluginData
if err := json.Unmarshal(content, &pluginsData); err != nil {
return []PluginData{}, err
}
slices.SortFunc(pluginsData, func(a, b PluginData) int {
return a.Order - b.Order
})
return pluginsData, nil
}
func watchFiles(watcher *fsnotify.Watcher) {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Has(fsnotify.Write) {
handleFileModified(event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}
var watchDirs = []string{
"Translation",
"Controller",
"XMLView",
"Assets",
"Table",
"Model",
"View",
"Data",
"Lib",
}
// watchPluginRecursive recursively traverse the plugin directories that are
// in the watchDirs array to include the existing directories to the watcher.
func watchPluginRecursive(watcher *fsnotify.Watcher, data PluginData) error {
for _, dir := range watchDirs {
path := filepath.Join("Plugins", data.Folder, dir)
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
continue
}
return err
}
if err := watcher.Add(path); err != nil {
return err
}
err = filepath.Walk(path, func(path string, fi fs.FileInfo, err error) error {
if err != nil {
return err
}
if !fi.IsDir() {
return nil
}
return watcher.Add(path)
})
if err != nil {
return err
}
}
return nil
}
// path is the name of the file relative to the html path, ej:
// Plugins/{plugin}/Controller/{file}.php
func handleFileModified(modpath string) {
parts := strings.Split(modpath, string(os.PathSeparator))
baseDir := parts[2]
pluginName := parts[1]
if baseDir != "Assets" && baseDir != "XMLView" && baseDir != "View" {
log.Printf("only assets are supported in this version %s ignored\n", modpath)
return
}
plugin, err := getPluginDataByName(pluginName)
if err != nil {
log.Println("file change ignored. no plugin data found for ", pluginName)
}
if !plugin.Enabled {
log.Println("Ignored. disabled plugin", pluginName)
return
}
priority := getPluginWithPriorityOver(modpath)
if priority.Name != plugin.Name {
log.Printf(
"the plugin '%s' has priority over the modified file. skipping\n",
priority.Name,
)
return
}
plugRelativePath := path.Join(parts[2:]...)
dynPath := filepath.Join("Dinamic", plugRelativePath)
if _, err := os.Stat(dynPath); err != nil && !errors.Is(err, os.ErrNotExist) {
log.Printf("an error ocurred reading the dynamic file. skipping %s\n", dynPath)
return
}
if err := os.Remove(dynPath); err != nil {
log.Printf("unable to remove %s. skipping\n", dynPath)
return
}
dynFile, err := os.Create(dynPath)
if err != nil {
log.Printf("unable to open %s. skipping update", dynPath)
return
}
defer dynFile.Close()
updatedFile, err := os.Open(modpath)
if err != nil {
log.Printf("unable to open the updated file %s\n", modpath)
return
}
defer updatedFile.Close()
if _, err := io.Copy(dynFile, updatedFile); err != nil {
log.Printf("unable to write the new content on %s\n", dynPath)
return
}
log.Printf("%s updated", dynPath)
}
func getPluginDataByName(name string) (PluginData, error) {
index := slices.IndexFunc(pluginsData, func(item PluginData) bool {
return item.Name == name
})
if index < 0 {
return PluginData{}, errors.New("plugin not found")
}
return pluginsData[index], nil
}
// pluginHasPriorityOnFile check if other plugins with more priority has priority
// over the change file.
func getPluginWithPriorityOver(modifiedPath string) PluginData {
pathParts := strings.Split(modifiedPath, "/")
plugRelativePath := path.Join(pathParts[2:]...)
plugin := PluginData{Order: -1}
for i := 0; i < len(pluginsData); i++ {
other := pluginsData[i]
if other.Order < plugin.Order {
continue
}
otherPath := path.Join("Plugins", other.Name, plugRelativePath)
_, err := os.Stat(otherPath)
if err == nil {
plugin = other
}
}
return plugin
}