/
data.go
377 lines (277 loc) · 7 KB
/
data.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package compactor
import (
"strings"
"github.com/mateussouzaweb/compactor/os"
)
//
// To handle data, we have 4 main data lists:
//
// - Plugins: Hold the registered plugins, used for processing.
// - Indexer: The indexer contains information of all source files. In watch mode, this is live updated.
// - Maps: Define the destination for each custom file. If is not in the index, then destination is equal copy.
// - Bundle: The reference model for the creating bundles. Bundle are always temporary structs.
//
// When compactor is executed, it creates bundles by parsing indexer and maps
// Then, each bundle is processed with matching plugins
//
// Plugins list
var _plugins []*Plugin
// Indexer list
var _indexer []*Item
// Maps list
var _maps []*Mapper
// Default bundle model
var Default = &Bundle{
Destination: Destination{
Hashed: true,
},
Compress: Compress{
Enabled: true,
},
SourceMap: SourceMap{
Enabled: true,
},
Progressive: Progressive{
Enabled: true,
},
}
// PLUGIN METHODS
// Register add a new plugin to the index
func Register(plugin *Plugin) {
_plugins = append(_plugins, plugin)
}
// Unregister removes all plugins that match the given extension
func Unregister(extension string) {
var list []*Plugin
for _, _plugin := range _plugins {
var match bool
for _, _extension := range _plugin.Extensions {
if _extension == extension {
match = true
break
}
}
if !match {
list = append(list, _plugin)
}
}
_plugins = list
}
// INDEXER METHODS
// Get the item that match file path on indexer
func Get(path string) *Item {
for _, item := range _indexer {
if item.Path == path {
return item
}
}
return &Item{}
}
// Append file path information to indexer
func Append(path string) error {
content, checksum, perm := os.Info(path)
item := Item{
Path: path,
Folder: os.Dir(path),
File: os.File(path),
Name: os.Name(path),
Extension: os.Extension(path),
Content: content,
Permission: perm,
Exists: os.Exist(path),
Checksum: checksum,
Previous: "",
}
_indexer = append(_indexer, &item)
return nil
}
// Update item information on indexer if match file path
func Update(path string) error {
for _, item := range _indexer {
if item.Path != path {
continue
}
exists := os.Exist(item.Path)
content, checksum, perm := os.Info(item.Path)
previous := item.Checksum
item.Content = content
item.Permission = perm
item.Exists = exists
item.Checksum = checksum
item.Previous = previous
break
}
return nil
}
// Remove item information from indexer if match file path
func Remove(path string) {
for _, item := range _indexer {
if item.Path != path {
continue
}
item.Content = ""
item.Exists = false
break
}
}
// Index walks on path and add files to the indexer
func Index(path string) error {
files, err := os.List(path)
if err != nil {
return err
}
for _, file := range files {
_ = Append(file)
}
return nil
}
// Matches run callback on indexer and append item if match
func Matches(callback func(item *Item) bool) []*Item {
var items []*Item
for _, item := range _indexer {
if callback(item) {
items = append(items, item)
}
}
return items
}
// MAPS METHODS
// Map add a new map registration
func Map(files []string, target string) {
_maps = append(_maps, &Mapper{
Files: files,
Target: target,
})
}
// BUNDLE METHODS
// NewBundle create and retrieve a new bundle
func NewBundle() *Bundle {
bundle := *Default
bundle.Extension = ""
bundle.Destination.File = ""
bundle.Destination.Folder = ""
bundle.Destination.Path = Default.Destination.Path
bundle.Destination.Hashed = Default.Destination.Hashed
return &bundle
}
// GetBundleFromMapper retrieve the final bundle from given mapper
func GetBundleFromMapper(mapper *Mapper) *Bundle {
bundle := NewBundle()
// Check if mapper destination is to file or folder
if strings.HasSuffix(mapper.Target, "/") {
bundle.Destination.Folder = bundle.CleanPath(mapper.Target)
} else {
bundle.Destination.File = bundle.CleanPath(mapper.Target)
}
// Use loop to respect mapper file order
for _, pattern := range mapper.Files {
found := Matches(func(item *Item) bool {
if !bundle.MatchPatterns(item.Path, []string{pattern}) {
return false
}
return bundle.ShouldInclude(item.Path)
})
bundle.Items = append(bundle.Items, found...)
}
if len(bundle.Items) != 0 {
bundle.Extension = bundle.Items[0].Extension
}
return bundle
}
// GetBundles retrieve every possible bundle with current indexer
func GetBundles() []*Bundle {
var bundles []*Bundle
used := make(map[string]bool)
// First create bundle from maps
for _, mapper := range _maps {
bundle := GetBundleFromMapper(mapper)
if len(bundle.Items) == 0 {
continue
}
bundles = append(bundles, bundle)
for _, item := range bundle.Items {
used[item.Path] = true
}
}
// Now process only file not included in previous bundles
for _, item := range _indexer {
if _, ok := used[item.Path]; ok {
continue
}
bundle := NewBundle()
bundle.Extension = item.Extension
bundle.Items = append(bundle.Items, item)
bundle.Destination.File = bundle.CleanPath(item.Path)
if bundle.ShouldInclude(item.Path) {
bundles = append(bundles, bundle)
}
}
return bundles
}
// GetBundleFor retrieve the related bundle for the file
func GetBundleFor(path string) *Bundle {
// TODO: When a file depends on another, that file should not go to another bundle
// It should be specially injected in the bundle, because of the dependency
// First check if file path included in bundle from maps
for _, mapper := range _maps {
bundle := GetBundleFromMapper(mapper)
if bundle.Destination.File == bundle.CleanPath(path) {
return bundle
}
for _, item := range bundle.Items {
if item.Path == path {
return bundle
}
}
}
item := Get(path)
bundle := NewBundle()
bundle.Extension = item.Extension
bundle.Items = append(bundle.Items, item)
bundle.Destination.File = bundle.CleanPath(item.Path)
if !bundle.ShouldInclude(item.Path) {
bundle = NewBundle()
}
return bundle
}
// PROCESS METHODS
// Process package by running processors
func Process(bundle *Bundle) error {
// Make sure folder exists to avoid issues
destination := bundle.ToDestination(bundle.Destination.File)
err := os.EnsureDirectory(destination)
if err != nil {
return err
}
// Determine action based on processable list
action := "DELETE"
for _, item := range bundle.Items {
if item.Exists {
action = "RUN"
}
}
// Find and run appropriated plugin by detecting extension
// Generic plugin is always the lastest, so at least one match should happen
for _, plugin := range _plugins {
match := false
for _, _extension := range plugin.Extensions {
if _extension == bundle.Extension {
match = true
break
}
}
if !match && len(plugin.Extensions) != 0 {
continue
}
if action == "RUN" {
err = plugin.Run(bundle)
} else {
err = plugin.Delete(bundle)
}
if err != nil {
return err
}
break
}
return err
}