浏览代码

add files

zhangjq 6 年之前
当前提交
49270fae02
共有 3 个文件被更改,包括 339 次插入0 次删除
  1. 174 0
      client/engineclient.go
  2. 39 0
      client/projectxml.go
  3. 126 0
      client/xsd.go

+ 174 - 0
client/engineclient.go

@@ -0,0 +1,174 @@
+package client
+
+import (
+	"bytes"
+	"fmt"
+	"io"
+	"mime/multipart"
+	"net/http"
+	"os"
+	"path/filepath"
+	"time"
+	"encoding/json"
+	"compress/gzip"
+	"io/ioutil"
+	"strings"
+	"git.qianqiusoft.com/qianqiusoft/light-apiengine/utils"
+)
+
+type EngineClient struct {
+	ProjectName string
+}
+
+type ResponeResult struct {
+	Code   int32 `json:"code"`
+	//描述
+	Msg   string `json:"msg"`
+	//数据
+	Data   []GenerateResult `json:"data"`
+}
+type GenerateResult struct {
+	Name string `json:"name"`
+	Content []byte `json:"content"`
+	Type string `json:"type"`
+}
+
+func NewEngineClient(project_name string) *EngineClient {
+	return &EngineClient{project_name}
+}
+
+func (c * EngineClient)InitDefalutFile(project_name string)  {
+	c.ProjectName = project_name
+	os.MkdirAll("controllers", os.ModePerm)
+	os.MkdirAll("models", os.ModePerm)
+	os.MkdirAll("routers", os.ModePerm)
+	os.MkdirAll("conf", os.ModePerm)
+	xml:=strings.Replace(DefaultProjectXML, "{project_name}", project_name,-1)
+	_,err:=os.Stat(project_name+".xml")
+	if os.IsNotExist(err){
+		ioutil.WriteFile(project_name+".xml",[]byte(xml), os.ModePerm)
+	}
+	ioutil.WriteFile(project_name+".xsd",[]byte(XSD), os.ModePerm)
+}
+
+func (c * EngineClient)GenerateCurrentProject() {
+	path,_:=utils.GetCurrentPath()
+	c.InitDefalutFile(c.ProjectName)
+	c.Generate(path+c.ProjectName+".xml")
+}
+
+func (c * EngineClient)Generate(xmlfile string)  {
+	time.Sleep(2000)
+	var result ResponeResult
+	bs := DoRequest(xmlfile)
+	if bs !=nil {
+		fmt.Println(string(bs.Bytes()))
+		err:=json.Unmarshal(bs.Bytes(), &result)
+		if err!=nil{
+			fmt.Println(err.Error())
+		}
+		for i := 0; i < len(result.Data); i++ {
+			var b bytes.Buffer
+			b.Write(result.Data[i].Content)
+			fmt.Println(b.Bytes())
+			unzip := unzipbytes(&b)
+			result.Data[i].Content = unzip.Bytes()
+		}
+		for i := 0; i < len(result.Data); i++ {
+			fmt.Println(string(result.Data[i].Content))
+			path:= result.Data[i].Name
+			path = strings.TrimLeft(path, c.ProjectName+"/")
+			ft:=result.Data[i].Type
+			if result.Data[i].Type == "main" {
+			}else if ft == "config"{
+				_,err := os.Stat("conf/app.conf")
+				if err == nil{
+					fmt.Println("配置文件已经存在,忽略...")
+				}else {
+					ioutil.WriteFile(path, result.Data[i].Content,os.ModePerm)
+				}
+			}else if ft =="controllers"{
+				if strings.Index(result.Data[i].Name, "_gen.go")>0 {
+					ioutil.WriteFile(path, result.Data[i].Content,os.ModePerm)
+				}else {
+					_, err := os.Stat(result.Data[i].Name)
+					if err==nil{
+						ioutil.WriteFile(path+"_new", result.Data[i].Content,os.ModePerm)
+					}else if os.IsNotExist(err){
+						ioutil.WriteFile(path, result.Data[i].Content,os.ModePerm)
+					}
+				}
+			}else {
+				err:=ioutil.WriteFile(path, result.Data[i].Content,os.ModePerm)
+				if err !=nil{
+					fmt.Println(err.Error())
+				}
+			}
+		}
+	}
+}
+
+
+func unzipbytes(bs *bytes.Buffer) bytes.Buffer{
+	r, _ := gzip.NewReader(bs)
+	defer r.Close()
+	var b bytes.Buffer
+	b.ReadFrom(r)
+	//undatas, _ := ioutil.ReadAll(r)
+	//fmt.Println("ungzip size:", len(undatas))
+	return b
+}
+
+func DoRequest(xmlfile string) *bytes.Buffer{
+	server:="http://ccbeetech.com:6166/api/v1/develop/generate"
+	request, err := newfileUploadRequest(server, nil, "xmlfile", xmlfile)
+	if err != nil {
+		fmt.Println(err)
+	}
+	client := &http.Client{}
+	resp, err := client.Do(request)
+	if err != nil {
+		fmt.Println(err)
+	} else {
+		body := &bytes.Buffer{}
+		_, err := body.ReadFrom(resp.Body)
+		if err != nil {
+			fmt.Println(err)
+		}
+		resp.Body.Close()
+		fmt.Println(resp.StatusCode)
+		fmt.Println(resp.Header)
+
+		//fmt.Println(body)
+		return  body
+	}
+	return nil
+}
+
+func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
+	file, err := os.Open(path)
+	if err != nil {
+		return nil, err
+	}
+	defer file.Close()
+
+	body := &bytes.Buffer{}
+	writer := multipart.NewWriter(body)
+	part, err := writer.CreateFormFile(paramName, filepath.Base(path))
+	if err != nil {
+		return nil, err
+	}
+	_, err = io.Copy(part, file)
+
+	for key, val := range params {
+		_ = writer.WriteField(key, val)
+	}
+	err = writer.Close()
+	if err != nil {
+		return nil, err
+	}
+
+	request, err := http.NewRequest("POST", uri, body)
+	request.Header.Add("Content-Type", writer.FormDataContentType())
+	return request, err
+}

