-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebresource.go
168 lines (138 loc) · 3.9 KB
/
webresource.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
// Dependency management for web resources like JS, CSS and other files.
package webresource
import (
"bytes"
"fmt"
"net/http"
"path"
"sort"
"strings"
)
// Module interface describes a webresource module with a name (matching the Go import path),
// an http.FileSystem with it's contents, and slice of other Modules that this one requires.
// By convention the Module instance for a particular package can be obtained by calling that
// package's top level Module() function.
type Module interface {
http.FileSystem
Name() string
Requires() []interface{}
}
func requireModules(ilist []interface{}) ModuleList {
ret := make(ModuleList, 0, len(ilist))
for _, i := range ilist {
iv, ok := i.(Module)
if !ok {
panic(fmt.Errorf("value type %T does not implement Module interface", i))
}
ret = append(ret, iv)
}
return ret
}
// Resolve walks the dependency tree for the resources provided and returns
// a ModuleList in the correct sequence according to dependency rules.
// The order of the input list is not important, the same input set will always
// result in the same output.
func Resolve(r ModuleList) ModuleList {
var ret ModuleList
// ensure we have stable sequence for input
r2 := make(ModuleList, len(r))
copy(r2, r)
sort.Sort(r2)
for _, m := range r2 {
// for each module, recusively resolve it's dependencies
mreqs := Resolve(requireModules(m.Requires()))
// and add each one to our output, unless it's already there
for _, mreq := range mreqs {
if ret.Named(mreq.Name()) == nil {
ret = append(ret, mreq)
}
}
// now do this module
if ret.Named(m.Name()) == nil {
ret = append(ret, m)
}
}
return ret
}
// ModuleList is a Module slice with some useful methods.
type ModuleList []Module
func (l ModuleList) Named(name string) Module {
for _, wr := range l {
if wr.Name() == name {
return wr
}
}
return nil
}
// Strings outputs the "%s" of each Module on its own line with the final newline trimmed.
func (l ModuleList) String() string {
var buf bytes.Buffer
for _, m := range l {
fmt.Fprintf(&buf, "%s\n", m)
}
return strings.TrimSpace(buf.String())
}
// Less sorts by Name()
func (p ModuleList) Less(i, j int) bool { return p[i].Name() < p[j].Name() }
func (p ModuleList) Len() int { return len(p) }
func (p ModuleList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type WalkFunc func(m Module, fullPath string, f http.File) error
// Walk will visit each file in each FileSystem contained in this Module by calling the fn function.
// This does not walk Requires(). Sequence is determined by the underlying Readdir() calls.
func (l ModuleList) Walk(ext string, fn WalkFunc) error {
for _, m := range l {
err := Walk(m, ext, fn)
if err != nil {
return err
}
}
return nil
}
// Walk will visit each file in the FileSystem contained in this Module by calling the fn function.
// This does not walk Requires(). Sequence is determined by the underlying Readdir() calls.
func Walk(m Module, ext string, fn WalkFunc) error {
return walkDir(m, "/", ext, fn)
}
func walkDir(m Module, root string, ext string, fn WalkFunc) error {
dirf, err := m.Open(root)
if err != nil {
return err
}
fis, err := dirf.Readdir(-1)
// close dir right after we're done read file infos to avoid unnecessary files left open for large trees
dirf.Close()
if err != nil {
return err
}
for _, fi := range fis {
// recurse into directory
if fi.IsDir() {
newRoot := path.Join(root, fi.Name())
err := walkDir(m, newRoot, ext, fn)
if err != nil {
return err
}
continue
}
// for files...
base := fi.Name()
// skip wrong ext
if path.Ext(base) != ext {
continue
}
// open and close each file right here
err := func() error {
fullPath := path.Join(root, fi.Name())
f, err := m.Open(fullPath)
if err != nil {
return err
}
defer f.Close()
return fn(m, fullPath, f)
}()
if err != nil {
return err
}
}
return nil
}