Skip to content

Commit

Permalink
feat: modify template name
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Jan 9, 2023
1 parent 079cd20 commit b746e56
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 38 deletions.
14 changes: 7 additions & 7 deletions cmd/hz/generator/custom_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func renderImportTpl(tplInfo *Template, data interface{}) ([]string, error) {

// renderAppendContent used to render append content for 'update' command
func renderAppendContent(tplInfo *Template, renderInfo interface{}) (string, error) {
tpl, err := template.New(tplInfo.Path).Parse(tplInfo.UpdateBehavior.AppendContentTpl)
tpl, err := template.New(tplInfo.Path).Parse(tplInfo.UpdateBehavior.AppendTpl)
if err != nil {
return "", fmt.Errorf("parse append content template(%s) failed, err: %v", tplInfo.Path, err)
}
Expand Down Expand Up @@ -290,10 +290,10 @@ func (pkgGen *HttpPackageGenerator) genLoopService(tplInfo *Template, filePathRe
}
} else { // update file based on update behavior, use 'switch' statements to make logic clearer
switch tplInfo.UpdateBehavior.Behavior {
case Unchanged:
case Skip:
// do nothing
logs.Infof("do not update file '%s', because the update behavior is 'Unchanged'", filePath)
case Regenerate:
case Cover:
// re-generate
logs.Infof("re-generate file '%s', because the update behavior is 'Regenerate'", filePath)
data := CustomizedFileForService{
Expand Down Expand Up @@ -395,10 +395,10 @@ func (pkgGen *HttpPackageGenerator) genLoopMethod(tplInfo *Template, filePathRen
}
} else { // update file based on update behavior, use 'switch' statements to make logic clearer
switch tplInfo.UpdateBehavior.Behavior {
case Unchanged:
case Skip:
// do nothing
logs.Infof("do not update file '%s', because the update behavior is 'Unchanged'", filePath)
case Regenerate:
case Cover:
// re-generate
logs.Infof("re-generate file '%s', because the update behavior is 'Regenerate'", filePath)
data := CustomizedFileForMethod{
Expand Down Expand Up @@ -448,10 +448,10 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f
}
} else { // update file based on update behavior, use 'switch' statements to make logic clearer
switch tplInfo.UpdateBehavior.Behavior {
case Unchanged:
case Skip:
// do nothing
logs.Infof("do not update file '%s', because the update behavior is 'Unchanged'", filePath)
case Regenerate:
case Cover:
// re-generate
logs.Infof("re-generate file '%s', because the update behavior is 'Regenerate'", filePath)
data := CustomizedFileForIDL{
Expand Down
3 changes: 1 addition & 2 deletions cmd/hz/generator/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,10 @@ func (pkgGen *HttpPackageGenerator) updateRegister(pkg, rDir, pkgName string) er
}

if !bytes.Contains(file, []byte(register.Pkg)) {
newFile, err := util.AddImport(registerPath, register.PkgAlias, register.Pkg)
file, err = util.AddImport(registerPath, register.PkgAlias, register.Pkg)
if err != nil {
return err
}
file = []byte(newFile)

insertReg := register.PkgAlias + ".Register(r)\n"
if bytes.Contains(file, []byte(insertReg)) {
Expand Down
24 changes: 12 additions & 12 deletions cmd/hz/generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,37 @@ type TemplateConfig struct {
}

const (
Unchanged = 0
Regenerate = 1
Append = 2
Skip = "skip"
Cover = "cover"
Append = "append"
)

type Template struct {
Default bool // Is the default package template
Default bool // Update command behavior; skip/cover/append
Path string `yaml:"path"` // The generated path and its filename, such as biz/handler/ping.go
Delims [2]string `yaml:"delims"` // Template Action Instruction Identifier, default: "{{}}"
Body string `yaml:"body"` // Render template, currently only supports go template syntax
LoopMethod bool `yaml:"loop_method"` // Loop generate files based on "method"
LoopService bool `yaml:"loop_service"` // loop generate files based on "service"
LoopService bool `yaml:"loop_service"` // Loop generate files based on "service"
UpdateBehavior UpdateBehavior `yaml:"update_behavior"` // Update command behavior; 0:unchanged, 1:regenerate, 2:append
}

type UpdateBehavior struct {
Behavior int `yaml:"behavior"` // Update command behavior; 0:unchanged, 1:regenerate, 2:append
Behavior string `yaml:"behavior"` // Update command behavior; skip/cover/append
// the following variables are used for append update
AppendKey string `yaml:"append_key"` // Append content based in key; for example: 'method'/'service'
InsertKey string `yaml:"insert_key"` // Insert content by "insert_key"
AppendContentTpl string `yaml:"append_content_tpl"` // Append content if UpdateBehavior is "append"
ImportTpl []string `yaml:"import_tpl"` // Import insert template
AppendKey string `yaml:"append_key"` // Append content based in key; for example: 'method'/'service'
InsertKey string `yaml:"insert_key"` // Insert content by "insert_key"
AppendTpl string `yaml:"append_content_tpl"` // Append content if UpdateBehavior is "append"
ImportTpl []string `yaml:"import_tpl"` // Import insert template
}

// TemplateGenerator contains information about the output template
type TemplateGenerator struct {
OutputDir string
Config *TemplateConfig
Excludes []string
tpls map[string]*template.Template // template name -> template
tplsInfo map[string]*Template // template name -> template info, for getting more template info
tpls map[string]*template.Template // "template name" -> "Template", it used to get the "parsed template" directly
tplsInfo map[string]*Template // "template name" -> "template info", it is used to get the original "template information"
dirs map[string]bool
isPackageTpl bool

Expand Down
28 changes: 11 additions & 17 deletions cmd/hz/util/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package util
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
Expand All @@ -27,26 +28,15 @@ import (
"golang.org/x/tools/go/ast/astutil"
)

func AddImport(file, alias, impt string) (string, error) {
func AddImport(file, alias, impt string) ([]byte, error) {
fset := token.NewFileSet()
path, _ := filepath.Abs(file)
f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
return "", fmt.Errorf("can not parse ast for file: %s, err: %v", path, err)
return nil, fmt.Errorf("can not parse ast for file: %s, err: %v", path, err)
}

added := astutil.AddNamedImport(fset, f, alias, impt)
if !added {
return "", fmt.Errorf("can not add import \"%s\" for file: %s, err: %v", impt, path, err)
}
var output []byte
buffer := bytes.NewBuffer(output)
err = format.Node(buffer, fset, f)
if err != nil {
return "", fmt.Errorf("can add import for file: %s, err: %v", path, err)
}

return buffer.String(), nil
return addImport(fset, f, alias, impt)
}

func AddImportForContent(fileContent []byte, alias, impt string) ([]byte, error) {
Expand All @@ -56,15 +46,19 @@ func AddImportForContent(fileContent []byte, alias, impt string) ([]byte, error)
return nil, fmt.Errorf("can not parse ast for file: %s, err: %v", fileContent, err)
}

return addImport(fset, f, alias, impt)
}

func addImport(fset *token.FileSet, f *ast.File, alias, impt string) ([]byte, error) {
added := astutil.AddNamedImport(fset, f, alias, impt)
if !added {
return nil, fmt.Errorf("can not add import \"%s\" for file: %s, err: %v", impt, fileContent, err)
return nil, fmt.Errorf("can not add import \"%s\" for file: %s", impt, f.Name.Name)
}
var output []byte
buffer := bytes.NewBuffer(output)
err = format.Node(buffer, fset, f)
err := format.Node(buffer, fset, f)
if err != nil {
return nil, fmt.Errorf("can add import for file: %s, err: %v", fileContent, err)
return nil, fmt.Errorf("can not add import for file: %s, err: %v", f.Name.Name, err)
}

return buffer.Bytes(), nil
Expand Down

0 comments on commit b746e56

Please sign in to comment.