Skip to content

Commit

Permalink
internal/shaderlister: refactoring: do not use github.com/hajimehoshi…
Browse files Browse the repository at this point in the history
…/ebiten/v2 command
  • Loading branch information
hajimehoshi committed Nov 10, 2024
1 parent 1d5a8bc commit 9b84981
Showing 1 changed file with 12 additions and 32 deletions.
44 changes: 12 additions & 32 deletions internal/shaderlister/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import (
"go/types"
"log/slog"
"os"
"os/exec"
"regexp"
"slices"
"strings"

"golang.org/x/tools/go/ast/inspector"
Expand Down Expand Up @@ -57,34 +55,28 @@ func xmain() error {
flag.Usage()
}

pkg := flag.Arg(0)
deps, err := listDeps(pkg)
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedName | packages.NeedTypes | packages.NeedImports | packages.NeedDeps | packages.NeedSyntax | packages.NeedTypesInfo,
}, flag.Args()...)
if err != nil {
return err
}
deps = slices.DeleteFunc(deps, func(name string) bool {

var shaders []Shader

packages.Visit(pkgs, func(pkg *packages.Package) bool {
path := pkg.PkgPath
// A standard library should not have a directive for shaders. Skip them.
if isStandardImportPath(name) {
if isStandardImportPath(path) {
return true
}
// A semi-standard library should not have a directive for shaders. Skip them.
if strings.HasPrefix(name, "golang.org/x/") {
if strings.HasPrefix(path, "golang.org/x/") {
return true
}
return false
})
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedName | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo,
}, append([]string{pkg}, deps...)...)
if err != nil {
return err
}

var shaders []Shader

for _, pkg := range pkgs {
shaders = appendShaderSources(shaders, pkg)
}
return true
}, nil)

w := bufio.NewWriter(os.Stdout)
enc := json.NewEncoder(w)
Expand All @@ -99,18 +91,6 @@ func xmain() error {
return nil
}

func listDeps(pkg string) ([]string, error) {
cmd := exec.Command("go", "list", "-f", `{{join .Deps ","}}`, pkg)
out, err := cmd.Output()
if err != nil {
if e, ok := err.(*exec.ExitError); ok {
return nil, fmt.Errorf("go list failed: %w\n%s", e, e.Stderr)
}
return nil, err
}
return strings.Split(strings.TrimSpace(string(out)), ","), nil
}

// isStandardImportPath reports whether $GOROOT/src/path should be considered part of the standard distribution.
//
// This is based on the implementation in the standard library (cmd/go/internal/search/search.go).
Expand Down

0 comments on commit 9b84981

Please sign in to comment.