1
0

gen.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2018 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build ignore
  15. package main
  16. import (
  17. "bufio"
  18. "fmt"
  19. "go/ast"
  20. "go/format"
  21. "go/parser"
  22. "go/token"
  23. "os"
  24. "os/exec"
  25. "path/filepath"
  26. "regexp"
  27. "runtime"
  28. "strconv"
  29. "strings"
  30. "golang.org/x/tools/go/ast/astutil"
  31. )
  32. func pngDir() (string, error) {
  33. dir, err := exec.Command("go", "list", "-f", "{{.Dir}}", "image/png").Output()
  34. if err != nil {
  35. return "", err
  36. }
  37. return strings.TrimSpace(string(dir)), nil
  38. }
  39. func pngFiles() ([]string, error) {
  40. files, err := exec.Command("go", "list", "-f", `{{join .GoFiles ","}}`, "image/png").Output()
  41. if err != nil {
  42. return nil, err
  43. }
  44. return strings.Split(strings.TrimSpace(string(files)), ","), nil
  45. }
  46. func run() error {
  47. reVer := regexp.MustCompile(`^go1\.(\d+)(\.\d+)?$`)
  48. verStr := runtime.Version()
  49. m := reVer.FindStringSubmatch(verStr)
  50. if m == nil {
  51. return fmt.Errorf("png: unexpected Go version: %s", verStr)
  52. }
  53. ver, err := strconv.Atoi(m[1])
  54. if err != nil {
  55. return err
  56. }
  57. if ver < 22 {
  58. return fmt.Errorf("png: use Go 1.22 or newer")
  59. }
  60. dir, err := pngDir()
  61. if err != nil {
  62. return err
  63. }
  64. files, err := pngFiles()
  65. if err != nil {
  66. return err
  67. }
  68. const prefix = "stdlib"
  69. matches, err := filepath.Glob(prefix + "*.go")
  70. if err != nil {
  71. return err
  72. }
  73. for _, f := range matches {
  74. if err := os.Remove(f); err != nil {
  75. return err
  76. }
  77. }
  78. for _, f := range files {
  79. out, err := os.Create(prefix + f)
  80. if err != nil {
  81. return err
  82. }
  83. defer out.Close()
  84. w := bufio.NewWriter(out)
  85. // TODO: Remove call of RegisterDecoder
  86. data, err := os.ReadFile(filepath.Join(dir, f))
  87. if err != nil {
  88. return err
  89. }
  90. fset := token.NewFileSet()
  91. tree, err := parser.ParseFile(fset, "", string(data), parser.ParseComments)
  92. if err != nil {
  93. return err
  94. }
  95. astutil.Apply(tree, func(c *astutil.Cursor) bool {
  96. stmt, ok := c.Node().(*ast.ExprStmt)
  97. if !ok {
  98. return true
  99. }
  100. call, ok := stmt.X.(*ast.CallExpr)
  101. if !ok {
  102. return true
  103. }
  104. s, ok := call.Fun.(*ast.SelectorExpr)
  105. if !ok {
  106. return true
  107. }
  108. receiver, ok := s.X.(*ast.Ident)
  109. if !ok {
  110. return true
  111. }
  112. // Delete registering PNG format.
  113. if receiver.Name == "image" && s.Sel.Name == "RegisterFormat" {
  114. c.Delete()
  115. }
  116. return true
  117. }, nil)
  118. fmt.Fprintln(w, "// Code generated by gen.go. DO NOT EDIT.")
  119. fmt.Fprintln(w)
  120. format.Node(w, fset, tree)
  121. if err := w.Flush(); err != nil {
  122. return err
  123. }
  124. }
  125. return nil
  126. }
  127. func main() {
  128. if err := run(); err != nil {
  129. panic(err)
  130. }
  131. }