Compare commits

..

1 Commits

Author SHA1 Message Date
68efab79ef improve command gen service 2022-06-24 15:35:16 +08:00
2 changed files with 10 additions and 19 deletions

View File

@ -171,11 +171,7 @@ func (c cGenService) Service(ctx context.Context, in cGenServiceInput) (out *cGe
}
}
// Go imports updating.
mlog.Printf(`goimports go files in "%s", it may take seconds...`, in.DstFolder)
utils.GoImports(in.DstFolder)
// Replica v1 to v2 for GoFrame.
// Replace v1 to v2 for GoFrame.
if err = c.replaceGeneratedServiceContentGFV2(in); err != nil {
return nil, err
}

View File

@ -10,18 +10,11 @@ import (
// GoFmt formats the source file.
func GoFmt(path string) {
if err := pretty(path, true); err != nil {
if err := doGoFmt(path, true); err != nil {
mlog.Fatalf(`error format "%s" go files: %v`, path, err)
}
}
// GoImports adds or removes import statements as necessary for the source file.
func GoImports(path string) {
if err := pretty(path); err != nil {
mlog.Fatalf(`error update "%s" go file imports: %v`, path, err)
}
}
// IsFileDoNotEdit checks and returns whether file contains `do not edit` key.
func IsFileDoNotEdit(filePath string) bool {
if !gfile.Exists(filePath) {
@ -30,8 +23,8 @@ func IsFileDoNotEdit(filePath string) bool {
return gstr.Contains(gfile.GetContents(filePath), consts.DoNotEditKey)
}
// pretty format go file and adds or removes import statements as necessary.
func pretty(filePath string, formatOnly ...bool) error {
// doGoFmt format go file and adds or removes import statements as necessary.
func doGoFmt(path string, formatOnly ...bool) error {
var genOpt *imports.Options
if len(formatOnly) > 0 {
genOpt = &imports.Options{
@ -49,11 +42,13 @@ func pretty(filePath string, formatOnly ...bool) error {
}
return string(res)
}
if gfile.IsFile(filePath) {
if gfile.ExtName(filePath) != "go" {
// File format.
if gfile.IsFile(path) {
if gfile.ExtName(path) != "go" {
return nil
}
return gfile.ReplaceFileFunc(replaceFunc, filePath)
return gfile.ReplaceFileFunc(replaceFunc, path)
}
return gfile.ReplaceDirFunc(replaceFunc, filePath, "*.go", true)
// Folder format.
return gfile.ReplaceDirFunc(replaceFunc, path, "*.go", true)
}