-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMakefile
300 lines (220 loc) · 11.6 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
# package
NAME ?= harp-proxy
VERSION ?= $(shell git describe 2>/dev/null || git rev-parse --short HEAD)
# poetry
POETRY ?= $(shell which poetry || echo "poetry")
POETRY_BUILD ?= $(POETRY) build
POETRY_INSTALL_OPTIONS ?=
RUN ?= $(if $(VIRTUAL_ENV),,$(POETRY) run)
PLATFORM ?= linux/amd64
# pytest
PYTEST ?= $(RUN) pytest
PYTEST_TARGETS ?= harp harp_apps tests
PYTEST_CPUS ?= auto
PYTEST_COMMON_OPTIONS ?= -n $(PYTEST_CPUS)
PYTEST_COVERAGE_OPTIONS ?= --cov=harp --cov=harp_apps --cov-report html:docs/_build/html/coverage
PYTEST_OPTIONS ?=
# docker
DOCKER ?= $(shell which docker || echo "docker")
DOCKER_OPTIONS ?=
DOCKER_IMAGE ?= $(NAME)
DOCKER_IMAGE_DEV ?= $(NAME)-dev
DOCKER_TAGS ?=
DOCKER_TAGS_SUFFIX ?=
DOCKER_BUILD_OPTIONS ?= --platform=$(PLATFORM)
DOCKER_BUILD_TARGET ?= runtime
DOCKER_NETWORK ?= harp
DOCKER_RUN_COMMAND ?=
DOCKER_RUN_OPTIONS ?=
# frontend
PNPM ?= $(shell which pnpm || echo "pnpm")
# misc.
SED ?= $(shell which gsed || which sed || echo "sed")
TESTC_COMMAND ?= poetry shell
TEST_SKIP_FRONT ?=
# constants
FRONTEND_DIR = harp_apps/dashboard/frontend
# default run options
HARP_OPTIONS ?= --example sqlite --example proxy:httpbin
HARP_MORE_OPTIONS ?=
HARP_SERVICES ?= server dashboard
.PHONY: start-dev start-dev-frontend
start-dev: install-dev # Starts a development instance with reasonable defaults (tune HARP_OPTIONS to replace).
$(POETRY) run harp start $(HARP_SERVICES) $(HARP_OPTIONS) $(HARP_MORE_OPTIONS)
start-dev-frontend: install-dev # Starts a frontend development instance with reasonable defaults (you'll have to a backend, useful to use an external debugger for example).
HARP_SERVICES=dashboard HARP_MORE_OPTIONS="--set dashboard.devserver.port=12121" $(MAKE) start-dev
########################################################################################################################
# Dependencies
########################################################################################################################
.PHONY: install install-dev install-frontend install-backend install-backend-dev wheel
install: install-frontend install-backend ## Installs harp dependencies (backend, dashboard) without development tools.
install-dev: ## Installs harp dependencies (backend, dashboard) with development tools.
POETRY_INSTALL_OPTIONS="-E dev" $(MAKE) install
cd $(FRONTEND_DIR); $(PNPM) exec playwright install
install-frontend: ## Installs harp dashboard dependencies (frontend).
cd $(FRONTEND_DIR); $(PNPM) install
install-backend: ## Installs harp dependencides (backend).
$(POETRY) install $(POETRY_INSTALL_OPTIONS)
install-backend-dev: ## Installs harp dependencies (backend) with development tools.
POETRY_INSTALL_OPTIONS="-E dev" $(MAKE) install
wheel:
mkdir -p dist
bin/sandbox "$(MAKE) install-dev build-frontend; \
rm -rf harp_apps/dashboard/frontend; \
sed '/^People & Credits/,$$ d' README.rst > README.rst.tmp; \
mv README.rst.tmp README.rst; \
$(POETRY_BUILD); \
cp dist/* $(PWD)/dist; \
twine check dist/*"
########################################################################################################################
# Documentation
########################################################################################################################
.PHONY: reference docs docs-dev
reference: harp ## Generates API reference documentation as ReST files (docs).
rm -rf docs/reference/core docs/reference/apps
mkdir -p docs/reference/core docs/reference/apps
$(RUN) bin/generate_apidoc
git add docs/reference/
docs: ## Build html documentation
$(RUN) $(MAKE) -C docs html
docs-dev: ## Spin up a livereload documentation server
$(RUN) $(MAKE) -C docs dev
########################################################################################################################
# Dashboard application
########################################################################################################################
.PHONY: build-frontend
build-frontend: ## Builds the harp dashboard frontend (compiles typescript and other sources into bundled version).
cd $(FRONTEND_DIR); $(PNPM) build
mv harp_apps/dashboard/frontend/dist harp_apps/dashboard/web
########################################################################################################################
# QA, tests and other CI/CD related stuff
########################################################################################################################
.PHONY: preqa qa qa-full types format format-backend format-frontend optimize-images
.PHONY: test test-backend test-frontend test-frontend-update test-frontend-ui-update
.PHONY: lint-frontend coverage cloc
preqa: types format reference ## Runs pre-qa checks (types generation, formatting, api reference).
-$(RUN) pre-commit
qa: preqa test ## Runs all QA checks, with most common databases.
qa-full: ## Runs all QA checks, including all supported databases.
TEST_ALL_DATABASES=true $(MAKE) qa
qa-nofront:
TEST_SKIP_FRONT=1 $(MAKE) qa
types: ## Generates frontend types from the python code.
$(RUN) bin/generate_types # old school
$(RUN) bin/generate_ts_types # new school
format: ## Formats the full codebase (backend and frontend).
$(MAKE) format-backend
test -z "$(TEST_SKIP_FRONT)" && $(MAKE) format-frontend || (cd $(FRONTEND_DIR); $(PNPM) prettier -w src/Models)
format-backend: ## Formats the backend codebase.
$(RUN) isort harp harp_apps tests
$(RUN) black harp harp_apps tests
$(RUN) ruff check --fix harp harp_apps tests
$(RUN) ruff format
format-frontend: install-frontend ## Formats the frontend codebase.
(cd $(FRONTEND_DIR); $(PNPM) lint:fix)
(cd $(FRONTEND_DIR); $(PNPM) prettier -w src)
optimize-images:
find docs -name \*.png | xargs optimizt
test: ## Runs all tests.
$(MAKE) test-backend
test -z "$(TEST_SKIP_FRONT)" && $(MAKE) test-frontend || echo "Skipped."
test-backend: install-backend-dev ## Runs backend tests.
$(PYTEST) $(PYTEST_TARGETS) \
--benchmark-disable \
$(PYTEST_COMMON_OPTIONS) \
$(PYTEST_OPTIONS)
test-frontend: install-frontend lint-frontend ## Runs frontend tests.
cd $(FRONTEND_DIR); $(PNPM) test:unit
cd $(FRONTEND_DIR); $(PNPM) test:browser
bin/runc_visualtests pnpm test:ui:dev
test-frontend-update: install-frontend lint-frontend ## Runs frontend tests while updating snapshots.
cd $(FRONTEND_DIR); $(PNPM) test:unit:update
test-frontend-ui-update: install-frontend lint-frontend ## Update user interface visual snapshots.
bin/runc_visualtests pnpm test:ui:update
lint-frontend: install-frontend ## Lints the frontend codebase.
cd $(FRONTEND_DIR); $(PNPM) build
coverage: ## Generates coverage report.
$(PYTEST) $(PYTEST_TARGETS) tests \
-m 'not subprocess' \
$(PYTEST_COVERAGE_OPTIONS) \
$(PYTEST_COMMON_OPTIONS) \
$(PYTEST_OPTIONS)
cloc:
cloc harp harp_apps tests --exclude-dir=node_modules,build,dist
########################################################################################################################
# Benchmarks
########################################################################################################################
.PHONY: benchmark benchmark-save
BENCHMARK_OPTIONS ?=
BENCHMARK_MIN_ROUNDS ?= 100
benchmark: ## Runs benchmarks.
$(PYTEST) tests/benchmarks \
$(BENCHMARK_OPTIONS) \
--benchmark-enable \
--benchmark-only \
--benchmark-disable-gc \
--benchmark-min-rounds=$(BENCHMARK_MIN_ROUNDS) \
--benchmark-group-by=group \
--benchmark-compare="0006" \
--benchmark-histogram \
$(PYTEST_OPTIONS)
benchmark-save: ## Runs benchmarks and saves the results.
BENCHMARK_OPTIONS='--benchmark-warmup=on --benchmark-warmup-iterations=50 --benchmark-save="$(shell git describe --tags --always --dirty)"' \
BENCHMARK_MIN_ROUNDS=500 \
$(MAKE) benchmark
########################################################################################################################
# Docker builds
########################################################################################################################
.PHONY: buildc pushc runc runc-shell runc-example-repositories
buildc: ## Builds the docker image.
# TODO: rm in trap ?
# TODO: document --progress=plain ?
echo $(VERSION) > version.txt
$(DOCKER) build --target=$(DOCKER_BUILD_TARGET) $(DOCKER_OPTIONS) $(DOCKER_BUILD_OPTIONS) -t $(DOCKER_IMAGE) $(foreach tag,$(VERSION) $(DOCKER_TAGS),-t $(DOCKER_IMAGE):$(tag)$(DOCKER_TAGS_SUFFIX)) .
-rm -f version.txt
pushc: ## Pushes the docker image to the registry.
for tag in $(VERSION) $(DOCKER_TAGS); do \
$(DOCKER) image push $(DOCKER_IMAGE):$$tag$(DOCKER_TAGS_SUFFIX); \
done
runc: ## Runs the docker image.
$(DOCKER) run -it --network $(DOCKER_NETWORK) $(DOCKER_OPTIONS) $(DOCKER_RUN_OPTIONS) -p 4000-4999:4000-4999 --rm $(DOCKER_IMAGE) $(DOCKER_RUN_COMMAND)
runc-shell: ## Runs a shell within the docker image.
$(DOCKER) run -it --network $(DOCKER_NETWORK) $(DOCKER_OPTIONS) $(DOCKER_RUN_OPTIONS) -p 4080:4080 --rm --entrypoint=/bin/bash $(DOCKER_IMAGE) $(DOCKER_RUN_COMMAND)
runc-example-repositories: ## Runs harp with the "repositories" example within the docker image.
$(DOCKER) run -it --network $(DOCKER_NETWORK) $(DOCKER_OPTIONS) $(DOCKER_RUN_OPTIONS) -p 4080:4080 -p 9001-9012:9001-9012 --rm $(DOCKER_IMAGE) --example repositories --set storage.url postgresql+asyncpg://harp:harp@harp-postgres-1/repositories
.PHONY: buildc-dev pushc-dev runc-dev runc-dev-shell
buildc-dev: ## Builds the development docker image.
DOCKER_IMAGE=$(DOCKER_IMAGE_DEV) DOCKER_BUILD_TARGET=development $(MAKE) buildc
pushc-dev: ## Pushes the development docker image to the registry.
DOCKER_IMAGE=$(DOCKER_IMAGE_DEV) $(MAKE) pushc
runc-dev: ## Runs the development docker image.
DOCKER_IMAGE=$(DOCKER_IMAGE_DEV) $(MAKE) runc
runc-dev-shell: ## Runs a shell within the development docker image.
DOCKER_IMAGE=$(DOCKER_IMAGE_DEV) $(MAKE) runc-shell
.PHONY: testc-shell testc-backend
testc-shell: ## Runs a shell in the development test suite environment.
$(DOCKER) rm -f docker || true
$(DOCKER) run --privileged -d --name docker --network $(DOCKER_NETWORK) --network-alias docker -e DOCKER_TLS_CERTDIR= $(DOCKER_OPTIONS) $(DOCKER_RUN_OPTIONS) docker:24.0.6-dind
DOCKER_OPTIONS="-e DOCKER_HOST=tcp://docker:2375/" DOCKER_RUN_COMMAND="-c \"bin/wait-until-docker-available && (cd src; docker compose up -d; $(TESTC_COMMAND))\"" $(MAKE) runc-dev-shell
$(DOCKER) stop docker
$(DOCKER) rm docker
testc-backend: ## Runs the backend test suite within the development docker image, with a docker in docker sidecar service.
DOCKER_OPTIONS="-e DOCKER_HOST=tcp://docker:2375/" TESTC_COMMAND="PYTEST_OPTIONS=-vv poetry run make test-backend" $(MAKE) testc-shell
########################################################################################################################
# Misc. utilities
########################################################################################################################
.PHONY: help clean clean-dist clean-docs clean-frontend-modules
help: ## Shows available commands.
@echo "Available commands:"
@echo
@grep -E '^[a-zA-Z_-]+:.*?##[\s]?.*$$' --no-filename $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?##"}; {printf " make \033[36m%-30s\033[0m %s\n", $$1, $$2}'
@echo
clean-frontend-modules: ## Cleans up the frontend node modules directory.
-rm -rf $(FRONTEND_DIR)/node_modules
clean-dist: ## Cleans up the distribution files (wheels...)
-rm -rf $(FRONTEND_DIR)/dist
-rm -rf dist
clean-docs: ## Cleanup the documentation builds.
-rm -rf docs/_build
clean: clean-frontend-modules clean-dist clean-docs ## Cleans up the project.
-rm -f benchmark_*.svg