+ 39 - 0
client/projectxml.go

@@ -0,0 +1,39 @@
+package client
+
+var DefaultProjectXML=`<?xml version="1.0" encoding="utf-8" ?>
+<application
+        xmlns="http://qianqiusoft.com/developer"
+        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+        xsi:schemaLocation="http://qianqiusoft.com/developer"
+        name="{project_name}"
+        desc="千秋开发平台" packagename="{project_name}">
+    <controllers>
+        <controller name="busniess" desc="开发平台">
+            <api name="generate" desc="生成数据" method="post">
+                <param name="hello" desc="配置文件" ref="$hello_bean"></param>
+                <return>
+                    <success ref="$sys_return"></success>
+                    <failure ref="$sys_return"></failure>
+                </return>
+            </api>
+        </controller>
+    </controllers>
+    <beans>
+        <bean name="hello_bean" desc="helloworld">
+            <prop name="id" type="string" caption="id"></prop>
+            <prop name="name" type="string" caption="name"></prop>
+        </bean>
+    </beans>
+    <tables>
+        <table name="hello" desc="hellworld">
+            <column isNull="false" isPK="true" name="id" caption="主键" dbtype="varchar(36)" type="string" size="36" />
+            <column isNull="false" name="user_id" caption="用户ID" type="string" size="36" dbtype="varchar(36)"/>
+            <column isNull="false" name="domain" caption="域" type="string" size="50" dbtype="varchar(50)"/>
+            <column isNull="false" name="create_by" caption="创建人" type="string" size="36" dbtype="varchar(36)"/>
+            <column isNull="false" name="create_time" caption="创建时间" type="datetime" />
+            <column isNull="false" name="last_update_by" caption="最后更新人" type="string" size="36" dbtype="varchar(36)"/>
+            <column isNull="false" name="last_update_date" caption="最后更新时间" type="datetime" />
+            <column isNull="false" name="del_flag" caption="是否删除 0:删除   1:正常" type="int32"/>
+        </table>
+    </tables>
+</application>`

+ 126 - 0
client/xsd.go

