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. // Replace v1 to v2 for GoFrame.
mlog.Printf(`goimports go files in "%s", it may take seconds...`, in.DstFolder)
utils.GoImports(in.DstFolder)
// Replica v1 to v2 for GoFrame.
if err = c.replaceGeneratedServiceContentGFV2(in); err != nil { if err = c.replaceGeneratedServiceContentGFV2(in); err != nil {
return nil, err return nil, err
} }

View File

@ -10,18 +10,11 @@ import (
// GoFmt formats the source file. // GoFmt formats the source file.
func GoFmt(path string) { 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) 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. // IsFileDoNotEdit checks and returns whether file contains `do not edit` key.
func IsFileDoNotEdit(filePath string) bool { func IsFileDoNotEdit(filePath string) bool {
if !gfile.Exists(filePath) { if !gfile.Exists(filePath) {
@ -30,8 +23,8 @@ func IsFileDoNotEdit(filePath string) bool {
return gstr.Contains(gfile.GetContents(filePath), consts.DoNotEditKey) return gstr.Contains(gfile.GetContents(filePath), consts.DoNotEditKey)
} }
// pretty format go file and adds or removes import statements as necessary. // doGoFmt format go file and adds or removes import statements as necessary.
func pretty(filePath string, formatOnly ...bool) error { func doGoFmt(path string, formatOnly ...bool) error {
var genOpt *imports.Options var genOpt *imports.Options
if len(formatOnly) > 0 { if len(formatOnly) > 0 {
genOpt = &imports.Options{ genOpt = &imports.Options{
@ -49,11 +42,13 @@ func pretty(filePath string, formatOnly ...bool) error {
} }
return string(res) return string(res)
} }
if gfile.IsFile(filePath) { // File format.
if gfile.ExtName(filePath) != "go" { if gfile.IsFile(path) {
if gfile.ExtName(path) != "go" {
return nil 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)
} }