Makefile 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. GOHOSTOS:=$(shell go env GOHOSTOS)
  2. GOPATH:=$(shell go env GOPATH)
  3. VERSION=$(shell git describe --tags --always)
  4. ifeq ($(GOHOSTOS), windows)
  5. #the `find.exe` is different from `find` in bash/shell.
  6. #to see https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/find.
  7. #changed to use git-bash.exe to run find cli or other cli friendly, caused of every developer has a Git.
  8. #Git_Bash= $(subst cmd\,bin\bash.exe,$(dir $(shell where git)))
  9. Git_Bash=$(subst \,/,$(subst cmd\,bin\bash.exe,$(dir $(shell where git))))
  10. INTERNAL_PROTO_FILES=$(shell $(Git_Bash) -c "find internal -name *.proto")
  11. API_PROTO_FILES=$(shell $(Git_Bash) -c "find api -name *.proto")
  12. else
  13. INTERNAL_PROTO_FILES=$(shell find internal -name *.proto)
  14. API_PROTO_FILES=$(shell find api -name *.proto)
  15. endif
  16. .PHONY: init
  17. # init env
  18. init:
  19. go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
  20. go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
  21. go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
  22. go install github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2@latest
  23. go install github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2@latest
  24. go install github.com/google/gnostic/cmd/protoc-gen-openapi@latest
  25. go install github.com/google/wire/cmd/wire@latest
  26. go install entgo.io/ent/cmd/ent@latest
  27. # 必须加入,这里需要升级一下 grpc 的版本
  28. # 项目默认配置生成的 grpc 文件里 的 grpc.SupportPackageIsVersion9 会报错
  29. go get - u google.golang.org/grpc
  30. .PHONY: config
  31. # generate internal proto
  32. config:
  33. protoc --proto_path=./internal \
  34. --proto_path=./third_party \
  35. --go_out=paths=source_relative:./internal \
  36. $(INTERNAL_PROTO_FILES)
  37. .PHONY: api
  38. # generate apis proto
  39. api:
  40. protoc --proto_path=./api \
  41. --proto_path=./third_party \
  42. --go_out=paths=source_relative:./api \
  43. --go-http_out=paths=source_relative:./api \
  44. --go-grpc_out=paths=source_relative:./api \
  45. --openapi_out=fq_schema_naming=true,default_response=false:. \
  46. $(API_PROTO_FILES)
  47. .PHONY: validate
  48. # generate validate proto
  49. validate:
  50. protoc --proto_path=. \
  51. --proto_path=./third_party \
  52. --go_out=paths=source_relative:. \
  53. --validate_out=paths=source_relative,lang=go:. \
  54. $(API_PROTO_FILES)
  55. .PHONY: build
  56. # build
  57. build:
  58. mkdir -p bin/ && go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/ ./...
  59. .PHONY: errors
  60. # generate errors
  61. errors:
  62. protoc --proto_path=. \
  63. --proto_path=./third_party \
  64. --go_out=paths=source_relative:. \
  65. --go-errors_out=paths=source_relative:. \
  66. $(API_PROTO_FILES)
  67. .PHONY: generate
  68. # generate
  69. generate:
  70. go mod tidy
  71. go get github.com/google/wire/cmd/wire@latest
  72. go generate ./...
  73. .PHONY: all
  74. # generate all
  75. all:
  76. make api;
  77. make validate;
  78. make errors;
  79. make config;
  80. make generate;
  81. # show help
  82. help:
  83. @echo ''
  84. @echo 'Usage:'
  85. @echo ' make [target]'
  86. @echo ''
  87. @echo 'Targets:'
  88. @awk '/^[a-zA-Z\-\_0-9]+:/ { \
  89. helpMessage = match(lastLine, /^# (.*)/); \
  90. if (helpMessage) { \
  91. helpCommand = substr($$1, 0, index($$1, ":")); \
  92. helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
  93. printf "\033[36m%-22s\033[0m %s\n", helpCommand,helpMessage; \
  94. } \
  95. } \
  96. { lastLine = $$0 }' $(MAKEFILE_LIST)
  97. .DEFAULT_GOAL := help