This commit is contained in:
2026-08-09 19:33:53 +08:00
parent 4d69cebbf9
commit 84f7ae6400
2 changed files with 29 additions and 15 deletions

View File

@@ -1,2 +1,2 @@
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
View File

@@ -3,6 +3,7 @@ package main
import (
"bytes"
"errors"
"flag"
"fmt"
"go/format"
"io"
@@ -22,11 +23,20 @@ import (
var ServicesName []string
func main() {
protogen.Options{}.Run(func(gen *protogen.Plugin) error {
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
// outPath 代码生成根目录,通过 --slc_opt=path=xxx 传入
var outPath string
// 以代码生成根目录为基准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
for _, f := range gen.Files {
serviceCount += len(f.Services)
@@ -35,14 +45,18 @@ func main() {
return nil
}
if !utils.PathExists("./internal") {
os.MkdirAll("./internal", 0755)
internalDir := filepath.Join(outPath, "internal")
serverDir := filepath.Join(internalDir, "server")
logicDir := filepath.Join(internalDir, "logic")
if !utils.PathExists(internalDir) {
os.MkdirAll(internalDir, 0755)
}
if !utils.PathExists("./internal/server") {
os.MkdirAll("./internal/server", 0755)
if !utils.PathExists(serverDir) {
os.MkdirAll(serverDir, 0755)
}
if !utils.PathExists("./internal/logic") {
os.MkdirAll("./internal/logic", 0755)
if !utils.PathExists(logicDir) {
os.MkdirAll(logicDir, 0755)
}
for _, f := range gen.Files {
@@ -106,13 +120,13 @@ func generateNewServerFile(services []string) error {
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
}
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()
//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 {
logicPath := "./internal/logic/" + toSnakeCase(service.GoName)
logicPath := filepath.Join(outPath, "internal", "logic", toSnakeCase(service.GoName))
if !utils.PathExists(logicPath) {
os.MkdirAll(logicPath, os.ModePerm)
}
moduleName := getModuleName()
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) {
continue
}