Skip to content

Commit fe3906a

Browse files
author
silaswei
committed
fix: Fix the anomalies of gen swagger and mono modes
1 parent b8c9fc1 commit fe3906a

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

cmd/jzero/internal/command/gen/genswagger/genswagger.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,18 @@ func Gen() (err error) {
108108
pluginName := getPluginNameFromFilePath(v)
109109
if pluginName != "" {
110110
// 插件文件处理:找到 desc/api 在路径中的位置
111-
descApiIndex := strings.Index(v, "/desc/api/")
111+
descApiPath := filepath.Join("desc", "api") + string(filepath.Separator)
112+
descApiIndex := strings.Index(v, descApiPath)
112113
var pluginApiDir string
113114
if descApiIndex == -1 {
114-
// 如果找不到 /desc/api/ 模式,尝试查找路径末尾是否以 desc/api 结尾
115-
if strings.HasSuffix(filepath.Dir(v), "/desc/api") {
115+
// 如果找不到 desc/api 模式,尝试查找路径末尾是否以 desc/api 结尾
116+
if strings.HasSuffix(filepath.Dir(v), filepath.Join("desc", "api")) {
116117
pluginApiDir = filepath.Dir(v)
117118
} else {
118119
return fmt.Errorf("invalid plugin api path: %s", v)
119120
}
120121
} else {
121-
pluginApiDir = v[:descApiIndex+len("/desc/api")]
122+
pluginApiDir = v[:descApiIndex+len(descApiPath)]
122123
}
123124

124125
var relErr error
@@ -127,7 +128,7 @@ func Gen() (err error) {
127128
return relErr
128129
}
129130
// 在插件目录下保持结构
130-
relPath = "plugins/" + pluginName + "/" + relPath
131+
relPath = filepath.Join("plugins", pluginName, relPath)
131132
} else {
132133
// 普通 API 文件处理
133134
relPath, err = filepath.Rel(config.C.ApiDir(), v)

cmd/jzero/internal/desc/desc.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,47 @@ func GetFrameType() (string, error) {
3131
// rpc 项目
3232
frameType = "rpc"
3333

34-
// 获取全量 proto 文件
35-
protoFiles, err := FindRpcServiceProtoFiles(config.C.ProtoDir())
36-
if err != nil {
37-
return "", err
38-
}
39-
40-
for _, v := range protoFiles {
41-
// parse proto
42-
protoParser := rpcparser.NewDefaultProtoParser()
43-
var parse rpcparser.Proto
44-
parse, err = protoParser.Parse(v, true)
34+
// 检查是否是 gateway 项目(优先检查 cmd/server.go)
35+
if isGatewayProject() {
36+
frameType = "gateway"
37+
} else {
38+
// 获取全量 proto 文件
39+
protoFiles, err := FindRpcServiceProtoFiles(config.C.ProtoDir())
4540
if err != nil {
4641
return "", err
4742
}
48-
if IsNeedGenProtoDescriptor(parse) {
49-
frameType = "gateway"
50-
break
43+
44+
for _, v := range protoFiles {
45+
// parse proto
46+
protoParser := rpcparser.NewDefaultProtoParser()
47+
var parse rpcparser.Proto
48+
parse, err = protoParser.Parse(v, true)
49+
if err != nil {
50+
return "", err
51+
}
52+
if IsNeedGenProtoDescriptor(parse) {
53+
frameType = "gateway"
54+
break
55+
}
5156
}
5257
}
5358
}
5459

5560
return frameType, nil
5661
}
5762

63+
// isGatewayProject 检查是否是 gateway 项目,通过检查 cmd/server.go 是否导入 gateway 包
64+
func isGatewayProject() bool {
65+
serverGoPath := filepath.Join("cmd", "server.go")
66+
content, err := os.ReadFile(serverGoPath)
67+
if err != nil {
68+
return false
69+
}
70+
contentStr := string(content)
71+
return strings.Contains(contentStr, `"github.com/zeromicro/go-zero/gateway"`) ||
72+
strings.Contains(contentStr, `github.com/zeromicro/go-zero/gateway`)
73+
}
74+
5875
func GetProtoDescriptorPath(protoPath string) string {
5976
rel, err := filepath.Rel(filepath.Join("desc", "proto"), protoPath)
6077
if err != nil {

0 commit comments

Comments
 (0)