From 174d2ab2e87303b289e8fcf87ddd804a1927b0c8 Mon Sep 17 00:00:00 2001 From: wangle <285273592@qq.com> Date: Sun, 26 Jul 2020 14:27:27 +0800 Subject: [PATCH 1/2] Compare content after formatting --- internal/app/module/beegopro/render.go | 20 +++++++++++--- internal/app/module/beegopro/util.go | 37 ++++++++------------------ 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/internal/app/module/beegopro/render.go b/internal/app/module/beegopro/render.go index 64c52fc..3be18a1 100644 --- a/internal/app/module/beegopro/render.go +++ b/internal/app/module/beegopro/render.go @@ -1,11 +1,13 @@ 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" @@ -120,15 +122,27 @@ 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 - if len(orgContent) == 0 || FileContentChange(string(orgContent),buf) { - err = r.write(r.FlushFile, buf) + 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 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 2cbfe23..f6baee6 100644 --- a/internal/app/module/beegopro/util.go +++ b/internal/app/module/beegopro/util.go @@ -11,13 +11,12 @@ import ( "os" "path" "path/filepath" - "regexp" "strings" "time" ) // write to file -func (c *RenderFile) write(filename string, buf string) (err error) { +func (c *RenderFile) write(filename string, buf []byte) (err error) { if utils.IsExist(filename) && !isNeedOverwrite(filename) { return } @@ -191,34 +190,19 @@ func getModelType(orm string) (inputType, goType, mysqlType, tag string) { return } -func FileContentChange(org,new string) bool { - if org == "" { - return false +func FileContentChange(org,new []byte) bool { + if len(org) == 0 { + return true } var orgContent,newContent string - 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(org), "\n") { + if i > 1 { + orgContent += 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, "") + for i, s := range strings.Split(string(new), "\n") { + if i > 1 { + newContent += s } } orgMd5 := md5.Sum([]byte(orgContent)) @@ -226,5 +210,6 @@ func FileContentChange(org,new string) bool { if orgMd5 != newMd5 { return true } + beeLogger.Log.Infof("File has no change in the content") return false } \ No newline at end of file From a9d3de0872034b4c11108723f6d15ce2a8ac5d52 Mon Sep 17 00:00:00 2001 From: wangle <285273592@qq.com> Date: Sun, 26 Jul 2020 15:48:36 +0800 Subject: [PATCH 2/2] Only contents in "CompareExcept" are excluded during comparison --- internal/app/module/beegopro/config.go | 2 ++ internal/app/module/beegopro/render.go | 5 +-- internal/app/module/beegopro/util.go | 47 ++++++++++++++++---------- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/internal/app/module/beegopro/config.go b/internal/app/module/beegopro/config.go index f4854fe..716533c 100644 --- a/internal/app/module/beegopro/config.go +++ b/internal/app/module/beegopro/config.go @@ -6,6 +6,8 @@ import ( "io/ioutil" ) +var CompareExcept = []string{"@BeeGenerateTime"} + func (c *Container) GenConfig() { if utils.IsExist(c.BeegoProFile) { beeLogger.Log.Fatalf("beego pro toml exist") diff --git a/internal/app/module/beegopro/render.go b/internal/app/module/beegopro/render.go index 3be18a1..7fe55a2 100644 --- a/internal/app/module/beegopro/render.go +++ b/internal/app/module/beegopro/render.go @@ -130,7 +130,8 @@ func (r *RenderFile) Exec(name string) { } // Replace or create when content changes output := []byte(buf) - if r.Option.EnableFormat && filepath.Ext(r.FlushFile) == ".go" { + ext := filepath.Ext(r.FlushFile) + if r.Option.EnableFormat && ext == ".go" { // format code var bts []byte bts, err = format.Source([]byte(buf)) @@ -141,7 +142,7 @@ func (r *RenderFile) Exec(name string) { output = bts } - if FileContentChange(orgContent,output) { + if FileContentChange(orgContent,output,GetSeg(ext)) { err = r.write(r.FlushFile, output) if err != nil { beeLogger.Log.Fatalf("Could not create file: %s", err) diff --git a/internal/app/module/beegopro/util.go b/internal/app/module/beegopro/util.go index f6baee6..b2bd05d 100644 --- a/internal/app/module/beegopro/util.go +++ b/internal/app/module/beegopro/util.go @@ -80,11 +80,7 @@ func (c *RenderFile) write(filename string, buf []byte) (err error) { } func isNeedOverwrite(fileName string) (flag bool) { - seg := "//" - ext := filepath.Ext(fileName) - if ext == ".sql" { - seg = "--" - } + seg := GetSeg(filepath.Ext(fileName)) f, err := os.Open(fileName) if err != nil { @@ -190,21 +186,12 @@ func getModelType(orm string) (inputType, goType, mysqlType, tag string) { return } -func FileContentChange(org,new []byte) bool { +func FileContentChange(org,new []byte, seg string) bool { if len(org) == 0 { return true } - var orgContent,newContent string - for i, s := range strings.Split(string(org), "\n") { - if i > 1 { - orgContent += s - } - } - for i, s := range strings.Split(string(new), "\n") { - if i > 1 { - newContent += s - } - } + orgContent := GetFilterContent(string(org),seg) + newContent := GetFilterContent(string(org),string(new)) orgMd5 := md5.Sum([]byte(orgContent)) newMd5:= md5.Sum([]byte(newContent)) if orgMd5 != newMd5 { @@ -212,4 +199,30 @@ func FileContentChange(org,new []byte) bool { } beeLogger.Log.Infof("File has no change in the content") return false +} + +func GetFilterContent(content string, seg string) string { + res := "" + for _, s := range strings.Split(content, "\n") { + s = strings.TrimSpace(strings.TrimPrefix(s, seg)) + var have = false + for _,except := range CompareExcept{ + if strings.HasPrefix(s, except) { + have = true + } + } + if !have { + res += s + } + } + return res +} + +func GetSeg(ext string) string { + switch ext { + case ".sql": + return "--" + default: + return "//" + } } \ No newline at end of file