forked from kevinburke/go-bindata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toc.go
173 lines (151 loc) · 3.52 KB
/
toc.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
// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// license. Its contents can be found at:
// http://creativecommons.org/publicdomain/zero/1.0/
package xbindata
import (
"bytes"
"fmt"
"go/format"
"io"
"path"
"sort"
"strings"
)
type assetTree struct {
Asset Asset
Path string
Children map[string]*assetTree
}
func newAssetTree() *assetTree {
tree := &assetTree{Path: "."}
tree.Children = make(map[string]*assetTree)
return tree
}
func (node *assetTree) child(name string) *assetTree {
rv, ok := node.Children[name]
if !ok {
rv = newAssetTree()
rv.Path = path.Join(node.Path, name)
// TODO: maintain this in sorted order
node.Children[name] = rv
}
return rv
}
func (root *assetTree) Add(route []string, asset Asset) {
for _, name := range route {
root = root.child(name)
}
root.Asset = asset
}
func ident(buf *bytes.Buffer, n int) {
for i := 0; i < n; i++ {
buf.WriteByte('\t')
}
}
func (root *assetTree) funcOrNil() string {
if root.Asset.Func == "" {
return "nil"
} else {
return root.Asset.Func
}
}
func (root *assetTree) writeGoMap(buf *bytes.Buffer, nident int) {
buf.Grow(35) // at least this size
if root.Asset.Func != "" {
_, _ = fmt.Fprintf(buf, "%s", root.Asset.Func)
} else {
_, _ = fmt.Fprintf(buf, "bc.NewDir(%d, %q, map[string]bc.Node{", nident, root.Path)
if len(root.Children) > 0 {
buf.WriteByte('\n')
// Sort to make output stable between invocations
filenames := make([]string, len(root.Children))
i := 0
for filename := range root.Children {
filenames[i] = filename
i++
}
sort.Strings(filenames)
for _, p := range filenames {
ident(buf, nident+1)
buf.WriteByte('"')
buf.WriteString(p)
buf.WriteString(`": `)
root.Children[p].writeGoMap(buf, nident+1)
}
ident(buf, nident)
}
buf.WriteString("})")
}
if nident > 0 {
buf.WriteByte(',')
}
buf.WriteByte('\n')
}
func (root *assetTree) WriteAsGoMap(w io.Writer) error {
_, err := w.Write([]byte(`
var _root bc.NodeDir = `))
if err != nil {
return err
}
buf := new(bytes.Buffer)
root.writeGoMap(buf, 0)
fmted, err := format.Source(buf.Bytes())
if err != nil {
return err
}
_, writeErr := w.Write(fmted)
return writeErr
}
func writeTOCTree(w io.Writer, toc []Asset) error {
tree := newAssetTree()
for i := range toc {
pathList := strings.Split(toc[i].Name, "/")
tree.Add(pathList, toc[i])
}
return tree.WriteAsGoMap(w)
}
// writeTOC writes the table of contents file.
func writeTOC(c *Config, buf *bytes.Buffer, toc []Asset) error {
writeTOCHeader(buf)
var start int64
for i := range toc {
if i != 0 {
// Newlines between elements make gofmt happy.
buf.WriteByte('\n')
}
if err := writeTOCAsset(c, start, buf, &toc[i]); err != nil {
return err
}
start += toc[i].Size
}
writeTOCFooter(c, buf)
return nil
}
// writeTOCHeader writes the table of contents file header.
func writeTOCHeader(buf *bytes.Buffer) {
buf.WriteString(`
// Assets is a table, holding each asset generator, mapped to its name.
var Assets *bc.Assets
func LoadDefault() {
Assets = bc.NewAssets(
`)
}
// writeTOCAsset writes a TOC entry for the given asset.
func writeTOCAsset(c *Config, start int64, w io.Writer, asset *Asset) (err error) {
_, err = w.Write([]byte("\t\t" + asset.Func + ","))
return err
}
// writeTOCFooter writes the table of contents file footer.
func writeTOCFooter(c *Config, buf *bytes.Buffer) {
data := `
)
`
if c.FileSystem {
data += `
DefaultFS = xbfs.NewFileSystem(Assets)
`
}
data += `}
`
buf.WriteString(data)
}