-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMakefile
202 lines (158 loc) · 5.2 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
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
## This is a self-documented Makefile. For usage information, run `make help`:
##
## For more information, refer to https://www.thapaliya.com/en/writings/well-documented-makefiles/
ROOTDIR := $(abspath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
DISTDIR := $(abspath $(ROOTDIR)/dist)
BUILD_VERSION := $(shell $(ROOTDIR)/scripts/version)
BUILD_COMMIT := $(shell git rev-parse HEAD^{commit})
BUILD_STAMP := $(shell date -u '+%Y-%m-%d %H:%M:%S+00:00')
ifneq (,)
$(foreach v, \
$(sort $(.VARIABLES)), \
$(info ===== $(origin $(v)) $(v) = $($(v))) \
)
endif
RUN_TOOL := $(ROOTDIR)/scripts/docker-run
include config.mk
-include local/Makefile
S := @
V :=
GO := GO111MODULE=on CGO_ENABLED=0 go
GO_VENDOR := $(if $(realpath $(ROOTDIR)/vendor/modules.txt),true,false)
GO_BUILD_COMMON_FLAGS := -trimpath
ifeq ($(GO_VENDOR),true)
GO_BUILD_MOD_FLAGS := -mod=vendor
GOLANGCI_LINT_MOD_FLAGS := --modules-download-mode=vendor
else
GO_BUILD_MOD_FLAGS := -mod=readonly
GOLANGCI_LINT_MOD_FLAGS := --modules-download-mode=readonly
endif
GO_BUILD_FLAGS := $(GO_BUILD_MOD_FLAGS) $(GO_BUILD_COMMON_FLAGS)
GO_PKGS ?= ./...
SH_FILES ?= $(shell find ./scripts -name *.sh)
GO_TEST_ARGS ?= $(GO_PKGS)
COMMANDS := $(shell test -d cmd && $(GO) list $(GO_BUILD_MOD_FLAGS) ./cmd/...)
EXAMPLES := $(shell test -d examples && $(GO) list $(GO_BUILD_MOD_FLAGS) ./examples/...)
VERSION_PKG := $(shell test -d internal/version && $(GO) list $(GO_BUILD_MOD_FLAGS) ./internal/version)
TEST_OUTPUT := $(DISTDIR)/test
.DEFAULT_GOAL := all
.PHONY: all
all: deps build
##@ Dependencies
.PHONY: deps-go
deps-go: ## Install Go dependencies.
ifeq ($(GO_VENDOR),true)
$(GO) mod vendor
else
$(GO) mod download
endif
$(GO) mod verify
$(GO) mod tidy -compat=1.17
.PHONY: deps
deps: deps-go ## Install all dependencies.
##@ Building
BUILD_GO_TARGETS := $(addprefix build-go-, $(COMMANDS) $(EXAMPLES))
.PHONY: $(BUILD_GO_TARGETS)
$(BUILD_GO_TARGETS): build-go-%:
$(call build_go_command,$*)
.PHONY: build-go
build-go: $(BUILD_GO_TARGETS) ## Build all Go binaries.
$(S) echo Done.
.PHONY: build
build: build-go ## Build everything.
scripts/go/bin/bra: scripts/go/go.mod
$(S) cd scripts/go; \
$(GO) mod download && \
$(GO) build -o ./bin/bra github.com/unknwon/bra
.PHONY: run
run: scripts/go/bin/bra ## Build and run web server on filesystem changes.
$(S) GO111MODULE=on scripts/go/bin/bra run
##@ Testing
.PHONY: test-go
test-go: ## Run Go tests.
test-go: export CGO_ENABLED=1 # -race needs CGO_ENABLED
test-go:
$(S) echo "test backend"
$(V) mkdir -p $(TEST_OUTPUT)
$(V) $(RUN_TOOL) gotestsum \
--format standard-verbose \
--jsonfile $(TEST_OUTPUT).json \
--junitfile $(TEST_OUTPUT).xml \
-- \
$(GO_BUILD_MOD_FLAGS) \
-cover \
-coverprofile=$(TEST_OUTPUT).cov \
-race \
$(GO_TEST_ARGS)
$(S) $(ROOTDIR)/scripts/report-test-coverage $(TEST_OUTPUT).cov
.PHONY: test
test: test-go ## Run all tests.
##@ Linting
.PHONY: golangci-lint
golangci-lint:
$(S) echo "lint via golangci-lint"
$(S) $(RUN_TOOL) golangci-lint run \
$(GOLANGCI_LINT_MOD_FLAGS) \
--config ./scripts/go/configs/golangci.yml \
$(GO_PKGS)
scripts/go/bin/gosec: scripts/go/go.mod
$(S) cd scripts/go && \
$(GO) mod download && \
$(GO) build -o ./bin/gosec github.com/securego/gosec/cmd/gosec
# TODO recheck the rules and leave only necessary exclusions
.PHONY: gosec
gosec: scripts/go/bin/gosec
$(S) echo "lint via gosec"
$(S) scripts/go/bin/gosec -quiet \
-exclude= \
-conf=./scripts/go/configs/gosec.json \
$(GO_PKGS)
.PHONY: go-vet
go-vet:
$(S) echo "lint via go vet"
$(S) $(GO) vet $(GO_BUILD_MOD_FLAGS) $(GO_PKGS)
.PHONY: lint-go
lint-go: go-vet golangci-lint gosec ## Run all Go code checks.
.PHONY: lint
lint: lint-go ## Run all code checks.
##@ Packaging
.PHONY: package
package: build ## Build Debian and RPM packages.
$(S) echo "Building Debian and RPM packages..."
$(S) sh scripts/package/package.sh
.PHONY: publish-packages
publish-packages: package ## Publish Debian and RPM packages to the repository.
$(S) echo "Publishing Debian and RPM packages...."
$(S) sh scripts/package/publish.sh
##@ Helpers
.PHONY: clean
clean: ## Clean up intermediate build artifacts.
$(S) echo "Cleaning intermediate build artifacts..."
$(V) rm -rf node_modules
$(V) rm -rf public/build
$(V) rm -rf dist/build
$(V) rm -rf dist/publish
.PHONY: distclean
distclean: clean ## Clean up all build artifacts.
$(S) echo "Cleaning all build artifacts..."
$(V) git clean -Xf
.PHONY: help
help: ## Display this help.
$(S) awk 'BEGIN {FS = ":.*##"; printf "Usage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
.PHONY: docker
docker: build
$(S) docker build -t $(DOCKER_TAG) ./
.PHONY: docker-push
docker-push: docker
$(S) docker push $(DOCKER_TAG)
$(S) docker tag $(DOCKER_TAG) $(DOCKER_TAG):$(BUILD_VERSION)
$(S) docker push $(DOCKER_TAG):$(BUILD_VERSION)
define build_go_command
$(S) echo 'Building $(1)'
$(S) mkdir -p dist
$(V) $(GO) build \
$(GO_BUILD_FLAGS) \
-o '$(DISTDIR)/$(notdir $(1))' \
-ldflags '-X "$(VERSION_PKG).commit=$(BUILD_COMMIT)" -X "$(VERSION_PKG).version=$(BUILD_VERSION)" -X "$(VERSION_PKG).buildstamp=$(BUILD_STAMP)"' \
'$(1)'
endef