|
|
@@ -3,6 +3,7 @@ package client
|
|
|
import (
|
|
|
"bytes"
|
|
|
"compress/gzip"
|
|
|
+ "crypto/md5"
|
|
|
"encoding/json"
|
|
|
"encoding/xml"
|
|
|
"fmt"
|
|
|
@@ -55,30 +56,30 @@ func (c *EngineClient) InitDefalutFile(project_name string) {
|
|
|
_, err := os.Stat(project_name + ".xml")
|
|
|
if os.IsNotExist(err) {
|
|
|
xml := strings.Replace(DefaultProjectXML, "{project_name}", project_name, -1)
|
|
|
- ioutil.WriteFile(project_name+".xml", []byte(xml), os.ModePerm)
|
|
|
+ TryWriteFile(project_name+".xml", []byte(xml))
|
|
|
}
|
|
|
|
|
|
_, err = os.Stat(project_name + ".xsd")
|
|
|
if os.IsNotExist(err) {
|
|
|
- ioutil.WriteFile(project_name+".xsd", []byte(XSD), os.ModePerm)
|
|
|
+ TryWriteFile(project_name+".xsd", []byte(XSD))
|
|
|
}
|
|
|
|
|
|
_, err = os.Stat("Dockerfile")
|
|
|
if os.IsNotExist(err) {
|
|
|
xml := strings.Replace(DockerFile, "{project_name}", project_name, -1)
|
|
|
- ioutil.WriteFile("Dockerfile", []byte(xml), os.ModePerm)
|
|
|
+ TryWriteFile("Dockerfile", []byte(xml))
|
|
|
}
|
|
|
_, err = os.Stat("build_docker.sh")
|
|
|
if os.IsNotExist(err) {
|
|
|
xml := strings.Replace(BuildSH, "{project_name}", project_name, -1)
|
|
|
- ioutil.WriteFile("build_docker.sh", []byte(xml), os.ModePerm)
|
|
|
+ TryWriteFile("build_docker.sh", []byte(xml))
|
|
|
}
|
|
|
|
|
|
_, err = os.Stat("README.md")
|
|
|
if os.IsNotExist(err) {
|
|
|
xml := strings.Replace(MARK_DOWN, "{project_name}", project_name, -1)
|
|
|
xml = strings.Replace(xml, "{!}", "`", -1)
|
|
|
- ioutil.WriteFile("README.md", []byte(xml), os.ModePerm)
|
|
|
+ TryWriteFile("README.md", []byte(xml))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -103,10 +104,10 @@ func (c *EngineClient) GenerateCurrentProjectToPath(destPath string) {
|
|
|
//c.GenSwagger(projMainXmlFileTemp)
|
|
|
|
|
|
path, _ := utils.GetCurrentPath()
|
|
|
- CopyDir(env.Get("GOPATH", "") + "/src/git.qianqiusoft.com/qianqiusoft/light-apiengine/sqlconfig",
|
|
|
+ CopyDir(env.Get("GOPATH", "")+"/src/git.qianqiusoft.com/qianqiusoft/light-apiengine/sqlconfig",
|
|
|
path+"vendor/git.qianqiusoft.com/qianqiusoft/light-apiengine/sqlconfig",
|
|
|
"./vendor/git.qianqiusoft.com/qianqiusoft/light-apiengine/sqlconfig",
|
|
|
- "../src/git.qianqiusoft.com/qianqiusoft/light-apiengine/sqlconfig",
|
|
|
+ "../src/git.qianqiusoft.com/qianqiusoft/light-apiengine/sqlconfig",
|
|
|
path+"sqlconfig")
|
|
|
}
|
|
|
|
|
|
@@ -163,39 +164,39 @@ func (c *EngineClient) GenerateToPath(xmlfile string, dest_path string) {
|
|
|
fmt.Println(path + "已经存在,忽略...")
|
|
|
// fmt.Println(result.Data[i].Content)
|
|
|
} else {
|
|
|
- ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
|
|
|
+ TryWriteFile(path, result.Data[i].Content)
|
|
|
}
|
|
|
} else if ft == "controllers" {
|
|
|
os.MkdirAll(filepath.Dir(path), os.ModePerm)
|
|
|
if strings.Index(path, "_gen.go") > 0 {
|
|
|
- ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
|
|
|
+ TryWriteFile(path, result.Data[i].Content)
|
|
|
} else {
|
|
|
_, err := os.Stat(path)
|
|
|
if err == nil {
|
|
|
- ioutil.WriteFile(path+"_new", result.Data[i].Content, os.ModePerm)
|
|
|
+ TryWriteFile(path+"_new", result.Data[i].Content)
|
|
|
} else if os.IsNotExist(err) {
|
|
|
- ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
|
|
|
+ TryWriteFile(path, result.Data[i].Content)
|
|
|
}
|
|
|
}
|
|
|
} else if ft == "routers" {
|
|
|
os.MkdirAll(filepath.Dir(path), os.ModePerm)
|
|
|
- ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
|
|
|
+ TryWriteFile(path, result.Data[i].Content)
|
|
|
} else if ft == "sql" {
|
|
|
if strings.Index(path, "_gen.xml") > 0 {
|
|
|
os.MkdirAll(filepath.Dir(path), os.ModePerm)
|
|
|
- ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
|
|
|
+ TryWriteFile(path, result.Data[i].Content)
|
|
|
} else {
|
|
|
_, err := os.Stat(path)
|
|
|
if err == nil {
|
|
|
- //ioutil.WriteFile(path+"_new", result.Data[i].Content, os.ModePerm)
|
|
|
+ //TryWriteFile(path+"_new", result.Data[i].Content)
|
|
|
} else if os.IsNotExist(err) {
|
|
|
os.MkdirAll(filepath.Dir(path), os.ModePerm)
|
|
|
- ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
|
|
|
+ TryWriteFile(path, result.Data[i].Content)
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
os.MkdirAll(filepath.Dir(path), os.ModePerm)
|
|
|
- err := ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
|
|
|
+ err := TryWriteFile(path, result.Data[i].Content)
|
|
|
if err != nil {
|
|
|
fmt.Println(err.Error())
|
|
|
}
|
|
|
@@ -203,6 +204,28 @@ func (c *EngineClient) GenerateToPath(xmlfile string, dest_path string) {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+func TryWriteFile(fileName string, dataB []byte) (err error) {
|
|
|
+ var dataA []byte
|
|
|
+ _, err = os.Stat(fileName)
|
|
|
+ needWriteFile := false
|
|
|
+ if err == nil {
|
|
|
+ //文件存在
|
|
|
+ dataA, err = ioutil.ReadFile(fileName)
|
|
|
+ md5A := md5.Sum(dataA)
|
|
|
+ md5B := md5.Sum(dataB)
|
|
|
+ if md5A != md5B {
|
|
|
+ needWriteFile = true
|
|
|
+ }
|
|
|
+ } else if os.IsNotExist(err) {
|
|
|
+ //文件不存在
|
|
|
+ needWriteFile = true
|
|
|
+ err = nil
|
|
|
+ }
|
|
|
+ if needWriteFile {
|
|
|
+ err = ioutil.WriteFile(fileName, dataB, os.ModePerm)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
|
|
|
/*
|
|
|
func (c *EngineClient) Generate(xmlfile string) {
|
|
|
@@ -311,9 +334,9 @@ func (c *EngineClient) MergeXmlToSingle() string {
|
|
|
fmt.Println("xml.Marshal(app) error " + err.Error())
|
|
|
return ""
|
|
|
}
|
|
|
- err = ioutil.WriteFile(projMainXmlFileTemp, bytess, os.ModePerm)
|
|
|
+ err = TryWriteFile(projMainXmlFileTemp, bytess)
|
|
|
if err != nil {
|
|
|
- fmt.Println("ioutil.WriteFile(projMainXmlFileTemp, bytess,os.ModePerm) error " + err.Error())
|
|
|
+ fmt.Println("TryWriteFile(projMainXmlFileTemp, bytess) error " + err.Error())
|
|
|
return ""
|
|
|
}
|
|
|
_, err = os.Stat(projMainXmlFileTemp)
|