-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
167 lines (144 loc) · 3.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"context"
_ "embed"
"encoding/gob"
"fmt"
"os/exec"
"slices"
"strings"
"cli-plugin-csharp-gen/generate"
hplugin "github.com/hashicorp/go-plugin"
"github.com/ignite/cli/ignite/services/plugin"
)
type Component string
const (
Component_Clients Component = "clients"
Component_Csproj Component = "csproj"
Component_Readme Component = "readme"
Component_Queries Component = "queries"
Component_Client Component = "client"
)
func Component_values() []Component {
return []Component{Component_Clients, Component_Csproj, Component_Readme, Component_Queries, Component_Client}
}
func Component_stringValues() (stringValues []string) {
for _, val := range Component_values() {
stringValues = append(stringValues, string(val))
}
return
}
func getBuildComponents(cmd *plugin.ExecutedCommand) (components []Component, err error) {
flags, err := cmd.NewFlags()
if err != nil {
return nil, err
}
rawComponents, _ := flags.GetStringSlice("components")
if len(rawComponents) == 0 {
return Component_values(), nil
}
for _, comp := range rawComponents {
if !slices.Contains(Component_stringValues(), comp) {
err = fmt.Errorf("buildcomponent '%s' does not exist; options are: [%s]", comp, strings.Join(Component_stringValues(), ", "))
return
}
components = append(components, Component(comp))
}
return
}
const cosmosCsharpPluginVersion = "0.1.0"
func init() {
gob.Register(plugin.Manifest{})
gob.Register(plugin.ExecutedCommand{})
gob.Register(plugin.ExecutedHook{})
}
type p struct{}
func (p) Manifest(context.Context) (*plugin.Manifest, error) {
return &plugin.Manifest{
Name: "csharp",
// Add commands here
Commands: []*plugin.Command{
// Example of a command
{
Use: "csharp-client",
Short: "Generates csharp client",
Long: "Generates csharp client",
Flags: []*plugin.Flag{
{Name: "out", Type: plugin.FlagTypeString, Usage: "csharp output directory"},
{
Name: "components",
Type: plugin.FlagTypeStringSlice,
Usage: fmt.Sprintf(
"components to be generated; options: [%s]",
strings.Join(Component_stringValues(), ", "),
),
},
},
PlaceCommandUnder: "generate",
},
},
// Add hooks here
Hooks: []*plugin.Hook{},
}, nil
}
func (p) Execute(ctx context.Context, cmd *plugin.ExecutedCommand, api plugin.ClientAPI) error {
buildComponents, err := getBuildComponents(cmd)
if err != nil {
return fmt.Errorf("error while getting build components: %s", err.Error())
}
err = installPlugin()
if err != nil {
return err
}
gen, err := generate.New(cmd)
if err != nil {
return err
}
var componentRegister = map[Component]func() error{
Component_Csproj: gen.GenerateCsproj,
Component_Readme: gen.GenerateReadme,
Component_Clients: func() error {
return gen.GenerateClients(ctx)
},
Component_Queries: func() error {
return gen.GenerateQueries(ctx)
},
Component_Client: func() error {
return gen.GenerateClient(ctx)
},
}
for _, comp := range buildComponents {
err = componentRegister[comp]()
if err != nil {
return err
}
}
return gen.Build()
}
func installPlugin() error {
fmt.Println("Installing protoc plugin...")
cmd := exec.Command(
"sh",
"-c",
fmt.Sprintf("go install \"github.com/DecentralCardgame/protoc-gen-cosmosCsharp@v%s\"", cosmosCsharpPluginVersion),
)
return cmd.Run()
}
func (p) ExecuteHookPre(_ context.Context, hook *plugin.ExecutedHook, api plugin.ClientAPI) error {
return nil
}
func (p) ExecuteHookPost(_ context.Context, hook *plugin.ExecutedHook, api plugin.ClientAPI) error {
return nil
}
func (p) ExecuteHookCleanUp(_ context.Context, hook *plugin.ExecutedHook, api plugin.ClientAPI) error {
return nil
}
func main() {
hplugin.Serve(&hplugin.ServeConfig{
HandshakeConfig: plugin.HandshakeConfig(),
Plugins: map[string]hplugin.Plugin{
"cli-plugin-csharp-gen": plugin.NewGRPC(&p{}),
},
GRPCServer: hplugin.DefaultGRPCServer,
})
}