Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
zodial committed Nov 15, 2023
2 parents 17a0f60 + 497b1b5 commit b280037
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.idea
/.idea
build
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ ATDIR := $(shell pwd)
# mac 系统更新path可能不全
export PATH := $(GOBIN):$(PATH)

build:
build-mac:
go build -ldflags="-w -s" -o $(GOBIN)/toolset ./

build-win:
go build -ldflags="-w -s" -o $(GOBIN)/toolset.exe ./

build-all:
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -s" -o ./build/toolset-linux ./
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -s" -o ./build/toolset-win.exe ./
go build -ldflags="-w -s" -o ./build/toolset-mac ./

# toolset make:protoc -go_out=plugins=grpc:@root/generate/proto -debug=true
53 changes: 44 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# toolset 一个go项目工具集合

1. 有 go 环境安装
````shell
go install github.com/go-home-admin/toolset@latest
````

2. 直接 [下载](https://github.com/go-home-admin/toolset/releases) 二进制文件, 放到任意 path 包含的路径下

# 展示
![image](https://github.com/go-home-admin/toolset/blob/main/show.gif)


Expand All @@ -12,14 +17,21 @@ user@macOs path $ toolset
Usage:
command [options] [arguments] [has]
Base Options:
-debug 是否显示明细
-root 获取项目跟路径, 默认当前目录
-h 显示帮助信息
-root 获取项目跟路径
Available commands:
help 帮助命令
make:bean 生成依赖注入的声明源代码文件
make:orm 根据配置文件连接数据库, 生成orm源码
make:protoc 组装和执行protoc命令
make:route 根据protoc文件定义, 生成路由信息和控制器文件
help 帮助命令
make 执行所有make命令, 默认参数
make:bean 生成依赖注入的声明源代码文件, 使用@Bean注解, 和inject引入
make:curd 生成curd基础代码, 默认使用交互输入, 便捷调用
make:grpc 根据protoc文件定义, 生成路grpc基础文件
make:js 根据swagger生成js请求文件
make:mongo 根据proto文件, 生成mongodb的orm源码
make:orm 根据配置文件连接数据库, 生成orm源码
make:protoc 组装和执行protoc命令
make:route 根据protoc文件定义, 生成路由信息和控制器文件
make:swagger 生成文档
````

## 生成ORM
Expand Down Expand Up @@ -66,14 +78,17 @@ user@macOs path $ toolset make:bean
user@macOs path $ toolset make:bean -h
Usage:
make:bean
-scan = shell(pwd)
-name = New{name}
-scan = @root
-skip = @root/generate
Arguments:
Option:
-scan 扫码目录下的源码
-name New函数别名, 如果兼容旧的项目可以设置
-scan 扫码目录下的源码; shell(pwd)
-skip 跳过目录
-h 显示帮助信息
-debug 是否显示明细
-root 获取项目跟路径, 默认当前目录
-h 显示帮助信息
Has:
-f 强制更新
Description:
Expand Down Expand Up @@ -102,4 +117,24 @@ service Controller {
}
}
message TResponse {}
````

# 生成 js+注释 文件
````shell
user@macOs toolset % toolset make:js -h
Usage:
make:js
-in = @root/web/swagger.json
-out = @root/resources/src/api/swagger_gen.js
Arguments:
Option:
-in swagger.json路径, 可本地可远程
-out js文件输出路径
-tag 只生成指定tag的请求
-debug 是否显示明细
-root 获取项目跟路径, 默认当前目录
-h 显示帮助信息
Has:
Description:
根据swagger生成js请求文件
````
13 changes: 11 additions & 2 deletions console/commands/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ func (BeanCommand) Execute(input command.Input) {
bc := newBeanCache()
for _, fileParser := range fileParsers {
bc.name = fileParser.PackageName
for _, goType := range fileParser.Types {
keys := make([]string, 0)
for k := range fileParser.Types {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
// 排序后循环 type
for _, k := range keys {
goType := fileParser.Types[k]
for _, attr := range goType.Attrs {
if attr.HasTag("inject") {
// 只收集使用到的 import
Expand Down Expand Up @@ -320,7 +327,9 @@ func getImportStr(bc beanCache, m map[string]string) string {
sk := sortMap(nm)
got := ""
for _, k := range sk {
got += "\n\t" + nm[k] + " \"" + k + "\""
if k != "" {
got += "\n\t" + nm[k] + " \"" + k + "\""
}
}

return got
Expand Down
2 changes: 1 addition & 1 deletion console/commands/protoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func genProtoTag(out string) {
fileNewStr = append(fileNewStr, newStr...)
}

defer os.WriteFile(file.Path, fileNewStr, 0760)
defer os.WriteFile(file.Path, []byte(strings.TrimSpace(string(fileNewStr))+"\n"), 0760)
}
}
}
Expand Down
25 changes: 23 additions & 2 deletions console/commands/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"os/exec"
"sort"
"strings"
)

Expand Down Expand Up @@ -453,14 +454,16 @@ func genRoutesFunc(g *ApiGroups, m map[string]string) string {
"\n\treturn map[*" + homeApi + ".Config]func(c *" + homeGin + ".Context){"

for _, server := range g.servers {
for s, v := range server.Opt {
for _, s := range forOpt(server.Opt) {
v := server.Opt[s]
if s == "http.Resource" {
str += "\n\t\t" + homeApi + ".Get(\"" + v.Val + "\"):" + "c." + parser.StringToSnake(server.Name) + ".GinHandleCurd,"
str += "\n\t\t" + homeApi + ".Post(\"" + v.Val + "\"):" + "c." + parser.StringToSnake(server.Name) + ".GinHandleCurd,"
str += "\n\t\t" + homeApi + ".Any(\"" + v.Val + "/:action\"):" + "c." + parser.StringToSnake(server.Name) + ".GinHandleCurd,"
}
}
for rName, rpc := range server.Rpc {
for _, rName := range forServerOpt(server.Rpc) {
rpc := server.Rpc[rName]
for _, options := range rpc.Opt {
for _, option := range options {
if strings.Index(option.Key, "http.") == 0 {
Expand All @@ -477,6 +480,24 @@ func genRoutesFunc(g *ApiGroups, m map[string]string) string {
return str + "\n\t}\n}"
}

func forServerOpt(m map[string]parser.ServiceRpc) []string {
keys := make([]string, 0)
for k := range m {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
return keys
}

func forOpt(m map[string]parser.Option) []string {
keys := make([]string, 0)
for k := range m {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
return keys
}

func genRoutesStruct(g *ApiGroups, m map[string]string) string {
str := "\n// @Bean" +
"\ntype " + parser.StringToHump(g.name) + "Routes struct {\n"
Expand Down

0 comments on commit b280037

Please sign in to comment.