-
Notifications
You must be signed in to change notification settings - Fork 8
feat: component manager generator refactor: remove unused component managers and update component references #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bitver
wants to merge
1
commit into
rodd-oss:new-api
Choose a base branch
from
bitver:new-api
base: new-api
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| // component.go: Go code generator for ECS component managers | ||
| // Usage: go run component.go | ||
| // Scans for structs with '//go:generate go tool component' and generates ids.go and managers.go | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "go/ast" | ||
| "go/parser" | ||
| "go/token" | ||
| "os" | ||
| "path/filepath" | ||
| "regexp" | ||
| "strings" | ||
| ) | ||
|
|
||
| // Accepts: -shared, -std, -example in any combination/order | ||
| var ( | ||
| generateRe = regexp.MustCompile(`//go:generate go tool component((?: -[a-z]+)*)`) | ||
| ) | ||
|
|
||
| type Component struct { | ||
| StructName string | ||
| File string | ||
| Shared bool | ||
| Std bool | ||
| Example bool | ||
| } | ||
|
|
||
| func main() { | ||
| _, err := os.Getwd() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Failed to get working directory: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| files, err := filepath.Glob("*.go") | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Failed to list Go files: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| var components []Component | ||
| for _, file := range files { | ||
| if strings.HasSuffix(file, "_test.go") { | ||
| continue | ||
| } | ||
| comps := processFile(file) | ||
| components = append(components, comps...) | ||
| } | ||
|
|
||
| if len(components) > 0 { | ||
| generateIdsGo(components) | ||
| generateManagersGo(components) | ||
| } | ||
| } | ||
|
|
||
| // processFile parses a Go file and returns all components with a go:generate comment. | ||
| func processFile(filename string) []Component { | ||
| fset := token.NewFileSet() | ||
| fileAst, err := parser.ParseFile(fset, filename, nil, parser.ParseComments) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Failed to parse %s: %v\n", filename, err) | ||
| return nil | ||
| } | ||
|
|
||
| // Map line number to comment text | ||
| comments := map[int]string{} | ||
| for _, cg := range fileAst.Comments { | ||
| for _, c := range cg.List { | ||
| comments[fset.Position(c.Pos()).Line] = c.Text | ||
| } | ||
| } | ||
|
|
||
| var components []Component | ||
| for _, decl := range fileAst.Decls { | ||
| gd, ok := decl.(*ast.GenDecl) | ||
| if !ok || gd.Tok != token.TYPE { | ||
| continue | ||
| } | ||
| for _, spec := range gd.Specs { | ||
| ts, ok := spec.(*ast.TypeSpec) | ||
| if !ok { | ||
| continue | ||
| } | ||
| // Accept both struct and type alias | ||
| if _, ok := ts.Type.(*ast.StructType); !ok { | ||
| if _, ok := ts.Type.(*ast.Ident); !ok { | ||
| continue | ||
| } | ||
| } | ||
| // Check for go:generate comment immediately above | ||
| line := fset.Position(gd.Pos()).Line - 1 | ||
| comment, ok := comments[line] | ||
| if !ok { | ||
| continue | ||
| } | ||
| matches := generateRe.FindStringSubmatch(comment) | ||
| if matches == nil { | ||
| continue | ||
| } | ||
| flags := matches[1] | ||
| // Split flags by space and check for each | ||
| flagSet := map[string]bool{} | ||
| for _, f := range strings.Fields(flags) { | ||
| flagSet[f] = true | ||
| } | ||
| components = append(components, Component{ | ||
| StructName: ts.Name.Name, | ||
| File: filename, | ||
| Shared: flagSet["-shared"], | ||
| Std: flagSet["-std"], | ||
| Example: flagSet["-example"], | ||
| }) | ||
| } | ||
| } | ||
| return components | ||
| } | ||
|
|
||
| // generateIdsGo creates ids.go for the detected components. | ||
| func generateIdsGo(components []Component) { | ||
| if len(components) == 0 { | ||
| return | ||
| } | ||
| pkgName := getPackageName(components[0].File) | ||
| var buf bytes.Buffer | ||
| buf.WriteString("// Code generated by component generator; DO NOT EDIT.\n") | ||
| buf.WriteString("\npackage " + pkgName + "\n\n") | ||
| buf.WriteString("import (\n") | ||
| if anyExample(components) { | ||
| buf.WriteString("\t\"gomp/stdcomponents\"\n") | ||
| } else { | ||
| buf.WriteString("\t\"gomp/pkg/ecs\"\n") | ||
| } | ||
| buf.WriteString(")\n\n") | ||
| buf.WriteString("const (\n") | ||
| if components[0].Example { | ||
| buf.WriteString("\t" + components[0].StructName + "ComponentId = iota + stdcomponents.StdLastComponentId\n") | ||
| for i := 1; i < len(components); i++ { | ||
| buf.WriteString("\t" + components[i].StructName + "ComponentId\n") | ||
| } | ||
| } else { | ||
| buf.WriteString("\tInvalidComponentId ecs.ComponentId = iota\n") | ||
| for _, c := range components { | ||
| buf.WriteString("\t" + c.StructName + "ComponentId\n") | ||
| } | ||
| buf.WriteString("\tStdLastComponentId\n") | ||
| } | ||
| buf.WriteString(")\n\n") | ||
| _ = os.WriteFile("ids.go", buf.Bytes(), 0644) | ||
| } | ||
|
|
||
| // getPackageName returns the package name for a Go file. | ||
| func getPackageName(filename string) string { | ||
| fset := token.NewFileSet() | ||
| fileAst, err := parser.ParseFile(fset, filename, nil, parser.PackageClauseOnly) | ||
| if err != nil { | ||
| return "main" | ||
| } | ||
| return fileAst.Name.Name | ||
| } | ||
|
|
||
| // anyExample returns true if any component is marked as -example. | ||
| func anyExample(components []Component) bool { | ||
| for _, c := range components { | ||
| if c.Example { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // generateManagersGo creates managers.go for the detected components. | ||
| func generateManagersGo(components []Component) { | ||
| if len(components) == 0 { | ||
| return | ||
| } | ||
| pkgName := getPackageName(components[0].File) | ||
| var buf bytes.Buffer | ||
| buf.WriteString("// Code generated by componentgen; DO NOT EDIT.\n") | ||
| buf.WriteString("\npackage " + pkgName + "\n\n") | ||
| buf.WriteString("import (\n\t\"gomp/pkg/ecs\"\n)\n\n") | ||
|
|
||
| for _, c := range components { | ||
| if c.Shared { | ||
| buf.WriteString(fmt.Sprintf("type %[1]sComponentManager = ecs.SharedComponentManager[%[1]s]\n\n", c.StructName)) | ||
| buf.WriteString(fmt.Sprintf("func New%[1]sComponentManager() %[1]sComponentManager {\n\treturn ecs.NewSharedComponentManager[%[1]s](%[1]sComponentId)\n}\n\n", c.StructName)) | ||
| } else { | ||
| buf.WriteString(fmt.Sprintf("type %[1]sComponentManager = ecs.ComponentManager[%[1]s]\n\n", c.StructName)) | ||
| buf.WriteString(fmt.Sprintf("func New%[1]sComponentManager() %[1]sComponentManager {\n\treturn ecs.NewComponentManager[%[1]s](%[1]sComponentId)\n}\n\n", c.StructName)) | ||
| } | ||
| } | ||
| _ = os.WriteFile("managers.go", buf.Bytes(), 0644) | ||
Check failureCode scanning / gosec Expect WriteFile permissions to be 0600 or less
Expect WriteFile permissions to be 0600 or less
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / gosec
Expect WriteFile permissions to be 0600 or less