| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- # #######################################################
- # Function :Makefile for go #
- # Platform :All Darwin Based Platform #
- # Version :1.0 #
- # Date :2020-12-17 #
- # Author :fanhaodong516@gmail.com #
- # Usage :make help #
- # #######################################################
- # 项目路径
- PROJECT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
- # Go相关参数参考: https://juejin.cn/post/6844903848583266311
- GOBUILD_ARGS := go build -v -ldflags "-s -w"
- GORUN_ARGS := GODEBUG=gctrace=1
- GOMOD_VENDOR := $(shell if [ -d vendor ];then echo -mod=vendor; fi)
- # 如果没有设置-race,则默认开启race
- ifndef race
- GOBUILD_ARGS += -race
- endif
- # go test 相关
- ifndef test_pkg
- test_pkg := ./...
- endif
- # Go的全局的环境变量, GOFLAGS必须清空, 防止其他参数干扰
- GOFLAGS :=
- GO111MODULE := on
- GOPROXY := https://goproxy.cn,direct
- GOPRIVATE := gitea.ckfah.com/cjjy*
- export GO111MODULE
- export GOPROXY
- export GOPRIVATE
- export GOFLAGS
- # 项目配置文件位置
- export ROOT_PATH := $(PROJECT_DIR)
- # 第一个是:全路径文件/ 第二个是:git diff文件
- GO_FILE := $(shell find . -name '*.go' | grep -v vendor )
- #GO_FILE := $(shell git diff --name-only --diff-filter=ACM | grep '.go' | grep -v vendor | grep -v _test.go)
- PKG_LIST := $(shell go list go-template/... | grep -v vendor )
- # 防止本地文件有重名的问题
- .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
- all: fmt govet run ## 启动项目程序
- init: ## 初始化项目环境依赖
- @git config --global url."git@gitea.ckfah.com:".insteadOf "https://gitea.ckfah.com"
- @if [ ! -e config/env.ini ];then cp config/env.dev.ini config/env.ini; fi
- @if [ ! -e config/apollo/apollo.json ];then cp config/apollo/apollo.dev.json config/apollo/apollo.json; fi
- build: init ## 构建go程序
- $(GOBUILD_ARGS) $(GOMOD_VENDOR) -o bin/app cmd/main.go
- run: build ## 启动Go程序
- $(GORUN_ARGS) bin/app
- fmt: gofmt goimports ## 格式化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 $(GOMOD_VENDOR) $(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 bin coverage.txt app.pid
- get: ## go get 包, eg: make get import=github.com/apache/rocketmq-client-go/v2@v2.0.0
- go get -u -v $(import)
- go mod tidy
- get-gocommon-last-tag: ## 获取gocommon框架最新的依赖tag
- go get -u -v gitea.ckfah.com/cjjy/gocommon
- go mod tidy
- get-gocommon-master: ## 获取gocommon框架的master分支
- go get -u -v gitea.ckfah.com/cjjy/ebike-city-op/gocommon@master
- go mod tidy
- test: fmt clean ## 单元测试, eg: make test test_func=TestDemo test_pkg=./business/util
- go test -v -race -short -cover -coverprofile=coverage.txt -covermode=atomic -run=$(test_func) $(test_pkg)
- benchmark: fmt clean ## 性能测试, eg: make benchmark test_func=BenchmarkDemo test_pkg=./business/util
- 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
- coverage: ## 打开浏览器展示测试覆盖率, 前提是你已经测试通过了
- go tool cover -html=coverage.txt
- docker_network:
- @docker network list | grep go-project; if [ $$? -eq 1 ]; then docker network create go-project; fi
- docker_image:
- cp -r ${HOME}/.ssh ./
- docker build -t go-template:latest .
- docker: docker_image docker_network ## 使用docker容器进行启动, 依靠绑定目录实现快速构建
- docker run -it --rm \
- --name go-template \
- -p 8080:8080 \
- --network go-project \
- -v ${HOME}/go/pkg:/root/go/pkg \
- -v /tmp/go/.cache/go-build:/root/.cache/go-build \
- -v /tmp/go/tmp:/tmp \
- -v $(PROJECT_DIR):/data/go-template \
- -v /data/log/go-template:/data/log/go-template \
- go-template:latest make run
- prometheus: docker_network ## 本地的prometheus监控,由于mac os 操作系统本身的原因导致无法看到进程信息,参考文档:https://prometheus.io/docs/prometheus
- @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
- docker run --rm \
- -p 9090:9090 \
- -v $(PROJECT_DIR)/bin/prometheus/prometheus.yml:/opt/bitnami/prometheus/conf/prometheus.yml \
- -v $(PROJECT_DIR)/bin/prometheus/data:/opt/bitnami/prometheus/data \
- --network go-project \
- bitnami/prometheus:latest
- 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)
|