engineclient.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. if strings.Index(path, "_gen.go") > 0 {
  125. ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
  126. } else {
  127. _, err := os.Stat(path)
  128. if err == nil {
  129. ioutil.WriteFile(path+"_new", result.Data[i].Content, os.ModePerm)
  130. } else if os.IsNotExist(err) {
  131. ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
  132. }
  133. }
  134. } else if ft == "routers" {
  135. os.MkdirAll(filepath.Dir(path), os.ModePerm)
  136. ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
  137. } else if ft =="sql"{
  138. if strings.Index(path, "_gen.xml") > 0 {
  139. os.MkdirAll(filepath.Dir(path), os.ModePerm)
  140. ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
  141. } else {
  142. _, err := os.Stat(path)
  143. if err == nil {
  144. //ioutil.WriteFile(path+"_new", result.Data[i].Content, os.ModePerm)
  145. } else if os.IsNotExist(err) {
  146. os.MkdirAll(filepath.Dir(path), os.ModePerm)
  147. ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
  148. }
  149. }
  150. } else{
  151. os.MkdirAll(filepath.Dir(path), os.ModePerm)
  152. err := ioutil.WriteFile(path, result.Data[i].Content, os.ModePerm)
  153. if err != nil {
  154. fmt.Println(err.Error())
  155. }
  156. }
  157. }
  158. }
  159. }
  160. func (c *EngineClient) Generate(xmlfile string) {
  161. c.GenerateToPath(xmlfile, "")
  162. c.GenSwagger(xmlfile)
  163. }
  164. func (c *EngineClient) GenSwagger(xmlfile string){
  165. server := "http://swagger.qianqiusoft.com"
  166. server += "/api/v1/upload"
  167. fmt.Println("===========================================================================>11")
  168. request, err := newfileUploadRequest(server, nil, "xmlfile", xmlfile)
  169. if err != nil {
  170. fmt.Println(err)
  171. }
  172. client := &http.Client{}
  173. resp, err := client.Do(request)
  174. if err != nil{
  175. fmt.Println("===========================================================================>", err.Error())
  176. }else{
  177. defer resp.Body.Close()
  178. bytess, err := ioutil.ReadAll(resp.Body)
  179. if err != nil{
  180. fmt.Println("=======================================>ioutil.ReadAll error", err.Error())
  181. }else{
  182. fmt.Println("=======================================>", string(bytess))
  183. }
  184. }
  185. }
  186. /**
  187. * @brief: merge xml
  188. * @param: none
  189. * @return: the path of final xml
  190. */
  191. func (c *EngineClient)MergeXmlToSingle()string{
  192. path, _ := utils.GetCurrentPath()
  193. // e.g.: c:/gopath/src/hanghua_background_proj
  194. projDir := fmt.Sprintf("%s%s.proj", path, c.ProjectName)
  195. projMainXmlFile := projDir + "/" + c.ProjectName + ".xml"
  196. projMainXmlFileTemp := projDir + "/" + c.ProjectName + "_temp.xml"
  197. _, err := os.Stat(projMainXmlFileTemp)
  198. if os.IsNotExist(err) {
  199. fmt.Println("-------------------------->remove project main file temp")
  200. os.Remove(projMainXmlFileTemp) // remove
  201. }
  202. _, err = os.Stat(projMainXmlFile)
  203. if os.IsNotExist(err) {
  204. fmt.Println("main xml file of " + c.ProjectName + " does not exist")
  205. return ""
  206. }
  207. app := XmlApplication{}
  208. bytess, _ := ioutil.ReadFile(projMainXmlFile)
  209. err = xml.Unmarshal(bytess, &app)
  210. if err != nil{
  211. fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
  212. return ""
  213. }
  214. controllers, err := scanControllers(projDir + "/controllers")
  215. if err != nil{
  216. fmt.Println("scanControllers error " + err.Error())
  217. return ""
  218. }
  219. beans, err := scanBeans(projDir + "/beans")
  220. if err != nil{
  221. fmt.Println("scanBeans error " + err.Error())
  222. return ""
  223. }
  224. tables, err := scanTables(projDir + "/tables")
  225. if err != nil{
  226. fmt.Println("scanTables error " + err.Error())
  227. return ""
  228. }
  229. if app.Controllers.ControllerList == nil{
  230. app.Controllers.ControllerList = []XmlController{}
  231. }
  232. for i := range controllers{
  233. app.Controllers.ControllerList = append(app.Controllers.ControllerList, controllers[i])
  234. }
  235. createVueApisFolder(app.Controllers.ControllerList)
  236. if app.Beans.BeanList == nil{
  237. app.Beans.BeanList = []XmlBean{}
  238. }
  239. for i := range beans{
  240. app.Beans.BeanList = append(app.Beans.BeanList, beans[i])
  241. }
  242. if app.Tables.TableList == nil{
  243. app.Tables.TableList = []XmlTable{}
  244. }
  245. for i := range tables{
  246. app.Tables.TableList = append(app.Tables.TableList, tables[i])
  247. }
  248. bytess, err = xml.Marshal(app)
  249. if err != nil{
  250. fmt.Println("xml.Marshal(app) error " + err.Error())
  251. return ""
  252. }
  253. err = ioutil.WriteFile(projMainXmlFileTemp, bytess,os.ModePerm)
  254. if err != nil{
  255. fmt.Println("ioutil.WriteFile(projMainXmlFileTemp, bytess,os.ModePerm) error " + err.Error())
  256. return ""
  257. }
  258. _, err = os.Stat(projMainXmlFileTemp)
  259. if os.IsNotExist(err) {
  260. fmt.Println("main xml file temp of " + c.ProjectName + " does not exist")
  261. return ""
  262. }else{
  263. fmt.Println("main xml file temp of " + c.ProjectName + " exist")
  264. }
  265. return projMainXmlFileTemp
  266. }
  267. func scanControllers(ctrldir string) ([]XmlController, error){
  268. _, err := os.Stat(ctrldir)
  269. if os.IsNotExist(err) {
  270. fmt.Println("controller dir does not exist", err.Error())
  271. return nil, err
  272. }
  273. controllers := []XmlController{}
  274. filePaths := []string{}
  275. filePaths, err = getAllFile(strings.TrimSuffix(ctrldir, "/"), filePaths)
  276. if err != nil{
  277. fmt.Println("controller getAllFile error", err.Error())
  278. return nil, err
  279. }
  280. for i := range filePaths {
  281. ctrl := XmlController{}
  282. ctrlfile := filePaths[i]
  283. _, err = os.Stat(ctrlfile)
  284. if os.IsNotExist(err) {
  285. fmt.Println("controller xml file " + ctrlfile + " does not exist")
  286. return controllers, err
  287. }
  288. bytess, _ := ioutil.ReadFile(ctrlfile)
  289. err = xml.Unmarshal(bytess, &ctrl)
  290. if err != nil {
  291. fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
  292. return controllers, err
  293. }
  294. controllers = append(controllers, ctrl)
  295. }
  296. return controllers, nil
  297. //err = filepath.Walk(ctrldir, func(path string, info os.FileInfo, err error) error {
  298. //
  299. // if err != nil {
  300. // return filepath.SkipDir
  301. // }
  302. // if info.IsDir(){
  303. // return nil
  304. // }
  305. //
  306. // ctrl := XmlController{}
  307. // ctrlfile := ctrldir + "/" + info.Name()
  308. // _, err = os.Stat(ctrlfile)
  309. // if os.IsNotExist(err) {
  310. // fmt.Println("controller xml file " + ctrlfile + " does not exist")
  311. // return filepath.SkipDir
  312. // }
  313. // bytess, _ := ioutil.ReadFile(ctrlfile)
  314. // err = xml.Unmarshal(bytess, &ctrl)
  315. // if err != nil{
  316. // fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
  317. // return filepath.SkipDir
  318. // }
  319. // controllers = append(controllers, ctrl)
  320. //
  321. // return nil
  322. //})
  323. //return controllers, err
  324. }
  325. func scanBeans(ctrldir string) ([]XmlBean, error){
  326. _, err := os.Stat(ctrldir)
  327. if os.IsNotExist(err) {
  328. fmt.Println("controller dir does not exist")
  329. return nil, err
  330. }
  331. beans := []XmlBean{}
  332. filePaths := []string{}
  333. filePaths, err = getAllFile(strings.TrimSuffix(ctrldir, "/"), filePaths)
  334. if err != nil{
  335. fmt.Println("controller getAllFile error", err.Error())
  336. return nil, err
  337. }
  338. for i := range filePaths {
  339. ctrl := XmlBean{}
  340. ctrlfile := filePaths[i]
  341. _, err = os.Stat(ctrlfile)
  342. if os.IsNotExist(err) {
  343. fmt.Println("controller xml file " + ctrlfile + " does not exist")
  344. return beans, err
  345. }
  346. bytess, _ := ioutil.ReadFile(ctrlfile)
  347. err = xml.Unmarshal(bytess, &ctrl)
  348. if err != nil {
  349. fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
  350. return beans, err
  351. }
  352. beans = append(beans, ctrl)
  353. }
  354. return beans, nil
  355. //err = filepath.Walk(ctrldir, func(path string, info os.FileInfo, err error) error {
  356. //
  357. // if err != nil {
  358. // return filepath.SkipDir
  359. // }
  360. // if info.IsDir(){
  361. // return nil
  362. // }
  363. //
  364. // bean := XmlBean{}
  365. // beanfile := ctrldir + "/" + info.Name()
  366. // _, err = os.Stat(beanfile)
  367. // if os.IsNotExist(err) {
  368. // fmt.Println("controller xml file " + beanfile + " does not exist")
  369. // return filepath.SkipDir
  370. // }
  371. // bytess, _ := ioutil.ReadFile(beanfile)
  372. // err = xml.Unmarshal(bytess, &bean)
  373. // if err != nil{
  374. // fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
  375. // return filepath.SkipDir
  376. // }
  377. // beans = append(beans, bean)
  378. //
  379. // return nil
  380. //})
  381. //
  382. //return beans, err
  383. }
  384. func scanTables(ctrldir string) ([]XmlTable, error){
  385. _, err := os.Stat(ctrldir)
  386. if os.IsNotExist(err) {
  387. fmt.Println("controller dir does not exist")
  388. return nil, err
  389. }
  390. tables := []XmlTable{}
  391. filePaths := []string{}
  392. filePaths, err = getAllFile(strings.TrimSuffix(ctrldir, "/"), filePaths)
  393. if err != nil{
  394. fmt.Println("controller getAllFile error", err.Error())
  395. return nil, err
  396. }
  397. for i := range filePaths {
  398. ctrl := XmlTable{}
  399. ctrlfile := filePaths[i]
  400. _, err = os.Stat(ctrlfile)
  401. if os.IsNotExist(err) {
  402. fmt.Println("controller xml file " + ctrlfile + " does not exist")
  403. return tables, err
  404. }
  405. bytess, _ := ioutil.ReadFile(ctrlfile)
  406. err = xml.Unmarshal(bytess, &ctrl)
  407. if err != nil {
  408. fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
  409. return tables, err
  410. }
  411. tables = append(tables, ctrl)
  412. }
  413. return tables, nil
  414. //err = filepath.Walk(ctrldir, func(path string, info os.FileInfo, err error) error {
  415. //
  416. // if err != nil {
  417. // return filepath.SkipDir
  418. // }
  419. // if info.IsDir(){
  420. // return nil
  421. // }
  422. //
  423. // table := XmlTable{}
  424. // tablefile := ctrldir + "/" + info.Name()
  425. // _, err = os.Stat(tablefile)
  426. // if os.IsNotExist(err) {
  427. // fmt.Println("controller xml file " + tablefile + " does not exist")
  428. // return filepath.SkipDir
  429. // }
  430. // bytess, _ := ioutil.ReadFile(tablefile)
  431. // err = xml.Unmarshal(bytess, &table)
  432. // if err != nil{
  433. // fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
  434. // return filepath.SkipDir
  435. // }
  436. // tables = append(tables, table)
  437. //
  438. // return nil
  439. //})
  440. //
  441. //return tables, err
  442. }
  443. func createVueApisFolder(controllers []XmlController){
  444. for i := range controllers{
  445. fmt.Println("------------------------------>" + controllers[i].Name)
  446. os.MkdirAll("vue/api/modules/" + controllers[i].Name, os.ModePerm)
  447. }
  448. }
  449. func getAllFile(pathname string, s []string) ([]string, error) {
  450. rd, err := ioutil.ReadDir(pathname)
  451. if err != nil {
  452. fmt.Println("read dir fail:", err)
  453. return s, err
  454. }
  455. for _, fi := range rd {
  456. if fi.IsDir() {
  457. fullDir := pathname + "/" + fi.Name()
  458. s, err = getAllFile(fullDir, s)
  459. if err != nil {
  460. fmt.Println("read dir fail:", err)
  461. return s, err
  462. }
  463. } else {
  464. if strings.HasSuffix(strings.ToLower(fi.Name()), ".xml") {
  465. fullName := pathname + "/" + fi.Name()
  466. s = append(s, fullName)
  467. }
  468. }
  469. }
  470. return s, nil
  471. }
  472. func unzipbytes(bs *bytes.Buffer) bytes.Buffer {
  473. r, _ := gzip.NewReader(bs)
  474. defer r.Close()
  475. var b bytes.Buffer
  476. b.ReadFrom(r)
  477. //undatas, _ := ioutil.ReadAll(r)
  478. //fmt.Println("ungzip size:", len(undatas))
  479. return b
  480. }
  481. func DoRequest(xmlfile string, server string) *bytes.Buffer {
  482. request, err := newfileUploadRequest(server, nil, "xmlfile", xmlfile)
  483. if err != nil {
  484. fmt.Println(err)
  485. }
  486. client := &http.Client{}
  487. resp, err := client.Do(request)
  488. if err != nil {
  489. fmt.Println(err)
  490. } else {
  491. body := &bytes.Buffer{}
  492. _, err := body.ReadFrom(resp.Body)
  493. if err != nil {
  494. fmt.Println(err)
  495. }
  496. resp.Body.Close()
  497. fmt.Println(resp.StatusCode)
  498. fmt.Println(resp.Header)
  499. //fmt.Println(body)
  500. return body
  501. }
  502. return nil
  503. }
  504. func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
  505. file, err := os.Open(path)
  506. if err != nil {
  507. return nil, err
  508. }
  509. defer file.Close()
  510. body := &bytes.Buffer{}
  511. writer := multipart.NewWriter(body)
  512. part, err := writer.CreateFormFile(paramName, filepath.Base(path))
  513. if err != nil {
  514. return nil, err
  515. }
  516. _, err = io.Copy(part, file)
  517. for key, val := range params {
  518. _ = writer.WriteField(key, val)
  519. }
  520. err = writer.Close()
  521. if err != nil {
  522. return nil, err
  523. }
  524. request, err := http.NewRequest("POST", uri, body)
  525. request.Header.Add("Content-Type", writer.FormDataContentType())
  526. return request, err
  527. }