一、go-task包安装使用说明
go-task是一个任务运行器/构建工具,它的目标是更简单,更容易使用。
可以在windows中构建并执行环境命令。
支持环境:
- windows
- mac
- linux
二、下载安装包
https://github.com/go-task/task/releases
下载对应的安装包
这里以windows64位系统为例,下载windows64的包
task_windows_amd64.zip
三、添加环境变量
解压到 D:\Program Files (x86)\task_windows_amd64
把 D:\Program Files (x86)\task_windows_amd64
添加到系统环境变量 Path
下
验证是否安装成功
新开终端,运行命令
Administrator@WIN-20240929XKQ MINGW64 /d/code/go/myProjects/zero-admin/yrpt/ytss_go_zero (dev)
$ task --version
Task version: v3.39.2 (h1:Zt7KXHmMNq5xWZ1ihphDb+n2zYLCo4BdRe09AnMMIgA=)
Administrator@WIN-20240929XKQ MINGW64 /d/code/go/myProjects/zero-admin/yrpt/ytss_go_zero (dev)
$ task -h
Usage: task [flags...] [task...]
Runs the specified task(s). Falls back to the "default" task if no task name
was specified, or lists all tasks if an unknown task name was specified.
Example: 'task hello' with the following 'Taskfile.yml' file will generate an
'output.txt' file with the content "hello".
'''
version: '3'
tasks:
hello:
cmds:
- echo "I am going to write a file named 'output.txt' now."
- echo "hello" > output.txt
generates:
- output.txt
'''
Options:
-c, --color Colored output. Enabled by default. Set flag to false or use NO_COLOR=1 to disable. (default true)
--completion string Generates shell completion script.
-C, --concurrency int Limit number tasks to run concurrently.
-d, --dir string Sets directory of execution.
-n, --dry Compiles and prints tasks in the order that they would be run, without executing them.
-x, --exit-code Pass-through the exit code of the task command.
--experiments Lists all the available experiments and whether or not they are enabled.
-f, --force Forces execution even when the task is up-to-date.
-g, --global Runs global Taskfile, from $HOME/{T,t}askfile.{yml,yaml}.
-h, --help Shows Task usage.
-i, --init Creates a new Taskfile.yml in the current folder.
--insecure Forces Task to download Taskfiles over insecure connections.
-I, --interval duration Interval to watch for changes.
-j, --json Formats task list as JSON.
-l, --list Lists tasks with description of current Taskfile.
-a, --list-all Lists tasks with or without a description.
--no-status Ignore status when listing tasks as JSON
-o, --output string Sets output style: [interleaved|group|prefixed].
--output-group-begin string Message template to print before a task's grouped output.
--output-group-end string Message template to print after a task's grouped output.
--output-group-error-only Swallow output from successful tasks.
-p, --parallel Executes tasks provided on command line in parallel.
-s, --silent Disables echoing.
--sort string Changes the order of the tasks when listed. [default|alphanumeric|none].
--status Exits with non-zero exit code if any of the given tasks is not up-to-date.
--summary Show summary about a task.
-t, --taskfile string Choose which Taskfile to run. Defaults to "Taskfile.yml".
-v, --verbose Enables verbose mode.
--version Show Task version.
-w, --watch Enables watch of the given task.
-y, --yes Assume "yes" as answer to all prompts.
说明已经安装好 task
命令
四、在go项目下新建配置文件 taskfile.yaml
## go-task是一个任务运行器/构建工具,它的目标是更简单,更容易使用。
## 使用说明文档:
## http://haimait.top/docs/golang/go-task
## 常用命令
## task -h 显示帮助信息
## task -l 列出所有任务
version: '3'
tasks:
gen_model: # 运行命令 task gen_model
desc: task gen_model :生成model所有模块代码 task gen_code
dir: ./
cmds: ## 多行是运行多条命令
- go run ./models/gen/generator.go
gen_code: # 运行命令 task gen_code
desc: task gen_code :生成 api 和 rpc 模板定义文件代码
dir: ./
cmds: ## 多行是运行多条命令
- cd ./deploy/generate-code-v2 && go run main.go golang zero --dsn "zero-admin:123456@tcp(182.92.148.112:3306)/ytss_api" --tableNames erp_printing_template --rpcClient pb --author wanghaima
# 生成api所有模块代码
gen_api: # 运行命令 task gen_api
desc: task gen_api :生成api所有模块代码
dir: ./
cmds: ## 多行是运行多条命令
- goctl api format ./source/index.api --dir ./
- goctl api go -api ./app/api/admin/admin.api -dir ./app/api/admin --style=go_zero --home=./deploy/goctl
# 合并 `.protoc` 文件
merge_rpc: # 运行命令 task merge_rpc
desc: task merge_rpc :合并 `.protoc` 文件
dir: ./
cmds:
- go run ./app/rpc/company/proto/main.go
# 生成rpc所有模块代码
gen_rpc: # 运行命令 task gen_rpc
desc: task gen_rpc :生成rpc所有模块代码
dir: ./
cmds: ## 多行是运行多条命令
# 生成company-rpc代码 公司管理
- go run ./app/rpc/company/proto/main.go && goctl rpc protoc ./app/rpc/company/company-rpc.proto --go_out=./app/rpc/company/ --go-grpc_out=./app/rpc/company/ --zrpc_out=./app/rpc/company/ --style go_zero --home=./deploy/goctl -m
# 生成 manu 代码 供应商管理
- go run ./app/rpc/manu/proto/main.go && goctl rpc protoc ./app/rpc/manu/manu-rpc.proto --go_out=./app/rpc/manu/ --go-grpc_out=./app/rpc/manu/ --zrpc_out=./app/rpc/manu/ --style go_zero --home=./deploy/goctl -m
run: # 启动服务命令 task run
desc: task run :启动服务命令 task run
dir: ./
cmds:
- echo "Starting services..."
# - go run ./app/rpc/company/companyrpc.go -f ./app/rpc/company/etc/companyrpc-local.yaml
# - ./app/rpc/manu/manu -f ./app/rpc/manu/etc/manurpc-dev.yaml
# - ./app/api/admin/admin -f ./app/api/admin/etc/admin-api-dev.yaml
# - nohup ./app/rpc/company/company -f ./app/rpc/company/etc/companyrpc-dev.yaml >> accessCompany.log 2>&1 &
# - nohup ./app/rpc/manu/manu -f ./app/rpc/manu/etc/manurpc-dev.yaml >> accessManu.log 2>&1 &
# - nohup ./app/api/admin/admin -f ./app/api/admin/etc/admin-api-dev.yaml >> accessAdmin.log 2>&1 &
五、查看命令列表
Administrator@WIN-20240929XKQ MINGW64 /d/code/go/myProjects/zero-admin/yrpt/ytss_go_zero (dev)
$ task -l
task: Available tasks for this project:
* gen_api: task gen_api :生成api所有模块代码
* gen_code: task gen_code :生成 api 和 rpc 模板定义文件代码
* gen_model: task gen_model :生成model所有模块代码 task gen_code
* gen_rpc: task gen_rpc :生成rpc所有模块代码
* merge_rpc: task merge_rpc :合并 `.protoc` 文件
* run: task run :启动服务命令 task run
六、运行命令
Administrator@WIN-20240929XKQ MINGW64 /d/code/go/myProjects/zero-admin/yrpt/ytss_go_zero (dev)
$ task gen_api
task: [gen_api] goctl api format ./source/index.api --dir ./
task: [gen_api] goctl api go -api ./app/api/admin/admin.api -dir ./app/api/admin --style=go_zero --home=./deploy/goctl
app/api/admin/etc/admin-api.yaml exists, ignored generation
app/api/admin/internal/config/config.go exists, ignored generation
……………………