Skip to content

Commit 35740d7

Browse files
committed
Golang 代码生成器 v0.0.1
0 parents  commit 35740d7

File tree

23 files changed

+1514
-0
lines changed

23 files changed

+1514
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/log/
2+
/test/
3+
/resource/autocode/
4+
/resource/font/
5+
/resource/temp/
6+
/resource/conf.yaml
7+
.idea/

.goreleaser.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This is an example goreleaser.yaml file with some sane defaults.
2+
# Make sure to check the documentation at http://goreleaser.com
3+
project_name: go-code-generator
4+
uploads:
5+
- name: production
6+
target: http://some.server/some/path/example-repo-local/{{ .ProjectName }}/{{ .Version }}/
7+
username: goreleaser
8+
before:
9+
hooks:
10+
# You may remove this if you don't use go modules.
11+
#- go mod download
12+
# you may remove this if you don't need go generate
13+
#- go generate ./...
14+
builds:
15+
- env:
16+
- CGO_ENABLED=0
17+
goos:
18+
- linux
19+
- windows
20+
archives:
21+
- replacements:
22+
linux: Linux
23+
windows: Windows
24+
386: i386
25+
amd64: x86_64
26+
format_overrides:
27+
- goos: windows
28+
format: zip
29+
files:
30+
- .env
31+
checksum:
32+
name_template: 'checksums.txt'
33+
snapshot:
34+
name_template: "{{ .Tag }}-snapshot"
35+
changelog:
36+
sort: asc
37+
filters:
38+
exclude:
39+
- '^docs:'
40+
- '^test:'

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## Golang 通用代码生成器 go-code-generator
2+
3+
#### v0.0.1
4+
5+
### 简介
6+
7+
> 采用Fyne 编写的Gui客户端,跨平台编译
8+
>
9+
> 根据项目架构连接数据库动态生成go代码
10+
>
11+
> 可以按照自己项目结构生成不同的目录结构
12+
>
13+
> 现在只支持MySQL
14+
15+
### 依赖组件
16+
17+
- gorm-v2
18+
- gin
19+
- gin-swagger
20+
21+
### 生成的结构
22+
23+
现在生成的类型
24+
25+
- model
26+
- mapper
27+
- service
28+
- api
29+
- router
30+
31+
生成的目录结构 会生成在当前程序运行目录 gen-code/ 下
32+
33+
------router
34+
35+
------project
36+
37+
----模块1
38+
39+
--model
40+
41+
--mapper
42+
43+
--service
44+
45+
--api
46+
47+
----模块2
48+
49+
--model
50+
51+
--mapper
52+
53+
--service
54+
55+
--api
56+
57+
……
58+
59+
### 动态模板
60+
61+
可以根据不同架构自行编辑生成模板文件
62+
63+
模板文件会在项目启动的时候
64+
65+
自动装载到当前程序运行目录 temp/ 下
66+
67+
68+

auto/data.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package auto
2+
3+
import (
4+
"time"
5+
)
6+
7+
//数据库表信息
8+
type TableInfo struct {
9+
TableName string `json:"tableName"`
10+
TableComment string `json:"tableComment"`
11+
CreateTime time.Time `json:"createTime"`
12+
Checked bool `json:"checked"`
13+
SupStructName string `json:"supStructName"`
14+
}
15+
16+
//表列信息
17+
type TableColumnInfo struct {
18+
ColumnName string `json:"columnName"`
19+
DataType string `json:"dataType"`
20+
ColumeComment string `json:"columeComment"`
21+
22+
FieldName string `json:"fieldName"`
23+
FieldType string `json:"fieldType"`
24+
FieldJson string `json:"fieldJson"`
25+
}

auto/model.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package auto
2+
3+
import (
4+
"code/gen/util/conf"
5+
"time"
6+
)
7+
8+
// 初始版本自动化代码工具
9+
type AutoCodeStruct struct {
10+
conf.ProjectConf
11+
TableInfo
12+
Fields []TableColumnInfo `json:"fields"`
13+
StructName string `json:"structName"`
14+
}
15+
16+
type GLOBALMODEL struct {
17+
ID int64 `json:"id"`
18+
CreateTime time.Time `json:"createTime"`
19+
UpdateTime time.Time `json:"updatedTime"`
20+
}

go.mod

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module code/gen
2+
3+
go 1.15
4+
5+
require (
6+
fyne.io/fyne v1.4.3
7+
fyne.io/fyne/v2 v2.0.0
8+
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
9+
github.com/lestrrat-go/strftime v1.0.4 // indirect
10+
github.com/mattn/go-sqlite3 v1.14.6 // indirect
11+
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5
12+
github.com/sirupsen/logrus v1.8.0
13+
github.com/spf13/viper v1.7.1
14+
gorm.io/driver/mysql v1.0.4
15+
gorm.io/driver/sqlite v1.1.4 // indirect
16+
gorm.io/gorm v1.20.12
17+
)

go.sum

Lines changed: 383 additions & 0 deletions
Large diffs are not rendered by default.

