123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590 |
- package main
- import (
- "bytes"
- "compress/gzip"
- "encoding/json"
- "encoding/xml"
- "fmt"
- "io"
- "io/ioutil"
- "mime/multipart"
- "net/http"
- "os"
- "path/filepath"
- "strings"
- )
- type EngineClient struct {
- ProjectName string
- ServerUrl 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, server_url string) *EngineClient {
- return &EngineClient{project_name, server_url}
- }
- func (c *EngineClient) InitDefalutFile(project_name string) {
- c.ProjectName = project_name
- os.MkdirAll("controllers/gen", os.ModePerm)
- os.MkdirAll("controllers/partial", os.ModePerm)
- os.MkdirAll("models", os.ModePerm)
- os.MkdirAll("routers", os.ModePerm)
- os.MkdirAll("conf", os.ModePerm)
- os.MkdirAll("sqlconfig", os.ModePerm)
- //os.MkdirAll("sqlconfig/gen", os.ModePerm)
- os.MkdirAll("doc", os.ModePerm)
- os.MkdirAll("web", os.ModePerm)
- _, 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)
- }
- _, err = os.Stat(project_name + ".xsd")
- if os.IsNotExist(err) {
- ioutil.WriteFile(project_name+".xsd", []byte(XSD), os.ModePerm)
- }
- _, err = os.Stat("Dockerfile")
- if os.IsNotExist(err) {
- xml := strings.Replace(DockerFile, "{project_name}", project_name, -1)
- ioutil.WriteFile("Dockerfile", []byte(xml), os.ModePerm)
- }
- _, 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)
- }
- _, 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)
- }
- }
- func (c *EngineClient) GenerateCurrentProject() (app *XmlApplication) {
- c.InitDefalutFile(c.ProjectName)
- // 0---0
- //path, _ := utils.GetCurrentPath()
- //c.Generate(path + c.ProjectName + ".xml")
- projMainXmlFileTemp := ""
- projMainXmlFileTemp, app = c.MergeXmlToSingle()
- if projMainXmlFileTemp == "" {
- fmt.Println("projMainXmlFileTemp is empty")
- return
- }
- c.Generate(projMainXmlFileTemp)
- path, _ := GetCurrentPath()
- CopyDir(os.Getenv("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",
- "../git.qianqiusoft.com/qianqiusoft/light-apiengine/sqlconfig",
- path+"sqlconfig")
- return
- }
- func (c *EngineClient) GenerateToPath(xmlfile string, dest_path string) {
- var result ResponeResult
- server := "http://qianqiusoft.com:6166"
- if c.ServerUrl != "" {
- server = c.ServerUrl
- }
- server += "/api/v1/develop/generate"
- bs := DoRequest(xmlfile, server)
- if bs != nil {
- 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)
- unzip := unzipbytes(&b)
- result.Data[i].Content = unzip.Bytes()
- }
- for i := 0; i < len(result.Data); i++ {
- path := result.Data[i].Name
- //fmt.Println(path)
- path = path[len(c.ProjectName)+1:]
- path = dest_path + path
- fmt.Println(path)
- ft := result.Data[i].Type
- if result.Data[i].Type == "main" {
- } else if ft == "config" || ft == "ci" {
- _, err := os.Stat(path)
- if err == nil {
- fmt.Println(path + "已经存在,忽略...")
- } else {
- ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
- }
- } 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)
- } else {
- _, err := os.Stat(path)
- 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 if ft == "routers" {
- os.MkdirAll(filepath.Dir(path), os.ModePerm)
- ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
- } 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)
- } else {
- _, err := os.Stat(path)
- if err == nil {
- //ioutil.WriteFile(path+"_new", result.Data[i].Content, os.ModePerm)
- } else if os.IsNotExist(err) {
- os.MkdirAll(filepath.Dir(path), os.ModePerm)
- ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
- }
- }
- } else {
- os.MkdirAll(filepath.Dir(path), os.ModePerm)
- err := ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
- if err != nil {
- fmt.Println(err.Error())
- }
- }
- }
- }
- }
- func (c *EngineClient) Generate(xmlfile string) {
- c.GenerateToPath(xmlfile, "")
- //c.GenSwagger(xmlfile)
- }
- func (c *EngineClient) GenSwagger(xmlfile string) {
- server := "http://swagger.pusher.i2erp.cn"
- server += "/api/v1/upload"
- fmt.Println("===========================================================================>11")
- 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.Error())
- } else {
- defer resp.Body.Close()
- bytess, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println("=======================================>ioutil.ReadAll error", err.Error())
- } else {
- fmt.Println("=======================================>", string(bytess))
- }
- }
- }
- /**
- * @brief: merge xml
- * @param: none
- * @return: the path of final xml
- */
- func (c *EngineClient) MergeXmlToSingle() (projMainXmlFileTemp string, app *XmlApplication) {
- path, _ := 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 "", nil
- }
- app = &XmlApplication{}
- bytess, _ := ioutil.ReadFile(projMainXmlFile)
- err = xml.Unmarshal(bytess, app)
- if err != nil {
- fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
- return "", nil
- }
- controllers, err := scanControllers(projDir + "/controllers")
- if err != nil {
- fmt.Println("scanControllers error " + err.Error())
- return "", nil
- }
- beans, err := scanBeans(projDir + "/beans")
- if err != nil {
- fmt.Println("scanBeans error " + err.Error())
- return "", nil
- }
- tables, err := scanTables(projDir + "/tables")
- if err != nil {
- fmt.Println("scanTables error " + err.Error())
- return "", nil
- }
- if app.Controllers.ControllerList == nil {
- app.Controllers.ControllerList = []XmlController{}
- }
- for i := range controllers {
- app.Controllers.ControllerList = append(app.Controllers.ControllerList, controllers[i])
- }
- createVueApisFolder(app.Controllers.ControllerList)
- 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 "", nil
- }
- err = ioutil.WriteFile(projMainXmlFileTemp, bytess, os.ModePerm)
- if err != nil {
- fmt.Println("ioutil.WriteFile(projMainXmlFileTemp, bytess,os.ModePerm) error " + err.Error())
- return "", nil
- }
- _, err = os.Stat(projMainXmlFileTemp)
- if os.IsNotExist(err) {
- fmt.Println("main xml file temp of " + c.ProjectName + " does not exist")
- return "", nil
- } else {
- fmt.Println("main xml file temp of " + c.ProjectName + " exist")
- }
- return
- }
- func scanControllers(ctrldir string) ([]XmlController, error) {
- _, err := os.Stat(ctrldir)
- if os.IsNotExist(err) {
- fmt.Println("controller dir does not exist", err.Error())
- return nil, err
- }
- controllers := []XmlController{}
- filePaths := []string{}
- filePaths, err = getAllFile(strings.TrimSuffix(ctrldir, "/"), filePaths)
- if err != nil {
- fmt.Println("controller getAllFile error", err.Error())
- return nil, err
- }
- for i := range filePaths {
- ctrl := XmlController{}
- ctrlfile := filePaths[i]
- _, err = os.Stat(ctrlfile)
- if os.IsNotExist(err) {
- fmt.Println("controller xml file " + ctrlfile + " does not exist")
- return controllers, err
- }
- ctrlfile = strings.Replace(ctrlfile, "\\", "/", -1)
- arr := strings.Split(ctrlfile, "/")
- bytess, _ := ioutil.ReadFile(ctrlfile)
- err = xml.Unmarshal(bytess, &ctrl)
- if err != nil {
- fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
- return controllers, err
- }
- // get sub dir name
- if arr[len(arr)-2] != "controllers" {
- // if sub dir is not controllers, set the dir attr
- ctrl.Dir = arr[len(arr)-2]
- }
- controllers = append(controllers, ctrl)
- }
- return controllers, nil
- //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{}
- filePaths := []string{}
- filePaths, err = getAllFile(strings.TrimSuffix(ctrldir, "/"), filePaths)
- if err != nil {
- fmt.Println("controller getAllFile error", err.Error())
- return nil, err
- }
- for i := range filePaths {
- ctrl := XmlBean{}
- ctrlfile := filePaths[i]
- _, err = os.Stat(ctrlfile)
- if os.IsNotExist(err) {
- fmt.Println("controller xml file " + ctrlfile + " does not exist")
- return beans, err
- }
- bytess, _ := ioutil.ReadFile(ctrlfile)
- err = xml.Unmarshal(bytess, &ctrl)
- if err != nil {
- fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
- return beans, err
- }
- beans = append(beans, ctrl)
- }
- return beans, nil
- //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{}
- filePaths := []string{}
- filePaths, err = getAllFile(strings.TrimSuffix(ctrldir, "/"), filePaths)
- if err != nil {
- fmt.Println("controller getAllFile error", err.Error())
- return nil, err
- }
- for i := range filePaths {
- ctrl := XmlTable{}
- ctrlfile := filePaths[i]
- _, err = os.Stat(ctrlfile)
- if os.IsNotExist(err) {
- fmt.Println("controller xml file " + ctrlfile + " does not exist")
- return tables, err
- }
- bytess, _ := ioutil.ReadFile(ctrlfile)
- err = xml.Unmarshal(bytess, &ctrl)
- if err != nil {
- fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
- return tables, err
- }
- tables = append(tables, ctrl)
- }
- return tables, nil
- //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 createVueApisFolder(controllers []XmlController) {
- for i := range controllers {
- fmt.Println("------------------------------>" + controllers[i].Name)
- os.MkdirAll("vue/api/modules/"+controllers[i].Name, os.ModePerm)
- }
- }
- func getAllFile(pathname string, s []string) ([]string, error) {
- rd, err := ioutil.ReadDir(pathname)
- if err != nil {
- fmt.Println("read dir fail:", err)
- return s, err
- }
- for _, fi := range rd {
- if fi.IsDir() {
- fullDir := pathname + "/" + fi.Name()
- s, err = getAllFile(fullDir, s)
- if err != nil {
- fmt.Println("read dir fail:", err)
- return s, err
- }
- } else {
- if strings.HasSuffix(strings.ToLower(fi.Name()), ".xml") {
- fullName := pathname + "/" + fi.Name()
- s = append(s, fullName)
- }
- }
- }
- return s, nil
- }
- 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, server string) *bytes.Buffer {
- 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
- }
|