GitRoot
Craft your forge, Build your project, Grow your community freely
1// SPDX-FileCopyrightText: 2026 Romain Maneschi <romain@gitroot.dev>
2//
3// SPDX-License-Identifier: EUPL-1.2
4
5package main
6
7import (
8 "go/ast"
9 "go/types"
10)
11
12const header = `// SPDX-FileCopyrightText: 2026 Romain Maneschi <romain@gitroot.dev>
13//
14// SPDX-License-Identifier: MIT
15
16`
17
18type Generate struct {
19 fromDir string
20 files []*FileToGen
21 structLocations map[string]string
22}
23
24type FileToGen struct {
25 OriginFilePath string
26 Structs []Struct
27 Enums []Enum
28 Funcs []Func
29 Methods []Method
30}
31
32type Struct struct {
33 TypeName string
34 Doc *ast.CommentGroup
35 Fields []*types.Var
36}
37
38type Enum struct {
39 TypeName string
40 TypeType string
41 Values []string
42}
43
44type WasmDirection string
45
46const (
47 WasmNone WasmDirection = ""
48 WasmExport WasmDirection = "export"
49 WasmImport WasmDirection = "import"
50)
51
52type Func struct {
53 Name string
54 Doc *ast.CommentGroup
55 Sig *types.Signature
56 Wasm WasmDirection
57}
58
59type Method struct {
60 StructName string
61 Name string
62 Doc *ast.CommentGroup
63 Sig *types.Signature
64 Wasm WasmDirection
65}
66
67func NewGeneration(fromDir string) *Generate {
68 return &Generate{
69 fromDir: fromDir,
70 files: []*FileToGen{},
71 structLocations: make(map[string]string),
72 }
73}
74
75func (g *Generate) getOrCreateFile(path string) *FileToGen {
76 for _, f := range g.files {
77 if f.OriginFilePath == path {
78 return f
79 }
80 }
81 newFile := &FileToGen{
82 OriginFilePath: path,
83 }
84 g.files = append(g.files, newFile)
85 return newFile
86}
87
88func (g *Generate) Execute(toDir string, callback func(filepath string, filecontent string)) error {
89 for _, fileToGen := range g.files {
90 if len(fileToGen.Structs) == 0 && len(fileToGen.Funcs) == 0 && len(fileToGen.Methods) == 0 {
91 continue
92 }
93 if filepath, filecontent, err := g.generateFile(fileToGen, Rust{}, toDir+"/rust/plugin/"); err != nil {
94 return err
95 } else {
96 callback(filepath, filecontent)
97 }
98 if filepath, filecontent, err := g.generateFile(fileToGen, Ts{}, toDir+"/ts/plugin/"); err != nil {
99 return err
100 } else {
101 callback(filepath, filecontent)
102 }
103 }
104 return nil
105}