main.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Package main provides various examples of Fyne API capabilities.
2+
package main
3+
4+
import (
5+
"code/gen/page"
6+
"code/gen/runner"
7+
"code/gen/util/logger"
8+
"fmt"
9+
"github.com/sirupsen/logrus"
10+
"net/url"
11+
"os"
12+
13+
"fyne.io/fyne/v2"
14+
"fyne.io/fyne/v2/app"
15+
"fyne.io/fyne/v2/theme"
16+
)
17+
18+
func init() {
19+
runner.Runner()
20+
}
21+
22+
const preferenceCurrentTutorial = "currentTutorial"
23+
24+
var topWindow fyne.Window
25+
26+
func shortcutFocused(s fyne.Shortcut, w fyne.Window) {
27+
if focused, ok := w.Canvas().Focused().(fyne.Shortcutable); ok {
28+
focused.TypedShortcut(s)
29+
}
30+
}
31+
32+
func main() {
33+
a := app.NewWithID("com.idmiss.generator")
34+
a.SetIcon(theme.FyneLogo())
35+
w := a.NewWindow("Golang 代码生成器")
36+
w.SetFixedSize(true)
37+
topWindow = w
38+
tutorial := page.Pages["welcome"].SetView(w)
39+
40+
databaseItem := fyne.NewMenuItem("数据库配置", func() {
41+
logger.Log.WithFields(logrus.Fields{"data": ""}).Info("数据库配置")
42+
//tutorial = container.NewBorder(container.NewVBox(widget.NewLabelWithStyle(page.Pages["database"].Title, fyne.TextAlignCenter, fyne.TextStyle{Bold: true}), widget.NewSeparator()), nil, nil, nil,page.Pages["database"].View(w))
43+
tutorial = page.Pages["database"].SetView(w)
44+
w.SetContent(tutorial)
45+
})
46+
projectItem := fyne.NewMenuItem("项目配置", func() {
47+
tutorial = page.Pages["project"].SetView(w)
48+
w.SetContent(tutorial)
49+
logger.Log.WithFields(logrus.Fields{"data": ""}).Info("项目配置")
50+
})
51+
fileItem := fyne.NewMenuItem("模板文件", func() {
52+
53+
fmt.Println("模板文件")
54+
})
55+
56+
helpMenu := fyne.NewMenu("帮助",
57+
fyne.NewMenuItem("查看文档", func() {
58+
u, _ := url.Parse("http://www.idmiss.com/")
59+
_ = a.OpenURL(u)
60+
}))
61+
62+
welcome := fyne.NewMenuItem("首页", func() {
63+
//tutorial := container.NewBorder(container.NewVBox(widget.NewLabelWithStyle(page.Pages["welcome"].Title, fyne.TextAlignCenter, fyne.TextStyle{Bold: true}), widget.NewSeparator()), nil, nil, nil,page.Pages["welcome"].View(w))
64+
tutorial = page.Pages["welcome"].SetView(w)
65+
w.SetContent(tutorial)
66+
})
67+
autocodeMenu := fyne.NewMenuItem("代码生成", func() {
68+
tutorial = page.Pages["autocode"].SetView(w)
69+
w.SetContent(tutorial)
70+
logger.Log.WithFields(logrus.Fields{"data": ""}).Info("代码生成")
71+
})
72+
mainMenu := fyne.NewMainMenu(
73+
// a quit item will be appended to our first menu
74+
fyne.NewMenu("文件", welcome),
75+
fyne.NewMenu("设置", databaseItem, fyne.NewMenuItemSeparator(), projectItem, fyne.NewMenuItemSeparator(), fileItem),
76+
fyne.NewMenu("生成器", autocodeMenu),
77+
helpMenu,
78+
)
79+
80+
w.SetMainMenu(mainMenu)
81+
w.SetMaster()
82+
83+
w.SetContent(tutorial)
84+
85+
w.Resize(fyne.NewSize(640, 460))
86+
w.ShowAndRun()
87+
88+
os.Unsetenv("FYNE_FONT")
89+
}

mapper/mapper.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package mapper
2+
3+
import (
4+
"code/gen/auto"
5+
"code/gen/util/conf"
6+
"errors"
7+
)
8+
9+
//获取数据库所有的表
10+
func GetTables(dbName string) (err error, data []auto.TableInfo) {
11+
if conf.DB == nil {
12+
return errors.New("未找到数据连接"), data
13+
}
14+
err = conf.DB.Raw("select table_name ,table_comment,CREATE_TIME create_time from information_schema.tables where table_schema = ? ", dbName).Scan(&data).Error
15+
return
16+
}
17+
18+
func GetColumns(dbName, tableName string) (err error, data []auto.TableColumnInfo) {
19+
if conf.DB == nil {
20+
return errors.New("未找到数据连接"), data
21+
}
22+
err = conf.DB.Raw("SELECT COLUMN_NAME column_name,DATA_TYPE data_type,COLUMN_COMMENT colume_comment FROM INFORMATION_SCHEMA.COLUMNS c WHERE table_name = ? AND table_schema = ?", tableName, dbName).Scan(&data).Error
23+
return
24+
}

0 commit comments

Comments
 (0)