-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMakefile
More file actions
229 lines (189 loc) · 7.79 KB
/
Copy pathMakefile
File metadata and controls
229 lines (189 loc) · 7.79 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
SHELL := /bin/bash
.SHELLFLAGS := -eu -o pipefail -c
.EXPORT_ALL_VARIABLES:
GOPATH ?= $(shell go env GOPATH)
CURDIR := $(shell pwd)
# Versions
ALPINE_VERSION ?= 3.20
DOCKER_IMAGE_TAG ?= latest
GOFUMPT_VERSION ?= v0.7.0
GOLANG_VERSION ?= 1.23
GOLANGCI_LINT_VERSION ?= v2.1.6
GOTHANKS_VERSION ?= latest
MOCKGEN_VERSION ?= v0.5.0
TPARSE_VERSION ?= v0.17.0
# Some colors (if supported)
define get_color
$(shell tput -Txterm $(1) $(2) 2>/dev/null || echo "")
endef
COLOR_CYAN = $(call get_color,setaf,6)
COLOR_GREEN = $(call get_color,setaf,2)
COLOR_RED = $(call get_color,setaf,1)
COLOR_RESET = $(call get_color,sgr0,)
COLOR_WHITE = $(call get_color,setaf,7)
COLOR_YELLOW = $(call get_color,setaf,3)
# Application
BINARY_NAME = axone-mcp
BINARY_AMD64 = $(BINARY_NAME).amd64
$(info 🐚 ${COLOR_GREEN}Fetching ${COLOR_CYAN}version${COLOR_RESET} from ${COLOR_YELLOW}git${COLOR_RESET}...)
VERSION = $(shell cat version)
$(info 🐚 ${COLOR_GREEN}Fetching ${COLOR_CYAN}commit${COLOR_RESET} from ${COLOR_YELLOW}git${COLOR_RESET}...)
COMMIT = $(shell git log -1 --format='%H')
# Directories
TARGET_DIR = ./target
TOOLS_DIR = $(TARGET_DIR)/tools
GOFUMPT_BIN = $(TOOLS_DIR)/gofumpt/$(GOFUMPT_VERSION)/gofumpt
GOLANGCI_LINT_BIN = $(TOOLS_DIR)/golangci-lint/$(GOLANGCI_LINT_VERSION)/golangci-lint
GOTHANKS_BIN = $(TOOLS_DIR)/gothanks/$(GOTHANKS_VERSION)/gothanks
MOCKGEN_BIN = $(TOOLS_DIR)/mockgen/$(MOCKGEN_VERSION)/mockgen
TPARSE_BIN = $(TOOLS_DIR)/tparse/$(TPARSE_VERSION)/tparse
# Build options
build_tags += $(BUILD_TAGS)
build_tags := $(strip $(build_tags))
whitespace := $(subst ,, )
comma := ,
build_tags_comma_sep := $(subst $(whitespace),$(comma),$(build_tags))
# Flags
LD_FLAGS = \
-X github.com/axone-protocol/axone-mcp/internal/version.Name=$(BINARY_NAME) \
-X github.com/axone-protocol/axone-mcp/internal/version.Version=$(VERSION) \
-X github.com/axone-protocol/axone-mcp/internal/version.Commit=$(COMMIT)
BUILD_FLAGS := -ldflags '$(LD_FLAGS)'
GO_BUILD := CGO_ENABLED=0 go build $(BUILD_FLAGS)
# Environments
ENVIRONMENTS = \
darwin-amd64 \
darwin-arm64 \
linux-amd64 \
windows-amd64
ENVIRONMENTS_TARGETS = $(addprefix build-go-, $(ENVIRONMENTS))
# Handle sed -i on Darwin
SED_FLAG=
SHELL_NAME := $(shell uname -s)
ifeq ($(SHELL_NAME),Darwin)
SED_FLAG := ""
endif
default: help
.PHONY: check-deps
check-deps: ## Check for required external tools (docker, curl)
@$(call echo_msg, 🛠, Checking, dependencies, ...)
@command -v docker > /dev/null || { echo "Error: docker is not installed. Aborting." >&2; exit 1; }
@command -v curl > /dev/null || { echo "Error: curl is not installed. Aborting." >&2; exit 1; }
.PHONY: deps
deps: ## Download Go module dependencies
@$(call echo_msg, 📥, Downloading, dependencies, ...)
@go mod download
.PHONY: tools
tools: $(GOLANGCI_LINT_BIN) $(GOTHANKS_BIN) $(GOFUMPT_BIN) $(MOCKGEN_BIN) $(TPARSE_BIN) ## Install necessary development tools
.PHONY: thanks
thanks: tools ## Thanks to the contributors
@$(call echo_msg, 🙏, Running, gothanks, ...)
@$(GOTHANKS_BIN) -y | grep -v "is already"
.PHONY: build
build: build-go ## Build the project
.PHONY: build-go
build-go: deps ## Build executable for the current environment (default build)
@$(call echo_msg, 🏗️, Building, ${BINARY_NAME}, into ${COLOR_YELLOW}${TARGET_DIR})
@$(call build-go,"","",${TARGET_DIR}/${BINARY_NAME})
.PHONY: build-go-all
build-go-all: $(ENVIRONMENTS_TARGETS) ## Build executables for all available environments
.PHONY: test
test: test-go ## Run all the tests
.PHONY: test-go
test-go: deps tools ## Run tests for Go source code
@$(call echo_msg, 🧪, Testing, project, ...)
@go test -v -coverprofile ./target/coverage.txt $$(go list ./... | grep -v "/mocks") -json \
| $(TPARSE_BIN)
.PHONY: lint
lint: lint-go ## Lint files
.PHONY: lint-go
lint-go: tools ## Lint Go source code
@$(call echo_msg, 🔍, Linting, Go code, ...)
@$(GOLANGCI_LINT_BIN) run ./...
.PHONY: format
format: format-go ## Format files
.PHONY: format-go
format-go: tools ## Format Go source code
@$(call echo_msg, 📐, Formatting, Go source code, ...)
@$(GOFUMPT_BIN) -w -l .
## Mock:
.PHONY: mock
mock: tools ## Generate all the mocks (for tests)
@$(call echo_msg, 🧱, Generating, mocks, ...)
@$(MOCKGEN_BIN) -destination=internal/mocks/clientconn_mock.go -package=mocks google.golang.org/grpc ClientConnInterface
.PHONY: docker
docker: build ## Build Docker container
@$(call echo_msg, 📦, Building, Docker container, ...)
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $(BINARY_AMD64) .
@docker build -t $(BINARY_NAME):$(DOCKER_IMAGE_TAG) .
.PHONY: clean
clean: clean-artifacts clean-tools ## Clean up
.PHONY: clean-artifacts
clean-artifacts: ## Clean up build artifacts
@$(call echo_msg, 🧹, Cleaning, build artifacts, ...)
@rm -rf $(TARGET_DIR)
.PHONY: clean-tools
clean-tools: ## Clean up tools
@$(call echo_msg, 🧹, Cleaning, tools, ...)
@rm -rf $(TOOLS_DIR)
.PHONY: help
help: ## Show this help.
@echo ''
@echo 'Usage:'
@echo ' ${COLOR_YELLOW}make${COLOR_RESET} ${COLOR_GREEN}<target>${COLOR_RESET}'
@echo ''
@echo 'Targets:'
@$(foreach V,$(sort $(.VARIABLES)), \
$(if $(filter-out environment% default automatic,$(origin $V)), \
$(if $(filter TOOL_%,$V), \
export $V="$($V)";))) \
awk 'BEGIN {FS = ":.*?## "} { \
if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf " ${COLOR_YELLOW}%-20s${COLOR_GREEN}%s${COLOR_RESET}\n", $$1, $$2} \
else if (/^## .*$$/) {printf " ${COLOR_CYAN}%s${COLOR_RESET}\n", substr($$1,4)} \
}' $(MAKEFILE_LIST) | envsubst
$(TOOLS_DIR):
@mkdir -p $(TOOLS_DIR)
$(GOLANGCI_LINT_BIN): | $(TOOLS_DIR)
@$(call echo_msg, 📦, Installing, golangci-lint, $(COLOR_YELLOW)$(GOLANGCI_LINT_VERSION)$(COLOR_RESET)...)
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | \
sh -s -- -b $(shell go env GOPATH)/bin $(GOLANGCI_LINT_VERSION)
@mkdir -p $(dir $(GOLANGCI_LINT_BIN))
@cp $(shell go env GOPATH)/bin/golangci-lint $(dir $(GOLANGCI_LINT_BIN))
$(GOTHANKS_BIN):
@$(call echo_msg, 📦, Installing, gothanks, $(COLOR_YELLOW)$(GOTHANKS_VERSION)$(COLOR_RESET)...)
@mkdir -p $(dir $(GOTHANKS_BIN))
@GOBIN="$$(cd $(dir $(GOTHANKS_BIN)) && pwd)" go install github.com/psampaz/gothanks@$(GOTHANKS_VERSION)
$(GOFUMPT_BIN):
@$(call echo_msg, 📦, Installing, gofumpt, $(COLOR_YELLOW)$(GOFUMPT_VERSION)$(COLOR_RESET)...)
@mkdir -p $(dir $(GOFUMPT_BIN))
@GOBIN="$$(cd $(dir $(GOFUMPT_BIN)) && pwd)" go install mvdan.cc/gofumpt@$(GOFUMPT_VERSION)
$(MOCKGEN_BIN):
@$(call echo_msg, 📦, Installing, mockgen, $(COLOR_YELLOW)$(MOCKGEN_VERSION)$(COLOR_RESET)...)
@mkdir -p $(dir $(MOCKGEN_BIN))
@GOBIN="$$(cd $(dir $(MOCKGEN_BIN)) && pwd)" go install go.uber.org/mock/mockgen@$(MOCKGEN_VERSION)
$(TPARSE_BIN):
@$(call echo_msg, 📦, Installing, tparse, $(COLOR_YELLOW)$(TPARSE_VERSION)$(COLOR_RESET)...)
@mkdir -p $(dir $(TPARSE_BIN))
@GOBIN="$$(cd $(dir $(TPARSE_BIN)) && pwd)" go install github.com/mfridman/tparse@$(TPARSE_VERSION)
$(ENVIRONMENTS_TARGETS):
@GOOS=$(word 3, $(subst -, ,$@)); \
GOARCH=$(word 4, $(subst -, ,$@)); \
FOLDER=${TARGET_DIR}/$$GOOS/$$GOARCH; \
if [ $$GOOS = "windows" ]; then \
EXTENSION=".exe"; \
else \
EXTENSION=""; \
fi; \
FILENAME=$$FOLDER/${BINARY_NAME}$$EXTENSION; \
$(call echo_msg, 🏗️, Building, ${BINARY_NAME}, for environment ${COLOR_YELLOW}$$GOOS ($$GOARCH)${COLOR_RESET} into ${COLOR_YELLOW}$$FOLDER) && \
$(call build-go,$$GOOS,$$GOARCH,$$FILENAME)
# Prints a message with color
# $(call echo_msg, <emoji>, <action>, <object>, <context>)
define echo_msg
echo "$(strip $(1)) ${COLOR_GREEN}$(strip $(2))${COLOR_RESET} ${COLOR_CYAN}$(strip $(3))${COLOR_RESET} $(strip $(4))${COLOR_RESET}"
endef
# Build go executable
# $(call build-go, <os>, <arch>, <filename>)
define build-go
GOOS=$1 GOARCH=$2 $(GO_BUILD) -o $3 ${CMD_ROOT}
endef