Makefile 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # #######################################################
  2. # Function :Makefile for go-common #
  3. # Platform :All Linux Based Platform #
  4. # Version :1.0 #
  5. # Date :2020-12-23 #
  6. # Usage :make #
  7. # #######################################################
  8. # 项目路径
  9. PROJECT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
  10. # go项目相关
  11. GO_FILE := $(shell find . -name '*.go' | grep -v vendor/ | grep -v _test.go)
  12. PKG_LIST := $(shell go list git.shuncheng.lu/bigthing/gocommon/... | grep -v vendor | grep -v cmd)
  13. ifndef test_pkg
  14. test_pkg := \
  15. git.shuncheng.lu/bigthing/gocommon/pkg/database/common_db \
  16. git.shuncheng.lu/bigthing/gocommon/pkg/database/db \
  17. git.shuncheng.lu/bigthing/gocommon/pkg/database/multi_db \
  18. git.shuncheng.lu/bigthing/gocommon/pkg/internal/util
  19. endif
  20. # Go的全局的环境变量。GOFLAGS必须清空,防止其他参数干扰
  21. GOFLAGS :=
  22. GO111MODULE := on
  23. GOPROXY := https://goproxy.cn,direct
  24. GOPRIVATE := git.shuncheng.lu*
  25. export GO111MODULE
  26. export GOPROXY
  27. export GOPRIVATE
  28. export GOFLAGS
  29. # 项目配置文件位置
  30. export ROOT_PATH := $(PROJECT_DIR)
  31. # 防止本地文件有重名的问题
  32. .PHONY : all fmt tool gofmt goimports golint govet clean get test testall benchmark help
  33. # make默认启动
  34. all: testall benchmark ## 测试全部test和benchmark
  35. fmt: gofmt goimports ## 格式化代码
  36. tool: fmt ## 构建go-app cli工具
  37. go build -v -ldflags "-s -w" -o bin/go-app tool/main.go
  38. gofmt:
  39. @for item in $(GO_FILE); do gofmt -d -w $$item; done
  40. goimports:
  41. @if [ ! -d ${HOME}/go/bin ]; then mkdir -p ${HOME}/go/bin; fi
  42. @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
  43. @for item in $(GO_FILE); do ${HOME}/go/bin/goimports -d -w $$item; done
  44. govet: ## 静态代码检测工具
  45. @go vet $(PKG_LIST)
  46. golint: ## 检测代码规范工具
  47. @if [ ! -d ${HOME}/go/bin ]; then mkdir -p ${HOME}/go/bin; fi
  48. @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
  49. @${HOME}/go/bin/golint $(PKG_LIST)
  50. clean: ## 清楚无用文件
  51. $(RM) -r coverage.txt
  52. get: ## go get 包, eg: make get import=github.com/apache/rocketmq-client-go/v2@v2.0.0
  53. go get -u -v $(import)
  54. test: fmt clean ## 单元测试, eg: make test test_func=TestInArrayUint64 test_pkg=./pkg/common
  55. go test -v -race -short -cover -coverprofile=coverage.txt -covermode=atomic -run=$(test_func) $(test_pkg)
  56. go tool cover -html=coverage.txt
  57. benchmark: fmt clean ## 性能测试, eg: make benchmark test_func=BenchmarkLoadDefaultOp test_pkg=./pkg/net/httpclent
  58. go test -v -run=none -bench=$(test_func) -benchmem $(test_pkg)
  59. testall: fmt clean ## 测试整个项目
  60. go test -v -race -short -cover -coverprofile=coverage.txt -covermode=atomic $(test_pkg)
  61. go tool cover -html=coverage.txt
  62. help: ## 帮助
  63. @awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {sub("\\\\n",sprintf("\n%22c"," "), $$2);printf " \033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)