|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
conf "github.com/zeromicro/go-zero/tools/goctl/config"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util/format"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
|
|
|
)
|
|
|
|
|
|
|
|
const svcTemplate = `package svc
|
|
|
|
|
|
|
|
import {{.imports}}
|
|
|
|
|
|
|
|
type ServiceContext struct {
|
|
|
|
Config config.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServiceContext(c config.Config) *ServiceContext {
|
|
|
|
return &ServiceContext{
|
|
|
|
Config:c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
// GenSvc generates the servicecontext.go file, which is the resource dependency of a service,
|
|
|
|
// such as rpc dependency, model dependency, etc.
|
|
|
|
func (g *DefaultGenerator) GenSvc(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
|
|
|
|
dir := ctx.GetSvc()
|
|
|
|
svcFilename, err := format.FileNamingFormat(cfg.NamingFormat, "service_context")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName := filepath.Join(dir.Filename, svcFilename+".go")
|
|
|
|
text, err := pathx.LoadTemplate(category, svcTemplateFile, svcTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.With("svc").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
|
|
|
"imports": fmt.Sprintf(`"%v"`, ctx.GetConfig().Package),
|
|
|
|
}, fileName, false)
|
|
|
|
}
|