1
0
mirror of https://github.com/beego/bee.git synced 2024-11-26 06:21:34 +00:00

Backup only when content changes

This commit is contained in:
wangle 2020-07-25 02:56:02 +08:00
parent ab318af124
commit 610cd901bb
2 changed files with 61 additions and 5 deletions

View File

@ -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)
}

View File

@ -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
}