You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.1 KiB

  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build ignore
  5. // Command makestatic reads a set of files and writes a Go source file to "static.go"
  6. // that declares a map of string constants containing contents of the input files.
  7. // It is intended to be invoked via "go generate" (directive in "gen.go").
  8. package main
  9. import (
  10. "bytes"
  11. "fmt"
  12. "go/format"
  13. "io/ioutil"
  14. "os"
  15. "time"
  16. "unicode/utf8"
  17. )
  18. var files = []string{
  19. "analysis/call3.png",
  20. "analysis/call-eg.png",
  21. "analysis/callers1.png",
  22. "analysis/callers2.png",
  23. "analysis/chan1.png",
  24. "analysis/chan2a.png",
  25. "analysis/chan2b.png",
  26. "analysis/error1.png",
  27. "analysis/help.html",
  28. "analysis/ident-def.png",
  29. "analysis/ident-field.png",
  30. "analysis/ident-func.png",
  31. "analysis/ipcg-func.png",
  32. "analysis/ipcg-pkg.png",
  33. "analysis/typeinfo-pkg.png",
  34. "analysis/typeinfo-src.png",
  35. "callgraph.html",
  36. "codewalk.html",
  37. "codewalkdir.html",
  38. "dirlist.html",
  39. "error.html",
  40. "example.html",
  41. "godoc.html",
  42. "godocs.js",
  43. "images/minus.gif",
  44. "images/plus.gif",
  45. "images/treeview-black-line.gif",
  46. "images/treeview-black.gif",
  47. "images/treeview-default-line.gif",
  48. "images/treeview-default.gif",
  49. "images/treeview-gray-line.gif",
  50. "images/treeview-gray.gif",
  51. "implements.html",
  52. "jquery.js",
  53. "jquery.treeview.css",
  54. "jquery.treeview.edit.js",
  55. "jquery.treeview.js",
  56. "methodset.html",
  57. "opensearch.xml",
  58. "package.html",
  59. "package.txt",
  60. "play.js",
  61. "playground.js",
  62. "search.html",
  63. "search.txt",
  64. "searchcode.html",
  65. "searchdoc.html",
  66. "searchtxt.html",
  67. "style.css",
  68. }
  69. func main() {
  70. if err := makestatic(); err != nil {
  71. fmt.Fprintln(os.Stderr, err)
  72. os.Exit(1)
  73. }
  74. }
  75. func makestatic() error {
  76. f, err := os.Create("static.go")
  77. if err != nil {
  78. return err
  79. }
  80. defer f.Close()
  81. buf := new(bytes.Buffer)
  82. fmt.Fprintf(buf, "%v\n\n%v\n\npackage static\n\n", license, warning)
  83. fmt.Fprintf(buf, "var Files = map[string]string{\n")
  84. for _, fn := range files {
  85. b, err := ioutil.ReadFile(fn)
  86. if err != nil {
  87. return err
  88. }
  89. fmt.Fprintf(buf, "\t%q: ", fn)
  90. if utf8.Valid(b) {
  91. fmt.Fprintf(buf, "`%s`", sanitize(b))
  92. } else {
  93. fmt.Fprintf(buf, "%q", b)
  94. }
  95. fmt.Fprintln(buf, ",\n")
  96. }
  97. fmt.Fprintln(buf, "}")
  98. fmtbuf, err := format.Source(buf.Bytes())
  99. if err != nil {
  100. return err
  101. }
  102. return ioutil.WriteFile("static.go", fmtbuf, 0666)
  103. }
  104. // sanitize prepares a valid UTF-8 string as a raw string constant.
  105. func sanitize(b []byte) []byte {
  106. // Replace ` with `+"`"+`
  107. b = bytes.Replace(b, []byte("`"), []byte("`+\"`\"+`"), -1)
  108. // Replace BOM with `+"\xEF\xBB\xBF"+`
  109. // (A BOM is valid UTF-8 but not permitted in Go source files.
  110. // I wouldn't bother handling this, but for some insane reason
  111. // jquery.js has a BOM somewhere in the middle.)
  112. return bytes.Replace(b, []byte("\xEF\xBB\xBF"), []byte("`+\"\\xEF\\xBB\\xBF\"+`"), -1)
  113. }
  114. const warning = `// Code generated by "makestatic"; DO NOT EDIT.`
  115. var license = fmt.Sprintf(`// Copyright %d The Go Authors. All rights reserved.
  116. // Use of this source code is governed by a BSD-style
  117. // license that can be found in the LICENSE file.`, time.Now().UTC().Year())