From 84f7ae64001b9b4c99f9ed2f65e85ff23ecaa76b Mon Sep 17 00:00:00 2001 From: yanweidong Date: Sun, 9 Aug 2026 19:33:53 +0800 Subject: [PATCH] fix bug --- build.sh | 2 +- main.go | 42 ++++++++++++++++++++++++++++-------------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/build.sh b/build.sh index b7961f3..a994047 100644 --- a/build.sh +++ b/build.sh @@ -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 \ No newline at end of file +protoc ./proto/*.proto --go_out=./pb --go-grpc_out=./pb --proto_path=./proto --slc_opt=path=./slc --slc_out=. \ No newline at end of file diff --git a/main.go b/main.go index f2666f2..2297383 100644 --- a/main.go +++ b/main.go @@ -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 }