generated from okp4/template-go
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Makefile
118 lines (96 loc) · 4.21 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
108
109
110
111
112
113
114
115
116
117
118
# ℹ Freely based on: https://gist.github.com/thomaspoignant/5b72d579bd5f311904d973652180c705
# Constants
BINARY_NAME = cosmos-faucet
TARGET_FOLDER = target
DIST_FOLDER = $(TARGET_FOLDER)/dist
DOCKER_IMAGE_GOLANG_CI = golangci/golangci-lint:v1.46.2
# 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/cosmos-faucet/internal/version.Name=$(BINARY_NAME) \
-X okp4/cosmos-faucet/internal/version.Version=$(VERSION) \
-X okp4/cosmos-faucet/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)
## Generate:
generate: generate-graphql generate-mock
generate-graphql: ## Generate graphql models and resolvers based on the schema
@echo "${COLOR_CYAN} 📋 Generate graphql models and resolvers ${BINARY_NAME}${COLOR_RESET}"
@go run github.com/99designs/gqlgen generate
generate-mock:
@echo "${COLOR_CYAN} 🧪 Generate test mocks ${BINARY_NAME}${COLOR_RESET}"
@mockery --srcpkg github.com/asynkron/protoactor-go/actor --name Context --output test/mock --outpkg mock --filename actor_context.go --structname ActorContext
## 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 -coverprofile ./target/coverage.txt ./...
## 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