Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梁国权
gin-vue-admin
提交
a2cad44d
G
gin-vue-admin
项目概览
梁国权
/
gin-vue-admin
与 Fork 源项目一致
Fork自
FLIPPED-AURORA / gin-vue-admin
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gin-vue-admin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
a2cad44d
编写于
7月 18, 2021
作者:
S
songzhibin97
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat:优化自动代码段
上级
e7e46bb6
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
64 addition
and
45 deletion
+64
-45
server/service/system/sys_auto_code.go
server/service/system/sys_auto_code.go
+64
-45
未找到文件。
server/service/system/sys_auto_code.go
浏览文件 @
a2cad44d
...
...
@@ -23,6 +23,52 @@ const (
basePath
=
"resource/template"
)
var
injectionPaths
[]
injectionMeta
func
Init
()
{
if
len
(
injectionPaths
)
!=
0
{
return
}
injectionPaths
=
[]
injectionMeta
{
{
path
:
filepath
.
Join
(
global
.
GVA_CONFIG
.
AutoCode
.
Root
,
global
.
GVA_CONFIG
.
AutoCode
.
Server
,
global
.
GVA_CONFIG
.
AutoCode
.
SInitialize
,
"gorm.go"
),
funcName
:
"MysqlTables"
,
structNameF
:
"autocode.%s{},"
,
},
{
path
:
filepath
.
Join
(
global
.
GVA_CONFIG
.
AutoCode
.
Root
,
global
.
GVA_CONFIG
.
AutoCode
.
Server
,
global
.
GVA_CONFIG
.
AutoCode
.
SInitialize
,
"router.go"
),
funcName
:
"Routers"
,
structNameF
:
"autocodeRouter.Init%sRouter(PrivateGroup)"
,
},
{
path
:
filepath
.
Join
(
global
.
GVA_CONFIG
.
AutoCode
.
Root
,
global
.
GVA_CONFIG
.
AutoCode
.
Server
,
global
.
GVA_CONFIG
.
AutoCode
.
SApi
,
"enter.go"
),
funcName
:
"ApiGroup"
,
structNameF
:
"%sApi"
,
},
{
path
:
filepath
.
Join
(
global
.
GVA_CONFIG
.
AutoCode
.
Root
,
global
.
GVA_CONFIG
.
AutoCode
.
Server
,
global
.
GVA_CONFIG
.
AutoCode
.
SRouter
,
"enter.go"
),
funcName
:
"RouterGroup"
,
structNameF
:
"%sRouter"
,
},
{
path
:
filepath
.
Join
(
global
.
GVA_CONFIG
.
AutoCode
.
Root
,
global
.
GVA_CONFIG
.
AutoCode
.
Server
,
global
.
GVA_CONFIG
.
AutoCode
.
SService
,
"enter.go"
),
funcName
:
"ServiceGroup"
,
structNameF
:
"%sService"
,
},
}
}
type
injectionMeta
struct
{
path
string
funcName
string
structNameF
string
// 带格式化的
}
type
tplData
struct
{
template
*
template
.
Template
locationPath
string
...
...
@@ -142,6 +188,7 @@ func (autoCodeService *AutoCodeService) CreateTemp(autoCode system.AutoCodeStruc
idBf
.
WriteString
(
";"
)
}
if
autoCode
.
AutoMoveFile
{
// 判断是否需要自动转移
Init
()
for
index
:=
range
dataList
{
autoCodeService
.
addAutoMoveFile
(
&
dataList
[
index
])
}
...
...
@@ -150,49 +197,10 @@ func (autoCodeService *AutoCodeService) CreateTemp(autoCode system.AutoCodeStruc
return
err
}
}
initializeGormFilePath
:=
filepath
.
Join
(
global
.
GVA_CONFIG
.
AutoCode
.
Root
,
global
.
GVA_CONFIG
.
AutoCode
.
Server
,
global
.
GVA_CONFIG
.
AutoCode
.
SInitialize
,
"gorm.go"
)
initializeRouterFilePath
:=
filepath
.
Join
(
global
.
GVA_CONFIG
.
AutoCode
.
Root
,
global
.
GVA_CONFIG
.
AutoCode
.
Server
,
global
.
GVA_CONFIG
.
AutoCode
.
SInitialize
,
"router.go"
)
initializeApiFilePath
:=
filepath
.
Join
(
global
.
GVA_CONFIG
.
AutoCode
.
Root
,
global
.
GVA_CONFIG
.
AutoCode
.
Server
,
global
.
GVA_CONFIG
.
AutoCode
.
SApi
,
"enter.go"
)
initializeAutoRouterFilePath
:=
filepath
.
Join
(
global
.
GVA_CONFIG
.
AutoCode
.
Root
,
global
.
GVA_CONFIG
.
AutoCode
.
Server
,
global
.
GVA_CONFIG
.
AutoCode
.
SRouter
,
"enter.go"
)
initializeServiceFilePath
:=
filepath
.
Join
(
global
.
GVA_CONFIG
.
AutoCode
.
Root
,
global
.
GVA_CONFIG
.
AutoCode
.
Server
,
global
.
GVA_CONFIG
.
AutoCode
.
SService
,
"enter.go"
)
err
=
utils
.
AutoInjectionCode
(
initializeGormFilePath
,
"MysqlTables"
,
"model."
+
autoCode
.
StructName
+
"{},"
)
if
err
!=
nil
{
return
err
}
err
=
utils
.
AutoInjectionCode
(
initializeRouterFilePath
,
"Routers"
,
"router.Init"
+
autoCode
.
StructName
+
"Router(PrivateGroup)"
)
if
err
!=
nil
{
return
err
}
err
=
utils
.
AutoInjectionCode
(
initializeApiFilePath
,
"ApiGroup"
,
autoCode
.
StructName
+
"Api"
)
if
err
!=
nil
{
return
err
}
err
=
utils
.
AutoInjectionCode
(
initializeAutoRouterFilePath
,
"RouterGroup"
,
autoCode
.
StructName
+
"Router"
)
if
err
!=
nil
{
return
err
}
err
=
utils
.
AutoInjectionCode
(
initializeServiceFilePath
,
"ServiceGroup"
,
autoCode
.
StructName
+
"Service"
)
err
=
injectionCode
(
autoCode
.
StructName
,
&
injectionCodeMeta
)
if
err
!=
nil
{
return
err
return
}
injectionCodeMeta
.
WriteString
(
fmt
.
Sprintf
(
"%s@%s@%s"
,
initializeGormFilePath
,
"MysqlTables"
,
"model."
+
autoCode
.
StructName
+
"{},"
))
injectionCodeMeta
.
WriteString
(
";"
)
injectionCodeMeta
.
WriteString
(
fmt
.
Sprintf
(
"%s@%s@%s"
,
initializeRouterFilePath
,
"Routers"
,
"router.Init"
+
autoCode
.
StructName
+
"Router(PrivateGroup)"
))
injectionCodeMeta
.
WriteString
(
";"
)
injectionCodeMeta
.
WriteString
(
fmt
.
Sprintf
(
"%s@%s@%s"
,
initializeApiFilePath
,
"ApiGroup"
,
autoCode
.
StructName
+
"Api"
))
injectionCodeMeta
.
WriteString
(
";"
)
injectionCodeMeta
.
WriteString
(
fmt
.
Sprintf
(
"%s@%s@%s"
,
initializeAutoRouterFilePath
,
"RouterGroup"
,
autoCode
.
StructName
+
"Router"
))
injectionCodeMeta
.
WriteString
(
";"
)
injectionCodeMeta
.
WriteString
(
fmt
.
Sprintf
(
"%s@%s@%s"
,
initializeServiceFilePath
,
"ServiceGroup"
,
autoCode
.
StructName
+
"Service"
))
// 保存生成信息
for
_
,
data
:=
range
dataList
{
if
len
(
data
.
autoMoveFilePath
)
!=
0
{
...
...
@@ -202,11 +210,10 @@ func (autoCodeService *AutoCodeService) CreateTemp(autoCode system.AutoCodeStruc
}
if
global
.
GVA_CONFIG
.
AutoCode
.
TransferRestart
{
//
go func() {
//
_ = utils.Reload()
//
}()
go
func
()
{
_
=
utils
.
Reload
()
}()
}
//return errors.New("创建代码成功并移动文件成功")
}
else
{
// 打包
if
err
=
utils
.
ZipFiles
(
"./ginvueadmin.zip"
,
fileList
,
"."
,
"."
);
err
!=
nil
{
return
err
...
...
@@ -472,3 +479,15 @@ func (autoCodeService *AutoCodeService) getNeedList(autoCode *system.AutoCodeStr
}
return
dataList
,
fileList
,
needMkdir
,
err
}
// injectionCode 封装代码注入
func
injectionCode
(
structName
string
,
bf
*
strings
.
Builder
)
error
{
for
_
,
meta
:=
range
injectionPaths
{
code
:=
fmt
.
Sprintf
(
meta
.
structNameF
,
structName
)
if
err
:=
utils
.
AutoInjectionCode
(
meta
.
path
,
meta
.
funcName
,
code
);
err
!=
nil
{
return
err
}
bf
.
WriteString
(
fmt
.
Sprintf
(
"%s@%s@%s;"
,
meta
.
path
,
meta
.
funcName
,
code
))
}
return
nil
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录