forked from evylang/evy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
348 lines (267 loc) · 10.9 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
348
# Run `make help` to display help
.DEFAULT_GOAL := $(or $(EVY_DEFAULT_GOAL),help)
# --- Global -------------------------------------------------------------------
O = out
COVERAGE = 80
VERSION ?= $(shell git describe --tags --dirty --always)
GOFILES = $(shell find . -name '*.go')
## Build, test, check coverage and lint
all: build-go test lint
@if [ -e .git/rebase-merge ]; then git --no-pager log -1 --pretty='%h %s'; fi
@echo '$(COLOUR_GREEN)Success$(COLOUR_NORMAL)'
test: test-go test-tiny test-cli check-coverage
lint: lint-go lint-sh check-prettier check-style check-fmt-evy
## Full clean build and up-to-date checks as run on CI
ci: clean check-uptodate all
check-uptodate: tidy fmt doc docs
test -z "$$(git status --porcelain)" || { git status; false; }
## Remove generated files
clean::
-rm -rf $(O)
.PHONY: all check-uptodate ci test lint clean
# --- Build --------------------------------------------------------------------
GO_LDFLAGS = -X main.version=$(VERSION)
CMDS = .
## Build evy binaries
build-go: embed | $(O)
go build -o $(O) -ldflags='$(GO_LDFLAGS)' $(CMDS)
## Build and install binaries in $GOBIN
install: embed
go install -ldflags='$(GO_LDFLAGS)' $(CMDS)
## Build and install slim binaries without embedded frontend in $GOBIN
install-slim: embed-slim
go install -ldflags='$(GO_LDFLAGS)' $(CMDS)
# Use `go version` to ensure the right go version is installed when using tinygo.
go-version:
go version
## Build with tinygo targeting wasm
# optimise for size, see https://www.fermyon.com/blog/optimizing-tinygo-wasm
build-tiny: go-version | $(O)
GOOS=wasip1 GOARCH=wasm tinygo build -o $(O)/evy-unopt.wasm -no-debug -ldflags='$(GO_LDFLAGS)' -stack-size=512kb ./pkg/wasm
wasm-opt -O3 $(O)/evy-unopt.wasm -o frontend/module/evy.wasm
cp -f $$(tinygo env TINYGOROOT)/targets/wasm_exec.js frontend/module/
echo '{ "version": "$(VERSION)" }' | jq > frontend/version.json
## Prepare frontend assets to be embedded into the binary
embed: build-tiny | $(O)
rm -rf $(O)/embed
go run ./build-tools/site-gen frontend $(O)/embed
## Prepare slim frontend assets, with placeholder index.html only, to be embedded into the binary
embed-slim: | $(O)
rm -rf $(O)/embed
mkdir $(O)/embed
cp build-tools/embed-slim-index.html $(O)/embed/index.html
## Tidy go modules with "go mod tidy"
tidy:
go mod tidy
## Format all go files with gofumpt, a stricter gofmt
fmt:
gofumpt -w $(GOFILES)
clean::
-rm -f frontend/module/evy.wasm
-rm -f frontend/module/wasm_exec.js
-rm -f frontend/version.json
.PHONY: build-go build-tiny embed embed-slim go-version install install-slim tidy
# --- Test ---------------------------------------------------------------------
COVERFILE = $(O)/coverage.txt
EXPORTDIR = $(O)/export-test
## Run non-tinygo tests and generate a coverage file
test-go: embed-slim | $(O)
go test -coverprofile=$(COVERFILE) ./...
## Test evy CLI
test-cli: build-go
rm -rf $(EXPORTDIR)
$(O)/evy serve export $(EXPORTDIR)
test -f $(EXPORTDIR)/index.html
test -f $(EXPORTDIR)/play/module/evy.wasm
test ! -L $(EXPORTDIR)/play/module/evy.wasm
## Run tinygo tests
test-tiny: go-version | $(O)
tinygo test ./...
## Check that test coverage meets the required level
check-coverage: test-go
@go tool cover -func=$(COVERFILE) | $(CHECK_COVERAGE) || $(FAIL_COVERAGE)
## Show test coverage in your browser
cover: test-go
go tool cover -html=$(COVERFILE)
CHECK_COVERAGE = awk -F '[ \t%]+' '/^total:/ {print; if ($$3 < $(COVERAGE)) exit 1}'
FAIL_COVERAGE = { echo '$(COLOUR_RED)FAIL - Coverage below $(COVERAGE)%$(COLOUR_NORMAL)'; exit 1; }
.PHONY: check-coverage cover test-cli test-go test-tiny
# --- Lint ---------------------------------------------------------------------
EVY_FILES = $(shell find frontend/play/samples -name '*.evy')
## Lint go source code
lint-go: embed-slim
golangci-lint run
## Format evy sample code
fmt-evy:
go run . fmt --write $(EVY_FILES)
check-fmt-evy:
go run . fmt --check $(EVY_FILES)
.PHONY: check-fmt-evy fmt-evy lint-go
# --- Docs ---------------------------------------------------------------------
doc: doctest godoc toc usage
DOCTEST_CMD = ./build-tools/doctest.awk $(md) > $(O)/out.md && mv $(O)/out.md $(md)
DOCTESTS = docs/builtins.md docs/spec.md
doctest: install-slim
$(foreach md,$(DOCTESTS),$(DOCTEST_CMD)$(nl))
TOC_CMD = ./build-tools/toc.awk $(md) > $(O)/out.md && mv $(O)/out.md $(md)
TOCFILES = docs/builtins.md docs/spec.md
toc:
$(foreach md,$(TOCFILES),$(TOC_CMD)$(nl))
USAGE_CMD = ./build-tools/gencmd.awk $(md) > $(O)/out.md && mv $(O)/out.md $(md)
USAGEFILES = docs/usage.md
usage: install-slim
$(foreach md,$(USAGEFILES),$(USAGE_CMD)$(nl))
GODOC_CMD = ./build-tools/gengodoc.awk $(filename) > $(O)/out.go && mv $(O)/out.go $(filename)
GODOCFILES = main.go
godoc: install-slim
$(foreach filename,$(GODOCFILES),$(GODOC_CMD)$(nl))
DOCS_TARGET_DIR = frontend/preview
## Generate static HTML documentation in frontend/preview from MarkDown in /docs
docs:
go run ./build-tools/md docs $(DOCS_TARGET_DIR) # TODO: move to `frontend/docs/` when ready
npx --prefix $(NODEPREFIX) -y prettier --write $(DOCS_TARGET_DIR)
clean::
find $(DOCS_TARGET_DIR) -mindepth 1 \
! -regex '$(DOCS_TARGET_DIR)/css.*' \
! -regex '$(DOCS_TARGET_DIR)/img.*' \
! -regex '$(DOCS_TARGET_DIR)/favicon.ico' \
! -regex '$(DOCS_TARGET_DIR)/404.html' \
-delete
.PHONY: doc docs doctest godoc sdocs toc usage
# --- frontend -----------------------------------------------------------------
NODEPREFIX = .hermit/node
NODELIB = $(NODEPREFIX)/lib
define PLAYWRIGHT_CMD_LOCAL
npm --prefix e2e ci > /dev/null
npx --prefix e2e playwright test --config e2e $(PLAYWRIGHT_ARGS)
endef
PLAYWRIGHT_OCI_IMAGE = mcr.microsoft.com/playwright:v1.41.1-jammy
PLAYWRIGHT_CMD_DOCKER = docker run --rm \
--volume $$(pwd):/work/ -w /work/ \
--network host --add-host=host.docker.internal:host-gateway \
--env BASEURL=$(BASEURL) \
--env NPM_CONFIG_UPDATE_NOTIFIER=false \
$(PLAYWRIGHT_OCI_IMAGE) /bin/bash -e -c "$(subst $(nl),;,$(PLAYWRIGHT_CMD_LOCAL))"
PLAYWRIGHT_CMD = $(PLAYWRIGHT_CMD_$(if $(USE_DOCKER),DOCKER,LOCAL))
# BASEURL needs to be in the environment so that `e2e/playwright.config.js`
# can see it when the `e2e` target is called.
# The firebase-deploy script sets BASEURL to the deployment URL on GitHub CI.
SERVEDIR_HOST = $(if $(USE_DOCKER),host.docker.internal,localhost)
export SERVEDIR_PORT ?= 8080
export BASEURL ?= http://$(SERVEDIR_HOST):$(SERVEDIR_PORT)
## Serve frontend on port 8080 by default to work with e2e target
serve:
servedir frontend
## Format code with prettier
prettier: | $(NODELIB)
npx --prefix $(NODEPREFIX) -y prettier --write .
## Ensure code is formatted with prettier
check-prettier: | $(NODELIB)
npx --prefix $(NODEPREFIX) -y prettier --check .
## Fix CSS files with stylelint
style: | $(NODELIB)
npm --prefix $(NODEPREFIX) ci
npx --prefix $(NODEPREFIX) stylelint -c $(NODEPREFIX)/.stylelintrc.json --fix frontend/**/*.css
## Lint CSS files with stylelint
check-style: | $(NODELIB)
npm --prefix $(NODEPREFIX) ci
npx --prefix $(NODEPREFIX) stylelint -c $(NODEPREFIX)/.stylelintrc.json frontend/**/*.css
## Install playwright on host system for `e2e` to use.
install-playwright:
npx --prefix e2e playwright install --with-deps chromium
## Run playwright locally, or in docker if run with `make USE_DOCKER=1 ...`
run-playwright:
@echo "running playwright against $(BASEURL)"
$(PLAYWRIGHT_CMD)
## Run end-to-end tests with playwright (see run-playwright)
e2e: run-playwright
## Run end-to-end tests and list failed snapshot test image files.
e2e-diff: PLAYWRIGHT_ARGS = --reporter json
e2e-diff:
$(PLAYWRIGHT_CMD) | \
jq -r '.suites[].suites.[]?.specs[].tests[].results[].attachments?.[].path'
## Make end-to-end testing golden screenshots with playwright (see run-playwright)
snaps: PLAYWRIGHT_ARGS = --update-snapshots
snaps: run-playwright
$(NODELIB):
@mkdir -p $@
.PHONY: check-prettier e2e prettier serve
# --- deploy -----------------------------------------------------------------
## Deploy to live channel on firebase prod, use with care!
## `firebase login` for first time local usage
deploy-prod: build-tiny
./build-tools/firebase-deploy prod live
## Deploy to live channel on firebase stage.
## `firebase login` for first time local usage
deploy-stage: build-tiny
./build-tools/firebase-deploy stage live
## Deploy to dev (or other) channel on firebase stage.
## `firebase login` for first time local usage
deploy: build-tiny
./build-tools/firebase-deploy stage
.PHONY: deploy deploy-prod deploy-stage
# --- scripts ------------------------------------------------------------------
SCRIPTS = build-tools/firebase-deploy .github/scripts/app_token
## Lint script files with shellcheck and shfmt
lint-sh:
shellcheck $(SCRIPTS)
shfmt --diff $(SCRIPTS)
## Format script files
fmt-sh:
shfmt --write $(SCRIPTS)
.PHONY: fmt-sh lint-sh
# --- Release -------------------------------------------------------------------
## Tag and release binaries for different OS on GitHub release
# We need to run embed first to generate the full website including evy.wasm
# for embedding in the go binary. goreleaser build hooks cannot be used as
# they run in parallel for each os/arch and cause a race condition.
release: nexttag embed
git tag $(NEXTTAG)
git push origin $(NEXTTAG)
[ -z "$(CI)" ] || GITHUB_TOKEN=$$(.github/scripts/app_token) || exit 1; \
goreleaser release --clean $(if $(RELNOTES),--release-header=$(RELNOTES))
nexttag:
$(eval NEXTTAG := $(shell $(NEXTTAG_CMD)))
$(eval RELNOTES := $(wildcard docs/release-notes/$(NEXTTAG).md))
.PHONY: nexttag release
define NEXTTAG_CMD
{
{ git tag --list --merged HEAD --sort=-v:refname; echo v0.0.0; }
| grep -E "^v?[0-9]+\.[0-9]+\.[0-9]+$$"
| head -n 1
| awk -F . '{ print $$1 "." $$2 "." $$3 + 1 }';
git diff --name-only @^ | sed -E -n 's|^docs/release-notes/(v[0-9]+\.[0-9]+\.[0-9]+)\.md$$|\1|p';
} | sort --reverse --version-sort | head -n 1
endef
# --- Utilities ----------------------------------------------------------------
COLOUR_NORMAL = $(shell tput sgr0 2>/dev/null)
COLOUR_RED = $(shell tput setaf 1 2>/dev/null)
COLOUR_GREEN = $(shell tput setaf 2 2>/dev/null)
COLOUR_WHITE = $(shell tput setaf 7 2>/dev/null)
help:
$(eval export HELP_AWK)
@awk "$${HELP_AWK}" $(MAKEFILE_LIST) | sort | column -s "$$(printf \\t)" -t
$(O):
@mkdir -p $@
.PHONY: help
# Awk script to extract and print target descriptions for `make help`.
define HELP_AWK
/^## / { desc = desc substr($$0, 3) }
/^[A-Za-z0-9%_-]+:/ && desc {
sub(/::?$$/, "", $$1)
printf "$(COLOUR_WHITE)%s$(COLOUR_NORMAL)\t%s\n", $$1, desc
desc = ""
}
endef
define nl
endef
ifndef ACTIVE_HERMIT
$(eval $(subst \n,$(nl),$(shell bin/hermit env -r | sed 's/^\(.*\)$$/export \1\\n/')))
endif
# Ensure make version is gnu make 3.82 or higher
ifeq ($(filter undefine,$(value .FEATURES)),)
$(error Unsupported Make version. \
$(nl)Use GNU Make 3.82 or higher (current: $(MAKE_VERSION)). \
$(nl)Activate 🐚 hermit with `. bin/activate-hermit` and run again \
$(nl)or use `bin/make`)
endif