engineclient.go 16 KB

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