-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
128 lines (101 loc) · 3.64 KB
/
Makefile
File metadata and controls
128 lines (101 loc) · 3.64 KB
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
.PHONY: go-module-version
go-module-version: go-check git-check
@echo "go get $(shell $(GO) list ./pkg/app)@$(shell $(GIT) rev-parse HEAD)"
.PHONY: examples
examples: go-check examples-mod examples-test examples-lint
@echo "Running examples tests and linting"
@$(GOLANGCI_LINT) run ./... --fix
.PHONY: examples-mod
examples-mod: go-check
@for dir in $$(find . -mindepth 2 -name go.mod | sed -r 's/(.*)(go.mod)/\1/g'); do \
echo "Running go mod tidy in $${dir}"; \
cd $(CURDIR)/$${dir} && go mod tidy && cd $(CURDIR); \
done
.PHONY: examples-test
examples-test: go-check
@for dir in $$(find . -mindepth 2 -name go.mod | sed -r 's/(.*)(go.mod)/\1/g'); do \
echo "Running tests in $${dir}"; \
cd $(CURDIR)/$${dir} && $(GO) test --race --cover ./... && cd $(CURDIR); \
done
.PHONY: examples-lint
examples-lint: golangci-lint
@for dir in $$(find . -mindepth 2 -name go.mod | sed -r 's/(.*)(go.mod)/\1/g'); do \
echo "Running linter in $${dir}"; \
cd $(CURDIR)/$${dir} && $(GOLANGCI_LINT) run ./... --fix && cd $(CURDIR); \
done
.PHONY: lint
lint: golangci-lint ## Run linter.
@$(GOLANGCI_LINT) run --fix
.PHONY: test
test: go-check
@$(GO) test --race --cover ./...
## Run all generate-* jobs in bulk.
.PHONY: generate
generate: update-workflows-go-version update-workflows-golangci-lint-version
##@ Dependencies
WHOAMI ?= $(shell whoami)
## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)
## Tool Binaries
GO=$(shell which go)
GIT=$(shell which git)
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
YQ = $(LOCALBIN)/yq
## TODO: remap in yaml file (version.yaml or smthng)
## Tool Versions
# GO_BUILDER_VERSION must be without 'v' prefix
GO_BUILDER_VERSION = 1.25.5
GOLANGCI_LINT_VERSION = v2.10.1
YQ_VERSION ?= v4.50.1
.PHONY: update-workflows-go-version
update-workflows-go-version: yq
for file in $$(find .github/workflows -name "*.yaml" -o -name "*.yml"); do \
if grep -q "actions/setup-go" $$file; then \
$(YQ) -i '(.env.GO_VERSION) = "$(GO_BUILDER_VERSION)"' $$file; \
fi; \
done
echo "Updated go-version in workflow files to $(GO_BUILDER_VERSION)"
.PHONY: update-workflows-golangci-lint-version
update-workflows-golangci-lint-version: yq
for file in $$(find .github/workflows -name "*.yaml" -o -name "*.yml"); do \
if grep -q "golangci/golangci-lint-action" $$file; then \
$(YQ) -i '(.env.GOLANGCI_LINT_VERSION) = "$(GOLANGCI_LINT_VERSION)"' $$file; \
fi; \
done
echo "Updated golangci-lint version in workflow files to $(GOLANGCI_LINT_VERSION)"
## Installed tools check
.PHONY: go-check
go-check:
$(call error-if-empty,$(GO),go)
.PHONY: git-check
git-check:
$(call error-if-empty,$(GIT),git)
## Tool installations
.PHONY: golangci-lint
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
$(GOLANGCI_LINT): $(LOCALBIN)
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
.PHONY: yq
yq: $(YQ) ## Download yq locally if necessary.
$(YQ): $(LOCALBIN)
$(call go-install-tool,$(YQ),github.com/mikefarah/yq/v4,$(YQ_VERSION))
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
# $1 - target path with name of binary
# $2 - package url which can be installed
# $3 - specific version of package
define go-install-tool
@[ -f "$(1)-$(3)" ] || { \
set -e; \
package=$(2)@$(3) ;\
echo "Downloading $${package}" ;\
rm -f $(1) || true ;\
GOBIN=$(LOCALBIN) GOTOOLCHAIN=$(GO_TOOLCHAIN_AUTOINSTALL_VERSION) go install $${package} ;\
mv $(1) $(1)-$(3) ;\
} ;\
ln -sf $(1)-$(3) $(1)
endef
define error-if-empty
@if [[ -z $(1) ]]; then echo "$(2) not installed"; false; fi
endef