11.DEFAULT_GOAL := build
22
33PKG := github.com/lightninglabs/loop
4+ TOOLS_DIR := tools
45
56GOTEST := GO111MODULE=on go test -v
67
8+
9+ GOIMPORTS_PKG := github.com/rinchsan/gosimports/cmd/gosimports
10+
711GO_BIN := ${GOPATH}/bin
12+ GOIMPORTS_BIN := $(GO_BIN ) /gosimports
13+
814GOBUILD := GO111MODULE=on go build -v
915GOINSTALL := GO111MODULE=on go install -v
1016GOMOD := GO111MODULE=on go mod
@@ -13,51 +19,37 @@ COMMIT := $(shell git describe --abbrev=40 --dirty)
1319LDFLAGS := -ldflags "-X $(PKG ) .Commit=$(COMMIT ) "
1420DEV_TAGS = dev
1521
16- GOFILES_NOVENDOR = $(shell find . -type f -name '* .go' -not -path "./vendor/* ")
22+ GOFILES_NOVENDOR = $(shell find . -type f -name '* .go' -not -path "./vendor/* " -not -name " * pb.go" -not -name " * pb.gw.go" -not -name " * .pb.json.go" )
1723GOLIST := go list $(PKG ) /... | grep -v '/vendor/'
1824
19- LINT_BIN := $(GO_BIN ) /golangci-lint
20- LINT_PKG := github.com/golangci/golangci-lint/cmd/golangci-lint
21- LINT_COMMIT := v1.18.0
22- LINT = $(LINT_BIN ) run -v
23-
24- DEPGET := cd /tmp && GO111MODULE=on go get -v
2525XARGS := xargs -L 1
2626
2727TEST_FLAGS = -test.timeout=20m
2828
2929UNIT := $(GOLIST ) | $(XARGS ) env $(GOTEST ) $(TEST_FLAGS )
3030
31+ # Linting uses a lot of memory, so keep it under control by limiting the number
32+ # of workers if requested.
33+ ifneq ($(workers ) ,)
34+ LINT_WORKERS = --concurrency=$(workers )
35+ endif
36+
37+ DOCKER_TOOLS = docker run -v $$(pwd ) :/build loop-tools
38+
3139GREEN := "\\033[0;32m"
3240NC := "\\033[0m"
3341define print
3442 echo $(GREEN ) $1$(NC )
3543endef
3644
37- $(LINT_BIN ) :
38- @$(call print, "Fetching linter")
39- $(DEPGET ) $(LINT_PKG ) @$(LINT_COMMIT )
40-
41- unit :
42- @$(call print, "Running unit tests.")
43- $(UNIT )
44-
45- fmt :
46- @$(call print, "Formatting source.")
47- gofmt -l -w -s $(GOFILES_NOVENDOR )
48-
49- lint : $(LINT_BIN )
50- @$(call print, "Linting source.")
51- $(LINT )
45+ # ============
46+ # DEPENDENCIES
47+ # ============
5248
53- mod-tidy :
54- @$(call print, "Tidying modules .")
55- $( GOMOD ) tidy
49+ $( GOIMPORTS_BIN ) :
50+ @$(call print, "Installing goimports .")
51+ cd $( TOOLS_DIR ) ; go install -trimpath $( GOIMPORTS_PKG )
5652
57- mod-check :
58- @$(call print, "Checking modules.")
59- $(GOMOD ) tidy
60- if test -n " $$ (git status | grep -e " go.mod\| go.sum" )" ; then echo " Running go mod tidy changes go.mod/go.sum" ; git status; git diff; exit 1; fi
6153
6254# ============
6355# INSTALLATION
@@ -94,3 +86,40 @@ clean:
9486 @$(call print, "Cleaning up.")
9587 rm -f ./loop-debug ./loopd-debug
9688 rm -rf ./vendor
89+
90+ # =======
91+ # TESTING
92+ # =======
93+
94+ unit :
95+ @$(call print, "Running unit tests.")
96+ $(UNIT )
97+
98+ # =========
99+ # UTILITIES
100+ # =========
101+
102+ fmt : $(GOIMPORTS_BIN )
103+ @$(call print, "Fixing imports.")
104+ gosimports -w $(GOFILES_NOVENDOR )
105+ @$(call print, "Formatting source.")
106+ gofmt -l -w -s $(GOFILES_NOVENDOR )
107+
108+ lint : docker-tools
109+ @$(call print, "Linting source.")
110+ $(DOCKER_TOOLS ) golangci-lint run -v $(LINT_WORKERS )
111+
112+ docker-tools :
113+ @$(call print, "Building tools docker image.")
114+ docker build -q -t loop-tools $(TOOLS_DIR )
115+
116+ mod-tidy :
117+ @$(call print, "Tidying modules.")
118+ $(GOMOD ) tidy
119+
120+ mod-check :
121+ @$(call print, "Checking modules.")
122+ $(GOMOD ) tidy
123+ if test -n " $$ (git status | grep -e " go.mod\| go.sum" )" ; then echo " Running go mod tidy changes go.mod/go.sum" ; git status; git diff; exit 1; fi
124+
125+
0 commit comments