From 677987a7d9528bf8fb494b3b1544fc170c5ea98c Mon Sep 17 00:00:00 2001 From: askuy Date: Mon, 27 Jul 2020 13:09:56 +0800 Subject: [PATCH] Revert "Compare content after formatting" This reverts commit 174d2ab2e87303b289e8fcf87ddd804a1927b0c8. --- internal/app/module/beegopro/render.go | 20 +++----------- internal/app/module/beegopro/util.go | 37 ++++++++++++++++++-------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/internal/app/module/beegopro/render.go b/internal/app/module/beegopro/render.go index 3be18a1..64c52fc 100644 --- a/internal/app/module/beegopro/render.go +++ b/internal/app/module/beegopro/render.go @@ -1,13 +1,11 @@ package beegopro import ( - "errors" "github.com/beego/bee/internal/pkg/system" beeLogger "github.com/beego/bee/logger" "github.com/davecgh/go-spew/spew" "github.com/flosch/pongo2" "github.com/smartwalle/pongo2render" - "go/format" "io/ioutil" "os" "path" @@ -122,27 +120,15 @@ func (r *RenderFile) Exec(name string) { var orgContent []byte if err == nil { if org, err := os.OpenFile(r.Descriptor.DstPath, os.O_RDONLY, 0666); err == nil { + defer org.Close() orgContent,_ = ioutil.ReadAll(org) - org.Close() } else { beeLogger.Log.Infof("file err %s", err) } } // Replace or create when content changes - output := []byte(buf) - if r.Option.EnableFormat && filepath.Ext(r.FlushFile) == ".go" { - // format code - var bts []byte - bts, err = format.Source([]byte(buf)) - if err != nil { - err = errors.New("format buf error " + err.Error()) - return - } - output = bts - } - - if FileContentChange(orgContent,output) { - err = r.write(r.FlushFile, output) + if len(orgContent) == 0 || FileContentChange(string(orgContent),buf) { + err = r.write(r.FlushFile, buf) if err != nil { beeLogger.Log.Fatalf("Could not create file: %s", err) return diff --git a/internal/app/module/beegopro/util.go b/internal/app/module/beegopro/util.go index f6baee6..2cbfe23 100644 --- a/internal/app/module/beegopro/util.go +++ b/internal/app/module/beegopro/util.go @@ -11,12 +11,13 @@ import ( "os" "path" "path/filepath" + "regexp" "strings" "time" ) // write to file -func (c *RenderFile) write(filename string, buf []byte) (err error) { +func (c *RenderFile) write(filename string, buf string) (err error) { if utils.IsExist(filename) && !isNeedOverwrite(filename) { return } @@ -190,19 +191,34 @@ func getModelType(orm string) (inputType, goType, mysqlType, tag string) { return } -func FileContentChange(org,new []byte) bool { - if len(org) == 0 { - return true +func FileContentChange(org,new string) bool { + if org == "" { + return false } var orgContent,newContent string - for i, s := range strings.Split(string(org), "\n") { - if i > 1 { - orgContent += s + jump := false + // expect tab character and blank space and "import(***)" + reg := regexp.MustCompile("\\s+") + for i, s := range strings.Split(org, "\n") { + if s == "import (" { + jump = true + } + if jump && s == ")" { + jump = false + } + if i > 2 && !jump { + orgContent += reg.ReplaceAllString(s, "") } } - for i, s := range strings.Split(string(new), "\n") { - if i > 1 { - newContent += s + for i, s := range strings.Split(new, "\n") { + if s == "import (" { + jump = true + } + if jump && s == ")" { + jump = false + } + if i > 2 && !jump { + newContent += reg.ReplaceAllString(s, "") } } orgMd5 := md5.Sum([]byte(orgContent)) @@ -210,6 +226,5 @@ func FileContentChange(org,new []byte) bool { if orgMd5 != newMd5 { return true } - beeLogger.Log.Infof("File has no change in the content") return false } \ No newline at end of file