gen.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // Copyright 2015 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. // Package gen contains common code for the various code generation tools in the
  5. // text repository. Its usage ensures consistency between tools.
  6. //
  7. // This package defines command line flags that are common to most generation
  8. // tools. The flags allow for specifying specific Unicode and CLDR versions
  9. // in the public Unicode data repository (https://www.unicode.org/Public).
  10. //
  11. // A local Unicode data mirror can be set through the flag -local or the
  12. // environment variable UNICODE_DIR. The former takes precedence. The local
  13. // directory should follow the same structure as the public repository.
  14. //
  15. // IANA data can also optionally be mirrored by putting it in the iana directory
  16. // rooted at the top of the local mirror. Beware, though, that IANA data is not
  17. // versioned. So it is up to the developer to use the right version.
  18. package gen // import "golang.org/x/text/internal/gen"
  19. import (
  20. "bytes"
  21. "flag"
  22. "fmt"
  23. "go/build"
  24. "go/format"
  25. "io"
  26. "io/ioutil"
  27. "log"
  28. "net/http"
  29. "os"
  30. "path"
  31. "path/filepath"
  32. "regexp"
  33. "strings"
  34. "sync"
  35. "unicode"
  36. "golang.org/x/text/unicode/cldr"
  37. )
  38. var (
  39. url = flag.String("url",
  40. "https://www.unicode.org/Public",
  41. "URL of Unicode database directory")
  42. iana = flag.String("iana",
  43. "http://www.iana.org",
  44. "URL of the IANA repository")
  45. unicodeVersion = flag.String("unicode",
  46. getEnv("UNICODE_VERSION", unicode.Version),
  47. "unicode version to use")
  48. cldrVersion = flag.String("cldr",
  49. getEnv("CLDR_VERSION", cldr.Version),
  50. "cldr version to use")
  51. )
  52. func getEnv(name, def string) string {
  53. if v := os.Getenv(name); v != "" {
  54. return v
  55. }
  56. return def
  57. }
  58. // Init performs common initialization for a gen command. It parses the flags
  59. // and sets up the standard logging parameters.
  60. func Init() {
  61. log.SetPrefix("")
  62. log.SetFlags(log.Lshortfile)
  63. flag.Parse()
  64. }
  65. const header = `// Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
  66. `
  67. // UnicodeVersion reports the requested Unicode version.
  68. func UnicodeVersion() string {
  69. return *unicodeVersion
  70. }
  71. // CLDRVersion reports the requested CLDR version.
  72. func CLDRVersion() string {
  73. return *cldrVersion
  74. }
  75. var tags = []struct{ version, buildTags string }{
  76. {"9.0.0", "!go1.10"},
  77. {"10.0.0", "go1.10,!go1.13"},
  78. {"11.0.0", "go1.13,!go1.14"},
  79. {"12.0.0", "go1.14"},
  80. }
  81. // buildTags reports the build tags used for the current Unicode version.
  82. func buildTags() string {
  83. v := UnicodeVersion()
  84. for _, e := range tags {
  85. if e.version == v {
  86. return e.buildTags
  87. }
  88. }
  89. log.Fatalf("Unknown build tags for Unicode version %q.", v)
  90. return ""
  91. }
  92. // IsLocal reports whether data files are available locally.
  93. func IsLocal() bool {
  94. dir, err := localReadmeFile()
  95. if err != nil {
  96. return false
  97. }
  98. if _, err = os.Stat(dir); err != nil {
  99. return false
  100. }
  101. return true
  102. }
  103. // OpenUCDFile opens the requested UCD file. The file is specified relative to
  104. // the public Unicode root directory. It will call log.Fatal if there are any
  105. // errors.
  106. func OpenUCDFile(file string) io.ReadCloser {
  107. return openUnicode(path.Join(*unicodeVersion, "ucd", file))
  108. }
  109. // OpenCLDRCoreZip opens the CLDR core zip file. It will call log.Fatal if there
  110. // are any errors.
  111. func OpenCLDRCoreZip() io.ReadCloser {
  112. return OpenUnicodeFile("cldr", *cldrVersion, "core.zip")
  113. }
  114. // OpenUnicodeFile opens the requested file of the requested category from the
  115. // root of the Unicode data archive. The file is specified relative to the
  116. // public Unicode root directory. If version is "", it will use the default
  117. // Unicode version. It will call log.Fatal if there are any errors.
  118. func OpenUnicodeFile(category, version, file string) io.ReadCloser {
  119. if version == "" {
  120. version = UnicodeVersion()
  121. }
  122. return openUnicode(path.Join(category, version, file))
  123. }
  124. // OpenIANAFile opens the requested IANA file. The file is specified relative
  125. // to the IANA root, which is typically either http://www.iana.org or the
  126. // iana directory in the local mirror. It will call log.Fatal if there are any
  127. // errors.
  128. func OpenIANAFile(path string) io.ReadCloser {
  129. return Open(*iana, "iana", path)
  130. }
  131. var (
  132. dirMutex sync.Mutex
  133. localDir string
  134. )
  135. const permissions = 0755
  136. func localReadmeFile() (string, error) {
  137. p, err := build.Import("golang.org/x/text", "", build.FindOnly)
  138. if err != nil {
  139. return "", fmt.Errorf("Could not locate package: %v", err)
  140. }
  141. return filepath.Join(p.Dir, "DATA", "README"), nil
  142. }
  143. func getLocalDir() string {
  144. dirMutex.Lock()
  145. defer dirMutex.Unlock()
  146. readme, err := localReadmeFile()
  147. if err != nil {
  148. log.Fatal(err)
  149. }
  150. dir := filepath.Dir(readme)
  151. if _, err := os.Stat(readme); err != nil {
  152. if err := os.MkdirAll(dir, permissions); err != nil {
  153. log.Fatalf("Could not create directory: %v", err)
  154. }
  155. ioutil.WriteFile(readme, []byte(readmeTxt), permissions)
  156. }
  157. return dir
  158. }
  159. const readmeTxt = `Generated by golang.org/x/text/internal/gen. DO NOT EDIT.
  160. This directory contains downloaded files used to generate the various tables
  161. in the golang.org/x/text subrepo.
  162. Note that the language subtag repo (iana/assignments/language-subtag-registry)
  163. and all other times in the iana subdirectory are not versioned and will need
  164. to be periodically manually updated. The easiest way to do this is to remove
  165. the entire iana directory. This is mostly of concern when updating the language
  166. package.
  167. `
  168. // Open opens subdir/path if a local directory is specified and the file exists,
  169. // where subdir is a directory relative to the local root, or fetches it from
  170. // urlRoot/path otherwise. It will call log.Fatal if there are any errors.
  171. func Open(urlRoot, subdir, path string) io.ReadCloser {
  172. file := filepath.Join(getLocalDir(), subdir, filepath.FromSlash(path))
  173. return open(file, urlRoot, path)
  174. }
  175. func openUnicode(path string) io.ReadCloser {
  176. file := filepath.Join(getLocalDir(), filepath.FromSlash(path))
  177. return open(file, *url, path)
  178. }
  179. // TODO: automatically periodically update non-versioned files.
  180. func open(file, urlRoot, path string) io.ReadCloser {
  181. if f, err := os.Open(file); err == nil {
  182. return f
  183. }
  184. r := get(urlRoot, path)
  185. defer r.Close()
  186. b, err := ioutil.ReadAll(r)
  187. if err != nil {
  188. log.Fatalf("Could not download file: %v", err)
  189. }
  190. os.MkdirAll(filepath.Dir(file), permissions)
  191. if err := ioutil.WriteFile(file, b, permissions); err != nil {
  192. log.Fatalf("Could not create file: %v", err)
  193. }
  194. return ioutil.NopCloser(bytes.NewReader(b))
  195. }
  196. func get(root, path string) io.ReadCloser {
  197. url := root + "/" + path
  198. fmt.Printf("Fetching %s...", url)
  199. defer fmt.Println(" done.")
  200. resp, err := http.Get(url)
  201. if err != nil {
  202. log.Fatalf("HTTP GET: %v", err)
  203. }
  204. if resp.StatusCode != 200 {
  205. log.Fatalf("Bad GET status for %q: %q", url, resp.Status)
  206. }
  207. return resp.Body
  208. }
  209. // TODO: use Write*Version in all applicable packages.
  210. // WriteUnicodeVersion writes a constant for the Unicode version from which the
  211. // tables are generated.
  212. func WriteUnicodeVersion(w io.Writer) {
  213. fmt.Fprintf(w, "// UnicodeVersion is the Unicode version from which the tables in this package are derived.\n")
  214. fmt.Fprintf(w, "const UnicodeVersion = %q\n\n", UnicodeVersion())
  215. }
  216. // WriteCLDRVersion writes a constant for the CLDR version from which the
  217. // tables are generated.
  218. func WriteCLDRVersion(w io.Writer) {
  219. fmt.Fprintf(w, "// CLDRVersion is the CLDR version from which the tables in this package are derived.\n")
  220. fmt.Fprintf(w, "const CLDRVersion = %q\n\n", CLDRVersion())
  221. }
  222. // WriteGoFile prepends a standard file comment and package statement to the
  223. // given bytes, applies gofmt, and writes them to a file with the given name.
  224. // It will call log.Fatal if there are any errors.
  225. func WriteGoFile(filename, pkg string, b []byte) {
  226. w, err := os.Create(filename)
  227. if err != nil {
  228. log.Fatalf("Could not create file %s: %v", filename, err)
  229. }
  230. defer w.Close()
  231. if _, err = WriteGo(w, pkg, "", b); err != nil {
  232. log.Fatalf("Error writing file %s: %v", filename, err)
  233. }
  234. }
  235. func fileToPattern(filename string) string {
  236. suffix := ".go"
  237. if strings.HasSuffix(filename, "_test.go") {
  238. suffix = "_test.go"
  239. }
  240. prefix := filename[:len(filename)-len(suffix)]
  241. return fmt.Sprint(prefix, "%s", suffix)
  242. }
  243. func updateBuildTags(pattern string) {
  244. for _, t := range tags {
  245. oldFile := fmt.Sprintf(pattern, t.version)
  246. b, err := ioutil.ReadFile(oldFile)
  247. if err != nil {
  248. continue
  249. }
  250. build := fmt.Sprintf("// +build %s", t.buildTags)
  251. b = regexp.MustCompile(`// \+build .*`).ReplaceAll(b, []byte(build))
  252. err = ioutil.WriteFile(oldFile, b, 0644)
  253. if err != nil {
  254. log.Fatal(err)
  255. }
  256. }
  257. }
  258. // WriteVersionedGoFile prepends a standard file comment, adds build tags to
  259. // version the file for the current Unicode version, and package statement to
  260. // the given bytes, applies gofmt, and writes them to a file with the given
  261. // name. It will call log.Fatal if there are any errors.
  262. func WriteVersionedGoFile(filename, pkg string, b []byte) {
  263. pattern := fileToPattern(filename)
  264. updateBuildTags(pattern)
  265. filename = fmt.Sprintf(pattern, UnicodeVersion())
  266. w, err := os.Create(filename)
  267. if err != nil {
  268. log.Fatalf("Could not create file %s: %v", filename, err)
  269. }
  270. defer w.Close()
  271. if _, err = WriteGo(w, pkg, buildTags(), b); err != nil {
  272. log.Fatalf("Error writing file %s: %v", filename, err)
  273. }
  274. }
  275. // WriteGo prepends a standard file comment and package statement to the given
  276. // bytes, applies gofmt, and writes them to w.
  277. func WriteGo(w io.Writer, pkg, tags string, b []byte) (n int, err error) {
  278. src := []byte(header)
  279. if tags != "" {
  280. src = append(src, fmt.Sprintf("// +build %s\n\n", tags)...)
  281. }
  282. src = append(src, fmt.Sprintf("package %s\n\n", pkg)...)
  283. src = append(src, b...)
  284. formatted, err := format.Source(src)
  285. if err != nil {
  286. // Print the generated code even in case of an error so that the
  287. // returned error can be meaningfully interpreted.
  288. n, _ = w.Write(src)
  289. return n, err
  290. }
  291. return w.Write(formatted)
  292. }
  293. // Repackage rewrites a Go file from belonging to package main to belonging to
  294. // the given package.
  295. func Repackage(inFile, outFile, pkg string) {
  296. src, err := ioutil.ReadFile(inFile)
  297. if err != nil {
  298. log.Fatalf("reading %s: %v", inFile, err)
  299. }
  300. const toDelete = "package main\n\n"
  301. i := bytes.Index(src, []byte(toDelete))
  302. if i < 0 {
  303. log.Fatalf("Could not find %q in %s.", toDelete, inFile)
  304. }
  305. w := &bytes.Buffer{}
  306. w.Write(src[i+len(toDelete):])
  307. WriteGoFile(outFile, pkg, w.Bytes())
  308. }