Makefile 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # #######################################################
  2. # Function :Makefile for go #
  3. # Platform :All Darwin Based Platform #
  4. # Version :1.0 #
  5. # Date :2020-12-17 #
  6. # Author :fanhaodong516@gmail.com #
  7. # Usage :make help #
  8. # #######################################################
  9. # 项目路径
  10. PROJECT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
  11. # Go相关参数参考: https://juejin.cn/post/6844903848583266311
  12. GOBUILD_ARGS := go build -v -ldflags "-s -w"
  13. GORUN_ARGS := GODEBUG=gctrace=1
  14. GOMOD_VENDOR := $(shell if [ -d vendor ];then echo -mod=vendor; fi)
  15. # 如果没有设置-race,则默认开启race
  16. ifndef race
  17. GOBUILD_ARGS += -race
  18. endif
  19. # go test 相关
  20. ifndef test_pkg
  21. test_pkg := ./...
  22. endif
  23. # Go的全局的环境变量, GOFLAGS必须清空, 防止其他参数干扰
  24. GOFLAGS :=
  25. GO111MODULE := on
  26. GOPROXY := https://goproxy.cn,direct
  27. GOPRIVATE := gitea.ckfah.com/cjjy*
  28. export GO111MODULE
  29. export GOPROXY
  30. export GOPRIVATE
  31. export GOFLAGS
  32. # 项目配置文件位置
  33. export ROOT_PATH := $(PROJECT_DIR)
  34. # 第一个是:全路径文件/ 第二个是:git diff文件
  35. GO_FILE := $(shell find . -name '*.go' | grep -v vendor )
  36. #GO_FILE := $(shell git diff --name-only --diff-filter=ACM | grep '.go' | grep -v vendor | grep -v _test.go)
  37. PKG_LIST := $(shell go list go-template/... | grep -v vendor )
  38. # 防止本地文件有重名的问题
  39. .PHONY : all init build run fmt gofmt goimports govet golint clean get get-gocommon-last-tag get-gocommon-master test testall benchmark help docker docker_image docker_network prometheus
  40. all: fmt govet run ## 启动项目程序
  41. init: ## 初始化项目环境依赖
  42. @git config --global url."git@gitea.ckfah.com:".insteadOf "https://gitea.ckfah.com"
  43. @if [ ! -e config/env.ini ];then cp config/env.dev.ini config/env.ini; fi
  44. @if [ ! -e config/apollo/apollo.json ];then cp config/apollo/apollo.dev.json config/apollo/apollo.json; fi
  45. build: init ## 构建go程序
  46. $(GOBUILD_ARGS) $(GOMOD_VENDOR) -o bin/app cmd/main.go
  47. run: build ## 启动Go程序
  48. $(GORUN_ARGS) bin/app
  49. fmt: gofmt goimports ## 格式化Go项目代码
  50. gofmt:
  51. @for item in $(GO_FILE); do gofmt -d -w $$item; done
  52. goimports:
  53. @if [ ! -d ${HOME}/go/bin ]; then mkdir -p ${HOME}/go/bin; fi
  54. @if [ ! -e ${HOME}/go/bin/goimports ]; then curl -o ${HOME}/go/bin/goimports https://anthony-wangpan.oss-accelerate.aliyuncs.com/software/2020/12-29/788bd0e30957478488d4159859d29a0e && chmod 0744 ${HOME}/go/bin/goimports; fi
  55. @for item in $(GO_FILE); do ${HOME}/go/bin/goimports -d -w $$item; done
  56. govet: ## 静态代码检测工具
  57. go vet $(GOMOD_VENDOR) $(PKG_LIST)
  58. golint: ## 检测代码规范工具
  59. @if [ ! -d ${HOME}/go/bin ]; then mkdir -p ${HOME}/go/bin; fi
  60. @if [ ! -e ${HOME}/go/bin/golint ]; then curl -o ${HOME}/go/bin/golint https://anthony-wangpan.oss-accelerate.aliyuncs.com/software/2020/12-30/6fda119141b84c77b0924e9d140704d0 && chmod 0744 ${HOME}/go/bin/golint; fi
  61. @${HOME}/go/bin/golint $(PKG_LIST)
  62. clean: ## 清理无用文件
  63. $(RM) -r bin coverage.txt app.pid
  64. get: ## go get 包, eg: make get import=github.com/apache/rocketmq-client-go/v2@v2.0.0
  65. go get -u -v $(import)
  66. go mod tidy
  67. get-gocommon-last-tag: ## 获取gocommon框架最新的依赖tag
  68. go get -u -v gitea.ckfah.com/cjjy/gocommon
  69. go mod tidy
  70. get-gocommon-master: ## 获取gocommon框架的master分支
  71. go get -u -v gitea.ckfah.com/cjjy/ebike-city-op/gocommon@master
  72. go mod tidy
  73. test: fmt clean ## 单元测试, eg: make test test_func=TestDemo test_pkg=./business/util
  74. go test -v -race -short -cover -coverprofile=coverage.txt -covermode=atomic -run=$(test_func) $(test_pkg)
  75. benchmark: fmt clean ## 性能测试, eg: make benchmark test_func=BenchmarkDemo test_pkg=./business/util
  76. go test -v -run=none -bench=$(test_func) -benchmem $(test_pkg)
  77. testall: fmt clean ## 测试整个项目
  78. go test -v -race -short -cover -coverprofile=coverage.txt -covermode=atomic $(test_pkg)
  79. go tool cover -html=coverage.txt
  80. coverage: ## 打开浏览器展示测试覆盖率, 前提是你已经测试通过了
  81. go tool cover -html=coverage.txt
  82. docker_network:
  83. @docker network list | grep go-project; if [ $$? -eq 1 ]; then docker network create go-project; fi
  84. docker_image:
  85. cp -r ${HOME}/.ssh ./
  86. docker build -t go-template:latest .
  87. docker: docker_image docker_network ## 使用docker容器进行启动, 依靠绑定目录实现快速构建
  88. docker run -it --rm \
  89. --name go-template \
  90. -p 8080:8080 \
  91. --network go-project \
  92. -v ${HOME}/go/pkg:/root/go/pkg \
  93. -v /tmp/go/.cache/go-build:/root/.cache/go-build \
  94. -v /tmp/go/tmp:/tmp \
  95. -v $(PROJECT_DIR):/data/go-template \
  96. -v /data/log/go-template:/data/log/go-template \
  97. go-template:latest make run
  98. prometheus: docker_network ## 本地的prometheus监控,由于mac os 操作系统本身的原因导致无法看到进程信息,参考文档:https://prometheus.io/docs/prometheus
  99. @if [ ! -e bin/prometheus/prometheus.yml ]; then mkdir -p bin/prometheus; curl -o bin/prometheus/prometheus.yml https://anthony-wangpan.oss-accelerate.aliyuncs.com/software/2021/4-9/863df45f80cf42cbb45dfc745e5fac3e.yml ; fi
  100. docker run --rm \
  101. -p 9090:9090 \
  102. -v $(PROJECT_DIR)/bin/prometheus/prometheus.yml:/opt/bitnami/prometheus/conf/prometheus.yml \
  103. -v $(PROJECT_DIR)/bin/prometheus/data:/opt/bitnami/prometheus/data \
  104. --network go-project \
  105. bitnami/prometheus:latest
  106. help: ## 帮助
  107. @awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {sub("\\\\n",sprintf("\n%22c"," "), $$2);printf " \033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)