diff --git a/cmd/gf/internal/cmd/cmd_gen_service.go b/cmd/gf/internal/cmd/cmd_gen_service.go index df0f65102..f1a61562c 100644 --- a/cmd/gf/internal/cmd/cmd_gen_service.go +++ b/cmd/gf/internal/cmd/cmd_gen_service.go @@ -3,6 +3,8 @@ package cmd import ( "context" "fmt" + "go/parser" + "go/token" "github.com/gogf/gf/cmd/gf/v2/internal/consts" "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog" @@ -233,16 +235,23 @@ func (c cGenService) Service(ctx context.Context, in cGenServiceInput) (out *cGe } func (c cGenService) calculateImportedPackages(fileContent string, srcImportedPackages *garray.SortedStrArray) (err error) { - var match []string - match, err = gregex.MatchString(`\s+import\s+\(([\s\S]+?)\)`, fileContent) + f, err := parser.ParseFile(token.NewFileSet(), "", fileContent, parser.ImportsOnly) if err != nil { return err } - if len(match) < 2 { - return nil + for _, s := range f.Imports { + if s.Path != nil { + if s.Name != nil { + // has alias and is not `_` + if pkgAlias := s.Name.String(); pkgAlias != "_" { + srcImportedPackages.Add(pkgAlias + " " + s.Path.Value) + } + } else { + // no alias + srcImportedPackages.Add(s.Path.Value) + } + } } - importPart := gstr.Trim(match[1]) - srcImportedPackages.Append(gstr.SplitAndTrim(importPart, "\n")...) return nil } diff --git a/cmd/gf/internal/utility/utils/utils.go b/cmd/gf/internal/utility/utils/utils.go index 831326242..087cc43f2 100644 --- a/cmd/gf/internal/utility/utils/utils.go +++ b/cmd/gf/internal/utility/utils/utils.go @@ -8,10 +8,30 @@ import ( "golang.org/x/tools/imports" ) -// GoFmt formats the source file. +// GoFmt formats the source file and adds or removes import statements as necessary. func GoFmt(path string) { - if err := doGoFmt(path, true); err != nil { - mlog.Fatalf(`error format "%s" go files: %v`, path, err) + replaceFunc := func(path, content string) string { + res, err := imports.Process(path, []byte(content), nil) + if err != nil { + mlog.Printf(`error format "%s" go files: %v`, path, err) + return content + } + return string(res) + } + + var err error + if gfile.IsFile(path) { + // File format. + if gfile.ExtName(path) != "go" { + return + } + err = gfile.ReplaceFileFunc(replaceFunc, path) + } else { + // Folder format. + err = gfile.ReplaceDirFunc(replaceFunc, path, "*.go", true) + } + if err != nil { + mlog.Printf(`error format "%s" go files: %v`, path, err) } } @@ -22,33 +42,3 @@ func IsFileDoNotEdit(filePath string) bool { } return gstr.Contains(gfile.GetContents(filePath), consts.DoNotEditKey) } - -// 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{ - Comments: true, - TabIndent: true, - TabWidth: 8, - FormatOnly: true, - } - } - replaceFunc := func(path, content string) string { - res, err := imports.Process(path, []byte(content), genOpt) - if err != nil { - mlog.Printf(`pretty go file "%s" failed: %v`, path, err) - return content - } - return string(res) - } - // File format. - if gfile.IsFile(path) { - if gfile.ExtName(path) != "go" { - return nil - } - return gfile.ReplaceFileFunc(replaceFunc, path) - } - // Folder format. - return gfile.ReplaceDirFunc(replaceFunc, path, "*.go", true) -}