toc.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 getFillerSize(tokenIndex int, lengths []int, nident int) int {
  47. var (
  48. curlen int = lengths[tokenIndex]
  49. maxlen int = 0
  50. substart int = 0
  51. subend int = 0
  52. spacediff int = 0
  53. )
  54. if curlen > 0 {
  55. substart = tokenIndex
  56. for (substart-1) >= 0 && lengths[substart-1] > 0 {
  57. substart -= 1
  58. }
  59. subend = tokenIndex
  60. for (subend+1) < len(lengths) && lengths[subend+1] > 0 {
  61. subend += 1
  62. }
  63. var candidate int
  64. for j := substart; j <= subend; j += 1 {
  65. candidate = lengths[j]
  66. if candidate > maxlen {
  67. maxlen = candidate
  68. }
  69. }
  70. spacediff = maxlen - curlen
  71. }
  72. return spacediff
  73. }
  74. func (root *assetTree) writeGoMap(w io.Writer, nident int) {
  75. fmt.Fprintf(w, "&bintree{%s, map[string]*bintree{", root.funcOrNil())
  76. if len(root.Children) > 0 {
  77. io.WriteString(w, "\n")
  78. // Sort to make output stable between invocations
  79. filenames := make([]string, len(root.Children))
  80. hasChildren := make(map[string]bool)
  81. i := 0
  82. for filename, node := range root.Children {
  83. filenames[i] = filename
  84. hasChildren[filename] = len(node.Children) > 0
  85. i++
  86. }
  87. sort.Strings(filenames)
  88. lengths := make([]int, len(root.Children))
  89. for i, filename := range filenames {
  90. if hasChildren[filename] {
  91. lengths[i] = 0
  92. } else {
  93. lengths[i] = len(filename)
  94. }
  95. }
  96. for i, p := range filenames {
  97. ident(w, nident+1)
  98. filler := strings.Repeat(" ", getFillerSize(i, lengths, nident))
  99. fmt.Fprintf(w, `"%s": %s`, p, filler)
  100. root.Children[p].writeGoMap(w, nident+1)
  101. }
  102. ident(w, nident)
  103. }
  104. io.WriteString(w, "}}")
  105. if nident > 0 {
  106. io.WriteString(w, ",")
  107. }
  108. io.WriteString(w, "\n")
  109. }
  110. func (root *assetTree) WriteAsGoMap(w io.Writer) error {
  111. _, err := fmt.Fprint(w, `type bintree struct {
  112. Func func() (*asset, error)
  113. Children map[string]*bintree
  114. }
  115. var _bintree = `)
  116. root.writeGoMap(w, 0)
  117. return err
  118. }
  119. func writeTOCTree(w io.Writer, toc []Asset) error {
  120. _, err := fmt.Fprintf(w, `// AssetDir returns the file names below a certain
  121. // directory embedded in the file by go-bindata.
  122. // For example if you run go-bindata on data/... and data contains the
  123. // following hierarchy:
  124. // data/
  125. // foo.txt
  126. // img/
  127. // a.png
  128. // b.png
  129. // then AssetDir("data") would return []string{"foo.txt", "img"}
  130. // AssetDir("data/img") would return []string{"a.png", "b.png"}
  131. // AssetDir("foo.txt") and AssetDir("nonexistent") would return an error
  132. // AssetDir("") will return []string{"data"}.
  133. func AssetDir(name string) ([]string, error) {
  134. node := _bintree
  135. if len(name) != 0 {
  136. canonicalName := strings.Replace(name, "\\", "/", -1)
  137. pathList := strings.Split(canonicalName, "/")
  138. for _, p := range pathList {
  139. node = node.Children[p]
  140. if node == nil {
  141. return nil, fmt.Errorf("Asset %%s not found", name)
  142. }
  143. }
  144. }
  145. if node.Func != nil {
  146. return nil, fmt.Errorf("Asset %%s not found", name)
  147. }
  148. rv := make([]string, 0, len(node.Children))
  149. for childName := range node.Children {
  150. rv = append(rv, childName)
  151. }
  152. return rv, nil
  153. }
  154. `)
  155. if err != nil {
  156. return err
  157. }
  158. tree := newAssetTree()
  159. for i := range toc {
  160. pathList := strings.Split(toc[i].Name, "/")
  161. tree.Add(pathList, toc[i])
  162. }
  163. return tree.WriteAsGoMap(w)
  164. }
  165. // writeTOC writes the table of contents file.
  166. func writeTOC(w io.Writer, toc []Asset) error {
  167. err := writeTOCHeader(w)
  168. if err != nil {
  169. return err
  170. }
  171. var maxlen = 0
  172. for i := range toc {
  173. l := len(toc[i].Name)
  174. if l > maxlen {
  175. maxlen = l
  176. }
  177. }
  178. for i := range toc {
  179. err = writeTOCAsset(w, &toc[i], maxlen)
  180. if err != nil {
  181. return err
  182. }
  183. }
  184. return writeTOCFooter(w)
  185. }
  186. // writeTOCHeader writes the table of contents file header.
  187. func writeTOCHeader(w io.Writer) error {
  188. _, err := fmt.Fprintf(w, `// Asset loads and returns the asset for the given name.
  189. // It returns an error if the asset could not be found or
  190. // could not be loaded.
  191. func Asset(name string) ([]byte, error) {
  192. canonicalName := strings.Replace(name, "\\", "/", -1)
  193. if f, ok := _bindata[canonicalName]; ok {
  194. a, err := f()
  195. if err != nil {
  196. return nil, fmt.Errorf("Asset %%s can't read by error: %%v", name, err)
  197. }
  198. return a.bytes, nil
  199. }
  200. return nil, fmt.Errorf("Asset %%s not found", name)
  201. }
  202. // MustAsset is like Asset but panics when Asset would return an error.
  203. // It simplifies safe initialization of global variables.
  204. func MustAsset(name string) []byte {
  205. a, err := Asset(name)
  206. if err != nil {
  207. panic("asset: Asset(" + name + "): " + err.Error())
  208. }
  209. return a
  210. }
  211. // AssetInfo loads and returns the asset info for the given name.
  212. // It returns an error if the asset could not be found or
  213. // could not be loaded.
  214. func AssetInfo(name string) (os.FileInfo, error) {
  215. canonicalName := strings.Replace(name, "\\", "/", -1)
  216. if f, ok := _bindata[canonicalName]; ok {
  217. a, err := f()
  218. if err != nil {
  219. return nil, fmt.Errorf("AssetInfo %%s can't read by error: %%v", name, err)
  220. }
  221. return a.info, nil
  222. }
  223. return nil, fmt.Errorf("AssetInfo %%s not found", name)
  224. }
  225. // AssetNames returns the names of the assets.
  226. func AssetNames() []string {
  227. names := make([]string, 0, len(_bindata))
  228. for name := range _bindata {
  229. names = append(names, name)
  230. }
  231. return names
  232. }
  233. // _bindata is a table, holding each asset generator, mapped to its name.
  234. var _bindata = map[string]func() (*asset, error){
  235. `)
  236. return err
  237. }
  238. // writeTOCAsset write a TOC entry for the given asset.
  239. func writeTOCAsset(w io.Writer, asset *Asset, maxlen int) error {
  240. spacediff := maxlen - len(asset.Name)
  241. filler := strings.Repeat(" ", spacediff)
  242. _, err := fmt.Fprintf(w, "\t%q: %s%s,\n", asset.Name, filler, asset.Func)
  243. return err
  244. }
  245. // writeTOCFooter writes the table of contents file footer.
  246. func writeTOCFooter(w io.Writer) error {
  247. _, err := fmt.Fprintf(w, `}
  248. `)
  249. return err
  250. }