1
0
mirror of https://github.com/beego/bee.git synced 2024-11-26 16:41:30 +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/davecgh/go-spew/spew"
"github.com/flosch/pongo2" "github.com/flosch/pongo2"
"github.com/smartwalle/pongo2render" "github.com/smartwalle/pongo2render"
"io/ioutil"
"os"
"path" "path"
"path/filepath" "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) beeLogger.Log.Fatalf("Could not create the %s render tmpl: %s", name, err)
return return
} }
err = r.write(r.FlushFile, buf) _, err = os.Stat(r.Descriptor.DstPath)
if err != nil { var orgContent []byte
beeLogger.Log.Fatalf("Could not create file: %s", err) if err == nil {
return 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 package beegopro
import ( import (
"crypto/md5"
"errors" "errors"
"fmt" "fmt"
"github.com/beego/bee/internal/pkg/utils" "github.com/beego/bee/internal/pkg/utils"
@ -10,6 +11,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"time" "time"
) )
@ -183,3 +185,41 @@ func getModelType(orm string) (inputType, goType, mysqlType, tag string) {
} }
return 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
}