feat: 1. 创建项目如果带有proto参数,在api目录放置一个go generate

pull/185/head
5 years ago
parent 5182ebf553
commit 2715aaf528
  1. 5
      tool/kratos/main.go
  2. 6
      tool/kratos/new.go
  3. 4
      tool/kratos/project.go
  4. 36
      tool/kratos/template.go

@ -35,6 +35,11 @@ func main() {
Usage: "whether to use protobuf for create project", Usage: "whether to use protobuf for create project",
Destination: &p.WithGRPC, Destination: &p.WithGRPC,
}, },
cli.StringFlag{
Name: "m",
Usage: "project module name for create project, for `go mod init`",
Destination: &p.ModuleName,
},
}, },
Action: runNew, Action: runNew,
}, },

@ -14,6 +14,11 @@ func runNew(ctx *cli.Context) error {
return errors.New("required project name") return errors.New("required project name")
} }
p.Name = ctx.Args()[0] p.Name = ctx.Args()[0]
if p.ModuleName == "" {
p.ModuleName = path.Join(p.Owner, p.Name)
}
if p.Path != "" { if p.Path != "" {
p.Path = path.Join(p.Path, p.Name) p.Path = path.Join(p.Path, p.Name)
} else { } else {
@ -26,6 +31,7 @@ func runNew(ctx *cli.Context) error {
} }
fmt.Printf("Project: %s\n", p.Name) fmt.Printf("Project: %s\n", p.Name)
fmt.Printf("Owner: %s\n", p.Owner) fmt.Printf("Owner: %s\n", p.Owner)
fmt.Printf("Module Name: %s\n", p.ModuleName)
fmt.Printf("WithGRPC: %t\n", p.WithGRPC) fmt.Printf("WithGRPC: %t\n", p.WithGRPC)
fmt.Printf("Directory: %s\n\n", p.Path) fmt.Printf("Directory: %s\n\n", p.Path)
fmt.Println("The application has been created.") fmt.Println("The application has been created.")

@ -16,6 +16,7 @@ type project struct {
Path string Path string
WithGRPC bool WithGRPC bool
Here bool Here bool
ModuleName string // 支持项目的自定义module名 (go.mod init)
} }
const ( const (
@ -36,6 +37,7 @@ const (
_tplTypeModel _tplTypeModel
_tplTypeGRPCServer _tplTypeGRPCServer
_tplTypeGomod _tplTypeGomod
_tplTypeAPIGogen
) )
var ( var (
@ -78,6 +80,7 @@ var (
_tplTypeHTTPToml: _tplHTTPToml, _tplTypeHTTPToml: _tplHTTPToml,
_tplTypeModel: _tplModel, _tplTypeModel: _tplModel,
_tplTypeGomod: _tplGoMod, _tplTypeGomod: _tplGoMod,
_tplTypeAPIGogen: _tplGogen,
} }
) )
@ -85,6 +88,7 @@ func create() (err error) {
if p.WithGRPC { if p.WithGRPC {
files[_tplTypeGRPCServer] = "/internal/server/grpc/server.go" files[_tplTypeGRPCServer] = "/internal/server/grpc/server.go"
files[_tplTypeAPIProto] = "/api/api.proto" files[_tplTypeAPIProto] = "/api/api.proto"
files[_tplTypeAPIGogen] = "/api/generate.go"
tpls[_tplTypeHTTPServer] = _tplPBHTTPServer tpls[_tplTypeHTTPServer] = _tplPBHTTPServer
tpls[_tplTypeGRPCServer] = _tplGRPCServer tpls[_tplTypeGRPCServer] = _tplGRPCServer
tpls[_tplTypeGRPCToml] = _tplGRPCToml tpls[_tplTypeGRPCToml] = _tplGRPCToml

@ -72,8 +72,8 @@ import (
"syscall" "syscall"
"time" "time"
"{{.Name}}/internal/server/http" "{{.ModuleName}}/internal/server/http"
"{{.Name}}/internal/service" "{{.ModuleName}}/internal/service"
"github.com/bilibili/kratos/pkg/conf/paladin" "github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/log" "github.com/bilibili/kratos/pkg/log"
) )
@ -122,9 +122,9 @@ import (
"syscall" "syscall"
"time" "time"
"{{.Name}}/internal/server/grpc" "{{.ModuleName}}/internal/server/grpc"
"{{.Name}}/internal/server/http" "{{.ModuleName}}/internal/server/http"
"{{.Name}}/internal/service" "{{.ModuleName}}/internal/service"
"github.com/bilibili/kratos/pkg/conf/paladin" "github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/log" "github.com/bilibili/kratos/pkg/log"
) )
@ -284,7 +284,7 @@ func (d *dao) pingRedis(ctx context.Context) (err error) {
import ( import (
"context" "context"
"{{.Name}}/internal/dao" "{{.ModuleName}}/internal/dao"
"github.com/bilibili/kratos/pkg/conf/paladin" "github.com/bilibili/kratos/pkg/conf/paladin"
) )
@ -324,8 +324,8 @@ import (
"context" "context"
"fmt" "fmt"
pb "{{.Name}}/api" pb "{{.ModuleName}}/api"
"{{.Name}}/internal/dao" "{{.ModuleName}}/internal/dao"
"github.com/bilibili/kratos/pkg/conf/paladin" "github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/golang/protobuf/ptypes/empty" "github.com/golang/protobuf/ptypes/empty"
@ -381,8 +381,8 @@ func (s *Service) Close() {
import ( import (
"net/http" "net/http"
"{{.Name}}/internal/model" "{{.ModuleName}}/internal/model"
"{{.Name}}/internal/service" "{{.ModuleName}}/internal/service"
"github.com/bilibili/kratos/pkg/conf/paladin" "github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/log" "github.com/bilibili/kratos/pkg/log"
@ -442,9 +442,9 @@ func howToStart(c *bm.Context) {
import ( import (
"net/http" "net/http"
pb "{{.Name}}/api" pb "{{.ModuleName}}/api"
"{{.Name}}/internal/model" "{{.ModuleName}}/internal/model"
"{{.Name}}/internal/service" "{{.ModuleName}}/internal/service"
"github.com/bilibili/kratos/pkg/conf/paladin" "github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/log" "github.com/bilibili/kratos/pkg/log"
@ -542,7 +542,7 @@ message HelloResp {
type Kratos struct { type Kratos struct {
Hello string Hello string
}` }`
_tplGoMod = `module {{.Name}} _tplGoMod = `module {{.ModuleName}}
go 1.12 go 1.12
@ -573,8 +573,8 @@ replace (
_tplGRPCServer = `package grpc _tplGRPCServer = `package grpc
import ( import (
pb "{{.Name}}/api" pb "{{.ModuleName}}/api"
"{{.Name}}/internal/service" "{{.ModuleName}}/internal/service"
"github.com/bilibili/kratos/pkg/conf/paladin" "github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/net/rpc/warden" "github.com/bilibili/kratos/pkg/net/rpc/warden"
) )
@ -597,5 +597,9 @@ func New(svc *service.Service) *warden.Server {
} }
return ws return ws
} }
`
_tplGogen = `package api
//go:generate kratos tool protoc --swagger --grpc --bm api.proto
` `
) )

Loading…
Cancel
Save