This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
/
gen-proto.go
90 lines (81 loc) · 2.11 KB
/
gen-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
package cmd
import (
"context"
"fmt"
"github.com/gobuffalo/genny/v2"
"github.com/ronaksoft/rony/config"
"github.com/ronaksoft/rony/log"
"github.com/spf13/cobra"
"os"
"os/exec"
"path/filepath"
)
/*
Creation Time: 2020 - Aug - 28
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
var GenProtoCmd = &cobra.Command{
Use: "gen-proto",
RunE: func(cmd *cobra.Command, args []string) error {
r := genny.WetRunner(context.Background())
if config.GetBool("dry-run") {
r = genny.DryRunner(context.Background())
}
r.Logger = log.DefaultLogger.Sugared()
g := genny.New()
compileProto(g)
gofmt(g)
goModuleTidy(g)
goModuleVendor(g)
// Create a Runner with the Generator customized by command's arguments
err := r.With(g)
if err != nil {
return err
}
return r.Run()
},
}
func compileProto(g *genny.Generator) {
// Compile proto files
folders := []string{"rpc", "model"}
for _, f := range folders {
_ = filepath.Walk(filepath.Join(".", f), func(path string, info os.FileInfo, err error) error {
if info == nil || info.IsDir() {
return nil
}
if filepath.Ext(info.Name()) == ".proto" {
projectPathAbs, _ := filepath.Abs(".")
folderPathAbs, _ := filepath.Abs(filepath.Dir(path))
// call protoc-gen-go
cmd1 := exec.Command(
"protoc",
fmt.Sprintf("-I=%s", projectPathAbs),
fmt.Sprintf("-I=%s", folderPathAbs),
fmt.Sprintf("-I=%s/vendor", projectPathAbs),
fmt.Sprintf("--go_out=paths=source_relative:%s", projectPathAbs),
path,
)
cmd1.Env = os.Environ()
cmd1.Dir = filepath.Dir(projectPathAbs)
g.Command(cmd1)
// generate protoc-gen-gorony
cmd2 := exec.Command(
"protoc",
fmt.Sprintf("-I=%s", projectPathAbs),
fmt.Sprintf("-I=%s", folderPathAbs),
fmt.Sprintf("-I=%s/vendor", projectPathAbs),
fmt.Sprintf("--gorony_out=paths=source_relative,plugin=server:%s", projectPathAbs),
path,
)
cmd2.Env = os.Environ()
cmd2.Dir = filepath.Dir(projectPathAbs)
g.Command(cmd2)
}
return nil
})
}
}