generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
107 lines (88 loc) · 3.71 KB
/
Makefile
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
# ℹ Freely based on: https://gist.github.com/thomaspoignant/5b72d579bd5f311904d973652180c705
# Constants
BINARY_NAME = template-go
TARGET_FOLDER = target
DIST_FOLDER = $(TARGET_FOLDER)/dist
DOCKER_IMAGE_GOLANG_CI = golangci/golangci-lint:v1.55
# Some colors
COLOR_GREEN = $(shell tput -Txterm setaf 2)
COLOR_YELLOW = $(shell tput -Txterm setaf 3)
COLOR_WHITE = $(shell tput -Txterm setaf 7)
COLOR_CYAN = $(shell tput -Txterm setaf 6)
COLOR_RESET = $(shell tput -Txterm sgr0)
# Flags
VERSION := $(shell cat version)
COMMIT := $(shell git log -1 --format='%H')
LD_FLAGS = \
-X okp4/template-go/internal/version.Name=$(BINARY_NAME) \
-X okp4/template-go/internal/version.Version=$(VERSION) \
-X okp4/template-go/internal/version.Commit=$(COMMIT)
BUILD_FLAGS := -ldflags '$(LD_FLAGS)'
# Commands
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))
.PHONY: all lint lint-go build build-go help
all: help
## Lint:
lint: lint-go ## Lint all available linters
lint-go: ## Lint go source code
@echo "${COLOR_CYAN}🔍 Inspecting go source code${COLOR_RESET}"
@docker run --rm \
-v `pwd`:/app:ro \
-w /app \
${DOCKER_IMAGE_GOLANG_CI} \
golangci-lint run -v ./...
## Build:
build: build-go ## Build all available artefacts (executable, docker image, etc.)
build-go: ## Build executable for the current environment (default build)
@echo "${COLOR_CYAN} 🏗️ Building project ${COLOR_RESET}${CMD_ROOT}${COLOR_CYAN}${COLOR_RESET} into ${COLOR_YELLOW}${DIST_FOLDER}${COLOR_RESET}"
@$(call build-go,"","",${DIST_FOLDER}/${BINARY_NAME})
build-go-all: $(ENVIRONMENTS_TARGETS) ## Build executables for all available environments
$(ENVIRONMENTS_TARGETS):
@GOOS=$(word 3, $(subst -, ,$@)); \
GOARCH=$(word 4, $(subst -, ,$@)); \
FOLDER=${DIST_FOLDER}/$$GOOS/$$GOARCH; \
if [ $$GOOS = "windows" ]; then \
EXTENSION=".exe"; \
fi; \
FILENAME=$$FOLDER/${BINARY_NAME}$$EXTENSION; \
echo "${COLOR_CYAN} 🏗️ Building project ${COLOR_RESET}${CMD_ROOT}${COLOR_CYAN} for environment ${COLOR_YELLOW}$$GOOS ($$GOARCH)${COLOR_RESET} into ${COLOR_YELLOW}$$FOLDER${COLOR_RESET}" && \
$(call build-go,$$GOOS,$$GOARCH,$$FILENAME)
## Install:
install: ## Install executable
@echo "${COLOR_CYAN} 🚚 Installing project ${BINARY_NAME}${COLOR_RESET}"
@go install $(BUILD_FLAGS) ${CMD_ROOT}
## Test:
test: test-go ## Pass all the tests
test-go: build ## Pass the test for the go source code
@echo "${COLOR_CYAN} 🧪 Passing go tests${COLOR_RESET}"
@go test -v -covermode=count -coverprofile ./target/coverage.out ./...
## Help:
help: ## Show this help.
@echo ''
@echo 'Usage:'
@echo ' ${COLOR_YELLOW}make${COLOR_RESET} ${COLOR_GREEN}<target>${COLOR_RESET}'
@echo ''
@echo 'Targets:'
@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)
@echo ''
@echo 'This Makefile depends on ${COLOR_CYAN}docker${COLOR_RESET}. To install it, please follow the instructions:'
@echo '- for ${COLOR_YELLOW}macOS${COLOR_RESET}: https://docs.docker.com/docker-for-mac/install/'
@echo '- for ${COLOR_YELLOW}Windows${COLOR_RESET}: https://docs.docker.com/docker-for-windows/install/'
@echo '- for ${COLOR_YELLOW}Linux${COLOR_RESET}: https://docs.docker.com/engine/install/'
# Build go executable
# $1: operating system (GOOS)
# $2: architecture (GOARCH)
# $3: filename of the executable generated
define build-go
GOOS=$1 GOARCH=$2 $(GO_BUiLD) -o $3 ${CMD_ROOT}
endef