This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
export-proto.go
144 lines (128 loc) · 3.53 KB
/
export-proto.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
package cmd
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/gobuffalo/genny/v2"
"github.com/ronaksoft/rony/config"
"github.com/ronaksoft/rony/internal/codegen"
"github.com/ronaksoft/rony/log"
"github.com/spf13/cobra"
)
const (
protoExt = ".proto"
)
var ExportProtoCmd = &cobra.Command{
Use: "export-proto",
RunE: func(cmd *cobra.Command, args []string) error {
err := config.BindCmdFlags(cmd)
if err != nil {
return err
}
r := genny.WetRunner(context.Background())
if config.GetBool("dry-run") {
r = genny.DryRunner(context.Background())
}
r.Logger = log.DefaultLogger.Sugared()
if len(args) == 0 {
args = append(args, "rpc", "model")
}
cFormat := codegen.ConstructorFormat(config.GetString("c.format"))
switch cFormat {
case codegen.Int64JSON, codegen.StringJSON:
default:
cFormat = codegen.StringJSON
}
cmd.Println("Folders:", args)
cmd.Println("Constructor Format:", cFormat)
g := genny.New()
exportProto(g, args, cFormat)
// Create a Runner with the Generator customized by command's arguments
err = r.With(g)
if err != nil {
return err
}
return r.Run()
},
}
func exportProto(g *genny.Generator, folders []string, cFormat codegen.ConstructorFormat) {
var (
files []string
projectPathAbs, _ = filepath.Abs(".")
folderPathAbs string
)
_ = os.MkdirAll(filepath.Join(projectPathAbs, "exports/proto"), os.ModePerm|0755)
for _, folder := range folders {
files = files[:0]
folderPathAbs, _ = filepath.Abs(filepath.Join(".", folder))
_ = filepath.Walk(filepath.Join(".", folder), func(path string, info os.FileInfo, err error) error {
if info == nil || info.IsDir() {
return nil
}
if filepath.Ext(info.Name()) == protoExt {
files = append(files, path)
}
return nil
})
// generate protoc-gen-gorony (Clean Proto Files)
args1 := []string{
fmt.Sprintf("-I=%s/src", os.Getenv("GOPATH")),
fmt.Sprintf("-I=%s", projectPathAbs),
fmt.Sprintf("-I=%s", folderPathAbs),
fmt.Sprintf("-I=%s/vendor", projectPathAbs),
fmt.Sprintf(
"--gorony_out=paths=source_relative,rony_opt=clean_proto:%s",
filepath.Join(projectPathAbs, "exports/proto"),
),
}
args1 = append(args1, files...)
cmd1 := exec.Command(
"protoc", args1...,
)
cmd1.Env = os.Environ()
cmd1.Dir = filepath.Dir(projectPathAbs)
cmd1.Stderr = os.Stderr
g.Command(cmd1)
// generate protoc-gen-gorony (Constructors)
args2 := []string{
fmt.Sprintf("-I=%s/src", os.Getenv("GOPATH")),
fmt.Sprintf("-I=%s", projectPathAbs),
fmt.Sprintf("-I=%s", folderPathAbs),
fmt.Sprintf("-I=%s/vendor", projectPathAbs),
fmt.Sprintf(
"--gorony_out=paths=source_relative,rony_opt=json_%s:%s",
cFormat,
filepath.Join(projectPathAbs, "exports/proto"),
),
}
args2 = append(args2, files...)
cmd2 := exec.Command(
"protoc", args2...,
)
cmd2.Env = os.Environ()
cmd2.Dir = filepath.Dir(projectPathAbs)
cmd2.Stderr = os.Stderr
g.Command(cmd2)
// generate protoc-gen-gorony (Swagger APIs)
args3 := []string{
fmt.Sprintf("-I=%s/src", os.Getenv("GOPATH")),
fmt.Sprintf("-I=%s", projectPathAbs),
fmt.Sprintf("-I=%s", folderPathAbs),
fmt.Sprintf("-I=%s/vendor", projectPathAbs),
fmt.Sprintf(
"--gorony_out=paths=source_relative,rony_opt=open_api:%s",
filepath.Join(projectPathAbs, "exports/proto"),
),
}
args3 = append(args3, files...)
cmd3 := exec.Command(
"protoc", args3...,
)
cmd3.Env = os.Environ()
cmd3.Dir = filepath.Dir(projectPathAbs)
cmd3.Stderr = os.Stderr
g.Command(cmd3)
}
}