debug.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. )
  9. // writeDebug writes the debug code file.
  10. func writeDebug(w io.Writer, c *Config, toc []Asset) error {
  11. err := writeDebugHeader(w, c)
  12. if err != nil {
  13. return err
  14. }
  15. err = writeAssetFS(w, c)
  16. if err != nil {
  17. return err
  18. }
  19. for i := range toc {
  20. err = writeDebugAsset(w, c, &toc[i])
  21. if err != nil {
  22. return err
  23. }
  24. }
  25. return nil
  26. }
  27. // writeDebugHeader writes output file headers.
  28. // This targets debug builds.
  29. func writeDebugHeader(w io.Writer, c *Config) error {
  30. var header string
  31. if c.HttpFileSystem {
  32. header = `import (
  33. "bytes"
  34. "net/http"
  35. "fmt"
  36. "io/ioutil"
  37. "os"
  38. "path/filepath"
  39. "strings"
  40. "time"`
  41. } else {
  42. header = `import (
  43. "fmt"
  44. "io/ioutil"
  45. "os"
  46. "path/filepath"
  47. "strings"
  48. "time"`
  49. }
  50. _, err := fmt.Fprintf(w, `%s
  51. )
  52. // bindataRead reads the given file from disk. It returns an error on failure.
  53. func bindataRead(path, name string) ([]byte, error) {
  54. buf, err := ioutil.ReadFile(path)
  55. if err != nil {
  56. err = fmt.Errorf("Error reading asset %%s at %%s: %%v", name, path, err)
  57. }
  58. return buf, err
  59. }
  60. type asset struct {
  61. bytes []byte
  62. info os.FileInfo
  63. }
  64. type bindataFileInfo struct {
  65. name string
  66. size int64
  67. mode os.FileMode
  68. modTime time.Time
  69. }
  70. // Name return file name
  71. func (fi bindataFileInfo) Name() string {
  72. return fi.name
  73. }
  74. // Size return file size
  75. func (fi bindataFileInfo) Size() int64 {
  76. return fi.size
  77. }
  78. // Mode return file mode
  79. func (fi bindataFileInfo) Mode() os.FileMode {
  80. return fi.mode
  81. }
  82. // ModTime return file modify time
  83. func (fi bindataFileInfo) ModTime() time.Time {
  84. return fi.modTime
  85. }
  86. // IsDir return file whether a directory
  87. func (fi bindataFileInfo) IsDir() bool {
  88. return fi.mode&os.ModeDir != 0
  89. }
  90. // Sys return file is sys mode
  91. func (fi bindataFileInfo) Sys() interface{} {
  92. return nil
  93. }
  94. `, header)
  95. return err
  96. }
  97. // writeDebugAsset write a debug entry for the given asset.
  98. // A debug entry is simply a function which reads the asset from
  99. // the original file (e.g.: from disk).
  100. func writeDebugAsset(w io.Writer, c *Config, asset *Asset) error {
  101. pathExpr := fmt.Sprintf("%q", asset.Path)
  102. if c.Dev {
  103. pathExpr = fmt.Sprintf("filepath.Join(rootDir, %q)", asset.Name)
  104. }
  105. _, err := fmt.Fprintf(w, `// %s reads file data from disk. It returns an error on failure.
  106. func %s() (*asset, error) {
  107. path := %s
  108. name := %q
  109. bytes, err := bindataRead(path, name)
  110. if err != nil {
  111. return nil, err
  112. }
  113. fi, err := os.Stat(path)
  114. if err != nil {
  115. err = fmt.Errorf("Error reading asset info %%s at %%s: %%v", name, path, err)
  116. }
  117. a := &asset{bytes: bytes, info: fi}
  118. return a, err
  119. }
  120. `, asset.Func, asset.Func, pathExpr, asset.Name)
  121. return err
  122. }