1
0
mirror of https://github.com/beego/bee.git synced 2024-11-21 13:40:53 +00:00

Revert "Compare content after formatting"

This reverts commit 174d2ab2e8.
This commit is contained in:
askuy 2020-07-27 13:09:56 +08:00 committed by GitHub
parent e6c630ea75
commit 677987a7d9
2 changed files with 29 additions and 28 deletions

View File

@ -1,13 +1,11 @@
package beegopro package beegopro
import ( import (
"errors"
"github.com/beego/bee/internal/pkg/system" "github.com/beego/bee/internal/pkg/system"
beeLogger "github.com/beego/bee/logger" beeLogger "github.com/beego/bee/logger"
"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"
"go/format"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
@ -122,27 +120,15 @@ func (r *RenderFile) Exec(name string) {
var orgContent []byte var orgContent []byte
if err == nil { if err == nil {
if org, err := os.OpenFile(r.Descriptor.DstPath, os.O_RDONLY, 0666); err == nil { if org, err := os.OpenFile(r.Descriptor.DstPath, os.O_RDONLY, 0666); err == nil {
defer org.Close()
orgContent,_ = ioutil.ReadAll(org) orgContent,_ = ioutil.ReadAll(org)
org.Close()
} else { } else {
beeLogger.Log.Infof("file err %s", err) beeLogger.Log.Infof("file err %s", err)
} }
} }
// Replace or create when content changes // Replace or create when content changes
output := []byte(buf) if len(orgContent) == 0 || FileContentChange(string(orgContent),buf) {
if r.Option.EnableFormat && filepath.Ext(r.FlushFile) == ".go" { err = r.write(r.FlushFile, buf)
// 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 { if err != nil {
beeLogger.Log.Fatalf("Could not create file: %s", err) beeLogger.Log.Fatalf("Could not create file: %s", err)
return return

View File

@ -11,12 +11,13 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"time" "time"
) )
// write to file // 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) { if utils.IsExist(filename) && !isNeedOverwrite(filename) {
return return
} }
@ -190,19 +191,34 @@ func getModelType(orm string) (inputType, goType, mysqlType, tag string) {
return return
} }
func FileContentChange(org,new []byte) bool { func FileContentChange(org,new string) bool {
if len(org) == 0 { if org == "" {
return true return false
} }
var orgContent,newContent string var orgContent,newContent string
for i, s := range strings.Split(string(org), "\n") { jump := false
if i > 1 { // expect tab character and blank space and "import***"
orgContent += s 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") { for i, s := range strings.Split(new, "\n") {
if i > 1 { if s == "import (" {
newContent += s jump = true
}
if jump && s == ")" {
jump = false
}
if i > 2 && !jump {
newContent += reg.ReplaceAllString(s, "")
} }
} }
orgMd5 := md5.Sum([]byte(orgContent)) orgMd5 := md5.Sum([]byte(orgContent))
@ -210,6 +226,5 @@ func FileContentChange(org,new []byte) bool {
if orgMd5 != newMd5 { if orgMd5 != newMd5 {
return true return true
} }
beeLogger.Log.Infof("File has no change in the content")
return false return false
} }