fix bug
This commit is contained in:
2
build.sh
2
build.sh
@@ -1,2 +1,2 @@
|
|||||||
go build -o D:\\devapps\\Go\\bin\\protoc-gen-slc.exe main.go
|
go build -o D:\\devapps\\Go\\bin\\protoc-gen-slc.exe main.go
|
||||||
protoc ./proto/*.proto --go_out=./pb --go-grpc_out=./pb --proto_path=./proto --slc_out=./slc
|
protoc ./proto/*.proto --go_out=./pb --go-grpc_out=./pb --proto_path=./proto --slc_opt=path=./slc --slc_out=.
|
||||||
42
main.go
42
main.go
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/format"
|
"go/format"
|
||||||
"io"
|
"io"
|
||||||
@@ -22,11 +23,20 @@ import (
|
|||||||
|
|
||||||
var ServicesName []string
|
var ServicesName []string
|
||||||
|
|
||||||
func main() {
|
// outPath 代码生成根目录,通过 --slc_opt=path=xxx 传入
|
||||||
protogen.Options{}.Run(func(gen *protogen.Plugin) error {
|
var outPath string
|
||||||
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
|
|
||||||
|
|
||||||
// 以代码生成根目录为基准;gen.Files 无 service 时不创建 internal/{server,logic}
|
func main() {
|
||||||
|
var flags flag.FlagSet
|
||||||
|
path := flags.String("path", ".", "code generation root directory")
|
||||||
|
|
||||||
|
protogen.Options{
|
||||||
|
ParamFunc: flags.Set,
|
||||||
|
}.Run(func(gen *protogen.Plugin) error {
|
||||||
|
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
|
||||||
|
outPath = *path
|
||||||
|
|
||||||
|
// gen.Files 无 service 时不创建 internal/{server,logic}
|
||||||
serviceCount := 0
|
serviceCount := 0
|
||||||
for _, f := range gen.Files {
|
for _, f := range gen.Files {
|
||||||
serviceCount += len(f.Services)
|
serviceCount += len(f.Services)
|
||||||
@@ -35,14 +45,18 @@ func main() {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !utils.PathExists("./internal") {
|
internalDir := filepath.Join(outPath, "internal")
|
||||||
os.MkdirAll("./internal", 0755)
|
serverDir := filepath.Join(internalDir, "server")
|
||||||
|
logicDir := filepath.Join(internalDir, "logic")
|
||||||
|
|
||||||
|
if !utils.PathExists(internalDir) {
|
||||||
|
os.MkdirAll(internalDir, 0755)
|
||||||
}
|
}
|
||||||
if !utils.PathExists("./internal/server") {
|
if !utils.PathExists(serverDir) {
|
||||||
os.MkdirAll("./internal/server", 0755)
|
os.MkdirAll(serverDir, 0755)
|
||||||
}
|
}
|
||||||
if !utils.PathExists("./internal/logic") {
|
if !utils.PathExists(logicDir) {
|
||||||
os.MkdirAll("./internal/logic", 0755)
|
os.MkdirAll(logicDir, 0755)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, f := range gen.Files {
|
for _, f := range gen.Files {
|
||||||
@@ -106,13 +120,13 @@ func generateNewServerFile(services []string) error {
|
|||||||
return fmt.Errorf("failed to format generated code: %w", err)
|
return fmt.Errorf("failed to format generated code: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
StringToFile("./internal/server/new.go", string(formattedCode))
|
StringToFile(filepath.Join(outPath, "internal", "server", "new.go"), string(formattedCode))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateServerFile(gen *protogen.Plugin, file *protogen.File, service *protogen.Service) error {
|
func generateServerFile(gen *protogen.Plugin, file *protogen.File, service *protogen.Service) error {
|
||||||
filename := fmt.Sprintf("./internal/server/%s_server.go", toSnakeCase(service.GoName))
|
filename := filepath.Join(outPath, "internal", "server", toSnakeCase(service.GoName)+"_server.go")
|
||||||
moduleName := getModuleName()
|
moduleName := getModuleName()
|
||||||
|
|
||||||
//create servers.
|
//create servers.
|
||||||
@@ -151,13 +165,13 @@ func generateServerFile(gen *protogen.Plugin, file *protogen.File, service *prot
|
|||||||
}
|
}
|
||||||
|
|
||||||
func generateLogicFile(gen *protogen.Plugin, file *protogen.File, service *protogen.Service) error {
|
func generateLogicFile(gen *protogen.Plugin, file *protogen.File, service *protogen.Service) error {
|
||||||
logicPath := "./internal/logic/" + toSnakeCase(service.GoName)
|
logicPath := filepath.Join(outPath, "internal", "logic", toSnakeCase(service.GoName))
|
||||||
if !utils.PathExists(logicPath) {
|
if !utils.PathExists(logicPath) {
|
||||||
os.MkdirAll(logicPath, os.ModePerm)
|
os.MkdirAll(logicPath, os.ModePerm)
|
||||||
}
|
}
|
||||||
moduleName := getModuleName()
|
moduleName := getModuleName()
|
||||||
for _, method := range service.Methods {
|
for _, method := range service.Methods {
|
||||||
filename := fmt.Sprintf("%s/%s.go", logicPath, toSnakeCase(method.GoName))
|
filename := filepath.Join(logicPath, toSnakeCase(method.GoName)+".go")
|
||||||
if utils.PathExists(filename) {
|
if utils.PathExists(filename) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user