diff --git a/cmd/jzero/internal/command/gen/genswagger/genswagger.go b/cmd/jzero/internal/command/gen/genswagger/genswagger.go index 525918613..f4706a1ce 100644 --- a/cmd/jzero/internal/command/gen/genswagger/genswagger.go +++ b/cmd/jzero/internal/command/gen/genswagger/genswagger.go @@ -108,17 +108,18 @@ func Gen() (err error) { pluginName := getPluginNameFromFilePath(v) if pluginName != "" { // 插件文件处理:找到 desc/api 在路径中的位置 - descApiIndex := strings.Index(v, "/desc/api/") + descApiPath := filepath.Join("desc", "api") + string(filepath.Separator) + descApiIndex := strings.Index(v, descApiPath) var pluginApiDir string if descApiIndex == -1 { - // 如果找不到 /desc/api/ 模式,尝试查找路径末尾是否以 desc/api 结尾 - if strings.HasSuffix(filepath.Dir(v), "/desc/api") { + // 如果找不到 desc/api 模式,尝试查找路径末尾是否以 desc/api 结尾 + if strings.HasSuffix(filepath.Dir(v), filepath.Join("desc", "api")) { pluginApiDir = filepath.Dir(v) } else { return fmt.Errorf("invalid plugin api path: %s", v) } } else { - pluginApiDir = v[:descApiIndex+len("/desc/api")] + pluginApiDir = v[:descApiIndex+len(descApiPath)] } var relErr error @@ -127,7 +128,7 @@ func Gen() (err error) { return relErr } // 在插件目录下保持结构 - relPath = "plugins/" + pluginName + "/" + relPath + relPath = filepath.Join("plugins", pluginName, relPath) } else { // 普通 API 文件处理 relPath, err = filepath.Rel(config.C.ApiDir(), v) diff --git a/cmd/jzero/internal/desc/desc.go b/cmd/jzero/internal/desc/desc.go index ce043ffde..6d8c648e2 100644 --- a/cmd/jzero/internal/desc/desc.go +++ b/cmd/jzero/internal/desc/desc.go @@ -31,23 +31,28 @@ func GetFrameType() (string, error) { // rpc 项目 frameType = "rpc" - // 获取全量 proto 文件 - protoFiles, err := FindRpcServiceProtoFiles(config.C.ProtoDir()) - if err != nil { - return "", err - } - - for _, v := range protoFiles { - // parse proto - protoParser := rpcparser.NewDefaultProtoParser() - var parse rpcparser.Proto - parse, err = protoParser.Parse(v, true) + // 检查是否是 gateway 项目(优先检查 cmd/server.go) + if isGatewayProject() { + frameType = "gateway" + } else { + // 获取全量 proto 文件 + protoFiles, err := FindRpcServiceProtoFiles(config.C.ProtoDir()) if err != nil { return "", err } - if IsNeedGenProtoDescriptor(parse) { - frameType = "gateway" - break + + for _, v := range protoFiles { + // parse proto + protoParser := rpcparser.NewDefaultProtoParser() + var parse rpcparser.Proto + parse, err = protoParser.Parse(v, true) + if err != nil { + return "", err + } + if IsNeedGenProtoDescriptor(parse) { + frameType = "gateway" + break + } } } } @@ -55,6 +60,15 @@ func GetFrameType() (string, error) { return frameType, nil } +// isGatewayProject 检查 third_party/grpc-gateway 目录是否存在 +func isGatewayProject() bool { + grpcGatewayPath := filepath.Join(config.C.ProtoDir(), "third_party", "grpc-gateway") + if _, err := os.Stat(grpcGatewayPath); err == nil { + return true + } + return false +} + func GetProtoDescriptorPath(protoPath string) string { rel, err := filepath.Rel(filepath.Join("desc", "proto"), protoPath) if err != nil {