list.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright © 2019 - 2020 Oscar Campos <oscar.campos@thepimpam.com>
  2. // Copyright © 2017 - William Edwards
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License
  15. package main
  16. import (
  17. "fmt"
  18. "go/ast"
  19. "go/parser"
  20. "go/token"
  21. "os"
  22. "git.alfi.li/gamelang/gdnative-go/gdnative"
  23. )
  24. func (cmd *listCmd) Run(ctx *context) error {
  25. fset := token.NewFileSet()
  26. packages, parseErr := parser.ParseDir(fset, ctx.Path, cmd.filter, parser.ParseComments)
  27. if parseErr != nil {
  28. return fmt.Errorf("could not parse Go files at %s: %w", ctx.Path, parseErr)
  29. }
  30. for pkg, p := range packages {
  31. fmt.Printf("Analyzing package: %s\n", pkg)
  32. gdregistrable := gdnative.LookupRegistrableTypeDeclarations(p)
  33. for key, data := range gdregistrable {
  34. base := data.GetBase()
  35. if base != "" {
  36. base = fmt.Sprintf("(%s)", base)
  37. }
  38. fmt.Printf("Found Structure: %s%s\n", key, base)
  39. properties := data.GetProperties()
  40. if len(properties) > 0 {
  41. fmt.Printf("\tProperties:\n")
  42. }
  43. for _, property := range properties {
  44. fmt.Printf("\t\t%s\n", property)
  45. }
  46. fmt.Printf("\tConstructor:\n\t\t%s\n", data.GetConstructor())
  47. fmt.Printf("\tDestructor:\n\t\t%s\n", data.GetDestructor())
  48. methods := data.GetMethods()
  49. if len(methods) > 0 {
  50. fmt.Printf("\tMethods:\n")
  51. }
  52. for _, method := range methods {
  53. fmt.Printf("\t\t%s\n", method)
  54. }
  55. signals := data.Signals()
  56. if len(signals) > 0 {
  57. fmt.Printf("\tSignals:\n")
  58. }
  59. for _, signal := range data.Signals() {
  60. fmt.Printf("\t\tsignal %s", signal.Name())
  61. }
  62. fmt.Println()
  63. }
  64. if ctx.Verbose {
  65. ast.Print(fset, p)
  66. }
  67. }
  68. return nil
  69. }
  70. func (cmd *listCmd) filter(info os.FileInfo) bool {
  71. if info.IsDir() {
  72. return false
  73. }
  74. length := len(info.Name())
  75. if length > 7 && info.Name()[length-7:length-2] == ".gen." {
  76. return false
  77. }
  78. return true
  79. }