Kaynağa Gözat

merge the controllers, beans and tables to main xml file

huangrf 6 yıl önce
ebeveyn
işleme
d58eee3db6
2 değiştirilmiş dosya ile 320 ekleme ve 3 silme
  1. 223 3
      client/engineclient.go
  2. 97 0
      client/struct.go

+ 223 - 3
client/engineclient.go

@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"compress/gzip"
 	"encoding/json"
+	"encoding/xml"
 	"fmt"
 	"io"
 	"io/ioutil"
@@ -40,7 +41,14 @@ func NewEngineClient(project_name string) *EngineClient {
 func (c *EngineClient) InitDefalutFile(project_name string) {
 	c.ProjectName = project_name
 	os.MkdirAll("controllers", os.ModePerm)
+	os.MkdirAll("controllers/gen", os.ModePerm)
+	os.MkdirAll("controllers/partial", os.ModePerm)
 	os.MkdirAll("models", os.ModePerm)
+	os.MkdirAll("models/modules", os.ModePerm)
+	os.MkdirAll("models/beans", os.ModePerm)
+	os.MkdirAll("models/sql", os.ModePerm)
+	os.MkdirAll("models/sql/conf", os.ModePerm)
+	os.MkdirAll("models/sql/vars", os.ModePerm)
 	os.MkdirAll("routers", os.ModePerm)
 	os.MkdirAll("conf", os.ModePerm)
 	os.MkdirAll("sqlconfig", os.ModePerm)
@@ -78,9 +86,18 @@ func (c *EngineClient) InitDefalutFile(project_name string) {
 }
 
 func (c *EngineClient) GenerateCurrentProject() {
-	path, _ := utils.GetCurrentPath()
 	c.InitDefalutFile(c.ProjectName)
-	c.Generate(path + c.ProjectName + ".xml")
+
+	// 0---0
+	//path, _ := utils.GetCurrentPath()
+	//c.Generate(path + c.ProjectName + ".xml")
+
+	projMainXmlFileTemp := c.MergeXmlToSingle()
+	if projMainXmlFileTemp == ""{
+		fmt.Println("projMainXmlFileTemp is empty")
+		return
+	}
+	c.Generate(projMainXmlFileTemp)
 }
 
 func (c *EngineClient) Generate(xmlfile string) {
@@ -140,6 +157,209 @@ func (c *EngineClient) Generate(xmlfile string) {
 	}
 }
 
+/**
+ * @brief: merge xml
+ * @param: none
+ * @return: the path of final xml
+ */
+func (c *EngineClient)MergeXmlToSingle()string{
+	path, _ := utils.GetCurrentPath()
+	// e.g.: c:/gopath/src/hanghua_background_proj
+	projDir := fmt.Sprintf("%s%s_proj", path, c.ProjectName)
+	projMainXmlFile := projDir + "/" + c.ProjectName + ".xml"
+	projMainXmlFileTemp := projDir + "/" + c.ProjectName + "_temp.xml"
+	_, err := os.Stat(projMainXmlFileTemp)
+	if os.IsNotExist(err) {
+		fmt.Println("-------------------------->remove project main file temp")
+		os.Remove(projMainXmlFileTemp)	// remove
+	}
+
+	_, err = os.Stat(projMainXmlFile)
+	if os.IsNotExist(err) {
+		fmt.Println("main xml file of " + c.ProjectName + " does not exist")
+		return ""
+	}
+
+	app := XmlApplication{}
+	bytess, _ := ioutil.ReadFile(projMainXmlFile)
+	err = xml.Unmarshal(bytess, &app)
+	if err != nil{
+		fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
+		return ""
+	}
+
+	controllers, err := scanControllers(projDir + "/controllers")
+	if err != nil{
+		fmt.Println("scanControllers error " + err.Error())
+		return ""
+	}
+
+	beans, err := scanBeans(projDir + "/beans")
+	if err != nil{
+		fmt.Println("scanBeans error " + err.Error())
+		return ""
+	}
+
+	tables, err := scanTables(projDir + "/tables")
+	if err != nil{
+		fmt.Println("scanTables error " + err.Error())
+		return ""
+	}
+
+	if app.Controllers.ControllerList == nil{
+		app.Controllers.ControllerList = []XmlController{}
+	}
+	for i := range controllers{
+		app.Controllers.ControllerList = append(app.Controllers.ControllerList, controllers[i])
+	}
+	if app.Beans.BeanList == nil{
+		app.Beans.BeanList = []XmlBean{}
+	}
+	for i := range beans{
+		app.Beans.BeanList = append(app.Beans.BeanList, beans[i])
+	}
+	if app.Tables.TableList == nil{
+		app.Tables.TableList = []XmlTable{}
+	}
+	for i := range tables{
+		app.Tables.TableList = append(app.Tables.TableList, tables[i])
+	}
+
+	bytess, err = xml.Marshal(app)
+	if err != nil{
+		fmt.Println("xml.Marshal(app) error " + err.Error())
+		return ""
+	}
+	err  = ioutil.WriteFile(projMainXmlFileTemp, bytess,os.ModePerm)
+	if err != nil{
+		fmt.Println("ioutil.WriteFile(projMainXmlFileTemp, bytess,os.ModePerm) error " + err.Error())
+		return ""
+	}
+	_, err = os.Stat(projMainXmlFileTemp)
+	if os.IsNotExist(err) {
+		fmt.Println("main xml file temp of " + c.ProjectName + " does not exist")
+		return ""
+	}else{
+		fmt.Println("main xml file temp of " + c.ProjectName + " exist")
+	}
+
+	return projMainXmlFileTemp
+}
+
+func scanControllers(ctrldir string) ([]XmlController, error){
+	_, err := os.Stat(ctrldir)
+	if os.IsNotExist(err) {
+		fmt.Println("controller dir does not exist")
+		return nil, err
+	}
+
+	controllers := []XmlController{}
+	err = filepath.Walk(ctrldir, func(path string, info os.FileInfo, err error) error {
+
+		if err != nil {
+			return filepath.SkipDir
+		}
+		if info.IsDir(){
+			return nil
+		}
+
+		ctrl := XmlController{}
+		ctrlfile := ctrldir + "/" + info.Name()
+		_, err = os.Stat(ctrlfile)
+		if os.IsNotExist(err) {
+			fmt.Println("controller xml file " + ctrlfile + " does not exist")
+			return filepath.SkipDir
+		}
+		bytess, _ := ioutil.ReadFile(ctrlfile)
+		err = xml.Unmarshal(bytess, &ctrl)
+		if err != nil{
+			fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
+			return filepath.SkipDir
+		}
+		controllers = append(controllers, ctrl)
+
+		return nil
+	})
+
+	return controllers, err
+}
+
+func scanBeans(ctrldir string) ([]XmlBean, error){
+	_, err := os.Stat(ctrldir)
+	if os.IsNotExist(err) {
+		fmt.Println("controller dir does not exist")
+		return nil, err
+	}
+
+	beans := []XmlBean{}
+	err = filepath.Walk(ctrldir, func(path string, info os.FileInfo, err error) error {
+
+		if err != nil {
+			return filepath.SkipDir
+		}
+		if info.IsDir(){
+			return nil
+		}
+
+		bean := XmlBean{}
+		beanfile := ctrldir + "/" + info.Name()
+		_, err = os.Stat(beanfile)
+		if os.IsNotExist(err) {
+			fmt.Println("controller xml file " + beanfile + " does not exist")
+			return filepath.SkipDir
+		}
+		bytess, _ := ioutil.ReadFile(beanfile)
+		err = xml.Unmarshal(bytess, &bean)
+		if err != nil{
+			fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
+			return filepath.SkipDir
+		}
+		beans = append(beans, bean)
+
+		return nil
+	})
+
+	return beans, err
+}
+
+func scanTables(ctrldir string) ([]XmlTable, error){
+	_, err := os.Stat(ctrldir)
+	if os.IsNotExist(err) {
+		fmt.Println("controller dir does not exist")
+		return nil, err
+	}
+
+	tables := []XmlTable{}
+	err = filepath.Walk(ctrldir, func(path string, info os.FileInfo, err error) error {
+
+		if err != nil {
+			return filepath.SkipDir
+		}
+		if info.IsDir(){
+			return nil
+		}
+
+		table := XmlTable{}
+		tablefile := ctrldir + "/" + info.Name()
+		_, err = os.Stat(tablefile)
+		if os.IsNotExist(err) {
+			fmt.Println("controller xml file " + tablefile + " does not exist")
+			return filepath.SkipDir
+		}
+		bytess, _ := ioutil.ReadFile(tablefile)
+		err = xml.Unmarshal(bytess, &table)
+		if err != nil{
+			fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
+			return filepath.SkipDir
+		}
+		tables = append(tables, table)
+
+		return nil
+	})
+
+	return tables, err
+}
+
 func unzipbytes(bs *bytes.Buffer) bytes.Buffer {
 	r, _ := gzip.NewReader(bs)
 	defer r.Close()
@@ -151,7 +371,7 @@ func unzipbytes(bs *bytes.Buffer) bytes.Buffer {
 }
 
 func DoRequest(xmlfile string) *bytes.Buffer {
-	server := "http://qianqiusoft.com:6166/api/v1/develop/generate"
+	server := "http://localhost:8080/api/v1/develop/generate"//"http://qianqiusoft.com:6166/api/v1/develop/generate"
 	request, err := newfileUploadRequest(server, nil, "xmlfile", xmlfile)
 	if err != nil {
 		fmt.Println(err)

+ 97 - 0
client/struct.go

@@ -0,0 +1,97 @@
+package client
+
+type XmlApplication struct {
+	ApplicationName string `xml:"name,attr"`
+	PackageName string `xml:"packagename,attr"`
+	Desc         string `xml:"desc,attr"`
+	Controllers XmlControllers `xml:"controllers"`
+	Tables      XmlTables      `xml:"tables"`
+	Beans 		XmlBeans       `xml:"beans"`
+}
+
+type XmlControllers struct {
+	ControllerList []XmlController `xml:"controller"`
+}
+
+type XmlController struct {
+	Name string `xml:"name,attr"`
+	Desc         string `xml:"desc,attr"`
+	Apis []XmlApi `xml:"api"`
+	ApplicationName string `xml:"-"`
+}
+
+type XmlApi struct {
+	Name string `xml:"name,attr"`
+	Desc string `xml:"desc,attr"`
+	Method string `xml:"method,attr"`
+	ParamList []XmlApiParam `xml:"param"`
+	Return XmlReturn `xml:"return"`
+}
+
+type XmlApiParam struct {
+	Name string `xml:"name,attr"`
+	TransType string `xml:"trans-type,attr"`
+	Type string `xml:"type,attr"`
+	Desc string `xml:"desc,attr"`
+	Ref string `xml:"ref,attr"`
+	Must bool `xml:"must,attr"`
+	DefaultValue string `xml:"default-value,attr"`
+}
+
+type XmlReturn struct {
+	Success XmlSuccess `xml:"success"`
+	Failure XmlFailure `xml:"failure"`
+}
+
+type XmlSuccess struct {
+	Ref string `xml:"ref,attr"`
+	Desc string `xml:"desc,attr"`
+}
+
+type XmlFailure struct {
+	Ref string `xml:"ref,attr"`
+	Desc string `xml:"desc,attr"`
+}
+
+//
+type XmlTables struct {
+	TableList []XmlTable `xml:"table"`
+}
+
+type XmlTable struct {
+	Name           string `xml:"name,attr"`
+	Desc           string `xml:"desc,attr"`
+	ImportDateTime bool
+	ColumnList        []XmlColumn `xml:"column"`
+}
+
+type XmlColumn struct {
+	Name         string `xml:"name,attr"`
+	Caption      string `xml:"caption,attr"`
+	IsNull       bool   `xml:"isNull,attr"`
+	IsPK         bool   `xml:"isPK,attr"`
+	IsUnique     bool   `xml:"isUnique,attr"`
+	Size         int    `xml:"size,attr"`
+	Type         string `xml:"type,attr"`
+	DbType       string `xml:"dbtype,attr"`
+	DefaultValue string `xml:"default-value,attr"`
+}
+
+type XmlBeans struct {
+	BeanList []XmlBean `xml:"bean"`
+}
+
+type XmlBean struct {
+	Name           string `xml:"name,attr"`
+	Desc           string `xml:"desc,attr"`
+	Inher		 string `xml:"inher,attr"`
+	ImportDateTime bool
+	PropList        []XmlProp `xml:"prop"`
+}
+
+type XmlProp struct {
+	Name         string `xml:"name,attr"`
+	Caption      string `xml:"caption,attr"`
+	Type         string `xml:"type,attr"`
+	DefaultValue string `xml:"default-value,attr"`
+}