-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathMakefile
143 lines (111 loc) · 3.7 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
.DEFAULT_GOAL := all
VERSION_MAJOR ?= 0
VERSION_MINOR ?= 2
VERSION_BUILD ?= 0
VERSION ?= v$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_BUILD)
REVISION ?= $(shell git describe --always)
BUILD_DATE ?= $(shell date +'%Y-%m-%dT%H:%M:%SZ')
RELEASE_TYPE ?= $(if $(shell git tag --contains $(REVISION) | grep $(VERSION)),stable,canary)
ORG := github.com/izumin5210
PROJECT := grapi
ROOT_PKG ?= $(ORG)/$(PROJECT)
TEMPLATE_PKG := pkg/grapicmd/internal/module/generator/template
MOCK_PKG := pkg/grapicmd/internal/module/testing
GENERATED_PKGS := $(TEMPLATE_PKG)
GENERATED_PKGS += $(MOCK_PKG)
SRC_FILES := $(shell git ls-files --cached --others --exclude-standard | grep -E "\.go$$" | grep -v ".snapshot")
GOFMT_TARGET := $(SRC_FILES)
$(foreach pkg,$(GENERATED_PKGS),$(eval GOFMT_TARGET := $(filter-out $(pkg)/%,$(GOFMT_TARGET))))
GOLINT_TARGET := $(shell go list ./... | grep -v "$(pkg)/testing")
$(foreach pkg,$(GENERATED_PKGS),$(eval GOLINT_TARGET := $(filter-out $(ROOT_PKG)/$(pkg),$(GOLINT_TARGET))))
GO_BUILD_FLAGS := -v
GO_TEST_FLAGS := -v
GO_COVER_FLAGS := -coverpkg $(shell echo $(GOLINT_TARGET) | tr ' ' ',') -coverprofile coverage.txt -covermode atomic
XC_ARCH := 386 amd64
XC_OS := darwin linux windows
# Utils
#----------------------------------------------------------------
define section
@printf "\e[34m--> $1\e[0m\n"
endef
# dep
#----------------------------------------------------------------
DEP_BIN_DIR := ./vendor/.bin/
DEP_SRCS := \
github.com/golang/mock/mockgen \
github.com/jessevdk/go-assets-builder \
github.com/mitchellh/gox
DEP_BINS := $(addprefix $(DEP_BIN_DIR),$(notdir $(DEP_SRCS)))
define dep-bin-tmpl
$(eval OUT := $(addprefix $(DEP_BIN_DIR),$(notdir $(1))))
$(OUT): dep
$(call section,Installing $(OUT))
@cd vendor/$(1) && GOBIN="$(shell pwd)/$(DEP_BIN_DIR)" go install .
endef
$(foreach src,$(DEP_SRCS),$(eval $(call dep-bin-tmpl,$(src))))
# App
#----------------------------------------------------------------
BIN_DIR := ./bin/
OUT_DIR := ./dist
GENERATED_BINS :=
PACKAGES :=
CMDS := $(wildcard ./cmd/*)
define cmd-tmpl
$(eval NAME := $(notdir $(1)))
$(eval OUT := $(addprefix $(BIN_DIR),$(NAME)))
$(eval LDFLAGS := -ldflags "-X main.name=$(NAME) -X main.version=$(VERSION) -X main.revision=$(REVISION) -X main.buildDate=$(BUILD_DATE) -X main.releaseType=$(RELEASE_TYPE)")
$(eval GENERATED_BINS += $(OUT))
$(OUT): $(SRC_FILES)
$(call section,Building $(OUT))
@go build $(GO_BUILD_FLAGS) $(LDFLAGS) -o $(OUT) $(1)
.PHONY: $(NAME)
$(NAME): $(OUT)
$(eval PACKAGES += $(NAME)-package)
.PHONY: $(NAME)-package
$(NAME)-package: $(NAME)
@PATH=$(shell pwd)/$(DEP_BIN_DIR):$$$$PATH gox \
$(LDFLAGS) \
-os="$(XC_OS)" \
-arch="$(XC_ARCH)" \
-output="$(OUT_DIR)/$(NAME)_{{.OS}}_{{.Arch}}" \
$(1)
endef
$(foreach src,$(CMDS),$(eval $(call cmd-tmpl,$(src))))
.PHONY: all
all: $(GENERATED_BINS)
# Commands
#----------------------------------------------------------------
.PHONY: setup
setup: dep $(DEP_BINS)
.PHONY: clean
clean:
rm -rf $(BIN_DIR)/*
.PHONY: clobber
clobber: clean
rm -rf vendor
.PHONY: dep
dep: Gopkg.toml Gopkg.lock
$(call section,Installing dependencies)
@dep ensure -v -vendor-only
.PHONY: gen
gen:
@PATH=$(shell pwd)/$(DEP_BIN_DIR):$$PATH go generate ./...
.PHONY: lint
lint:
$(call section,Linting)
@gofmt -e -d -s $(GOFMT_TARGET) | awk '{ e = 1; print $0 } END { if (e) exit(1) }'
@echo $(GOLINT_TARGET) | xargs -n1 golint -set_exit_status
.PHONY: test
test:
$(call section,Testing)
@go test $(GO_TEST_FLAGS) ./...
.PHONY: cover
cover:
$(call section,Testing with coverage)
@go test $(GO_TEST_FLAGS) $(GO_COVER_FLAGS) ./...
.PHONY: test-integration
test-integration:
$(call section,Integration Testing)
cd _tests && go test $(GO_TEST_FLAGS) ./...
.PHONY: packages
packages: $(PACKAGES)