-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
347 lines (299 loc) · 10.5 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
SHELL:=/usr/bin/env bash
BUILD:=$(shell date -u +%s)
PROJECT_SINCE:=$(shell echo $$(($$(git log --pretty=format:"%ad" --date=unix|tail -1)/(24*60*60)*(24*60*60))))
AUTO_COUNT_SINCE:=$(shell echo $$((($(BUILD)-$(PROJECT_SINCE))/(24*60*60))))
AUTO_COUNT_YEAR:=$(shell echo $$(($(AUTO_COUNT_SINCE)/365)))
AUTO_COUNT_DAY:=$(shell echo $$(($(AUTO_COUNT_SINCE)%365)))
AUTO_COUNT_MINUTE:=$(shell echo $$(($(BUILD)%(24*60*60))))
CODEBASE_NUMBER:=0
SERIAL:=$(CODEBASE_NUMBER).$(AUTO_COUNT_YEAR).$(AUTO_COUNT_DAY)-$(AUTO_COUNT_MINUTE)
TAG:=$(shell git describe --tags || echo NO-TAG)
HASH:=$(shell git describe --always --dirty)
BRANCH:=$(shell git symbolic-ref --short HEAD)
LDFLAGS:=-ldflags=" \
-X \"main.branch=$(BRANCH)\" \
-X \"main.build=$(BUILD)\" \
-X \"main.tag=$(TAG)\" \
-X main.hash=$(HASH) \
-X main.serial=$(SERIAL) \
"
COMMIT:=4b825dc
REVIEWDOG:=| reviewdog -efm='%f:%l:%c: %m' -diff="git diff $(COMMIT) HEAD"
GO:=go
GOM:=GO111MODULE=on $(GO)
GOPATH:=$(shell $(GO) env GOPATH)
PKG:=$(shell $(GO) list)
GOBIN:=$(notdir $(PKG))
GOLIST:=$(shell $(GO) list ./...)
GODIR:=$(patsubst $(PKG)/%,%,$(wordlist 2,$(words $(GOLIST)),$(GOLIST)))
STATIK:=$(wildcard assets/gcp/bq/savedqueries/*.sql)
STATIKGO:=statik/statik.go
PROTO:=$(shell find . -type d -name ".?*" -prune -or -type d -name vendor -prune -or -type f -name "*.proto" -print)
PB_GO:=$(PROTO:.proto=.pb.go)
PB_VALIDATE_GO:=$(PROTO:.proto=.pb.validate.go)
IF_GO:=$(shell find . -type d -name vendor -prune\
-or -type f -name "entity-*.go" -print\
-or -type f -name "if-*.go" -print\
-or -type f -name "vo-*.go" -print\
)
GOFILE:=$(filter-out %.pb.go $(IF_GO),$(shell find . -type d -name vendor -prune -or -type f -name "*.go" -print))
GOSRC:=$(GOFILE) $(PB_GO)
CEL:=$(shell find . -type d -name vendor -prune -or -type f -name "*.cel.txt" -print)
GOCEL:=$(patsubst %.cel.txt,%_gen.go,$(CEL))
#GOGEN:=$(shell find . -type d -name vendor -prune -or -type f -name "*.go" -print|xargs grep "^//go:generate " -l)
APP_DIR_PATH:=app
GOPHERJS:=$(APP_DIR_PATH)/web/gopher.js
LIBGO:=$(wildcard lib/*.go)
GOLIB:=$(LIBGO:.go=.so)
.SUFFIXES: .go .so
.go.so:
$(GO) build -buildmode=c-shared -o $@ $<
%.pb.go: %.proto
( type prototool > /dev/null 2>&1 ) && prototool all $< || :
%.pb.validate.go: %.proto
d=$(dir $<);\
( type protoc > /dev/null 2>&1 ) && protoc\
-I $$d\
-I vendor\
--validate_out="lang=go:$$d"\
$<
.PHONY: all
all: $(GOBIN) $(GOLIB) $(APP_DIR_PATH)/build
.PHONY: cloud-config
cloud-config:
assets/script/cloud-config.sh
.PHONY: uml
uml:
for i in domain application infra service $(shell find usecase -maxdepth 2 -type d -print); do\
[ -d $$i ] && gouml init --file $$i --out $$i/$$(basename $$i).puml || echo no exists $$i;\
done
for i in $$(find . -type f -name "*.puml" -print); do\
echo -e -n "@startuml\n\n\n\n@enduml\n" | cmp $$i > /dev/null 2>&1\
&& (rm $$i; echo removed empty uml $$i) || echo -n;\
done
$(STATIKGO): $(STATIK)
( type statik > /dev/null 2>&1 ) && statik -src=$(STATIK)
.PHONY: statik
statik: $(STATIKGO)
$(PB_VALIDATE_GO):
.PHONY: proto
proto: vendor $(PB_GO) $(PB_VALIDATE_GO)
.PHONY: golang
golang: $(GOBIN) $(GOLIB)
.PHONY: gopherjs
gopherjs: $(GOPHERJS)
.PHONY: dart
dart: $(APP_DIR_PATH)/build
# do not use bundler in Docker container
ifeq ($(shell test -f /proc/self/cgroup && cat /proc/self/cgroup|grep -q docker;echo $$?),0)
BUNDLE_EXEC:=
else
BUNDLE_EXEC:=bundle exec
BUNDLE:=.bundle/bundle
Gemfile.lock: Gemfile
bundle install --path $(BUNDLE)
$(BUNDLE): Gemfile.lock
bundle install --path $(BUNDLE)
$(HTML): $(BUNDLE)
endif
deps: go.mod
$(GOM) mod download
vendor: go.mod
$(GOM) mod vendor
$(GOM) mod tidy
-git checkout -f vendor
.PHONY: generate
generate: vendor
rm -f $(IF_GO)
-@for pkg in $(GOLIST); do\
go generate $$pkg/...;\
done
make clean
$(IF_GO): $(filter-out $(IF_GO),$(GOSRC))
-go generate ./...
%_gen.go: %.go %.cel.txt
-go generate ./...
.PHONY: go-get
go-get: $(GOSRC)
echo > go.mod
rm -rf vendor
time \
$(GOM) build $(LDFLAGS)
$(GOBIN): deps $(GOSRC) $(GOCEL)
time \
$(GOM) build $(LDFLAGS)" -X \"main.semver=$(SERIAL)+$(HASH)\""
GOX_OPTION:=-osarch="darwin/amd64 linux/amd64 linux/arm"
.PHONY: channel
channel: deps $(GOSRC)
time \
GO111MODULE=on gox $(GOX_OPTION) \
-output="assets/gox/$(BRANCH)/$(SERIAL)+$(HASH)/{{.OS}}-{{.Arch}}" \
$(LDFLAGS)" -X \"main.semver=$(SERIAL)+$(HASH)\" -X \"main.channel=channel/$(BRANCH)\""
mkdir -p docs/channel/$(BRANCH)/$(GOBIN)
./assets/script/selfupdate.sh docs/channel/$(BRANCH)/$(GOBIN) 3
rm -rf docs/channel/$(BRANCH)/*.json
time go-selfupdate -o docs/channel/$(BRANCH)/$(GOBIN) assets/gox/$(BRANCH)/$(SERIAL)+$(HASH) $(SERIAL)+$(HASH)
.PHONY: release
release: deps $(GOSRC) $(GOCEL)
time \
GO111MODULE=on gox $(GOX_OPTION) \
-output="assets/gox/$(TAG)/{{.OS}}-{{.Arch}}" \
$(LDFLAGS)" -X \"main.semver=$(TAG)\" -X \"main.channel=release\""
mkdir -p docs/release/$(GOBIN)
./assets/script/selfupdate.sh docs/release/$(GOBIN) 3
rm -rf docs/channel/$(BRANCH)/*.json
time go-selfupdate -o docs/release/$(GOBIN) assets/gox/$(TAG) $(TAG)
.PHONY: package
package:
./assets/script/package.sh
$(GOPHERJS): $(GOSRC) $(GOCEL)
@# https://github.com/gopherjs/gopherjs/issues/598#issuecomment-282563634
-find $(GOPATH)/pkg -depth 1 -type d -name "*_js" -exec rm -fr {} \;
-find $(GOPATH)/pkg -depth 1 -type d -name "*_js_min" -exec rm -fr {} \;
gopherjs build --tags gopherjs --minify $(PKG)/hackernews/gopherjs --output $@
NODE_MODULES_BASE:=node_modules
UGLIFYJS:=$(NODE_MODULES_BASE)/.bin/uglifyjs
$(NODE_MODULES_BASE): package.json
npm install
.PHONY: uglifyjs
uglifyjs: $(GOPHERJS) $(NODE_MODULES_BASE)
cd $(dir $<) && ../../$(UGLIFYJS) --compress --mangle --output $(notdir $<) $(notdir $<)
SLIM:=$(foreach dir,lib web,$(shell find $(APP_DIR_PATH)/$(dir) -type f -name "*.slim" -print))
HTML:=$(SLIM:.slim=.html)
.SUFFIXES: .slim .html
.slim.html:
$(BUNDLE_EXEC) slimrb --pretty --option splat_prefix="'**'" --option code_attr_delims="{'{' => '}'}" --option attr_list_delims="{'{' => '}'}" $< > $@
html: $(HTML)
PUB_SPEC:=$(shell find . -type d -name build -prune -o -type f -name pubspec.yaml -print)
PUB_LOCK:=$(PUB_SPEC:.yaml=.lock)
.SUFFIXES: .yaml .lock
.yaml.lock:
(cd $(dir $@) && pub get)
$(APP_DIR_PATH)/.packages: $(PUB_LOCK)
(cd $(dir $@) && pub get)
DART:=hackernews/dart/lib/hackernews.dart
G_DART:=$(DART:.dart=.g.dart)
.SUFFIXES: .dart .g.dart
.dart.g.dart:
(cd hackernews/dart && pub run build_runner build --delete-conflicting-outputs)
.PHONY: $(APP_DIR_PATH)/build
$(APP_DIR_PATH)/build: uglifyjs $(PUB_LOCK) $(APP_DIR_PATH)/.packages $(G_DART) $(HTML)
(cd $(APP_DIR_PATH)\
&& pub run build_runner build --delete-conflicting-outputs --release --fail-on-severe --output build\
&& pub run pwa --exclude ".DS_Store,packages/**,.packages,*.dart,*.js.deps,*.js.info.json,*.js.map,*.js.tar.gz,*.module"\
)
rm -rf $(APP_DIR_PATH)/build/web/packages/test
find $(APP_DIR_PATH)/build/web -type f -name "*.dart" -exec rm -f {} \;
find $(APP_DIR_PATH)/build/web -type f -name "*.slim" -exec rm -f {} \;
while [ `find $(APP_DIR_PATH)/build/web -type d -empty |wc -l` != 0 ] ; do find $(APP_DIR_PATH)/build/web -type d -empty -exec rm -rf {} + ; done
UNAME:=$(shell uname -s)
ifeq ($(UNAME),Darwin)
CONVERT_PREFIX:=docker-compose run
endif
CONVERT:=$(CONVERT_PREFIX) convert -verbose -colorspace sRGB -transparent white -density 256x256
$(APP_DIR_PATH)/web/images/icons/android-icon-%.png \
$(APP_DIR_PATH)/web/images/icons/favicon-%.png \
: $(APP_DIR_PATH)/web/images/logo.svg
$(CONVERT) -filter Point $< -trim +filter -resize $* $@
$(APP_DIR_PATH)/web/favicon.ico: $(APP_DIR_PATH)/web/images/logo.svg
$(CONVERT) -filter Point $< -trim +filter -resize 16x -define icon:auto-resize -colors 256 $@
RELEASE:=--release
.PHONY: serve
serve: $(GOPHERJS) $(PUB_LOCK) $(G_DART) $(HTML)
(cd $(APP_DIR_PATH) && webdev serve $(RELEASE))
.PHONY: fixes-webdev
fixes-webdev:
-# https://github.com/dart-lang/sdk/issues/33601#issuecomment-402469804
( type pubglobalupdate > /dev/null 2>&1 ) || pub global activate pubglobalupdate ; pubglobalupdate
.PHONY: deploy
deploy: $(APP_DIR_PATH)/build
firebase deploy
.PHONY: clean
clean:
rm -f $(GOBIN) $(GOLIB) $(wildcard lib/*.h)
rm -rf package $(APP_DIR_PATH)/build
find . -type f -name coverprofile -delete
find . -type f -name "coverage.*" -depth 1 -delete
find . -name .DS_Store -delete
find assets -type d -name assets -delete
rm -rf assets/debian && git checkout -f assets/debian
for file in $$(find . -type d -name vendor -prune\
-or -type f -name "entity-*.go" -print\
-or -type f -name "gen-*.go" -print\
-or -type f -name "if-*.go" -print\
-or -type f -name "vo-*.go" -print\
); do\
sed -i '' 's|"github.com/michilu/boilerplate/vendor/|"|g' $$file;\
chmod -x $$file;\
done
for file in $$(find pb -type d -name vendor -prune\
-or -type f -name "*.go" -print\
); do\
sed -i '' '/"github.com\/michilu\/boilerplate\/pb"/d' $$file;\
done
sed -i '' -E 's/) pb\./) /g' pb/*.go
sed -i '' -E 's/\*pb\./\*/g' pb/*.go
.PHONY: test
test: deps
$(GOM) test -race $(PKG)/...
[ "$$(./assets/script/denylist.sh)" = "" ] || (./assets/script/denylist.sh; exit 1)
.PHONY: pprof
pprof:
pprof -http=: localhost:8888/debug/pprof/profile
.PHONY: bench
bench:
$(GOM) test -bench . -benchmem -count 5 -run none $(PKG)/... | tee bench/now.txt
[ -f bench/before.txt ] && ( type benchcmp > /dev/null 2>&1 ) && benchcmp bench/before.txt bench/now.txt || :
COVERMODE:=atomic
.PHONY: coverage
coverage:
@for pkg in $(GOLIST); do\
echo start test for $$pkg;\
$(GOM) test $$pkg -race -coverprofile=$${pkg#$(PKG)/}/coverprofile -covermode=$(COVERMODE);\
done
echo "mode: $(COVERMODE)" > coverage.txt\
&& find . -type f -name coverprofile -exec tail -n+2 {} >>coverage.txt \; -delete
$(GOM) tool cover -html=coverage.txt -o coverage.html
.PHONY: lint
lint:
-echo $(PROTO) | xargs -n1 prototool lint
@echo
-gofmt -s -w .
@echo
-echo $(GOLIST) | xargs -L1 golint
@echo
-deadcode $(GODIR) 2>&1
@echo
-find $(GODIR) -type f -exec misspell {} \; $(REVIEWDOG)
@echo
-staticcheck $(GOLIST) $(REVIEWDOG)
@echo
-errcheck $(GOLIST) $(REVIEWDOG)
@echo
-safesql $(GOLIST)
@echo
-goconst $(GOLIST) $(REVIEWDOG)
@echo
-$(GOM) vet $(GOLIST) $(REVIEWDOG)
@echo
-$(GOM) vet -shadow $(GOLIST) $(REVIEWDOG)
@echo
-aligncheck $(GOLIST) $(REVIEWDOG)
@echo
-gosimple $(GOLIST) $(REVIEWDOG)
@echo
-unconvert $(GOLIST) $(REVIEWDOG)
@echo
-interfacer $(GOLIST) $(REVIEWDOG)
.PHONY: review
review:
$(MAKE) lint COMMIT:=master
.PHONY: review-dupl
review-dupl:
-git diff $(COMMIT) HEAD --name-only --diff-filter=AM|grep -e "\.go$$" | xargs dupl
.PHONY: godoc
godoc:
mkdir -p $@
for i in $(GOLIST); do\
godocdown $$i > "godoc/$${i//\//-}".md;\
done