mirror of
https://github.com/beego/bee.git
synced 2024-11-22 15:10:54 +00:00
Compare content after formatting
This commit is contained in:
parent
610cd901bb
commit
528b75f644
@ -1,11 +1,13 @@
|
|||||||
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"
|
||||||
@ -120,15 +122,27 @@ 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
|
||||||
if len(orgContent) == 0 || FileContentChange(string(orgContent),buf) {
|
output := []byte(buf)
|
||||||
err = r.write(r.FlushFile, 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 {
|
if err != nil {
|
||||||
beeLogger.Log.Fatalf("Could not create file: %s", err)
|
beeLogger.Log.Fatalf("Could not create file: %s", err)
|
||||||
return
|
return
|
||||||
|
@ -6,18 +6,16 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/beego/bee/internal/pkg/utils"
|
"github.com/beego/bee/internal/pkg/utils"
|
||||||
beeLogger "github.com/beego/bee/logger"
|
beeLogger "github.com/beego/bee/logger"
|
||||||
"go/format"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"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 string) (err error) {
|
func (c *RenderFile) write(filename string, buf []byte) (err error) {
|
||||||
if utils.IsExist(filename) && !isNeedOverwrite(filename) {
|
if utils.IsExist(filename) && !isNeedOverwrite(filename) {
|
||||||
return
|
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"))
|
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)
|
beeLogger.Log.Infof("bak file '%s'", bakName)
|
||||||
if err := os.Rename(filename, bakName); err != nil {
|
if err := os.Rename(filename, bakName); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
err = errors.New("file is bak error, path is " + bakName)
|
err = errors.New("file is bak error, path is " + bakName)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -54,20 +53,7 @@ func (c *RenderFile) write(filename string, buf string) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
output := []byte(buf)
|
err = ioutil.WriteFile(filename, buf, 0644)
|
||||||
|
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errors.New("write write file " + err.Error())
|
err = errors.New("write write file " + err.Error())
|
||||||
return
|
return
|
||||||
@ -186,34 +172,19 @@ func getModelType(orm string) (inputType, goType, mysqlType, tag string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func FileContentChange(org,new string) bool {
|
func FileContentChange(org,new []byte) bool {
|
||||||
if org == "" {
|
if len(org) == 0 {
|
||||||
return false
|
return true
|
||||||
}
|
}
|
||||||
var orgContent,newContent string
|
var orgContent,newContent string
|
||||||
jump := false
|
for i, s := range strings.Split(string(org), "\n") {
|
||||||
// expect tab character and blank space and "import(***)"
|
if i > 1 {
|
||||||
reg := regexp.MustCompile("\\s+")
|
orgContent += 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") {
|
for i, s := range strings.Split(string(new), "\n") {
|
||||||
if s == "import (" {
|
if i > 1 {
|
||||||
jump = true
|
newContent += s
|
||||||
}
|
|
||||||
if jump && s == ")" {
|
|
||||||
jump = false
|
|
||||||
}
|
|
||||||
if i > 2 && !jump {
|
|
||||||
newContent += reg.ReplaceAllString(s, "")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
orgMd5 := md5.Sum([]byte(orgContent))
|
orgMd5 := md5.Sum([]byte(orgContent))
|
||||||
@ -221,5 +192,6 @@ func FileContentChange(org,new string) 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
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user