-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
145 lines (115 loc) · 4.37 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
PACKAGE = stash.kopano.io/km/mattermost-plugin-notifymatters
PACKAGE_NAME = kopano-$(shell basename $(PACKAGE))
# Tools
GO ?= go
GOFMT ?= gofmt
GLIDE ?= glide
GOLINT ?= golint
GO2XUNIT ?= go2xunit
# Cgo
CGO_ENABLED ?= 0
# Variables
PWD := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
DATE ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
VERSION ?= $(shell git describe --tags --always --dirty --match=v* 2>/dev/null | sed 's/^v//' || \
cat $(CURDIR)/.version 2> /dev/null || echo 0.0.0-unreleased)
GOPATH = $(CURDIR)/.gopath
BASE = $(GOPATH)/src/$(PACKAGE)
PKGS = $(or $(PKG),$(shell cd $(BASE) && env GOPATH=$(GOPATH) $(GO) list ./... | grep -v "^$(PACKAGE)/vendor/"))
TESTPKGS = $(shell env GOPATH=$(GOPATH) $(GO) list -f '{{ if or .TestGoFiles .XTestGoFiles }}{{ .ImportPath }}{{ end }}' $(PKGS) 2>/dev/null)
CMDS = $(or $(CMD),$(addprefix cmd/,$(notdir $(shell find "$(PWD)/cmd/" -type d))))
TIMEOUT = 30
export GOPATH CGO_ENABLED
# Build
.PHONY: all
all: fmt vendor | $(CMDS) webapp
$(BASE): ; $(info creating local GOPATH ...)
@mkdir -p $(dir $@)
@ln -sf $(CURDIR) $@
.PHONY: $(CMDS)
$(CMDS): vendor | $(BASE) ; $(info building $@ ...) @
cd $(BASE) && $(GO) build \
-tags release \
-asmflags '-trimpath=$(GOPATH)' \
-gcflags '-trimpath=$(GOPATH)' \
-ldflags '-s -w -X $(PACKAGE)/version.Version=$(VERSION) -X $(PACKAGE)/version.BuildDate=$(DATE) -extldflags -static' \
-o bin/$(notdir $@) $(PACKAGE)/$@
.PHONY: webapp
webapp:
$(MAKE) -C webapp js
# Helpers
.PHONY: lint
lint: go-lint webapp-lint
.PHONY: go-lint
go-lint: vendor | $(BASE) ; $(info running golint ...) @
@cd $(BASE) && ret=0 && for pkg in $(PKGS); do \
test -z "$$($(GOLINT) $$pkg | tee /dev/stderr)" || ret=1 ; \
done ; exit $$ret
.PHONY: webapp-lint
webapp-lint: ; $(info running webapp lint ...)
$(MAKE) -C webapp lint
.PHONY: webapp-lint-checkstyle
webapp-lint-checkstyle: ; $(info running webapp lint checkstyle ...)
$(MAKE) -C webapp lint-checkstyle || true
.PHONY: fmt
fmt: ; $(info running gofmt ...) @
@ret=0 && for d in $$($(GO) list -f '{{.Dir}}' ./... | grep -v /vendor/); do \
$(GOFMT) -l -w $$d/*.go || ret=$$? ; \
done ; exit $$ret
# Tests
TEST_TARGETS := test-default test-bench test-short test-race test-verbose
.PHONY: $(TEST_TARGETS)
test-bench: ARGS=-run=_Bench* -test.benchmem -bench=.
test-short: ARGS=-short
test-race: ARGS=-race
test-race: CGO_ENABLED=1
test-verbose: ARGS=-v
$(TEST_TARGETS): NAME=$(MAKECMDGOALS:test-%=%)
$(TEST_TARGETS): test
.PHONY: test
test: vendor | $(BASE) ; $(info running $(NAME:%=% )tests ...) @
@cd $(BASE) && CGO_ENABLED=$(CGO_ENABLED) $(GO) test -timeout $(TIMEOUT)s $(ARGS) $(TESTPKGS)
TEST_XML_TARGETS := test-xml-default test-xml-short test-xml-race
.PHONY: $(TEST_XML_TARGETS)
test-xml-short: ARGS=-short
test-xml-race: ARGS=-race
test-xml-race: CGO_ENABLED=1
$(TEST_XML_TARGETS): NAME=$(MAKECMDGOALS:test-%=%)
$(TEST_XML_TARGETS): test-xml
.PHONY: test-xml
test-xml: vendor | $(BASE) ; $(info running $(NAME:%=% )tests ...) @
@mkdir -p test
cd $(BASE) && 2>&1 CGO_ENABLED=$(CGO_ENABLED) $(GO) test -timeout $(TIMEOUT)s $(ARGS) -v $(TESTPKGS) | tee test/tests.output
$(shell test -s test/tests.output && $(GO2XUNIT) -fail -input test/tests.output -output test/tests.xml)
# Glide
glide.lock: glide.yaml | $(BASE) ; $(info updating dependencies ...)
@cd $(BASE) && $(GLIDE) update
@touch $@
vendor: glide.lock | $(BASE) ; $(info retrieving dependencies ...)
@cd $(BASE) && $(GLIDE) --quiet install
@ln -nsf . vendor/src
@touch $@
# Dist
.PHONY: dist
dist: webapp/build/notifymatters_bundle.js bin/notifymattersd plugin.json ; $(info building dist tarball ...)
@rm -rf "dist/${PACKAGE_NAME}-${VERSION}"
@mkdir -p "dist/${PACKAGE_NAME}-${VERSION}"
@cd dist && \
cp -avf ../LICENSE.txt "${PACKAGE_NAME}-${VERSION}" && \
cp -avf ../README.md "${PACKAGE_NAME}-${VERSION}" && \
cp -avf ../bin/* "${PACKAGE_NAME}-${VERSION}" && \
cp -avr ../webapp/build "${PACKAGE_NAME}-${VERSION}/webapp" && \
cp -avr ../plugin.json "${PACKAGE_NAME}-${VERSION}" && \
sed -i s/0.0.0-no-proper-build/${VERSION}/g "${PACKAGE_NAME}-${VERSION}/plugin.json" && \
tar --owner=0 --group=0 -czvf ${PACKAGE_NAME}-${VERSION}.tar.gz "${PACKAGE_NAME}-${VERSION}" && \
cd ..
# Rest
.PHONY: clean
clean: ; $(info cleaning ...) @
@rm -rf $(GOPATH)
@rm -rf bin
@rm -rf test/test.*
@$(MAKE) -C webapp clean
.PHONY: version
version:
@echo $(VERSION)