toc.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
  2. // license. Its contents can be found at:
  3. // http://creativecommons.org/publicdomain/zero/1.0/
  4. package bindata
  5. import (
  6. "fmt"
  7. "io"
  8. "sort"
  9. "strings"
  10. )
  11. type assetTree struct {
  12. Asset Asset
  13. Children map[string]*assetTree
  14. }
  15. func newAssetTree() *assetTree {
  16. tree := &assetTree{}
  17. tree.Children = make(map[string]*assetTree)
  18. return tree
  19. }
  20. func (node *assetTree) child(name string) *assetTree {
  21. rv, ok := node.Children[name]
  22. if !ok {
  23. rv = newAssetTree()
  24. node.Children[name] = rv
  25. }
  26. return rv
  27. }
  28. func (root *assetTree) Add(route []string, asset Asset) {
  29. for _, name := range route {
  30. root = root.child(name)
  31. }
  32. root.Asset = asset
  33. }
  34. func ident(w io.Writer, n int) {
  35. for i := 0; i < n; i++ {
  36. w.Write([]byte{'\t'})
  37. }
  38. }
  39. func (root *assetTree) funcOrNil() string {
  40. if root.Asset.Func == "" {
  41. return "nil"
  42. } else {
  43. return root.Asset.Func
  44. }
  45. }
  46. func (root *assetTree) writeGoMap(w io.Writer, nident int) {
  47. fmt.Fprintf(w, "&bintree{%s, map[string]*bintree{", root.funcOrNil())
  48. if len(root.Children) > 0 {
  49. io.WriteString(w, "\n")
  50. // Sort to make output stable between invocations
  51. filenames := make([]string, len(root.Children))
  52. i := 0
  53. for filename, _ := range root.Children {
  54. filenames[i] = filename
  55. i++
  56. }
  57. sort.Strings(filenames)
  58. for _, p := range filenames {
  59. ident(w, nident+1)
  60. fmt.Fprintf(w, `"%s": `, p)
  61. root.Children[p].writeGoMap(w, nident+1)
  62. }
  63. ident(w, nident)
  64. }
  65. io.WriteString(w, "}}")
  66. if nident > 0 {
  67. io.WriteString(w, ",")
  68. }
  69. io.WriteString(w, "\n")
  70. }
  71. func (root *assetTree) WriteAsGoMap(w io.Writer) error {
  72. _, err := fmt.Fprint(w, `type bintree struct {
  73. Func func() (*asset, error)
  74. Children map[string]*bintree
  75. }
  76. var _bintree = `)
  77. root.writeGoMap(w, 0)
  78. return err
  79. }
  80. func writeTOCTree(w io.Writer, toc []Asset) error {
  81. _, err := fmt.Fprintf(w, `// AssetDir returns the file names below a certain
  82. // directory embedded in the file by go-bindata.
  83. // For example if you run go-bindata on data/... and data contains the
  84. // following hierarchy:
  85. // data/
  86. // foo.txt
  87. // img/
  88. // a.png
  89. // b.png
  90. // then AssetDir("data") would return []string{"foo.txt", "img"}
  91. // AssetDir("data/img") would return []string{"a.png", "b.png"}
  92. // AssetDir("foo.txt") and AssetDir("notexist") would return an error
  93. // AssetDir("") will return []string{"data"}.
  94. func AssetDir(name string) ([]string, error) {
  95. node := _bintree
  96. if len(name) != 0 {
  97. cannonicalName := strings.Replace(name, "\\", "/", -1)
  98. pathList := strings.Split(cannonicalName, "/")
  99. for _, p := range pathList {
  100. node = node.Children[p]
  101. if node == nil {
  102. return nil, fmt.Errorf("Asset %%s not found", name)
  103. }
  104. }
  105. }
  106. if node.Func != nil {
  107. return nil, fmt.Errorf("Asset %%s not found", name)
  108. }
  109. rv := make([]string, 0, len(node.Children))
  110. for childName := range node.Children {
  111. rv = append(rv, childName)
  112. }
  113. return rv, nil
  114. }
  115. `)
  116. if err != nil {
  117. return err
  118. }
  119. tree := newAssetTree()
  120. for i := range toc {
  121. pathList := strings.Split(toc[i].Name, "/")
  122. tree.Add(pathList, toc[i])
  123. }
  124. return tree.WriteAsGoMap(w)
  125. }
  126. // writeTOC writes the table of contents file.
  127. func writeTOC(w io.Writer, toc []Asset) error {
  128. err := writeTOCHeader(w)
  129. if err != nil {
  130. return err
  131. }
  132. for i := range toc {
  133. err = writeTOCAsset(w, &toc[i])
  134. if err != nil {
  135. return err
  136. }
  137. }
  138. return writeTOCFooter(w)
  139. }
  140. // writeTOCHeader writes the table of contents file header.
  141. func writeTOCHeader(w io.Writer) error {
  142. _, err := fmt.Fprintf(w, `// Asset loads and returns the asset for the given name.
  143. // It returns an error if the asset could not be found or
  144. // could not be loaded.
  145. func Asset(name string) ([]byte, error) {
  146. cannonicalName := strings.Replace(name, "\\", "/", -1)
  147. if f, ok := _bindata[cannonicalName]; ok {
  148. a, err := f()
  149. if err != nil {
  150. return nil, fmt.Errorf("Asset %%s can't read by error: %%v", name, err)
  151. }
  152. return a.bytes, nil
  153. }
  154. return nil, fmt.Errorf("Asset %%s not found", name)
  155. }
  156. // MustAsset is like Asset but panics when Asset would return an error.
  157. // It simplifies safe initialization of global variables.
  158. func MustAsset(name string) []byte {
  159. a, err := Asset(name)
  160. if err != nil {
  161. panic("asset: Asset(" + name + "): " + err.Error())
  162. }
  163. return a
  164. }
  165. // AssetInfo loads and returns the asset info for the given name.
  166. // It returns an error if the asset could not be found or
  167. // could not be loaded.
  168. func AssetInfo(name string) (os.FileInfo, error) {
  169. cannonicalName := strings.Replace(name, "\\", "/", -1)
  170. if f, ok := _bindata[cannonicalName]; ok {
  171. a, err := f()
  172. if err != nil {
  173. return nil, fmt.Errorf("AssetInfo %%s can't read by error: %%v", name, err)
  174. }
  175. return a.info, nil
  176. }
  177. return nil, fmt.Errorf("AssetInfo %%s not found", name)
  178. }
  179. // AssetNames returns the names of the assets.
  180. func AssetNames() []string {
  181. names := make([]string, 0, len(_bindata))
  182. for name := range _bindata {
  183. names = append(names, name)
  184. }
  185. return names
  186. }
  187. // _bindata is a table, holding each asset generator, mapped to its name.
  188. var _bindata = map[string]func() (*asset, error){
  189. `)
  190. return err
  191. }
  192. // writeTOCAsset write a TOC entry for the given asset.
  193. func writeTOCAsset(w io.Writer, asset *Asset) error {
  194. _, err := fmt.Fprintf(w, "\t%q: %s,\n", asset.Name, asset.Func)
  195. return err
  196. }
  197. // writeTOCFooter writes the table of contents file footer.
  198. func writeTOCFooter(w io.Writer) error {
  199. _, err := fmt.Fprintf(w, `}
  200. `)
  201. return err
  202. }