Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
167 lines (141 loc) · 5.46 KB
/
Copy pathMakefile
File metadata and controls
167 lines (141 loc) · 5.46 KB
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
# Fall back to the installer's location so uv works right after `make uv`
UV := $(or $(shell command -v uv 2>/dev/null),$(HOME)/.local/bin/uv)
FLASK := $(UV) run flask --app makeitso
PSQL := psql
DB_NAME := mis
HTMX_VERSION := 2.0.11
ALPINE_VERSION := 3.17.4
BC_SANS_VERSION := 2.1.2
VENDOR_DIR := src/makeitso/static/vendor
.PHONY: help
help:
@echo "make uv Install uv if it is not already installed"
@echo "make install Install uv, Python and all dependencies into .venv"
@echo "make run Run the dev server on http://localhost:8000"
@echo "make shell Open a Python shell with the app loaded"
@echo "make redis-install Install Redis with Homebrew if it is not already installed"
@echo "make redis Run a local Redis server (installs it first if needed)"
@echo "make worker Run a background job worker (RQ)"
@echo "make sync_stacks Queue syncs for stale stacks, like the CronJob does"
@echo "make create_db Create the $(DB_NAME) database if it does not exist"
@echo "make init_db Initialise the migrations folder (already done in this repo)"
@echo "make migrate Generate a migration: make migrate ARGS='-m \"message\"'"
@echo "make upgrade_db Apply pending migrations"
@echo "make downgrade_db Roll back migrations: ARGS='<revision>' or ARGS='-1'"
@echo "make drop_db Drop the $(DB_NAME) database if it exists (deletes all data)"
@echo "make reset_db Drop the $(DB_NAME) database, recreate it and apply all migrations (deletes all data)"
@echo "make lint Check code style and common mistakes (ruff)"
@echo "make format Auto-format code and fix lint issues (ruff)"
@echo "make typecheck Check types (ty)"
@echo "make test Run the tests against the mis_test database: ARGS='-k name' to filter"
@echo "make coverage Run the tests and show which code they don't cover (htmlcov/index.html)"
@echo "make vendor Download pinned front-end libraries into static/vendor"
.PHONY: uv
uv:
@test -x "$(UV)" || { \
echo "Installing uv..."; \
curl -LsSf https://astral.sh/uv/install.sh | sh; \
}
.PHONY: install
install: uv
$(UV) sync
.PHONY: run
run:
$(FLASK) run --debug --port 8000
.PHONY: shell
shell:
$(FLASK) shell
# Install the Redis server with Homebrew unless redis-server is already available.
.PHONY: redis-install
redis-install:
@command -v redis-server >/dev/null 2>&1 || { \
command -v brew >/dev/null 2>&1 || { \
echo "Homebrew not found. Install Redis with your package manager,"; \
echo "e.g. 'sudo apt install redis-server' (Debian/Ubuntu)."; \
exit 1; \
}; \
echo "Installing Redis..."; \
brew install redis; \
}
# Run Redis; --save "" disables snapshots, so no dump.rdb is written to the repo
.PHONY: redis
redis: redis-install
redis-server --save ""
# Run an RQ worker through `flask worker`, so jobs have the Flask app context
.PHONY: worker
# OBJC_...=YES stops macOS from killing each job's forked process (ignored on Linux)
worker:
OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES $(FLASK) worker
.PHONY: workers
workers:
(trap 'kill 0' SIGINT; \
OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES $(FLASK) worker --queue syncs & \
OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES $(FLASK) worker --queue deploys)
# Queue syncs for stacks not synced in a while; a running worker does the syncing
.PHONY: sync_stacks
sync_stacks:
$(FLASK) stacks sync
.PHONY: create_db
create_db:
@$(PSQL) -d postgres -tc "SELECT count(*) FROM pg_database WHERE datname = '$(DB_NAME)'" | \
grep -q 1 || \
$(PSQL) -d postgres -c "CREATE DATABASE $(DB_NAME)"
.PHONY: init_db
init_db:
$(FLASK) db init
# Named migration: ARGS='-m "named-migration"'
.PHONY: migrate
migrate:
@if [ -z "$(ARGS)" ]; then \
echo "Error: ARGS is empty. Please name your migration."; \
exit 1; \
else \
$(FLASK) db migrate $(ARGS); \
fi
.PHONY: upgrade_db
upgrade_db:
$(FLASK) db upgrade $(ARGS)
# ARGS='<revision_id>' - downgrade to specific revision
# ARGS='-3' - downgrade the last 3 revisions
.PHONY: downgrade_db
downgrade_db:
$(FLASK) db downgrade $(ARGS)
# FORCE disconnects open sessions, e.g. a running `make run`
.PHONY: drop_db
drop_db:
$(PSQL) -d postgres -c "DROP DATABASE IF EXISTS $(DB_NAME) WITH (FORCE)"
.PHONY: reset_db
reset_db: drop_db create_db upgrade_db
.PHONY: lint
lint:
$(UV) run ruff check .
$(UV) run ruff format --check .
.PHONY: format
format:
$(UV) run ruff check --fix .
$(UV) run ruff format .
.PHONY: typecheck
typecheck:
$(UV) run ty check
# Tests use their own database (mis_test), created here if missing
.PHONY: test
test:
$(MAKE) create_db DB_NAME=mis_test
$(UV) run --env-file .env pytest $(ARGS)
# Same tests, plus which lines they ran; open htmlcov/index.html for the line-by-line view
.PHONY: coverage
coverage:
$(MAKE) create_db DB_NAME=mis_test
$(UV) run --env-file .env pytest --cov --cov-report=term --cov-report=html $(ARGS)
.PHONY: vendor
vendor:
mkdir -p $(VENDOR_DIR)/htmx $(VENDOR_DIR)/alpinejs
curl -fsSL https://cdn.jsdelivr.net/npm/htmx.org@$(HTMX_VERSION)/dist/htmx.min.js -o $(VENDOR_DIR)/htmx/htmx.min.js
curl -fsSL https://cdn.jsdelivr.net/npm/@alpinejs/csp@$(ALPINE_VERSION)/dist/cdn.min.js -o $(VENDOR_DIR)/alpinejs/alpine.min.js
mkdir -p $(VENDOR_DIR)/bc-sans
for f in fonts/BCSans-Regular.woff2 fonts/BCSans-Bold.woff2 LICENSE_OFL.txt; do \
curl -fsSL https://cdn.jsdelivr.net/npm/@bcgov/bc-sans@$(BC_SANS_VERSION)/$$f -o $(VENDOR_DIR)/bc-sans/$$(basename $$f); \
done
.PHONY: release
release:
@yarn install && yarn release-it