@@ -8,15 +8,11 @@ import (
88 "strconv"
99 "strings"
1010
11- "github.com/sunshinev/go-sword/core/utils"
12-
11+ _ "github.com/go-sql-driver/mysql"
1312 "github.com/sunshinev/go-sword/assets/resource"
14-
1513 "github.com/sunshinev/go-sword/assets/stub"
16-
1714 "github.com/sunshinev/go-sword/config"
18-
19- _ "github.com/go-sql-driver/mysql"
15+ "github.com/sunshinev/go-sword/core/utils"
2016)
2117
2218type Generator struct {
@@ -42,61 +38,51 @@ type FileInstance struct {
4238 IsNew bool `json:"is_new"`
4339}
4440
45- func (g * Generator ) Init (c * config.Config ) * Generator {
46- g .config = c
47- g .ColumnDataTypes = make (map [string ]string )
48- return g
41+ func (g Generator ) Init () * Generator {
42+ return & Generator {
43+ config : config .GlobalConfig ,
44+ ColumnDataTypes : map [string ]string {},
45+ }
4946}
5047
5148// Entry
52- func (g * Generator ) parseTable (s * Sword , table string ) {
49+ func (g * Generator ) parseTable (table string ) {
5350
54- columnDataTypes , err := utils.Db2struct {}.Convert (s , table )
51+ columnDataTypes , err := utils.Db2struct {}.Convert (table )
5552 if err != nil {
5653 panic (err .Error ())
5754 }
5855
59- structName := strings .Replace (strings .Title (strings .Replace ("hi_ni_hao" , "_" , " " , - 1 )), " " , "" , - 1 )
60-
6156 // Set columns
6257 for _ , r := range * columnDataTypes {
6358 g .Columns = append (g .Columns , r .ColumnName )
6459 g .ColumnDataTypes [r .ColumnName ] = r .DataType
6560 }
66-
6761 g .Columns = utils .ResortMySQLFields (& g .Columns )
68-
69- struc := utils.Db2struct {}.FetchWholeStructFile (table , structName , table , columnDataTypes )
70-
7162 // Set TableName
7263 g .TableName = table
7364 // Set PackageName
7465 g .PackageName = table
7566 // Set StructName
67+ structName := strings .Replace (strings .Title (strings .Replace (table , "_" , " " , - 1 )), " " , "" , - 1 )
7668 g .StructName = structName
7769 // Set Content
78- g .Struc = struc
79- }
80-
81- func (g * Generator ) Preview (s * Sword , table string ) {
82- g .parseTable (s , table )
83- // Main.go
84- g .gMainFile ()
85- // Core.go
86- g .gCoreFile ()
87- // Route.go
88- g .gRouteFile ()
89- // Model
90- g .gModelFile ()
91- // Controller
92- g .gControllerFile ()
93- // Response
94- g .gResponseFile ()
95- // Html
96- // default.html
97- g .gHtmlDefaultFile ()
98- // list.html
99- g .gHtmlListFile ()
70+ g .Struc = utils.Db2struct {}.FetchWholeStructFile ("model" , structName , table , columnDataTypes )
71+ }
72+
73+ func (g * Generator ) Preview (table string ) {
74+ g .parseTable (table )
75+
76+ g .gGoModFile () // go.mod
77+ g .gMainFile () // Main.go
78+ g .gCoreFile () // Core.go
79+ g .gRouteFile () // Route.go
80+ g .gModelFile () // Model
81+ g .gControllerFile () // Controller
82+ g .gResponseFile () // Response
83+
84+ g .gHtmlDefaultFile () // default.html
85+ g .gHtmlListFile () // list.htm
10086 g .gHtmlCreateFile ()
10187 g .gHtmlDetailFile ()
10288 g .gHtmlEditFile ()
@@ -108,7 +94,7 @@ func (g *Generator) Preview(s *Sword, table string) {
10894}
10995
11096func (g * Generator ) Generate (s * Sword , table string , files []string ) {
111- g .Preview (s , table )
97+ g .Preview (table )
11298
11399 for _ , file := range g .FileList {
114100 var path = file .FilePath
@@ -432,8 +418,17 @@ func (g *Generator) createRouteContent(path string) string {
432418 return content
433419}
434420
435- func (g * Generator ) gMainFile () {
421+ func (g * Generator ) gGoModFile () {
422+ var file = & FileInstance {
423+ FilePath : filepath .Join (g .config .RootPath , "go.mod" ),
424+ FileName : "go.mod" ,
425+ FileContent : g .createGoModContent (),
426+ }
436427
428+ g .FileList = append (g .FileList , file )
429+ }
430+
431+ func (g * Generator ) gMainFile () {
437432 var file = & FileInstance {
438433 FilePath : filepath .Join (g .config .RootPath , "main.go" ),
439434 FileName : "main.go" ,
@@ -443,6 +438,17 @@ func (g *Generator) gMainFile() {
443438 g .FileList = append (g .FileList , file )
444439}
445440
441+ func (g * Generator ) createGoModContent () string {
442+ // Read stub
443+ data , err := stub .Asset ("stub/go.mod.stub" )
444+ if err != nil {
445+ panic (err .Error ())
446+ }
447+ content := string (data )
448+ content = strings .ReplaceAll (content , "<<module_name>>" , g .config .RootPath )
449+ return content
450+ }
451+
446452func (g * Generator ) createMainContent () string {
447453 // Read stub
448454 data , err := stub .Asset ("stub/main.stub" )
@@ -454,11 +460,11 @@ func (g *Generator) createMainContent() string {
454460
455461 content := string (data )
456462
457- content = strings .ReplaceAll (content , "<<db_host>>" , g .config .Database .Host )
458- content = strings .ReplaceAll (content , "<<db_user>>" , g .config .Database .User )
459- content = strings .ReplaceAll (content , "<<db_password>>" , g .config .Database .Password )
460- content = strings .ReplaceAll (content , "<<db_port>>" , strconv .Itoa (g .config .Database .Port ))
461- content = strings .ReplaceAll (content , "<<db_database>>" , g .config .Database .Database )
463+ content = strings .ReplaceAll (content , "<<db_host>>" , g .config .DatabaseSet .Host )
464+ content = strings .ReplaceAll (content , "<<db_user>>" , g .config .DatabaseSet .User )
465+ content = strings .ReplaceAll (content , "<<db_password>>" , g .config .DatabaseSet .Password )
466+ content = strings .ReplaceAll (content , "<<db_port>>" , strconv .Itoa (g .config .DatabaseSet .Port ))
467+ content = strings .ReplaceAll (content , "<<db_database>>" , g .config .DatabaseSet .Database )
462468 content = strings .ReplaceAll (content , "<<import_core>>" , str )
463469 return content
464470}
0 commit comments