engineclient.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. package client
  2. import (
  3. "bytes"
  4. "compress/gzip"
  5. "encoding/json"
  6. "encoding/xml"
  7. "fmt"
  8. "git.qianqiusoft.com/qianqiusoft/light-apiengine/env"
  9. "git.qianqiusoft.com/qianqiusoft/light-apiengine/utils"
  10. "git.qianqiusoft.com/qianqiusoft/light-apiengine-client/code_gen"
  11. "io"
  12. "io/ioutil"
  13. "mime/multipart"
  14. "net/http"
  15. "os"
  16. "path/filepath"
  17. "strings"
  18. )
  19. type EngineClient struct {
  20. ProjectName string
  21. ServerUrl string
  22. }
  23. type ResponeResult struct {
  24. Code int32 `json:"code"`
  25. //描述
  26. Msg string `json:"msg"`
  27. //数据
  28. Data []GenerateResult `json:"data"`
  29. }
  30. type GenerateResult struct {
  31. Name string `json:"name"`
  32. Content []byte `json:"content"`
  33. Type string `json:"type"`
  34. }
  35. func NewEngineClient(project_name string, server_url string) *EngineClient {
  36. return &EngineClient{project_name, server_url}
  37. }
  38. func (c *EngineClient) InitDefalutFile(project_name string) {
  39. c.ProjectName = project_name
  40. os.MkdirAll("controllers/gen", os.ModePerm)
  41. os.MkdirAll("controllers/partial", os.ModePerm)
  42. os.MkdirAll("models", os.ModePerm)
  43. os.MkdirAll("routers", os.ModePerm)
  44. os.MkdirAll("conf", os.ModePerm)
  45. os.MkdirAll("sqlconfig", os.ModePerm)
  46. //os.MkdirAll("sqlconfig/gen", os.ModePerm)
  47. os.MkdirAll("doc", os.ModePerm)
  48. os.MkdirAll("web", os.ModePerm)
  49. _, err := os.Stat(project_name + ".xml")
  50. if os.IsNotExist(err) {
  51. xml := strings.Replace(DefaultProjectXML, "{project_name}", project_name, -1)
  52. ioutil.WriteFile(project_name+".xml", []byte(xml), os.ModePerm)
  53. }
  54. _, err = os.Stat(project_name + ".xsd")
  55. if os.IsNotExist(err) {
  56. ioutil.WriteFile(project_name+".xsd", []byte(XSD), os.ModePerm)
  57. }
  58. _, err = os.Stat("Dockerfile")
  59. if os.IsNotExist(err) {
  60. xml := strings.Replace(DockerFile, "{project_name}", project_name, -1)
  61. ioutil.WriteFile("Dockerfile", []byte(xml), os.ModePerm)
  62. }
  63. _, err = os.Stat("build_docker.sh")
  64. if os.IsNotExist(err) {
  65. xml := strings.Replace(BuildSH, "{project_name}", project_name, -1)
  66. ioutil.WriteFile("build_docker.sh", []byte(xml), os.ModePerm)
  67. }
  68. _, err = os.Stat("README.md")
  69. if os.IsNotExist(err) {
  70. xml := strings.Replace(MARK_DOWN, "{project_name}", project_name, -1)
  71. xml = strings.Replace(xml, "{!}", "`", -1)
  72. ioutil.WriteFile("README.md", []byte(xml), os.ModePerm)
  73. }
  74. }
  75. func (c *EngineClient) GenerateCurrentProject() {
  76. c.GenerateCurrentProjectToPath("")
  77. }
  78. func (c *EngineClient) GenerateCurrentProjectToPath(destPath string) {
  79. c.InitDefalutFile(c.ProjectName)
  80. // 0---0
  81. //path, _ := utils.GetCurrentPath()
  82. //c.Generate(path + c.ProjectName + ".xml")
  83. projMainXmlFileTemp := c.MergeXmlToSingle()
  84. if projMainXmlFileTemp == "" {
  85. fmt.Println("projMainXmlFileTemp is empty")
  86. return
  87. }
  88. c.GenerateToPath(projMainXmlFileTemp, destPath)
  89. //c.GenSwagger(projMainXmlFileTemp)
  90. path, _ := utils.GetCurrentPath()
  91. CopyDir(env.Get("GOPATH", "") + "/src/git.qianqiusoft.com/qianqiusoft/light-apiengine/sqlconfig",
  92. path+"vendor/git.qianqiusoft.com/qianqiusoft/light-apiengine/sqlconfig",
  93. "./vendor/git.qianqiusoft.com/qianqiusoft/light-apiengine/sqlconfig",
  94. "../src/git.qianqiusoft.com/qianqiusoft/light-apiengine/sqlconfig",
  95. path+"sqlconfig")
  96. }
  97. func (c *EngineClient) GenerateToPath(xmlfile string, dest_path string) {
  98. var result ResponeResult
  99. isCompress := true
  100. var bJson []byte
  101. if c.ServerUrl == "local" {
  102. isCompress = false
  103. //dest_path+="../i2-erp-compare/"
  104. bs := code_gen.DoGenerate(xmlfile, isCompress)
  105. bJson = bs
  106. } else {
  107. server := "http://qianqiusoft.com:6166"
  108. if c.ServerUrl != "" {
  109. server = c.ServerUrl
  110. }
  111. server += "/api/v1/develop/generate"
  112. bs := DoRequest(xmlfile, server)
  113. bJson = bs.Bytes()
  114. }
  115. if bJson != nil {
  116. err := json.Unmarshal(bJson, &result)
  117. if err != nil {
  118. fmt.Println(err.Error())
  119. }
  120. if isCompress {
  121. for i := 0; i < len(result.Data); i++ {
  122. var b bytes.Buffer
  123. b.Write(result.Data[i].Content)
  124. unzip := unzipbytes(&b)
  125. result.Data[i].Content = unzip.Bytes()
  126. }
  127. }
  128. for i := 0; i < len(result.Data); i++ {
  129. path := result.Data[i].Name
  130. //fmt.Println(path)
  131. path = path[len(c.ProjectName)+1:]
  132. path = dest_path + path
  133. fmt.Println(path)
  134. ft := result.Data[i].Type
  135. if ft == "main" {
  136. //fmt.Println(string(result.Data[i].Content))
  137. } else if ft == "config" || ft == "ci" {
  138. _, err := os.Stat(path)
  139. if err == nil {
  140. fmt.Println(path + "已经存在,忽略...")
  141. // fmt.Println(result.Data[i].Content)
  142. } else {
  143. ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
  144. }
  145. } else if ft == "controllers" {
  146. os.MkdirAll(filepath.Dir(path), os.ModePerm)
  147. if strings.Index(path, "_gen.go") > 0 {
  148. ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
  149. } else {
  150. _, err := os.Stat(path)
  151. if err == nil {
  152. ioutil.WriteFile(path+"_new", result.Data[i].Content, os.ModePerm)
  153. } else if os.IsNotExist(err) {
  154. ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
  155. }
  156. }
  157. } else if ft == "routers" {
  158. os.MkdirAll(filepath.Dir(path), os.ModePerm)
  159. ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
  160. } else if ft == "sql" {
  161. if strings.Index(path, "_gen.xml") > 0 {
  162. os.MkdirAll(filepath.Dir(path), os.ModePerm)
  163. ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
  164. } else {
  165. _, err := os.Stat(path)
  166. if err == nil {
  167. //ioutil.WriteFile(path+"_new", result.Data[i].Content, os.ModePerm)
  168. } else if os.IsNotExist(err) {
  169. os.MkdirAll(filepath.Dir(path), os.ModePerm)
  170. ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
  171. }
  172. }
  173. } else {
  174. os.MkdirAll(filepath.Dir(path), os.ModePerm)
  175. err := ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
  176. if err != nil {
  177. fmt.Println(err.Error())
  178. }
  179. }
  180. }
  181. }
  182. }
  183. /*
  184. func (c *EngineClient) Generate(xmlfile string) {
  185. c.GenerateToPath(xmlfile, "")
  186. //c.GenSwagger(xmlfile)
  187. }
  188. */
  189. func (c *EngineClient) GenSwagger(xmlfile string) {
  190. server := "http://swagger.pusher.i2erp.cn"
  191. server += "/api/v1/upload"
  192. fmt.Println("===========================================================================>11")
  193. request, err := newfileUploadRequest(server, nil, "xmlfile", xmlfile)
  194. if err != nil {
  195. fmt.Println(err)
  196. }
  197. client := &http.Client{}
  198. resp, err := client.Do(request)
  199. if err != nil {
  200. fmt.Println("===========================================================================>", err.Error())
  201. } else {
  202. defer resp.Body.Close()
  203. bytess, err := ioutil.ReadAll(resp.Body)
  204. if err != nil {
  205. fmt.Println("=======================================>ioutil.ReadAll error", err.Error())
  206. } else {
  207. fmt.Println("=======================================>", string(bytess))
  208. }
  209. }
  210. }
  211. /**
  212. * @brief: merge xml
  213. * @param: none
  214. * @return: the path of final xml
  215. */
  216. func (c *EngineClient) MergeXmlToSingle() string {
  217. path, _ := utils.GetCurrentPath()
  218. // e.g.: c:/gopath/src/hanghua_background_proj
  219. projDir := fmt.Sprintf("%s%s.proj", path, c.ProjectName)
  220. projMainXmlFile := projDir + "/" + c.ProjectName + ".xml"
  221. projMainXmlFileTemp := projDir + "/" + c.ProjectName + "_temp.xml"
  222. _, err := os.Stat(projMainXmlFileTemp)
  223. if os.IsNotExist(err) {
  224. fmt.Println("-------------------------->remove project main file temp")
  225. os.Remove(projMainXmlFileTemp) // remove
  226. }
  227. _, err = os.Stat(projMainXmlFile)
  228. if os.IsNotExist(err) {
  229. fmt.Println("main xml file of " + c.ProjectName + " does not exist")
  230. return ""
  231. }
  232. app := XmlApplication{}
  233. bytess, _ := ioutil.ReadFile(projMainXmlFile)
  234. err = xml.Unmarshal(bytess, &app)
  235. if err != nil {
  236. fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
  237. return ""
  238. }
  239. controllers, err := scanControllers(projDir + "/controllers")
  240. if err != nil {
  241. fmt.Println("scanControllers error " + err.Error())
  242. return ""
  243. }
  244. beans, err := scanBeans(projDir + "/beans")
  245. if err != nil {
  246. fmt.Println("scanBeans error " + err.Error())
  247. return ""
  248. }
  249. tables, err := scanTables(projDir + "/tables")
  250. if err != nil {
  251. fmt.Println("scanTables error " + err.Error())
  252. return ""
  253. }
  254. if app.Controllers.ControllerList == nil {
  255. app.Controllers.ControllerList = []XmlController{}
  256. }
  257. for i := range controllers {
  258. app.Controllers.ControllerList = append(app.Controllers.ControllerList, controllers[i])
  259. }
  260. createVueApisFolder(app.Controllers.ControllerList)
  261. if app.Beans.BeanList == nil {
  262. app.Beans.BeanList = []XmlBean{}
  263. }
  264. for i := range beans {
  265. app.Beans.BeanList = append(app.Beans.BeanList, beans[i])
  266. }
  267. if app.Tables.TableList == nil {
  268. app.Tables.TableList = []XmlTable{}
  269. }
  270. for i := range tables {
  271. app.Tables.TableList = append(app.Tables.TableList, tables[i])
  272. }
  273. bytess, err = xml.Marshal(app)
  274. if err != nil {
  275. fmt.Println("xml.Marshal(app) error " + err.Error())
  276. return ""
  277. }
  278. err = ioutil.WriteFile(projMainXmlFileTemp, bytess, os.ModePerm)
  279. if err != nil {
  280. fmt.Println("ioutil.WriteFile(projMainXmlFileTemp, bytess,os.ModePerm) error " + err.Error())
  281. return ""
  282. }
  283. _, err = os.Stat(projMainXmlFileTemp)
  284. if os.IsNotExist(err) {
  285. fmt.Println("main xml file temp of " + c.ProjectName + " does not exist")
  286. return ""
  287. } else {
  288. fmt.Println("main xml file temp of " + c.ProjectName + " exist")
  289. }
  290. return projMainXmlFileTemp
  291. }
  292. func scanControllers(ctrldir string) ([]XmlController, error) {
  293. _, err := os.Stat(ctrldir)
  294. if os.IsNotExist(err) {
  295. fmt.Println("controller dir does not exist", err.Error())
  296. return nil, err
  297. }
  298. controllers := []XmlController{}
  299. filePaths := []string{}
  300. filePaths, err = getAllFile(strings.TrimSuffix(ctrldir, "/"), filePaths)
  301. if err != nil {
  302. fmt.Println("controller getAllFile error", err.Error())
  303. return nil, err
  304. }
  305. for i := range filePaths {
  306. ctrl := XmlController{}
  307. ctrlfile := filePaths[i]
  308. _, err = os.Stat(ctrlfile)
  309. if os.IsNotExist(err) {
  310. fmt.Println("controller xml file " + ctrlfile + " does not exist")
  311. return controllers, err
  312. }
  313. ctrlfile = strings.Replace(ctrlfile, "\\", "/", -1)
  314. arr := strings.Split(ctrlfile, "/")
  315. bytess, _ := ioutil.ReadFile(ctrlfile)
  316. err = xml.Unmarshal(bytess, &ctrl)
  317. if err != nil {
  318. fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
  319. return controllers, err
  320. }
  321. // get sub dir name
  322. if arr[len(arr)-2] != "controllers" {
  323. // if sub dir is not controllers, set the dir attr
  324. ctrl.Dir = arr[len(arr)-2]
  325. }
  326. controllers = append(controllers, ctrl)
  327. }
  328. return controllers, nil
  329. //err = filepath.Walk(ctrldir, func(path string, info os.FileInfo, err error) error {
  330. //
  331. // if err != nil {
  332. // return filepath.SkipDir
  333. // }
  334. // if info.IsDir(){
  335. // return nil
  336. // }
  337. //
  338. // ctrl := XmlController{}
  339. // ctrlfile := ctrldir + "/" + info.Name()
  340. // _, err = os.Stat(ctrlfile)
  341. // if os.IsNotExist(err) {
  342. // fmt.Println("controller xml file " + ctrlfile + " does not exist")
  343. // return filepath.SkipDir
  344. // }
  345. // bytess, _ := ioutil.ReadFile(ctrlfile)
  346. // err = xml.Unmarshal(bytess, &ctrl)
  347. // if err != nil{
  348. // fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
  349. // return filepath.SkipDir
  350. // }
  351. // controllers = append(controllers, ctrl)
  352. //
  353. // return nil
  354. //})
  355. //return controllers, err
  356. }
  357. func scanBeans(ctrldir string) ([]XmlBean, error) {
  358. _, err := os.Stat(ctrldir)
  359. if os.IsNotExist(err) {
  360. fmt.Println("controller dir does not exist")
  361. return nil, err
  362. }
  363. beans := []XmlBean{}
  364. filePaths := []string{}
  365. filePaths, err = getAllFile(strings.TrimSuffix(ctrldir, "/"), filePaths)
  366. if err != nil {
  367. fmt.Println("controller getAllFile error", err.Error())
  368. return nil, err
  369. }
  370. for i := range filePaths {
  371. ctrl := XmlBean{}
  372. ctrlfile := filePaths[i]
  373. _, err = os.Stat(ctrlfile)
  374. if os.IsNotExist(err) {
  375. fmt.Println("controller xml file " + ctrlfile + " does not exist")
  376. return beans, err
  377. }
  378. bytess, _ := ioutil.ReadFile(ctrlfile)
  379. err = xml.Unmarshal(bytess, &ctrl)
  380. if err != nil {
  381. fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
  382. return beans, err
  383. }
  384. beans = append(beans, ctrl)
  385. }
  386. return beans, nil
  387. //err = filepath.Walk(ctrldir, func(path string, info os.FileInfo, err error) error {
  388. //
  389. // if err != nil {
  390. // return filepath.SkipDir
  391. // }
  392. // if info.IsDir(){
  393. // return nil
  394. // }
  395. //
  396. // bean := XmlBean{}
  397. // beanfile := ctrldir + "/" + info.Name()
  398. // _, err = os.Stat(beanfile)
  399. // if os.IsNotExist(err) {
  400. // fmt.Println("controller xml file " + beanfile + " does not exist")
  401. // return filepath.SkipDir
  402. // }
  403. // bytess, _ := ioutil.ReadFile(beanfile)
  404. // err = xml.Unmarshal(bytess, &bean)
  405. // if err != nil{
  406. // fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
  407. // return filepath.SkipDir
  408. // }
  409. // beans = append(beans, bean)
  410. //
  411. // return nil
  412. //})
  413. //
  414. //return beans, err
  415. }
  416. func scanTables(ctrldir string) ([]XmlTable, error) {
  417. _, err := os.Stat(ctrldir)
  418. if os.IsNotExist(err) {
  419. fmt.Println("controller dir does not exist")
  420. return nil, err
  421. }
  422. tables := []XmlTable{}
  423. filePaths := []string{}
  424. filePaths, err = getAllFile(strings.TrimSuffix(ctrldir, "/"), filePaths)
  425. if err != nil {
  426. fmt.Println("controller getAllFile error", err.Error())
  427. return nil, err
  428. }
  429. for i := range filePaths {
  430. ctrl := XmlTable{}
  431. ctrlfile := filePaths[i]
  432. _, err = os.Stat(ctrlfile)
  433. if os.IsNotExist(err) {
  434. fmt.Println("controller xml file " + ctrlfile + " does not exist")
  435. return tables, err
  436. }
  437. bytess, _ := ioutil.ReadFile(ctrlfile)
  438. err = xml.Unmarshal(bytess, &ctrl)
  439. if err != nil {
  440. fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
  441. return tables, err
  442. }
  443. tables = append(tables, ctrl)
  444. }
  445. return tables, nil
  446. //err = filepath.Walk(ctrldir, func(path string, info os.FileInfo, err error) error {
  447. //
  448. // if err != nil {
  449. // return filepath.SkipDir
  450. // }
  451. // if info.IsDir(){
  452. // return nil
  453. // }
  454. //
  455. // table := XmlTable{}
  456. // tablefile := ctrldir + "/" + info.Name()
  457. // _, err = os.Stat(tablefile)
  458. // if os.IsNotExist(err) {
  459. // fmt.Println("controller xml file " + tablefile + " does not exist")
  460. // return filepath.SkipDir
  461. // }
  462. // bytess, _ := ioutil.ReadFile(tablefile)
  463. // err = xml.Unmarshal(bytess, &table)
  464. // if err != nil{
  465. // fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
  466. // return filepath.SkipDir
  467. // }
  468. // tables = append(tables, table)
  469. //
  470. // return nil
  471. //})
  472. //
  473. //return tables, err
  474. }
  475. func createVueApisFolder(controllers []XmlController) {
  476. for i := range controllers {
  477. fmt.Println("------------------------------>" + controllers[i].Name)
  478. os.MkdirAll("vue/api/modules/"+controllers[i].Name, os.ModePerm)
  479. }
  480. }
  481. func getAllFile(pathname string, s []string) ([]string, error) {
  482. rd, err := ioutil.ReadDir(pathname)
  483. if err != nil {
  484. fmt.Println("read dir fail:", err)
  485. return s, err
  486. }
  487. for _, fi := range rd {
  488. if fi.IsDir() {
  489. fullDir := pathname + "/" + fi.Name()
  490. s, err = getAllFile(fullDir, s)
  491. if err != nil {
  492. fmt.Println("read dir fail:", err)
  493. return s, err
  494. }
  495. } else {
  496. if strings.HasSuffix(strings.ToLower(fi.Name()), ".xml") {
  497. fullName := pathname + "/" + fi.Name()
  498. s = append(s, fullName)
  499. }
  500. }
  501. }
  502. return s, nil
  503. }
  504. func unzipbytes(bs *bytes.Buffer) bytes.Buffer {
  505. r, _ := gzip.NewReader(bs)
  506. defer r.Close()
  507. var b bytes.Buffer
  508. b.ReadFrom(r)
  509. //undatas, _ := ioutil.ReadAll(r)
  510. //fmt.Println("ungzip size:", len(undatas))
  511. return b
  512. }
  513. func DoRequest(xmlfile string, server string) *bytes.Buffer {
  514. request, err := newfileUploadRequest(server, nil, "xmlfile", xmlfile)
  515. if err != nil {
  516. fmt.Println(err)
  517. }
  518. client := &http.Client{}
  519. resp, err := client.Do(request)
  520. if err != nil {
  521. fmt.Println(err)
  522. } else {
  523. body := &bytes.Buffer{}
  524. _, err := body.ReadFrom(resp.Body)
  525. if err != nil {
  526. fmt.Println(err)
  527. }
  528. resp.Body.Close()
  529. fmt.Println(resp.StatusCode)
  530. fmt.Println(resp.Header)
  531. //fmt.Println(body)
  532. return body
  533. }
  534. return nil
  535. }
  536. func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
  537. file, err := os.Open(path)
  538. if err != nil {
  539. return nil, err
  540. }
  541. defer file.Close()
  542. body := &bytes.Buffer{}
  543. writer := multipart.NewWriter(body)
  544. part, err := writer.CreateFormFile(paramName, filepath.Base(path))
  545. if err != nil {
  546. return nil, err
  547. }
  548. _, err = io.Copy(part, file)
  549. for key, val := range params {
  550. _ = writer.WriteField(key, val)
  551. }
  552. err = writer.Close()
  553. if err != nil {
  554. return nil, err
  555. }
  556. request, err := http.NewRequest("POST", uri, body)
  557. request.Header.Add("Content-Type", writer.FormDataContentType())
  558. return request, err
  559. }