update handler generation (#27)

* 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
This commit is contained in:
Keson
2020-08-29 00:15:15 +08:00
committed by GitHub
parent f411178a4f
commit 0734bbcab3
15 changed files with 74 additions and 183 deletions

View File

@@ -33,7 +33,6 @@ type (
ProtoSource string
TargetDir string
SharedDir string
GoPath string
console.Console
}
)
@@ -83,7 +82,6 @@ func MustCreateRpcContext(protoSrc, targetDir, sharedDir, serviceName string, id
ProtoSource: filepath.Base(srcFp),
TargetDir: targetDirFp,
SharedDir: sharedFp,
GoPath: info.GoPath,
Console: log,
}
}

View File

@@ -7,41 +7,29 @@ import (
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/console"
)
const (
constGo = "go"
constProtoC = "protoc"
constGoModOn = "go env GO111MODULE"
constGoMod = "go env GOMOD"
constGoModCache = "go env GOMODCACHE"
constGoPath = "go env GOPATH"
constProtoCGenGo = "protoc-gen-go"
)
type (
Project struct {
Path string
Name string
GoPath string
Protobuf Protobuf
GoMod GoMod
Path string
Name string
GoMod GoMod
}
GoMod struct {
ModOn bool
GoModCache string
GoMod string
Module string
}
Protobuf struct {
Path string
Module string
}
)
@@ -56,32 +44,23 @@ func prepare(log console.Console) (*Project, error) {
if err != nil {
return nil, err
}
_, err = exec.LookPath(constProtoCGenGo)
if err != nil {
return nil, err
}
var (
goModOn bool
goMod, goModCache, module string
goPath string
name, path string
protobufModule string
goMod, module string
goPath string
name, path string
)
ret, err := execx.Run(constGoModOn)
ret, err := execx.Run(constGoMod)
if err != nil {
return nil, err
}
goModOn = strings.TrimSpace(ret) == "on"
ret, err = execx.Run(constGoMod)
if err != nil {
return nil, err
}
goMod = strings.TrimSpace(ret)
ret, err = execx.Run(constGoModCache)
if err != nil {
return nil, err
}
goModCache = strings.TrimSpace(ret)
ret, err = execx.Run(constGoPath)
if err != nil {
return nil, err
@@ -90,9 +69,6 @@ func prepare(log console.Console) (*Project, error) {
goPath = strings.TrimSpace(ret)
src := filepath.Join(goPath, "src")
if len(goMod) > 0 {
if goModCache == "" {
goModCache = filepath.Join(goPath, "pkg", "mod")
}
path = filepath.Dir(goMod)
name = filepath.Base(path)
data, err := ioutil.ReadFile(goMod)
@@ -105,9 +81,6 @@ func prepare(log console.Console) (*Project, error) {
return nil, err
}
} else {
if goModCache == "" {
goModCache = src
}
pwd, err := os.Getwd()
if err != nil {
return nil, err
@@ -125,47 +98,11 @@ func prepare(log console.Console) (*Project, error) {
module = name
}
protobuf := filepath.Join(goModCache, protobufModule)
if !util.FileExists(protobuf) {
return nil, fmt.Errorf("expected protobuf module in path: %s,please ensure you has already [go get github.com/golang/protobuf]", protobuf)
}
var protoCGenGoFilename string
os := runtime.GOOS
switch os {
case "darwin":
protoCGenGoFilename = filepath.Join(goPath, "bin", "protoc-gen-go")
case "windows":
protoCGenGoFilename = filepath.Join(goPath, "bin", "protoc-gen-go.exe")
default:
return nil, fmt.Errorf("unexpeted os: %s", os)
}
if !util.FileExists(protoCGenGoFilename) {
sh := "go install " + filepath.Join(protobuf, constProtoCGenGo)
log.Warning(sh)
stdout, err := execx.Run(sh)
if err != nil {
return nil, err
}
log.Info(stdout)
}
if !util.FileExists(protoCGenGoFilename) {
return nil, fmt.Errorf("protoc-gen-go is not found")
}
return &Project{
Name: name,
Path: path,
GoPath: goPath,
Protobuf: Protobuf{
Path: protobuf,
},
Name: name,
Path: path,
GoMod: GoMod{
ModOn: goModOn,
GoModCache: goModCache,
GoMod: goMod,
Module: module,
Module: module,
},
}, nil
}