diff --git a/.gitignore b/.gitignore index 757fee3..6bd8561 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/.idea \ No newline at end of file +/.idea +build \ No newline at end of file diff --git a/Makefile b/Makefile index 5f7c918..41f0190 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/README.md b/README.md index 9a47128..e969ebc 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 @@ -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: @@ -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请求文件 ```` \ No newline at end of file diff --git a/console/commands/bean.go b/console/commands/bean.go index 4294156..9ea21d1 100644 --- a/console/commands/bean.go +++ b/console/commands/bean.go @@ -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 @@ -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 diff --git a/console/commands/protoc.go b/console/commands/protoc.go index c455f79..8ea6856 100644 --- a/console/commands/protoc.go +++ b/console/commands/protoc.go @@ -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) } } } diff --git a/console/commands/route.go b/console/commands/route.go index 1194ff5..35cae0b 100644 --- a/console/commands/route.go +++ b/console/commands/route.go @@ -8,6 +8,7 @@ import ( "log" "os" "os/exec" + "sort" "strings" ) @@ -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 { @@ -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"