-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
106 lines (85 loc) · 2.84 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
PROJ_NAME = bhlquest
VERSION = $(shell git describe --tags)
VER = $(shell git describe --tags --abbrev=0)
DATE = $(shell TZ=UTC date +'%Y-%m-%d_%H:%M:%ST%Z')
NO_C = CGO_ENABLED=0
FLAGS_LD = -ldflags "-X github.com/gnames/$(PROJ_NAME)/pkg.Build=$(DATE) \
-X github.com/gnames/$(PROJ_NAME)/pkg.Version=$(VERSION)"
FLAGS_REL = -trimpath -ldflags "-s -w -X codeberg.org/dimus/$(PROJ_NAME)/pkg.Build=$(DATE)"
FLAGS_SHARED = $(NO_C) GOARCH=amd64
RELEASE_DIR = /tmp
TEST_OPTS = -v -shuffle=on -race -coverprofile=coverage.txt -covermode=atomic
GOCMD = go
GOTEST = $(GOCMD) test
GOVET = $(GOCMD) vet
GOBUILD = $(GOCMD) build $(FLAGS_LD)
GORELEASE = $(GOCMD) build $(FLAGS_REL)
GOCLEAN = $(GOCMD) clean
GOINSTALL = $(GOCMD) install $(FLAGS_LD)
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)
.PHONY: all build clean
all: install
## Dependencies
deps: ## Download dependencies
$(GOCMD) mod download;
## Tools
tools: deps ## Install tools
@cat tools.go | grep _ | awk -F'"' '{print $$2}' | xargs -tI % go install %
## Build:
build: openapi ## Build binary
$(NO_C) $(GOCMD) build \
-o $(PROJ_NAME) \
$(FLAGS_LD) \
.
## Build Release
buildrel: openapi ## Build binary without debug info and with hardcoded version
$(NO_C) $(GOCMD) build \
-o $(PROJ_NAME) \
$(FLAGS_REL) \
.
## Install:
install: openapi ## Build and install binary
$(NO_C) $(GOINSTALL)
## Release
release: openapi buildrel dockerhub ## Build and package binaries for a release
$(GOCLEAN); \
$(FLAGS_SHARED) GOOS=linux $(GORELEASE); \
tar zcvf $(RELEASE_DIR)/$(PROJ_NAME)-$(VER)-linux.tar.gz $(PROJ_NAME); \
$(GOCLEAN);
## Clean
clean: ## Clean all the files and binaries generated by the Makefile
rm -rf ./out
## Test
test: ## Run the tests of the project
$(GOTEST) $(TEST_OPTS) ./...
## Coverage
coverage: ## Run the tests of the project and export the coverage
$(GOTEST) -cover -covermode=count -coverprofile=profile.cov ./...
$(GOCMD) tool cover -func profile.cov
## OpenAPI generation
openapi: ## Generate documentation for OpenAPI
swag init -g server.go -d ./internal/io/web --parseDependency --parseInternal
docker: buildrel
docker buildx build -t gnames/$(PROJ_NAME):latest -t gnames/$(PROJ_NAME):$(VERSION) .; \
$(GOCLEAN);
dockerhub: docker
docker push gnames/$(PROJ_NAME); \
docker push gnames/$(PROJ_NAME):$(VERSION)
## Help:
help: ## Show this help
@echo ''
@echo 'Usage:'
@echo ' $(YELLOW)make${RESET} $(GREEN)<target>$(RESET)'
@echo ''
@echo 'Targets:'
@awk 'BEGIN {FS = ":.*?## "} { \
if (/^[0-9a-zA-Z_-]+:.*?##.*$$/) \
{printf " $(YELLOW)%-20s$(GREEN)%s$(RESET)\n", $$1, $$2} \
else if (/^## .*$$/) {printf " $(CYAN)%s$(RESET)\n", substr($$1,4)} \
}' $(MAKEFILE_LIST)
## Version
version: ## Display current version
@echo $(VERSION)