Skip to content

Commit bc8b662

Browse files
committed
chore: update makefile
1 parent f4ccdd4 commit bc8b662

File tree

2 files changed

+267
-47
lines changed

2 files changed

+267
-47
lines changed

Makefile

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,8 @@
1-
GO ?= go
2-
DOCKER_IMAGE ?= ultreme/golang-repo-template
1+
GOPKG ?= ultre.me/golang-repo-template
2+
DOCKER_IMAGE ?= ultreme/golang-repo-template
3+
GOBINS ?= .
4+
NPM_PACKAGES ?= .
35

4-
.PHONY: install
5-
install:
6-
$(GO) install .
6+
all: test install
77

8-
.PHONY: test
9-
test: unittest lint tidy
10-
11-
.PHONY: unittest
12-
unittest:
13-
echo "" > /tmp/coverage.txt
14-
set -e; for dir in `find . -type f -name "go.mod" | sed 's@/[^/]*$$@@' | sort | uniq`; do ( set -xe; \
15-
cd $$dir; \
16-
$(GO) test -mod=readonly -v -cover -coverprofile=/tmp/profile.out -covermode=atomic -race ./...; \
17-
if [ -f /tmp/profile.out ]; then \
18-
cat /tmp/profile.out >> /tmp/coverage.txt; \
19-
rm -f /tmp/profile.out; \
20-
fi); done
21-
mv /tmp/coverage.txt .
22-
23-
.PHONY: lint
24-
lint:
25-
set -e; for dir in `find . -type f -name "go.mod" | sed 's@/[^/]*$$@@' | sort | uniq`; do ( set -xe; \
26-
cd $$dir; \
27-
golangci-lint run --verbose ./...; \
28-
); done
29-
30-
.PHONY: tidy
31-
tidy:
32-
set -e; for dir in `find . -type f -name "go.mod" | sed 's@/[^/]*$$@@' | sort | uniq`; do ( set -xe; \
33-
cd $$dir; \
34-
$(GO) mod tidy; \
35-
); done
36-
37-
.PHONY: release
38-
release:
39-
goreleaser --snapshot --skip-publish --rm-dist
40-
@echo -n "Do you want to release? [y/N] " && read ans && [ $${ans:-N} = y ]
41-
goreleaser --rm-dist
42-
43-
.PHONY: docker
44-
docker:
45-
docker build \
46-
--build-arg VCS_REF=`git rev-parse --short HEAD` \
47-
--build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
48-
--build-arg VERSION=`git describe --tags --always` \
49-
-t $(DOCKER_IMAGE) .
8+
-include rules.mk

