-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
149 lines (123 loc) · 4.63 KB
/
Makefile
File metadata and controls
149 lines (123 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# ---------- Makefile for Sphere Project ----------
MODULE := $(shell go list -m)
MODULE_NAME ?= $(lastword $(subst /, ,$(MODULE)))
# ---------- Build Config ----------
GIT_TAG ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)
BUILD_TAG ?= $(if $(BUILD_VERSION),$(BUILD_VERSION),$(GIT_TAG))
BUILD_TIME := $(shell date +"%Y%m%d-%H%M%S")
BUILD_VER ?= $(BUILD_TAG)@$(BUILD_TIME)
# ---------- Arch Config ----------
CURRENT_OS := $(shell uname | tr '[:upper:]' '[:lower:]')
CURRENT_ARCH := $(shell uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')
BUILD_PLATFORMS ?= linux/amd64 linux/arm64
# ---------- Docker Config ----------
DOCKER_VER ?= $(BUILD_TAG)_$(BUILD_TIME)
DOCKER_IMAGE ?= ghcr.io/tbxark/$(MODULE_NAME):${DOCKER_VER}
DOCKER_FILE ?= cmd/app/Dockerfile
# ---------- Go Build Config ----------
LD_FLAGS ?= -X $(MODULE)/internal/config.BuildVersion=$(BUILD_VER)
GO ?= go
GO_TAGS ?= jsoniter#,embed_dash
GO_RUN ?= CGO_ENABLED=0 $(GO) run -ldflags "$(LD_FLAGS)" -tags=$(GO_TAGS)
GO_BUILD ?= CGO_ENABLED=0 $(GO) build -trimpath -ldflags "$(LD_FLAGS)" -tags=$(GO_TAGS)
GO_INSTALL ?= $(GO) install
# ---------- Go Tools ----------
BUF_CLI ?= buf
SWAG_CLI ?= swag
WIRE_CLI ?= wire
SPHERE_CLI ?= sphere-cli
GOLANG_CI_LINT ?= golangci-lint
INTERNAL_TOOLS ?= $(GO) run -tags spheretools
.PHONY: \
build build/all clean\
gen/wire gen/proto gen/all \
build/docker build/multi-docker \
run deploy lint fmt \
install init help
# ---------- Build Tools ----------
build: ## Build binary for current architecture
$(GO_BUILD) -o ./build/$(CURRENT_OS)_$(CURRENT_ARCH)/ ./...
build/%:
$(eval PLATFORM = $(subst /, ,$*))
$(eval GOOS = $(word 1, $(PLATFORM)))
$(eval GOARCH = $(word 2, $(PLATFORM)))
GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO_BUILD) -o ./build/$(subst /,_,$*)/ ./...
build/all: $(addprefix build/,$(BUILD_PLATFORMS)) ## Build for all supported platforms
# ---------- Generate Tools ----------
clean: ## Clean gen code and build files
rm -rf ./api/*
rm -rf ./build/*
rm -rf ./swagger/*
rm -rf ./internal/pkg/database/ent/*
gen/wire: ## Generate wire code
cd cmd/app/ && $(WIRE_CLI) gen
gen/conf: ## Generate example config
$(INTERNAL_TOOLS) ./cmd/tools/config gen
gen/proto: ## Generate proto files and run protoc plugins
$(BUF_CLI) dep update
$(BUF_CLI) dep prune
$(BUF_CLI) generate
$(BUF_CLI) generate --template buf.binding.yaml
gen/docs: gen/proto ## Generate swagger docs
$(SWAG_CLI) init \
--output ./swagger/api \
--tags api.v1 \
--instanceName API \
-g docs/docs.api.go \
--parseDependency
gen/all: clean gen/docs gen/wire fmt ## Generate all code
# ---------- Build Docker ----------
build/docker: ## Build docker image
docker build \
-t $(DOCKER_IMAGE) \
. \
-f $(DOCKER_FILE) \
--provenance=false \
--build-arg \
BUILD_VERSION=$(BUILD_VER)
build/multi-docker: ## Build multi-arch docker image
docker buildx build \
--platform=linux/amd64,linux/arm64 \
-t $(DOCKER_IMAGE) \
. \
-f $(DOCKER_FILE) \
--push \
--provenance=false \
--build-arg BUILD_VERSION=$(BUILD_VER)
# ---------- Tools ----------
run: ## Run the application
$(GO_RUN) -race $(MODULE)/cmd/app
run/swag: ## Run the swagger server
$(INTERNAL_TOOLS) $(MODULE)/cmd/tools/docs
deploy: ## Deploy binary
./devops/deploy/deploy.sh
lint: ## Run linter
$(GOLANG_CI_LINT) run --no-config --fix
$(BUF_CLI) lint
fmt: ## Run formatter and fix issues
$(GO) mod tidy
$(GO) fmt ./...
$(BUF_CLI) format -w
$(GOLANG_CI_LINT) fmt --no-config --enable gofmt,goimports
# ---------- Install Tools ----------
install: ## Install dependencies tools
$(GO_INSTALL) github.com/google/wire/cmd/wire@latest
$(GO_INSTALL) github.com/swaggo/swag/cmd/swag@latest
$(GO_INSTALL) github.com/bufbuild/buf/cmd/buf@latest
$(GO_INSTALL) github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest
$(GO_INSTALL) google.golang.org/protobuf/cmd/protoc-gen-go@latest
$(GO_INSTALL) github.com/go-sphere/sphere-cli@latest
$(GO_INSTALL) github.com/go-sphere/protoc-gen-route@latest
$(GO_INSTALL) github.com/go-sphere/protoc-gen-sphere@latest
$(GO_INSTALL) github.com/go-sphere/protoc-gen-sphere-errors@latest
$(GO_INSTALL) github.com/go-sphere/protoc-gen-sphere-binding@latest
init: ## Init all dependencies
$(GO) mod download
$(MAKE) install
$(MAKE) gen/all
$(BUF_CLI) dep update
$(GO) mod tidy
help: ## Show this help message
@echo "\n\033[1mSphere build tool.\033[0m Usage: make [target]\n"
@grep -h "##" $(MAKEFILE_LIST) | grep -v grep | sed -e 's/\(.*\):.*##\(.*\)/\1:\2/' | column -t -s ':' | sed -e 's/^/ /'
.DEFAULT_GOAL := help