/
data.go
206 lines (154 loc) · 3.66 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
package compactor
import (
"github.com/mateussouzaweb/compactor/os"
)
// Index list
// The index contains information of all source files
var _items []*Item
// Plugins list
// Hold the registered plugins, used for processing
var _plugins []*Plugin
// Bundles list
// Contains the reference for created bundles
var _bundles []*Bundle
// Default bundle model
var Default = &Bundle{
Destination: Destination{
Hashed: true,
},
Compress: Compress{
Enabled: true,
},
SourceMap: SourceMap{
Enabled: true,
},
Progressive: Progressive{
Enabled: true,
},
}
// Get the item that match file path on index
func Get(path string) *Item {
for _, item := range _items {
if item.Path == path {
return item
}
}
return &Item{}
}
// Append file path information to index
func Append(path string, root string) error {
location := os.Relative(path, root)
content, checksum, perm := os.Info(path)
item := Item{
Path: path,
Root: root,
Location: location,
Folder: os.Dir(location),
File: os.File(location),
Name: os.Name(location),
Extension: os.Extension(location),
Content: content,
Permission: perm,
Exists: os.Exist(path),
Checksum: checksum,
Previous: "",
}
_items = append(_items, &item)
return nil
}
// Update item information on index if match file path
func Update(path string) error {
for _, item := range _items {
if item.Path != path {
continue
}
exists := os.Exist(item.Path)
content, checksum, perm := os.Info(item.Path)
current := item.Checksum
item.Content = content
item.Permission = perm
item.Exists = exists
item.Checksum = checksum
if item.Checksum != current {
item.Previous = current
}
break
}
return nil
}
// Remove item information from index if match file path
func Remove(path string) {
for _, item := range _items {
if item.Path != path {
continue
}
item.Content = ""
item.Exists = false
break
}
}
// Matches run callback on index and append item if match
func Matches(callback func(item *Item) bool) []*Item {
var items []*Item
for _, item := range _items {
if callback(item) {
items = append(items, item)
}
}
return items
}
// AddPlugin add a new plugin to the index
func AddPlugin(plugin *Plugin) {
_plugins = append(_plugins, plugin)
}
// RemovePlugin removes all plugins from index that match the given namespace
func RemovePlugin(namespace string) {
var list []*Plugin
for _, _plugin := range _plugins {
if namespace != _plugin.Namespace {
list = append(list, _plugin)
}
}
_plugins = list
}
// GetPlugin retrieves the first found plugin for the given extension
func GetPlugin(extension string) *Plugin {
for _, plugin := range _plugins {
// Extension plugin
for _, _extension := range plugin.Extensions {
if _extension == extension {
return plugin
}
}
// Generic plugin
// Generic plugin is always the lastest, so at least one match should happen
if len(plugin.Extensions) == 0 {
return plugin
}
}
return &Plugin{}
}
// SetBundles renew the index of bundles with a new list
func SetBundles(bundles []*Bundle) {
_bundles = bundles
}
// GetBundle retrieve the related bundle for the file
func GetBundle(path string) *Bundle {
for _, bundle := range _bundles {
sourcePath := bundle.ToSource(path)
if bundle.Item.Path == path || bundle.Item.Path == sourcePath {
return bundle
}
relatedPaths := bundle.Item.GetRelatedPaths(true)
for _, relatedPath := range relatedPaths {
if relatedPath == path || relatedPath == sourcePath {
return bundle
}
}
}
return &Bundle{}
}
// GetBundles retrieve all bundles in the index
func GetBundles() []*Bundle {
return _bundles
}