mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
gfcli: fix imports parse and update gofmt (#1979)
This commit is contained in:
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user