rpc service generation (#26)
* 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
This commit is contained in:
111
tools/goctl/rpc/ctx/ctx.go
Normal file
111
tools/goctl/rpc/ctx/ctx.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package ctx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||
)
|
||||
|
||||
const (
|
||||
flagSrc = "src"
|
||||
flagDir = "dir"
|
||||
flagShared = "shared"
|
||||
flagService = "service"
|
||||
flagIdea = "idea"
|
||||
)
|
||||
|
||||
type (
|
||||
RpcContext struct {
|
||||
ProjectPath string
|
||||
ProjectName stringx.String
|
||||
ServiceName stringx.String
|
||||
CurrentPath string
|
||||
Module string
|
||||
ProtoFileSrc string
|
||||
ProtoSource string
|
||||
TargetDir string
|
||||
SharedDir string
|
||||
GoPath string
|
||||
console.Console
|
||||
}
|
||||
)
|
||||
|
||||
func MustCreateRpcContext(protoSrc, targetDir, sharedDir, serviceName string, idea bool) *RpcContext {
|
||||
log := console.NewConsole(idea)
|
||||
info, err := prepare(log)
|
||||
log.Must(err)
|
||||
|
||||
if stringx.From(protoSrc).IsEmptyOrSpace() {
|
||||
log.Fatalln("expected proto source, but nothing found")
|
||||
}
|
||||
srcFp, err := filepath.Abs(protoSrc)
|
||||
log.Must(err)
|
||||
|
||||
if !util.FileExists(srcFp) {
|
||||
log.Fatalln("%s is not exists", srcFp)
|
||||
}
|
||||
current := filepath.Dir(srcFp)
|
||||
if stringx.From(targetDir).IsEmptyOrSpace() {
|
||||
targetDir = current
|
||||
}
|
||||
if stringx.From(sharedDir).IsEmptyOrSpace() {
|
||||
sharedDir = filepath.Join(current, "shared")
|
||||
}
|
||||
targetDirFp, err := filepath.Abs(targetDir)
|
||||
log.Must(err)
|
||||
|
||||
sharedFp, err := filepath.Abs(sharedDir)
|
||||
log.Must(err)
|
||||
|
||||
if stringx.From(serviceName).IsEmptyOrSpace() {
|
||||
serviceName = getServiceFromRpcStructure(targetDirFp)
|
||||
}
|
||||
serviceNameString := stringx.From(serviceName)
|
||||
if serviceNameString.IsEmptyOrSpace() {
|
||||
log.Fatalln("service name is not found")
|
||||
}
|
||||
|
||||
return &RpcContext{
|
||||
ProjectPath: info.Path,
|
||||
ProjectName: stringx.From(info.Name),
|
||||
ServiceName: serviceNameString,
|
||||
CurrentPath: current,
|
||||
Module: info.GoMod.Module,
|
||||
ProtoFileSrc: srcFp,
|
||||
ProtoSource: filepath.Base(srcFp),
|
||||
TargetDir: targetDirFp,
|
||||
SharedDir: sharedFp,
|
||||
GoPath: info.GoPath,
|
||||
Console: log,
|
||||
}
|
||||
}
|
||||
func MustCreateRpcContextFromCli(ctx *cli.Context) *RpcContext {
|
||||
os := runtime.GOOS
|
||||
switch os {
|
||||
case "darwin":
|
||||
case "windows":
|
||||
logx.Must(fmt.Errorf("windows will support soon"))
|
||||
default:
|
||||
logx.Must(fmt.Errorf("unexpected os: %s", os))
|
||||
}
|
||||
protoSrc := ctx.String(flagSrc)
|
||||
targetDir := ctx.String(flagDir)
|
||||
sharedDir := ctx.String(flagShared)
|
||||
serviceName := ctx.String(flagService)
|
||||
idea := ctx.Bool(flagIdea)
|
||||
return MustCreateRpcContext(protoSrc, targetDir, sharedDir, serviceName, idea)
|
||||
}
|
||||
|
||||
func getServiceFromRpcStructure(targetDir string) string {
|
||||
targetDir = filepath.Clean(targetDir)
|
||||
suffix := filepath.Join("cmd", "rpc")
|
||||
return filepath.Base(strings.TrimSuffix(targetDir, suffix))
|
||||
}
|
||||
208
tools/goctl/rpc/ctx/project.go
Normal file
208
tools/goctl/rpc/ctx/project.go
Normal file
@@ -0,0 +1,208 @@
|
||||
package ctx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"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"
|
||||
)
|
||||
|
||||
var (
|
||||
errProtobufNotFound = errors.New("github.com/golang/protobuf is not found,please ensure you has already [go get github.com/golang/protobuf]")
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
GoMod struct {
|
||||
ModOn bool
|
||||
GoModCache string
|
||||
GoMod string
|
||||
Module string
|
||||
}
|
||||
Protobuf struct {
|
||||
Path string
|
||||
}
|
||||
)
|
||||
|
||||
func prepare(log console.Console) (*Project, error) {
|
||||
log.Info("check go env ...")
|
||||
_, err := exec.LookPath(constGo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = exec.LookPath(constProtoC)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
goModOn bool
|
||||
goMod, goModCache, module string
|
||||
goPath string
|
||||
name, path string
|
||||
protobufModule string
|
||||
)
|
||||
ret, err := execx.Run(constGoModOn)
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
module, err = matchModule(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
protobufModule, err = matchProtoBuf(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
if goModCache == "" {
|
||||
goModCache = src
|
||||
}
|
||||
pwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(pwd, src) {
|
||||
return nil, fmt.Errorf("%s: project is not in go mod and go path", pwd)
|
||||
}
|
||||
r := strings.TrimPrefix(pwd, src+string(filepath.Separator))
|
||||
name = filepath.Dir(r)
|
||||
if name == "." {
|
||||
name = r
|
||||
}
|
||||
path = filepath.Join(src, name)
|
||||
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,
|
||||
},
|
||||
GoMod: GoMod{
|
||||
ModOn: goModOn,
|
||||
GoModCache: goModCache,
|
||||
GoMod: goMod,
|
||||
Module: module,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// github.com/golang/protobuf@{version}
|
||||
func matchProtoBuf(data []byte) (string, error) {
|
||||
text := string(data)
|
||||
re := regexp.MustCompile(`(?m)(github.com/golang/protobuf)\s+(v[0-9.]+)`)
|
||||
matches := re.FindAllStringSubmatch(text, -1)
|
||||
if len(matches) == 0 {
|
||||
return "", errProtobufNotFound
|
||||
}
|
||||
groups := matches[0]
|
||||
if len(groups) < 3 {
|
||||
return "", errProtobufNotFound
|
||||
}
|
||||
return fmt.Sprintf("%s@%s", groups[1], groups[2]), nil
|
||||
}
|
||||
|
||||
func matchModule(data []byte) (string, error) {
|
||||
text := string(data)
|
||||
re := regexp.MustCompile(`(?m)^\s*module\s+[a-z0-9/\-.]+$`)
|
||||
matches := re.FindAllString(text, -1)
|
||||
if len(matches) == 1 {
|
||||
target := matches[0]
|
||||
index := strings.Index(target, "module")
|
||||
return strings.TrimSpace(target[index+6:]), nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
Reference in New Issue
Block a user