-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
209 lines (164 loc) · 5.86 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
203
204
205
206
207
208
209
PROJECT_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
BINARY_NAME := cli-name
# - binary dependencies
GOTESTSUM_VERSION := v1.11.0
GOFUMPT_VERSION := v0.6.0
GOLANGCI_LINT_VERSION := v1.57.2
ENTR_VERSION := 5.5
# - multi-platform build
OS_LIST := darwin linux
ARCH_LIST := arm64 amd64
BUILD_TARGETS := $(foreach os,$(OS_LIST),$(foreach arch,$(ARCH_LIST),bin/$(BINARY_NAME)-$(os)-$(arch)))
RELEASE_TARGETS := $(foreach os,$(OS_LIST),$(foreach arch,$(ARCH_LIST),bin/$(BINARY_NAME)-$(os)-$(arch).tar.gz))
# - remote testing
REMOTE_HOSTS := $(shell cat hosts.txt 2> /dev/null || echo "")
# - default target
.DEFAULT_GOAL := run
# - install binary dependencies
bin/entr:
# https://eradman.com/entrproject/entr.1.html
@curl -o bin/entr-$(ENTR_VERSION).tar.gz https://eradman.com/entrproject/code/entr-$(ENTR_VERSION).tar.gz
@tar -xf bin/entr-$(ENTR_VERSION).tar.gz -C bin
@cd bin/entr-$(ENTR_VERSION) && ./configure && make
@mv bin/entr-$(ENTR_VERSION)/entr bin/entr
@rm -rf bin/entr-$(ENTR_VERSION)*
bin/git-chglog:
@mkdir -p $(@D)
@GOBIN=$(PROJECT_DIR)/$(@D) go install github.com/git-chglog/git-chglog/cmd/git-chglog@latest
bin/golangci-lint:
@mkdir -p $(@D)
@GOBIN=$(PROJECT_DIR)/$(@D) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
bin/gofumpt:
@mkdir -p $(@D)
@GOBIN=$(PROJECT_DIR)/$(@D) go install mvdan.cc/gofumpt@$(GOFUMPT_VERSION)
bin/gotestsum:
@mkdir -p $(@D)
@GOBIN=$(PROJECT_DIR)/$(@D) go install gotest.tools/gotestsum@$(GOTESTSUM_VERSION)
# - run, test, lint, format etc.
.env:
@echo "" > .env
.PHONY: run
run: .env
@source .env && go run cmd/$(BINARY_NAME)/main.go
.PHONY: run-watch
run-watch: bin/entr
$(eval flags := rd)
@trap '_run_=false' SIGINT; \
while $$_run_; do \
find . -name "*.go" -type f | bin/entr -$(flags) $(MAKE) run; \
sleep 0.1; \
done ;\
.PHONY: lint
lint: bin/golangci-lint
@bin/golangci-lint run
.PHONY: lint-fix
lint-fix: bin/golangci-lint
@bin/golangci-lint run --fix
.PHONY: test
test: bin/gotestsum lint
@echo ""
@bin/gotestsum --format testdox -- -coverprofile=cover.out -coverpkg=./... $(shell go list ./... | grep -v /tools/ | grep -v /cmd/)
@go tool cover -func=cover.out
cover.out:
@if [ ! -f cover.out ]; then $(MAKE) test; fi
cover.txt: cover.out
@go tool cover -func=cover.out -o cover.txt
.PHONY: cover.html
cover.html: cover.out
@go tool cover -func=cover.out
@go tool cover -html=cover.out -o cover.html
@ex -sc '%s/<style>/<style>@import url("nord.css");/' -c 'x' cover.html
# - build and release
.PHONY: build
build: $(BUILD_TARGETS)
.PHONY: $(BUILD_TARGETS)
$(BUILD_TARGETS):
@$(eval parts = $(subst -, ,$(subst $(BINARY_NAME),,$@)))
@$(eval os = $(word 2, $(parts)))
@$(eval arch = $(word 3, $(parts)))
GOOS=$(os) GOARCH=$(arch) CGO_ENABLED=0 go build -ldflags "-s -w" -o $@ cmd/$(BINARY_NAME)/main.go
.PHONY: release
release: VERSION = $(shell go run cmd/$(BINARY_NAME)/main.go --responses $(RESPONSES_FILE) --version)
release: $(RELEASE_TARGETS)
.PHONY: $(RELEASE_TARGETS)
$(RELEASE_TARGETS): clean build
@cp $(shell echo $@ | sed s/.tar.gz//) bin/$(BINARY_NAME)
cd bin && tar -czf $(shell basename $@) $(BINARY_NAME)
@rm bin/$(BINARY_NAME)
.PHONY: docker-build
docker-build:
docker build $(PROJECT_DIR) -t $(BINARY_NAME):latest
.PHONY: docker-run
docker-run: docker-build
docker run --rm $(BINARY_NAME):latest --version
# - tools
.PHONY: changelog
changelog: bin/git-chglog
@echo ""
@echo "generating changelog using next tag: $(next_tag):"
@git tag -l | xargs git tag -d > /dev/null 2>&1
@git fetch --tags > /dev/null 2>&1
@bin/git-chglog --no-emoji -o CHANGELOG.md --next-tag $(next_tag)
# - ci checks
.PHONY: check
check: check-version check-cover check-commit
.PHONY: check-version
check-version:
@echo ""
@go run tools/versioncheck/main.go $(tag)
.PHONY: check-cover
check-cover: cover.txt
@echo ""
@cat cover.txt
@go run tools/covercheck/main.go
.PHONY: check-commit
check-commit:
@echo ""
@go run tools/commitcheck/main.go '$(message)'
# - remote
.PHONY: remote-run
remote-run: build
@echo ""
@echo "running on $(host)"
@ssh ubuntu@$(host) 'rm -rf /home/ubuntu/$(BINARY_NAME);'
@scp bin/$(BINARY_NAME)-linux-amd64 ubuntu@$(host):/home/ubuntu/$(BINARY_NAME)
@scp .env ubuntu@$(host):/home/ubuntu/
@ssh ubuntu@$(host) 'source /home/ubuntu/.env; /home/ubuntu/$(BINARY_NAME)'
@ssh ubuntu@$(host) 'rm /home/ubuntu/.env /home/ubuntu/$(BINARY_NAME)'
.PHONY: remote-%-all
remote-%-all:
@for host in $(REMOTE_HOSTS); do $(MAKE) remote-$* host=$$host; done
.PHONY: remote-install
remote-install: build
@echo ""
@echo "installing $(BINARY_NAME) on $(host)"
@ssh ubuntu@$(host) 'rm -rf /home/ubuntu/$(BINARY_NAME); mkdir -p /home/ubuntu/$(BINARY_NAME)'
@scp bin/$(BINARY_NAME)-linux-amd64 ubuntu@$(host):/home/ubuntu/$(BINARY_NAME)/
@scp systemd/$(BINARY_NAME).env ubuntu@$(host):/home/ubuntu/$(BINARY_NAME)/
@scp systemd/$(BINARY_NAME).timer ubuntu@$(host):/home/ubuntu/$(BINARY_NAME)/
@scp systemd/$(BINARY_NAME).service ubuntu@$(host):/home/ubuntu/$(BINARY_NAME)/
@scp systemd/install.sh ubuntu@$(host):/home/ubuntu/$(BINARY_NAME)/
@ssh ubuntu@$(host) 'chmod +x /home/ubuntu/$(BINARY_NAME)/install.sh; /home/ubuntu/$(BINARY_NAME)/install.sh'
.PHONY: remote-uninstall
remote-uninstall:
@echo ""
@echo "uninstalling $(BINARY_NAME) from $(host)"
@ssh ubuntu@$(host) 'rm -rf /home/ubuntu/$(BINARY_NAME); mkdir -p /home/ubuntu/$(BINARY_NAME)'
@scp systemd/uninstall.sh ubuntu@$(host):/home/ubuntu/$(BINARY_NAME)/
@ssh ubuntu@$(host) 'chmod +x /home/ubuntu/$(BINARY_NAME)/uninstall.sh; /home/ubuntu/$(BINARY_NAME)/uninstall.sh'
# - rename cli
rename-cli: clean
@echo ""
@echo "renaming project to $(BINARY_NAME)"
@find . \
-path ./.git -prune -o \
-name Makefile -prune -o \
-type f \
-exec ex -sc '%s/cli-name/$(BINARY_NAME)/g' -c 'x' {} \;
@mv cmd/cli-name cmd/$(BINARY_NAME)
@git status
# - clean
.PHONY: clean
clean:
rm -rf bin/*
rm -rf cover.*