-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile.golang
188 lines (146 loc) · 4.97 KB
/
Makefile.golang
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
SHELL = /bin/sh
CFG ?= .env
PRG ?= $(shell basename $$PWD)
PRG_DEST ?= $(PRG)
# -----------------------------------------------------------------------------
# Build config
SOURCES = $(shell find . -maxdepth 4 -mindepth 1 -path ./data -prune -o -name '*.go')
APP_VERSION ?= $(shell git describe --tags --always)
# Last project tag (used in `make changelog`)
RELEASE ?= $(shell git describe --tags --abbrev=0 --always)
# Repository address (compiled into main.repo)
REPO ?= $(shell git config --get remote.origin.url)
GO ?= go
GOLANG_IMAGE ?= ghcr.io/dopos/golang-alpine
GOLANG_VERSION ?= v1.22.3-alpine3.20
TARGETOS ?= linux
TARGETARCH ?= amd64
LDFLAGS := -s -w -extldflags '-static'
# Distros: arch
ALLARCH ?= "linux/amd64 linux/386 darwin/amd64 linux/arm linux/arm64"
# Distros: destination
DIRDIST ?= dist
# Path to golang package docs
GODOC_REPO ?= github.com/!le!kovr/$(PRG)
# App docker image
DOCKER_IMAGE ?= ghcr.io/lekovr/$(PRG)
.PHONY: build build-standalone run fmt lint ci-lint vet test cov-html cov-func cov-total cov-clean changelog
# ------------------------------------------------------------------------------
## Compile operations
#:
## Build app
build: $(PRG_DEST)
# Use: make build PRG_DEST=app-static
%-static: CGO_ENABLED=0
$(PRG_DEST): $(SOURCES) go.mod
@GOOS=$${TARGETOS} GOARCH=$${TARGETARCH} CGO_ENABLED=$(CGO_ENABLED) \
$(GO) build -v -o $@ -ldflags \
"$${LDFLAGS} -X main.version=$(APP_VERSION) -X main.repo=$${REPO}" \
./cmd/$(PRG)
## Build & run app
run: $(PRG)
./$(PRG) --log.debug --root=static --listen $(LISTEN) --listen_grpc $(LISTEN_GRPC)
## Format go sources
fmt:
$(GO) fmt ./...
## Check and sort imports
fmt-gci:
@which gci > /dev/null || $(GO) install github.com/daixiang0/gci@latest
@gci write . --skip-generated -s standard -s default
## Run lint
lint:
@which golint > /dev/null || $(GO) install golang.org/x/lint/golint@latest
@golint ./...
## Run golangci-lint
ci-lint:
@golangci-lint run ./...
## Run vet
vet:
@$(GO) vet ./...
## Run tests
test: lint vet coverage.out
test-race:
$(GO) test -tags test -race -covermode=atomic -coverprofile=$@ ./...
# internal target
coverage.out: $(SOURCES)
@#GIN_MODE=release $(GO) test -test.v -test.race -coverprofile=$@ -covermode=atomic ./...
$(GO) test -tags test -covermode=atomic -coverprofile=$@ ./...
## Open coverage report in browser
cov-html: cov
$(GO) tool cover -html=coverage.out
## Show code coverage per func
cov-func: coverage.out
$(GO) tool cover -func coverage.out
## Show total code coverage
cov-total: coverage.out
@$(GO) tool cover -func coverage.out | grep total: | awk '{print $$3}'
## Clean coverage report
cov-clean:
rm -f coverage.*
## count LoC without generated code
cloc:
@cloc --md --fullpath --exclude-dir=zgen --not-match-f=./proto/README.md \
--not-match-f=static/js/api.js --not-match-f=static/js/service.swagger.json .
## Changes from last tag
clog:
@echo Changes since $(RELEASE)
@echo
@git log $(RELEASE)..@ --pretty=format:"* %s"
# ------------------------------------------------------------------------------
## GRPC operations
#:
BUF_IMG ?= ghcr.io/apisite/gogens
#BUF_IMG ?= buf
## Generate files
buf-gen:
docker run --rm -v `pwd`:/mnt/pwd -w /mnt/pwd $(BUF_IMG) --debug generate --template buf.gen.yaml --path proto
## Run buf command
buf-cmd:
docker run --rm -it -v `pwd`:/mnt/pwd -w /mnt/pwd $(BUF_IMG) $(CMD)
## Run sh command
buf-sh:
docker run --rm -it --entrypoint /bin/sh -v `pwd`:/mnt/pwd -w /mnt/pwd $(BUF_IMG) $(CMD)
.PHONY: buf.lock
## Fetch buf.lock
buf.lock:
@id=$$(docker create $(BUF_IMG)) ; \
docker cp $$id:/app/$@ $@ ; \
docker rm -v $$id
## Generate JS API
js: static/js/api.js
static/js/api.js: zgen/ts/proto/service.pb.ts
docker run --rm -v `pwd`:/mnt/pwd -w /mnt/pwd --entrypoint /go/bin/esbuild $(BUF_IMG) \
zgen/ts/proto/service.pb.ts --bundle --outfile=/mnt/pwd/static/js/api.js --global-name=AppAPI
# ------------------------------------------------------------------------------
## Prepare distros
#:
## build app for all platforms
buildall: lint vet
@echo "*** $@ ***" ; \
for a in "$(ALLARCH)" ; do \
echo "** $${a%/*} $${a#*/}" ; \
P=$(PRG)-$${a%/*}_$${a#*/} ; \
$(MAKE) -s build TARGETOS=$${a%/*} TARGETARCH=$${a#*/} PRG_DEST=$$P ; \
done
## create disro files
dist: clean buildall
@echo "*** $@ ***"
@[ -d $(DIRDIST) ] || mkdir $(DIRDIST)
@sha256sum $(PRG)-* > $(DIRDIST)/SHA256SUMS ; \
for a in "$(ALLARCH)" ; do \
echo "** $${a%/*} $${a#*/}" ; \
P=$(PRG)-$${a%/*}_$${a#*/} ; \
zip "$(DIRDIST)/$$P.zip" "$$P" README.md README.ru.md screenshot.png; \
rm "$$P" ; \
done
## clean generated files
clean:
@echo "*** $@ ***" ; \
for a in "$(ALLARCH)" ; do \
P=$(PRG)_$${a%/*}_$${a#*/} ; \
[ -f $$P ] && rm $$P || true ; \
done
@[ -d $(DIRDIST) ] && rm -rf $(DIRDIST) || true
@[ -f $(PRG) ] && rm -f $(PRG) || true
@rm -f $(PRG)-* || true
@[ ! -f coverage.out ] || rm coverage.out