ebiten/internal/png/gen.go

112 lines
2.3 KiB
Go
Raw Permalink Normal View History

2018-04-09 20:47:04 +02:00
// Copyright 2018 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build ignore
2018-04-09 20:47:04 +02:00
// +build ignore
package main
import (
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"golang.org/x/tools/go/ast/astutil"
)
func run() error {
2019-11-25 16:13:37 +01:00
// TODO: Use go/packages with specifying build tags so that stdlibfuzz can be avoided.
2018-04-09 20:47:04 +02:00
dir := filepath.Join(runtime.GOROOT(), "src", "image", "png")
files, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, f := range files {
if f.IsDir() {
continue
}
if strings.HasSuffix(f.Name(), "_test.go") {
continue
}
in, err := os.Open(filepath.Join(dir, f.Name()))
if err != nil {
return err
}
defer in.Close()
out, err := os.Create("stdlib" + f.Name())
if err != nil {
return err
}
defer out.Close()
// TODO: Remove call of RegisterDecoder
data, err := ioutil.ReadAll(in)
if err != nil {
return err
}
fset := token.NewFileSet()
tree, err := parser.ParseFile(fset, "", string(data), parser.ParseComments)
if err != nil {
return err
}
astutil.Apply(tree, func(c *astutil.Cursor) bool {
stmt, ok := c.Node().(*ast.ExprStmt)
if !ok {
return true
}
call, ok := stmt.X.(*ast.CallExpr)
if !ok {
return true
}
s, ok := call.Fun.(*ast.SelectorExpr)
if !ok {
return true
}
receiver, ok := s.X.(*ast.Ident)
if !ok {
return true
}
// Delete registering PNG format.
if receiver.Name == "image" && s.Sel.Name == "RegisterFormat" {
c.Delete()
}
return true
}, nil)
2018-04-27 20:37:19 +02:00
fmt.Fprintln(out, "// Code generated by gen.go. DO NOT EDIT.")
2018-04-09 20:47:04 +02:00
fmt.Fprintln(out)
format.Node(out, fset, tree)
}
return nil
}
func main() {
if err := run(); err != nil {
panic(err)
}
}