summaryrefslogtreecommitdiff
path: root/Makefile
blob: 193fd547298e000efa2abdf8c0794cb5e46affbb (plain)
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
BIN_DIR = bin

DEV_TOOLS_DIR = $(BIN_DIR)/dev-tools
DEV_TOOLS = ./vendor/github.com/golang/mock/mockgen ./vendor/github.com/go-swagger/go-swagger/cmd/swagger
MOCKGEN_BIN = $(DEV_TOOLS_DIR)/mockgen
SWAGGER_BIN = $(DEV_TOOLS_DIR)/swagger

DEV_TOOLS_BIN = $(MOCKGEN_BIN) $(SWAGGER_BIN)

rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))

WELES_FILES = $(filter-out *_test.go, $(call rwildcard, , *.go))

SERVER_MAIN = cmd/weles-server/main.go
SERVER_BIN = $(BIN_DIR)/weles

BUILD_DOCKER_IMAGE = weles-build-img
BUILD_DOCKER_CONTAINER = weles-build

DOC_DIR = doc
SPHINX_DOCKER_IMAGE = weles-doc-image
SPHINX_DOCKER_CONTAINER = weles-doc-container
SPHINX_DOCKER_CONTAINER_WORKDIR = /doc
SWAGGER_DOCKER_IMAGE = swaggerapi/swagger-codegen-cli
SWAGGER_DOCKER_CONTAINER = weles-swagger-container
SWAGGER_DOCKER_CONTAINER_WORKDIR = /local

.PHONY: all
all: docker-build

.PHONY: clean
clean: clean-docker-build

.PHONY: docker-build
docker-build: $(SERVER_BIN)
	docker rm $(BUILD_DOCKER_CONTAINER)

$(SERVER_BIN): docker-container | $(BIN_DIR)
	docker cp "$(BUILD_DOCKER_CONTAINER):/$(@F)" $(@)

.PHONY: docker-container
docker-container: docker-build-img
	docker create --name "$(BUILD_DOCKER_CONTAINER)" "$(BUILD_DOCKER_IMAGE)"

.PHONY: docker-build-img
docker-build-img:
	docker build --tag "$(BUILD_DOCKER_IMAGE)" .

$(BIN_DIR):
	mkdir -p "$(BIN_DIR)"

.PHONY: clean-docker-build
clean-docker-build:
	-docker rm $(BUILD_DOCKER_CONTAINER)
	-docker rmi $(BUILD_DOCKER_IMAGE)
	-rm -f "$(SERVER_BIN)"
	-rmdir "$(BIN_DIR)"

.PHONY: server
server: vendor $(WELES_FILES)
	go build -o $(SERVER_BIN) $(SERVER_MAIN)

# dep ensure is run after swagger generation to update Gopkg.lock with packages needed to build server
.PHONY: swagger-server-generate
swagger-server-generate:  swagger.yml COPYING
	./$(DEV_TOOLS_DIR)/swagger generate server \
		-A weles \
		-f ./swagger.yml \
		-m ../weles \
		-s ./server \
		-r ./COPYING \
		--flag-strategy pflag \
		--exclude-main \
		--skip-models \
		--compatibility-mode=modern
	dep ensure

.PHONY: swagger-models-generate
swagger-models-generate:  swagger.yml COPYING
	./$(DEV_TOOLS_DIR)/swagger generate model \
		-f ./swagger.yml \
		-m ../weles \
		-s ./server \
		-r ./COPYING \

.PHONY: docs
docs: docs-swagger docs-sphinx

.PHONY: clean-docs
clean-docs: clean-docs-swagger clean-docker-swagger clean-docs-sphinx clean-docker-sphinx

.PHONY: docs-sphinx
docs-sphinx: docker-image-sphinx
	docker run \
		--rm \
		--volume "`pwd`:${SPHINX_DOCKER_CONTAINER_WORKDIR}" \
		--user "`id -u $$USER`:`id -g $$USER`" \
		--name "${SPHINX_DOCKER_CONTAINER}" "${SPHINX_DOCKER_IMAGE}" \
		make -C "${SPHINX_DOCKER_CONTAINER_WORKDIR}/${DOC_DIR}" html

.PHONY: docker-image-sphinx
docker-image-sphinx:
	docker build -t "${SPHINX_DOCKER_IMAGE}" ${DOC_DIR}

.PHONY: clean-docs-sphinx
clean-docs-sphinx:
	-rm -r "${DOC_DIR}/build"

.PHONY: clean-docker-sphinx
clean-docker-sphinx:
	-docker rm "${SPHINX_DOCKER_CONTAINER}"
	-docker rmi "${SPHINX_DOCKER_IMAGE}"

# docs-swagger generates API documentation in HTML format, to be used by
# Sphinx.
.PHONY: docs-swagger
docs-swagger: swagger.yml
	mkdir -p ${DOC_DIR}/source/_static/swagger
	docker run \
		--rm \
		-v "`pwd`:${SWAGGER_DOCKER_CONTAINER_WORKDIR}" \
		--user "`id -u $$USER`:`id -g $$USER`" \
		--name "${SWAGGER_DOCKER_CONTAINER}" \
		"${SWAGGER_DOCKER_IMAGE}"\
		generate \
			-i "${SWAGGER_DOCKER_CONTAINER_WORKDIR}/swagger.yml" \
			-l html \
			-o "${SWAGGER_DOCKER_CONTAINER_WORKDIR}/${DOC_DIR}/source/_static/swagger/"

.PHONY: clean-docs-swagger
clean-docs-swagger:
	-rm -rf doc/source/_static/swagger

.PHONY: clean-docker-swagger
clean-docker-swagger:
	-docker rm "${SWAGGER_DOCKER_CONTAINER}"
	-docker rmi "${SWAGGER_DOCKER_IMAGE}"

vendor: Gopkg.lock
	dep ensure -v -vendor-only

Gopkg.lock: Gopkg.toml
	dep ensure -v -no-vendor

.PHONY: dep-update
dep-update: clean-vendor
	dep ensure -update

# clean-vendor has not been added to vendor dependencies as dep is able to check out appropriate
# packages on versions set in the Gopkg.lock file. Removing vendor would force dep to re-download
# all the packages instead of only the missing ones. Global prune is turned off (see Gopkg.toml
# for explanation) thus vendor recipe will leave unused packages in the vendor/ directory. If that
# bothers you, run sequentially clean-vendor and vendor recipes.
.PHONY: clean-vendor
clean-vendor:
	rm -rf vendor

# Due to lack of standard approach to naming and separation of both interfaces and generated mock files
# below recipe does not have any file dependencies and is PHONY. Interface changes should be rare thus
# it is up to the developer to regenerate mocks after interface changes.
.PHONY: mocks
mocks: tools
	go generate ./mock
	go generate ./manager
	go generate ./controller/mock

.PHONY: tools
tools: vendor $(DEV_TOOLS_BIN)

# This recipe will rebuild all tools on vendor directory change.
# Due to short build time it is not treated as issue.
$(DEV_TOOLS_DIR)/%: $(DEV_TOOLS)
	go build -o $@ $(filter %$(@F),$(DEV_TOOLS))