-
Notifications
You must be signed in to change notification settings - Fork 52
/
generate.go
112 lines (90 loc) · 2.84 KB
/
generate.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
package proto
import (
"fmt"
"bytes"
"github.com/commitdev/sprout/util"
"github.com/commitdev/sprout/config"
"github.com/commitdev/sprout/templator"
"log"
"os"
"os/exec"
)
func Generate(templator *templator.Templator, config *config.SproutConfig) {
GenerateIDLMakefile(templator, config)
GenerateProtoHealth(templator, config)
GenerateServiceProtobufFiles(templator, config)
GenerateProtoServiceLibs(config)
GenerateGoModIDL(templator, config)
}
func GenerateGoModIDL(templator *templator.Templator, config *config.SproutConfig) {
idlPath := fmt.Sprintf("%s-idl", config.Name)
idlOutput := fmt.Sprintf("%s/go.mod", idlPath)
err := util.CreateDirIfDoesNotExist(idlPath)
f, err := os.Create(idlOutput)
if err != nil {
log.Printf("Error: %v", err)
}
templator.Go.GoModIDL.Execute(f, config)
}
func GenerateIDLMakefile(templator *templator.Templator, config *config.SproutConfig) {
makeFilePath := fmt.Sprintf("%s-idl", config.Name)
makeFileOutput := fmt.Sprintf("%s/Makefile", makeFilePath)
err := util.CreateDirIfDoesNotExist(makeFilePath)
if err != nil {
log.Printf("Error generating prototool config: %v", err)
}
f, err := os.Create(makeFileOutput)
if err != nil {
log.Printf("Error: %v", err)
}
templator.MakefileTemplate.Execute(f, config)
}
func GenerateProtoHealth(templator *templator.Templator, config *config.SproutConfig) {
protoHealthPath := fmt.Sprintf("%s-idl/proto/health", config.Name)
protoHealthOutput := fmt.Sprintf("%s/health.proto", protoHealthPath)
err := util.CreateDirIfDoesNotExist(protoHealthPath)
if err != nil {
log.Printf("Error: %v", err)
}
f, err := os.Create(protoHealthOutput)
if err != nil {
log.Printf("Error: %v", err)
}
templator.ProtoHealthTemplate.Execute(f, config)
}
func GenerateServiceProtobufFiles(templator *templator.Templator, cfg *config.SproutConfig) {
protoPath := fmt.Sprintf("%s-idl/proto", cfg.Name)
for _, s := range cfg.Services {
serviceProtoDir := fmt.Sprintf("%s/%s", protoPath, s.Name)
err := os.Mkdir(serviceProtoDir, os.ModePerm)
if os.IsExist(err) {
log.Printf("%s service proto exists skipping.", s.Name)
continue
}
serviceProtoFilePath := fmt.Sprintf("%s/%s.proto", serviceProtoDir, s.Name)
f, err := os.Create(serviceProtoFilePath)
data:= struct {
*config.SproutConfig
ServiceName string
} {
cfg,
s.Name,
}
templator.ProtoServiceTemplate.Execute(f, data)
}
}
func GenerateProtoServiceLibs(config *config.SproutConfig) {
idlRoot := fmt.Sprintf("%s-idl", config.Name)
cmd := exec.Command("make", "generate")
cmd.Dir = idlRoot
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
log.Print("Generating proto service libs...")
if err != nil {
log.Printf("Failed running command in: %v", cmd.Dir)
log.Printf("Error executing protoc generation: %v %v", err, stderr.String())
}
}