From 0f97b2019ac6cae589f4338d446713ee59c91154 Mon Sep 17 00:00:00 2001 From: Keson Date: Sat, 29 Aug 2020 14:30:17 +0800 Subject: [PATCH] rpc generation support windows (#28) * add execute files * add protoc-osx * add rpc generation * add rpc generation * add: rpc template generation * update usage * fixed env prepare for project in go path * optimize gomod cache * add README.md * format error * reactor templatex.go * remove waste code * update project.go & README.md * update project.go & README.md * rpc generation supports windows --- tools/goctl/rpc/CHANGELOG.md | 3 +++ tools/goctl/rpc/README.md | 3 --- tools/goctl/rpc/ctx/ctx.go | 4 +--- tools/goctl/rpc/gen/gendir.go | 9 +++++++++ tools/goctl/rpc/gen/genshared.go | 16 ++++++++-------- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/tools/goctl/rpc/CHANGELOG.md b/tools/goctl/rpc/CHANGELOG.md index 4579048b..37be49b9 100644 --- a/tools/goctl/rpc/CHANGELOG.md +++ b/tools/goctl/rpc/CHANGELOG.md @@ -1,5 +1,8 @@ # Change log +# 2020-08-29 +* 新增支持windows生成 + # 2020-08-27 * 新增支持rpc模板生成 * 新增支持rpc服务生成 diff --git a/tools/goctl/rpc/README.md b/tools/goctl/rpc/README.md index 5a02f198..8db5d969 100644 --- a/tools/goctl/rpc/README.md +++ b/tools/goctl/rpc/README.md @@ -134,9 +134,6 @@ OPTIONS: 的标识,请注意不要将也写业务性代码写在里面。 -# 下一步规划 -* 尽快支持windows端rpc生成 - diff --git a/tools/goctl/rpc/ctx/ctx.go b/tools/goctl/rpc/ctx/ctx.go index e2625d28..88ead96a 100644 --- a/tools/goctl/rpc/ctx/ctx.go +++ b/tools/goctl/rpc/ctx/ctx.go @@ -88,9 +88,7 @@ func MustCreateRpcContext(protoSrc, targetDir, sharedDir, serviceName string, id func MustCreateRpcContextFromCli(ctx *cli.Context) *RpcContext { os := runtime.GOOS switch os { - case "darwin": - case "windows": - logx.Must(fmt.Errorf("windows will support soon")) + case "darwin", "windows": default: logx.Must(fmt.Errorf("unexpected os: %s", os)) } diff --git a/tools/goctl/rpc/gen/gendir.go b/tools/goctl/rpc/gen/gendir.go index ef42c5e1..62aadbc2 100644 --- a/tools/goctl/rpc/gen/gendir.go +++ b/tools/goctl/rpc/gen/gendir.go @@ -2,6 +2,7 @@ package gen import ( "path/filepath" + "runtime" "strings" "github.com/tal-tech/go-zero/tools/goctl/util" @@ -41,5 +42,13 @@ func (g *defaultRpcGenerator) mustGetPackage(dir string) string { target := g.dirM[dir] projectPath := g.Ctx.ProjectPath relativePath := strings.TrimPrefix(target, projectPath) + os := runtime.GOOS + switch os { + case "windows": + relativePath = filepath.ToSlash(relativePath) + case "darwin": + default: + g.Ctx.Fatalln("unexpected os: %s", os) + } return g.Ctx.Module + relativePath } diff --git a/tools/goctl/rpc/gen/genshared.go b/tools/goctl/rpc/gen/genshared.go index 756fa08d..e6539f70 100644 --- a/tools/goctl/rpc/gen/genshared.go +++ b/tools/goctl/rpc/gen/genshared.go @@ -2,12 +2,12 @@ package gen import ( "fmt" + "github.com/tal-tech/go-zero/tools/goctl/rpc/execx" "os" "os/exec" "path/filepath" "strings" - "github.com/tal-tech/go-zero/tools/goctl/rpc/execx" "github.com/tal-tech/go-zero/tools/goctl/rpc/parser" "github.com/tal-tech/go-zero/tools/goctl/util" ) @@ -119,6 +119,8 @@ func (g *defaultRpcGenerator) genShared() error { "types": typeCode, }, filename, true) + _, err = exec.LookPath("mockgen") + mockGenInstalled := err == nil for _, service := range file.Service { filename := filepath.Join(g.Ctx.SharedDir, fmt.Sprintf("%smodel.go", service.Name.Lower())) functions, err := g.getFuncs(service) @@ -144,15 +146,13 @@ func (g *defaultRpcGenerator) genShared() error { if err != nil { return err } + // if mockgen is already installed, it will generate code of gomock for shared files + _, err = exec.LookPath("mockgen") + if mockGenInstalled { + execx.Run(fmt.Sprintf("go generate %s", filename)) + } } - // if mockgen is already installed, it will generate code of gomock for shared files - _, err = exec.LookPath("mockgen") - if err != nil { - g.Ctx.Warning("warning:mockgen is not found") - } else { - execx.Run(fmt.Sprintf("cd %s \ngo generate", g.Ctx.SharedDir)) - } return nil }