engineclient.go 15 KB

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