plugin_initialize_v2.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package ast
  2. import (
  3. "fmt"
  4. "go/ast"
  5. "io"
  6. )
  7. type PluginInitializeV2 struct {
  8. Base
  9. Type Type // 类型
  10. Path string // 文件路径
  11. PluginPath string // 插件路径
  12. RelativePath string // 相对路径
  13. ImportPath string // 导包路径
  14. StructName string // 结构体名称
  15. PackageName string // 包名
  16. }
  17. func (a *PluginInitializeV2) Parse(filename string, writer io.Writer) (file *ast.File, err error) {
  18. if filename == "" {
  19. if a.RelativePath == "" {
  20. filename = a.PluginPath
  21. a.RelativePath = a.Base.RelativePath(a.PluginPath)
  22. return a.Base.Parse(filename, writer)
  23. }
  24. a.PluginPath = a.Base.AbsolutePath(a.RelativePath)
  25. filename = a.PluginPath
  26. }
  27. return a.Base.Parse(filename, writer)
  28. }
  29. func (a *PluginInitializeV2) Injection(file *ast.File) error {
  30. if !CheckImport(file, a.ImportPath) {
  31. NewImport(a.ImportPath).Injection(file)
  32. funcDecl := FindFunction(file, "bizPluginV2")
  33. stmt := CreateStmt(fmt.Sprintf("PluginInitV2(engine, %s.Plugin)", a.PackageName))
  34. funcDecl.Body.List = append(funcDecl.Body.List, stmt)
  35. }
  36. return nil
  37. }
  38. func (a *PluginInitializeV2) Rollback(file *ast.File) error {
  39. return nil
  40. }
  41. func (a *PluginInitializeV2) Format(filename string, writer io.Writer, file *ast.File) error {
  42. if filename == "" {
  43. filename = a.PluginPath
  44. }
  45. return a.Base.Format(filename, writer, file)
  46. }