From b4a1aec0e682c305d7dc8c9f1b8cbd0cf6a7399c Mon Sep 17 00:00:00 2001 From: wangle <285273592@qq.com> Date: Fri, 24 Jul 2020 15:15:29 +0800 Subject: [PATCH 1/4] bak file err --- internal/app/module/beegopro/util.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/app/module/beegopro/util.go b/internal/app/module/beegopro/util.go index 79f02a3c3..7fd459f48 100644 --- a/internal/app/module/beegopro/util.go +++ b/internal/app/module/beegopro/util.go @@ -20,7 +20,7 @@ func (c *RenderFile) write(filename string, buf string) (err error) { return } - filePath := path.Dir(filename) + filePath := filepath.Dir(filename) err = createPath(filePath) if err != nil { err = errors.New("write create path " + err.Error()) @@ -37,7 +37,7 @@ func (c *RenderFile) write(filename string, buf string) (err error) { name := path.Base(filename) if utils.IsExist(filename) { - bakName := fmt.Sprintf("%s/%s.%s.bak", filePathBak, name, time.Now().Format("2006.01.02.15.04.05")) + bakName := fmt.Sprintf("%s/%s.%s.bak", filePathBak, filepath.Base(name), time.Now().Format("2006.01.02.15.04.05")) beeLogger.Log.Infof("bak file '%s'", bakName) if err := os.Rename(filename, bakName); err != nil { err = errors.New("file is bak error, path is " + bakName) From ab318af124fcb356c488e81d55493e6dc23c0dd6 Mon Sep 17 00:00:00 2001 From: wangle <285273592@qq.com> Date: Fri, 24 Jul 2020 16:24:09 +0800 Subject: [PATCH 2/4] Change bee version --- cmd/commands/version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/commands/version/version.go b/cmd/commands/version/version.go index 4eebd9f21..a1ce71066 100644 --- a/cmd/commands/version/version.go +++ b/cmd/commands/version/version.go @@ -57,7 +57,7 @@ Prints the current Bee, Beego and Go version alongside the platform information. } var outputFormat string -const version = "1.11.0" +const version = "1.12.0" func init() { fs := flag.NewFlagSet("version", flag.ContinueOnError) From 610cd901bb8e6a54e96a144fa20429fe15d2a1d4 Mon Sep 17 00:00:00 2001 From: wangle <285273592@qq.com> Date: Sat, 25 Jul 2020 02:56:02 +0800 Subject: [PATCH 3/4] Backup only when content changes --- internal/app/module/beegopro/render.go | 26 +++++++++++++---- internal/app/module/beegopro/util.go | 40 ++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/internal/app/module/beegopro/render.go b/internal/app/module/beegopro/render.go index ba5d2b7f9..8bc0686bb 100644 --- a/internal/app/module/beegopro/render.go +++ b/internal/app/module/beegopro/render.go @@ -6,6 +6,8 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/flosch/pongo2" "github.com/smartwalle/pongo2render" + "io/ioutil" + "os" "path" "path/filepath" ) @@ -114,10 +116,24 @@ func (r *RenderFile) Exec(name string) { beeLogger.Log.Fatalf("Could not create the %s render tmpl: %s", name, err) return } - err = r.write(r.FlushFile, buf) - if err != nil { - beeLogger.Log.Fatalf("Could not create file: %s", err) - return + _, err = os.Stat(r.Descriptor.DstPath) + 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) + } 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) + if err != nil { + beeLogger.Log.Fatalf("Could not create file: %s", err) + return + } + beeLogger.Log.Infof("create file '%s' from %s", r.FlushFile, r.PackageName) } - beeLogger.Log.Infof("create file '%s' from %s", r.FlushFile, r.PackageName) } + diff --git a/internal/app/module/beegopro/util.go b/internal/app/module/beegopro/util.go index 7fd459f48..cc191671d 100644 --- a/internal/app/module/beegopro/util.go +++ b/internal/app/module/beegopro/util.go @@ -1,6 +1,7 @@ package beegopro import ( + "crypto/md5" "errors" "fmt" "github.com/beego/bee/internal/pkg/utils" @@ -10,6 +11,7 @@ import ( "os" "path" "path/filepath" + "regexp" "strings" "time" ) @@ -183,3 +185,41 @@ func getModelType(orm string) (inputType, goType, mysqlType, tag string) { } return } + +func FileContentChange(org,new string) bool { + if org == "" { + return false + } + 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(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)) + newMd5:= md5.Sum([]byte(newContent)) + if orgMd5 != newMd5 { + return true + } + return false +} \ No newline at end of file From 528b75f6443d90f8da428baad867f3b9db21bc3d Mon Sep 17 00:00:00 2001 From: wangle <285273592@qq.com> Date: Sun, 26 Jul 2020 13:40:03 +0800 Subject: [PATCH 4/4] Compare content after formatting --- internal/app/module/beegopro/render.go | 20 ++++++++-- internal/app/module/beegopro/util.go | 54 +++++++------------------- 2 files changed, 30 insertions(+), 44 deletions(-) diff --git a/internal/app/module/beegopro/render.go b/internal/app/module/beegopro/render.go index 8bc0686bb..b6becf4e5 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 cc191671d..922747bb9 100644 --- a/internal/app/module/beegopro/util.go +++ b/internal/app/module/beegopro/util.go @@ -6,18 +6,16 @@ import ( "fmt" "github.com/beego/bee/internal/pkg/utils" beeLogger "github.com/beego/bee/logger" - "go/format" "io/ioutil" "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 } @@ -42,6 +40,7 @@ func (c *RenderFile) write(filename string, buf string) (err error) { bakName := fmt.Sprintf("%s/%s.%s.bak", filePathBak, filepath.Base(name), time.Now().Format("2006.01.02.15.04.05")) beeLogger.Log.Infof("bak file '%s'", bakName) if err := os.Rename(filename, bakName); err != nil { + fmt.Println(err) err = errors.New("file is bak error, path is " + bakName) return err } @@ -54,20 +53,7 @@ func (c *RenderFile) write(filename string, buf string) (err error) { return } - output := []byte(buf) - - if c.Option.EnableFormat && filepath.Ext(filename) == ".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 - } - - err = ioutil.WriteFile(filename, output, 0644) + err = ioutil.WriteFile(filename, buf, 0644) if err != nil { err = errors.New("write write file " + err.Error()) return @@ -186,34 +172,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)) @@ -221,5 +192,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