Merge pull request #39 from elazarl/pullreq
Support directory listing with AssetDir(dirname string)
This commit is contained in:
commit
54e5992f47
|
@ -72,7 +72,11 @@ func Translate(c *Config) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write table of contents
|
// Write table of contents
|
||||||
return writeTOC(bfd, toc)
|
if err := writeTOC(bfd, toc); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Write hierarchical tree of assets
|
||||||
|
return writeTOCTree(bfd, toc)
|
||||||
}
|
}
|
||||||
|
|
||||||
// findFiles recursively finds all the file paths in the given directory tree.
|
// findFiles recursively finds all the file paths in the given directory tree.
|
||||||
|
|
113
toc.go
113
toc.go
|
@ -7,8 +7,121 @@ package bindata
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type assetTree struct {
|
||||||
|
Asset Asset
|
||||||
|
Children map[string]*assetTree
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAssetTree() *assetTree {
|
||||||
|
tree := &assetTree{}
|
||||||
|
tree.Children = make(map[string]*assetTree)
|
||||||
|
return tree
|
||||||
|
}
|
||||||
|
|
||||||
|
func (node *assetTree) child(name string) *assetTree {
|
||||||
|
rv, ok := node.Children[name]
|
||||||
|
if !ok {
|
||||||
|
rv = newAssetTree()
|
||||||
|
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(w io.Writer, n int) {
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
w.Write([]byte{'\t'})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (root *assetTree) funcOrNil() string {
|
||||||
|
if root.Asset.Func == "" {
|
||||||
|
return "nil"
|
||||||
|
} else {
|
||||||
|
return root.Asset.Func
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (root *assetTree) writeGoMap(w io.Writer, nident int) {
|
||||||
|
fmt.Fprintf(w, "&_bintree_t{%s, map[string]*_bintree_t{\n", root.funcOrNil())
|
||||||
|
for p, child := range root.Children {
|
||||||
|
ident(w, nident+1)
|
||||||
|
fmt.Fprintf(w, `"%s": `, p)
|
||||||
|
child.writeGoMap(w, nident+1)
|
||||||
|
}
|
||||||
|
ident(w, nident)
|
||||||
|
io.WriteString(w, "}}")
|
||||||
|
if nident > 0 {
|
||||||
|
io.WriteString(w, ",")
|
||||||
|
}
|
||||||
|
io.WriteString(w, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (root *assetTree) WriteAsGoMap(w io.Writer) error {
|
||||||
|
_, err := fmt.Fprint(w, `type _bintree_t struct {
|
||||||
|
Func func() ([]byte, error)
|
||||||
|
Children map[string]*_bintree_t
|
||||||
|
}
|
||||||
|
var _bintree = `)
|
||||||
|
root.writeGoMap(w, 0)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeTOCTree(w io.Writer, toc []Asset) error {
|
||||||
|
_, err := fmt.Fprintf(w, `// AssetDir returns the file names below a certain
|
||||||
|
// directory embedded in the file by go-bindata.
|
||||||
|
// For example if you run go-bindata on data/... and data contains the
|
||||||
|
// following hierarchy:
|
||||||
|
// data/
|
||||||
|
// foo.txt
|
||||||
|
// img/
|
||||||
|
// a.png
|
||||||
|
// b.png
|
||||||
|
// then AssetDir("data") would return []string{"foo.txt", "img"}
|
||||||
|
// AssetDir("data/img") would return []string{"a.png", "b.png"}
|
||||||
|
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
|
||||||
|
func AssetDir(name string) ([]string, error) {
|
||||||
|
cannonicalName := strings.Replace(name, "\\", "/", -1)
|
||||||
|
pathList := strings.Split(cannonicalName, "/")
|
||||||
|
node := _bintree
|
||||||
|
for _, p := range pathList {
|
||||||
|
node = node.Children[p]
|
||||||
|
if node == nil {
|
||||||
|
return nil, fmt.Errorf("Asset %%s not found", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if node.Func != nil {
|
||||||
|
return nil, fmt.Errorf("Asset %%s not found", name)
|
||||||
|
}
|
||||||
|
rv := make([]string, 0, len(node.Children))
|
||||||
|
for name := range node.Children {
|
||||||
|
rv = append(rv, name)
|
||||||
|
}
|
||||||
|
return rv, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tree := newAssetTree()
|
||||||
|
for i := range toc {
|
||||||
|
pathList := strings.Split(toc[i].Name, string(os.PathSeparator))
|
||||||
|
tree.Add(pathList, toc[i])
|
||||||
|
}
|
||||||
|
return tree.WriteAsGoMap(w)
|
||||||
|
}
|
||||||
|
|
||||||
// writeTOC writes the table of contents file.
|
// writeTOC writes the table of contents file.
|
||||||
func writeTOC(w io.Writer, toc []Asset) error {
|
func writeTOC(w io.Writer, toc []Asset) error {
|
||||||
err := writeTOCHeader(w)
|
err := writeTOCHeader(w)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user