rules.mk

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
# +--------------------------------------------------------------+
2+
# | * * * moul.io/rules.mk |
3+
# +--------------------------------------------------------------+
4+
# | |
5+
# | ++ ______________________________________ |
6+
# | ++++ / \ |
7+
# | ++++ | | |
8+
# | ++++++++++ | https://moul.io/rules.mk is a set | |
9+
# | +++ | | of common Makefile rules that can | |
10+
# | ++ | | be configured from the Makefile | |
11+
# | + -== ==| | or with environment variables. | |
12+
# | ( <*> <*> | | |
13+
# | | | /| Manfred Touron | |
14+
# | | _) / | manfred.life | |
15+
# | | +++ / \______________________________________/ |
16+
# | \ =+ / |
17+
# | \ + |
18+
# | |\++++++ |
19+
# | | ++++ ||// |
20+
# | ___| |___ _||/__ __|
21+
# | / --- \ \| ||| __ _ ___ __ __/ /|
22+
# |/ | | \ \ / / ' \/ _ \/ // / / |
23+
# || | | | | | /_/_/_/\___/\_,_/_/ |
24+
# +--------------------------------------------------------------+
25+
26+
all: help
27+
28+
##
29+
## Common helpers
30+
##
31+
32+
rwildcard = $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
33+
34+
##
35+
## rules.mk
36+
##
37+
ifneq ($(wildcard rules.mk),)
38+
.PHONY: rulesmk.bumpdeps
39+
rulesmk.bumpdeps:
40+
wget -O rules.mk https://raw.githubusercontent.com/moul/rules.mk/master/rules.mk
41+
BUMPDEPS_STEPS += rulesmk.bumpdeps
42+
endif
43+
44+
##
45+
## Maintainer
46+
##
47+
48+
ifneq ($(wildcard .git/HEAD),)
49+
.PHONY: generate.authors
50+
generate.authors:
51+
echo "# This file lists all individuals having contributed content to the repository." > AUTHORS
52+
echo "# For how it is generated, see 'https://github.com/moul/rules.mk'" >> AUTHORS
53+
echo >> AUTHORS
54+
git log --format='%aN <%aE>' | LC_ALL=C.UTF-8 sort -uf >> AUTHORS
55+
GENERATE_STEPS += generate.authors
56+
endif
57+
58+
##
59+
## Golang
60+
##
61+
62+
ifndef GOPKG
63+
ifneq ($(wildcard go.mod),)
64+
GOPKG = $(shell sed '/module/!d;s/^omdule\ //' go.mod)
65+
endif
66+
endif
67+
ifdef GOPKG
68+
GO ?= go
69+
GOPATH ?= $(HOME)/go
70+
GO_INSTALL_OPTS ?=
71+
72+
ifdef GOBINS
73+
.PHONY: go.install
74+
go.install:
75+
@set -e; for dir in $(GOBINS); do ( set -xe; \
76+
cd $$dir; \
77+
$(GO) install $(GO_INSTALL_OPTS) .; \
78+
); done
79+
INSTALL_STEPS += go.install
80+
81+
.PHONY: go.release
82+
go.release:
83+
goreleaser --snapshot --skip-publish --rm-dist
84+
@echo -n "Do you want to release? [y/N] " && read ans && \
85+
if [ $${ans:-N} = y ]; then set -xe; goreleaser --rm-dist; fi
86+
RELEASE_STEPS += go.release
87+
endif
88+
89+
.PHONY: go.unittest
90+
go.unittest:
91+
echo "" > /tmp/coverage.txt
92+
@set -e; for dir in `find . -type f -name "go.mod" | grep -v /vendor/ | sed 's@/[^/]*$$@@' | sort | uniq`; do ( set -xe; \
93+
cd $$dir; \
94+
$(GO) test -v -cover -coverprofile=/tmp/profile.out -covermode=atomic -race ./...; \
95+
if [ -f /tmp/profile.out ]; then \
96+
cat /tmp/profile.out >> /tmp/coverage.txt; \
97+
rm -f /tmp/profile.out; \
98+
fi); done
99+
mv /tmp/coverage.txt .
100+
101+
.PHONY: go.lint
102+
go.lint:
103+
@set -e; for dir in `find . -type f -name "go.mod" | grep -v /vendor/ | sed 's@/[^/]*$$@@' | sort | uniq`; do ( set -xe; \
104+
cd $$dir; \
105+
golangci-lint run --verbose ./...; \
106+
); done
107+
108+
.PHONY: go.tidy
109+
go.tidy:
110+
@set -e; for dir in `find . -type f -name "go.mod" | grep -v /vendor/ | sed 's@/[^/]*$$@@' | sort | uniq`; do ( set -xe; \
111+
cd $$dir; \
112+
$(GO) mod tidy; \
113+
); done
114+
115+
.PHONY: go.build
116+
go.build:
117+
@set -e; for dir in `find . -type f -name "go.mod" | grep -v /vendor/ | sed 's@/[^/]*$$@@' | sort | uniq`; do ( set -xe; \
118+
cd $$dir; \
119+
$(GO) build ./...; \
120+
); done
121+
122+
.PHONY: go.bump-deps
123+
go.bumpdeps:
124+
@set -e; for dir in `find . -type f -name "go.mod" | grep -v /vendor/ | sed 's@/[^/]*$$@@' | sort | uniq`; do ( set -xe; \
125+
cd $$dir; \
126+
$(GO) get -u ./...; \
127+
); done
128+
129+
.PHONY: go.bump-deps
130+
go.fmt:
131+
if ! command -v goimports &>/dev/null; then GO111MODULE=off go get golang.org/x/tools/cmd/goimports; fi
132+
@set -e; for dir in `find . -type f -name "go.mod" | grep -v /vendor/ | sed 's@/[^/]*$$@@' | sort | uniq`; do ( set -xe; \
133+
cd $$dir; \
134+
goimports -w . \
135+
); done
136+
137+
BUILD_STEPS += go.build
138+
BUMPDEPS_STEPS += go.bumpdeps
139+
TIDY_STEPS += go.tidy
140+
LINT_STEPS += go.lint
141+
UNITTEST_STEPS += go.unittest
142+
FMT_STEPS += go.fmt
143+
endif
144+
145+
##
146+
## Node
147+
##
148+
149+
ifndef NPM_PACKAGES
150+
ifneq ($(wildcard package.json),)
151+
NPM_PACKAGES = .
152+
endif
153+
endif
154+
ifdef NPM_PACKAGES
155+
.PHONY: npm.publish
156+
npm.publish:
157+
@echo -n "Do you want to npm publish? [y/N] " && read ans && \
158+
@if [ $${ans:-N} = y ]; then \
159+
set -e; for dir in $(NPM_PACKAGES); do ( set -xe; \
160+
cd $$dir; \
161+
npm publish --access=public; \
162+
); done; \
163+
fi
164+
RELEASE_STEPS += npm.publish
165+
endif
166+
167+
##
168+
## Docker
169+
##
170+
171+
ifndef DOCKER_IMAGE
172+
ifneq ($(wildcard Dockerfile),)
173+
DOCKER_IMAGE = $(notdir $(PWD))
174+
endif
175+
endif
176+
ifdef DOCKER_IMAGE
177+
ifneq ($(DOCKER_IMAGE),none)
178+
.PHONY: docker.build
179+
docker.build:
180+
docker build \
181+
--build-arg VCS_REF=`git rev-parse --short HEAD` \
182+
--build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
183+
--build-arg VERSION=`git describe --tags --always` \
184+
-t $(DOCKER_IMAGE) .
185+
186+
BUILD_STEPS += docker.build
187+
endif
188+
endif
189+
190+
##
191+
## Common
192+
##
193+
194+
TEST_STEPS += $(UNITTEST_STEPS)
195+
TEST_STEPS += $(LINT_STEPS)
196+
TEST_STEPS += $(TIDY_STEPS)
197+
198+
ifneq ($(strip $(TEST_STEPS)),)
199+
.PHONY: test
200+
test: $(PRE_TEST_STEPS) $(TEST_STEPS)
201+
endif
202+
203+
ifdef INSTALL_STEPS
204+
.PHONY: install
205+
install: $(PRE_INSTALL_STEPS) $(INSTALL_STEPS)
206+
endif
207+
208+
ifdef UNITTEST_STEPS
209+
.PHONY: unittest
210+
unittest: $(PRE_UNITTEST_STEPS) $(UNITTEST_STEPS)
211+
endif
212+
213+
ifdef LINT_STEPS
214+
.PHONY: lint
215+
lint: $(PRE_LINT_STEPS) $(FMT_STEPS) $(LINT_STEPS)
216+
endif
217+
218+
ifdef TIDY_STEPS
219+
.PHONY: tidy
220+
tidy: $(PRE_TIDY_STEPS) $(TIDY_STEPS)
221+
endif
222+
223+
ifdef BUILD_STEPS
224+
.PHONY: build
225+
build: $(PRE_BUILD_STEPS) $(BUILD_STEPS)
226+
endif
227+
228+
ifdef RELEASE_STEPS
229+
.PHONY: release
230+
release: $(PRE_RELEASE_STEPS) $(RELEASE_STEPS)
231+
endif
232+
233+
ifdef BUMPDEPS_STEPS
234+
.PHONY: bumpdeps
235+
bumpdeps: $(PRE_BUMDEPS_STEPS) $(BUMPDEPS_STEPS)
236+
endif
237+
238+
ifdef FMT_STEPS
239+
.PHONY: fmt
240+
fmt: $(PRE_FMT_STEPS) $(FMT_STEPS)
241+
endif
242+
243+
ifdef GENERATE_STEPS
244+
.PHONY: generate
245+
generate: $(PRE_GENERATE_STEPS) $(GENERATE_STEPS)
246+
endif
247+
248+
.PHONY: help
249+
help:
250+
@echo "General commands:"
251+
@[ "$(BUILD_STEPS)" != "" ] && echo " build" || true
252+
@[ "$(BUMPDEPS_STEPS)" != "" ] && echo " bumpdeps" || true
253+
@[ "$(FMT_STEPS)" != "" ] && echo " fmt" || true
254+
@[ "$(GENERATE_STEPS)" != "" ] && echo " generate" || true
255+
@[ "$(INSTALL_STEPS)" != "" ] && echo " install" || true
256+
@[ "$(LINT_STEPS)" != "" ] && echo " lint" || true
257+
@[ "$(RELEASE_STEPS)" != "" ] && echo " release" || true
258+
@[ "$(TEST_STEPS)" != "" ] && echo " test" || true
259+
@[ "$(TIDY_STEPS)" != "" ] && echo " tidy" || true
260+
@[ "$(UNITTEST_STEPS)" != "" ] && echo " unittest" || true
261+
@# FIXME: list other commands

0 commit comments

Comments
 (0)