@@ -0,0 +1,126 @@
+package client
+
+var XSD=`<?xml version="1.0" standalone="yes"?>
+<xs:schema id="NewDataSet" xmlns=""
+           targetNamespace="http://ccbeetech.com:6166/api/develop/get_xsd"
+           xmlns:tns="http://ccbeetech.com:6166/api/develop/get_xsd"
+           xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
+  <xs:element name="application">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element name="controllers" minOccurs="0" maxOccurs="unbounded">
+          <xs:complexType>
+            <xs:sequence>
+              <xs:element name="controller" minOccurs="0" maxOccurs="unbounded">
+                <xs:complexType>
+                  <xs:sequence>
+                    <xs:element name="api" minOccurs="0" maxOccurs="unbounded">
+                      <xs:complexType>
+                        <xs:sequence>
+                          <xs:element name="param" minOccurs="0" maxOccurs="unbounded">
+                            <xs:complexType>
+                              <xs:attribute name="name" type="xs:string" />
+                              <xs:attribute name="ref" type="xs:string" />
+                              <xs:attribute name="desc" type="xs:string" />
+                              <xs:attribute name="type" type="xs:string" />
+                            </xs:complexType>
+                          </xs:element>
+                          <xs:element name="return" minOccurs="0" maxOccurs="unbounded">
+                            <xs:complexType>
+                              <xs:sequence>
+                                <xs:element name="success" minOccurs="0" maxOccurs="unbounded">
+                                  <xs:complexType>
+                                    <xs:attribute name="ref" type="xs:string" />
+                                  </xs:complexType>
+                                </xs:element>
+                                <xs:element name="failure" minOccurs="0" maxOccurs="unbounded">
+                                  <xs:complexType>
+                                    <xs:attribute name="ref" type="xs:string" />
+                                  </xs:complexType>
+                                </xs:element>
+                              </xs:sequence>
+                            </xs:complexType>
+                          </xs:element>
+                        </xs:sequence>
+                        <xs:attribute name="name" type="xs:string" />
+                        <xs:attribute name="desc" type="xs:string" />
+                        <xs:attribute name="method" type="xs:string" />
+                      </xs:complexType>
+                    </xs:element>
+                  </xs:sequence>
+                  <xs:attribute name="name" type="xs:string" />
+                  <xs:attribute name="desc" type="xs:string" />
+                </xs:complexType>
+              </xs:element>
+            </xs:sequence>
+          </xs:complexType>
+        </xs:element>
+        <xs:element name="beans" minOccurs="0" maxOccurs="unbounded">
+          <xs:complexType>
+            <xs:sequence>
+              <xs:element name="bean" minOccurs="0" maxOccurs="unbounded">
+                <xs:complexType>
+                  <xs:sequence>
+                    <xs:element name="prop" minOccurs="0" maxOccurs="unbounded">
+                      <xs:complexType>
+                        <xs:attribute name="name" type="xs:string" />
+                        <xs:attribute name="caption" type="xs:string" />
+                        <xs:attribute name="type" type="xs:string" />
+                      </xs:complexType>
+                    </xs:element>
+                  </xs:sequence>
+                  <xs:attribute name="name" type="xs:string" />
+                  <xs:attribute name="desc" type="xs:string" />
+                  <xs:attribute name="inher" type="xs:string" />
+                </xs:complexType>
+              </xs:element>
+            </xs:sequence>
+          </xs:complexType>
+        </xs:element>
+        <xs:element name="tables" minOccurs="0" maxOccurs="unbounded">
+          <xs:complexType>
+            <xs:sequence>
+              <xs:element name="table" minOccurs="0" maxOccurs="unbounded">
+                <xs:complexType>
+                  <xs:sequence>
+                    <xs:element name="column" minOccurs="0" maxOccurs="unbounded">
+                      <xs:complexType>
+                        <xs:sequence>
+                          <xs:element name="auto" minOccurs="0" maxOccurs="unbounded">
+                            <xs:complexType>
+                              <xs:attribute name="value" type="xs:string" />
+                              <xs:attribute name="update" type="xs:string" />
+                            </xs:complexType>
+                          </xs:element>
+                        </xs:sequence>
+                        <xs:attribute name="isNull" type="xs:string" />
+                        <xs:attribute name="isPK" type="xs:string" />
+                        <xs:attribute name="name" type="xs:string" />
+                        <xs:attribute name="caption" type="xs:string" />
+                        <xs:attribute name="dbtype" type="xs:string" />
+                        <xs:attribute name="type" type="xs:string" />
+                        <xs:attribute name="size" type="xs:string" />
+                      </xs:complexType>
+                    </xs:element>
+                  </xs:sequence>
+                  <xs:attribute name="name" type="xs:string" />
+                  <xs:attribute name="desc" type="xs:string" />
+                </xs:complexType>
+              </xs:element>
+            </xs:sequence>
+          </xs:complexType>
+        </xs:element>
+      </xs:sequence>
+      <xs:attribute name="name" type="xs:string" />
+      <xs:attribute name="desc" type="xs:string" />
+      <xs:attribute name="packagename" type="xs:string" />
+    </xs:complexType>
+  </xs:element>
+  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
+    <xs:complexType>
+      <xs:choice minOccurs="0" maxOccurs="unbounded">
+        <xs:element ref="application" />
+      </xs:choice>
+    </xs:complexType>
+  </xs:element>
+</xs:schema>`