Makefile 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Custom configuration | 独立配置
  2. # Service name | 项目名称
  3. SERVICE=Wechat
  4. # Service name in specific style | 项目经过style格式化的名称
  5. SERVICE_STYLE=wechat
  6. # Service name in lowercase | 项目名称全小写格式
  7. SERVICE_LOWER=wechat
  8. # Service name in snake format | 项目名称下划线格式
  9. SERVICE_SNAKE=wechat
  10. # Service name in snake format | 项目名称短杠格式
  11. SERVICE_DASH=wechat
  12. # The project version, if you don't use git, you should set it manually | 项目版本,如果不使用git请手动设置
  13. VERSION=$(shell git describe --tags --always)
  14. # The project file name style | 项目文件命名风格
  15. PROJECT_STYLE=go_zero
  16. # Whether to use i18n | 是否启用 i18n
  17. PROJECT_I18N=false
  18. # The suffix after build or compile | 构建后缀
  19. PROJECT_BUILD_SUFFIX=api
  20. # Swagger type, support yml,json | Swagger 文件类型,支持yml,json
  21. SWAGGER_TYPE=json
  22. # Ent enabled features | Ent 启用的官方特性
  23. ENT_FEATURE=sql/execquery,sql/upsert,intercept
  24. # Auto generate API data for initialization | 自动生成 API 初始化数据
  25. AUTO_API_INIT_DATA=true
  26. # The arch of the build | 构建的架构
  27. GOARCH=amd64
  28. # ---- You may not need to modify the codes below | 下面的代码大概率不需要更改 ----
  29. GO ?= go
  30. GOFMT ?= gofmt "-s"
  31. GOFILES := $(shell find . -name "*.go")
  32. LDFLAGS := -s -w
  33. .PHONY: test
  34. test: # Run test for the project | 运行项目测试
  35. go test -v --cover ./internal/..
  36. .PHONY: fmt
  37. fmt: # Format the codes | 格式化代码
  38. $(GOFMT) -w $(GOFILES)
  39. .PHONY: lint
  40. lint: # Run go linter | 运行代码错误分析
  41. golangci-lint run -D staticcheck
  42. .PHONY: tools
  43. tools: # Install the necessary tools | 安装必要的工具
  44. $(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest;
  45. $(GO) install github.com/go-swagger/go-swagger/cmd/swagger@latest
  46. .PHONY: docker
  47. docker: # Build the docker image | 构建 docker 镜像
  48. docker build -f Dockerfile -t ${DOCKER_USERNAME}/$(SERVICE_DASH)-$(PROJECT_BUILD_SUFFIX):${VERSION} .
  49. @echo "Build docker successfully"
  50. .PHONY: publish-docker
  51. publish-docker: # Publish docker image | 发布 docker 镜像
  52. echo "${DOCKER_PASSWORD}" | docker login --username ${DOCKER_USERNAME} --password-stdin https://${REPO}
  53. docker push ${DOCKER_USERNAME}/$(SERVICE_DASH)-$(PROJECT_BUILD_SUFFIX):${VERSION}
  54. @echo "Publish docker successfully"
  55. .PHONY: gen-swagger
  56. gen-swagger: # Generate swagger file | 生成 swagger 文件
  57. swagger generate spec --output=./$(SERVICE_STYLE).$(SWAGGER_TYPE) --scan-models
  58. @echo "Generate swagger successfully"
  59. .PHONY: serve-swagger
  60. serve-swagger: # Run the swagger server | 运行 swagger 服务
  61. lsof -i:36666 | awk 'NR!=1 {print $2}' | xargs killall -9 || true
  62. swagger serve -F=swagger --port 36666 $(SERVICE_STYLE).$(SWAGGER_TYPE)
  63. @echo "Serve swagger-ui successfully"
  64. .PHONY: gen-api
  65. gen-api: # Generate API files | 生成 API 的代码
  66. goctls api go --api ./desc/all.api --dir ./ --trans_err=false --style=$(PROJECT_STYLE)
  67. # swagger generate spec --output=./$(SERVICE_STYLE).$(SWAGGER_TYPE) --scan-models
  68. @echo "Generate API codes successfully"
  69. .PHONY: gen-ent
  70. gen-ent: # Generate Ent codes | 生成 Ent 的代码
  71. go run -mod=mod entgo.io/ent/cmd/ent generate --template glob="./ent/template/*.tmpl" ./ent/schema --feature $(ENT_FEATURE)
  72. @echo "Generate Ent codes successfully"
  73. .PHONY: gen-api-ent-logic
  74. gen-api-ent-logic: # Generate CRUD logic from Ent, need to set model and group | 根据 Ent 生成 CRUD 代码,需要设置 model 和 group
  75. goctls api ent --schema=./ent/schema --style=$(PROJECT_STYLE) --api_service_name=$(SERVICE) --output=./ --model=$(model) --group=$(group) --i18n=$(PROJECT_I18N) --overwrite=true --api_data=$(AUTO_API_INIT_DATA)
  76. @echo "Generate CRUD codes from Ent successfully"
  77. .PHONY: build-win
  78. build-win: # Build project for Windows | 构建Windows下的可执行文件
  79. env CGO_ENABLED=0 GOOS=windows GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -trimpath -o $(SERVICE_STYLE)_$(PROJECT_BUILD_SUFFIX).exe $(SERVICE_STYLE).go
  80. @echo "Build project for Windows successfully"
  81. .PHONY: build-mac
  82. build-mac: # Build project for MacOS | 构建MacOS下的可执行文件
  83. env CGO_ENABLED=0 GOOS=darwin GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -trimpath -o $(SERVICE_STYLE)_$(PROJECT_BUILD_SUFFIX) $(SERVICE_STYLE).go
  84. @echo "Build project for MacOS successfully"
  85. .PHONY: build-linux
  86. build-linux: # Build project for Linux | 构建Linux下的可执行文件
  87. env CGO_ENABLED=0 GOOS=linux GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -trimpath -o $(SERVICE_STYLE)_$(PROJECT_BUILD_SUFFIX) $(SERVICE_STYLE).go
  88. @echo "Build project for Linux successfully"
  89. .PHONY: help
  90. help: # Show help | 显示帮助
  91. @grep -E '^[a-zA-Z0-9 -]+:.*#' Makefile | sort | while read -r l; do printf "\033[1;32m$$(echo $$l | cut -f 1 -d':')\033[00m:$$(echo $$l | cut -f 2- -d'#')\n"; done