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