aboutsummaryrefslogtreecommitdiffstats
path: root/Makefile
blob: e8cfde3dc0de745fac111dd7c582621d96613e60 (plain) (blame)
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
SHELL := /bin/sh

IMAGE ?= kj-diagramming:latest
BUILD_DIR := excalidraw-app/build
PROJECT_MARKER := excalidraw-app/App.tsx
CONTAINER_NAME ?= kj-diagramming
HOST_PORT ?= 8000
CONTAINER_PORT ?= 8000
CONTAINER_ENGINE := $(shell if command -v docker >/dev/null 2>&1; then printf '%s' docker; elif command -v podman >/dev/null 2>&1; then printf '%s' podman; fi)

.PHONY: help build clean container serve stop check-yarn check-container-engine check-build-dir

help:
	@echo "targets:"
	@echo "  make build      build static assets into $(BUILD_DIR)"
	@echo "  make clean      rm $(BUILD_DIR) (with checks)"
	@echo "  make container  build kj-diagramming image"
	@echo "  make serve      run $(CONTAINER_NAME) in daemon mode"
	@echo "  make stop       stop $(CONTAINER_NAME)"

check-yarn:
	@if ! command -v yarn >/dev/null 2>&1; then \
		echo "warning: yarn is not installed."; \
		echo "install yarn to use: make build"; \
	fi

check-container-engine:
	@if [ -z "$(CONTAINER_ENGINE)" ]; then \
		echo "error: neither docker nor podman is installed."; \
		echo "install docker or podman, then run: make container"; \
		exit 1; \
	fi
	@echo "using container engine: $(CONTAINER_ENGINE)"

check-build-dir:
	@if [ ! -d "$(BUILD_DIR)" ]; then \
		echo "error: $(BUILD_DIR) is missing."; \
		echo "run: make build"; \
		exit 1; \
	fi

build:
	@if ! command -v yarn >/dev/null 2>&1; then \
		echo "error: yarn is not installed."; \
		echo "install yarn, then run: make build"; \
		exit 1; \
	fi
	@echo "building static assets into $(BUILD_DIR)..."
	@NODE_ENV=production yarn build:app:docker

clean:
	@if [ -f "$(PROJECT_MARKER)" ] || [ -d "$(BUILD_DIR)" ]; then \
		if [ -d "$(BUILD_DIR)" ]; then \
			echo "removing $(BUILD_DIR)..."; \
			rm -rf "$(BUILD_DIR)"; \
		else \
			echo "nothing to clean: $(BUILD_DIR) does not exist."; \
		fi; \
	else \
		echo "safety check failed: not in kj-diagramming repo root and $(BUILD_DIR) was not found."; \
		echo "refusing to run rm."; \
		exit 1; \
	fi

container: check-yarn check-container-engine clean build
	@echo "building image $(IMAGE)..."
	@$(CONTAINER_ENGINE) build -f Dockerfile -t "$(IMAGE)" .

serve: check-container-engine
	@set -eu; \
	engine="$(CONTAINER_ENGINE)"; \
	name="$(CONTAINER_NAME)"; \
	image="$(IMAGE)"; \
	host_port="$(HOST_PORT)"; \
	container_port="$(CONTAINER_PORT)"; \
	if ! $$engine image inspect "$$image" >/dev/null 2>&1; then \
		echo "error: image $$image was not found."; \
		echo "run: make container"; \
		exit 1; \
	fi; \
	if $$engine ps --format '{{.Names}}' | grep -Fxq "$$name"; then \
		echo "error: container $$name is already running."; \
		echo "run: make stop"; \
		exit 1; \
	fi; \
	if command -v ss >/dev/null 2>&1; then \
		while ss -ltn "sport = :$$host_port" 2>/dev/null | grep -q LISTEN; do \
			host_port=$$((host_port + 1)); \
		done; \
	elif command -v lsof >/dev/null 2>&1; then \
		while lsof -nP -iTCP:$$host_port -sTCP:LISTEN >/dev/null 2>&1; do \
			host_port=$$((host_port + 1)); \
		done; \
	elif command -v netstat >/dev/null 2>&1; then \
		while netstat -ltn 2>/dev/null | awk '{print $$4}' | grep -Eq "[:.]$$host_port$$"; do \
			host_port=$$((host_port + 1)); \
		done; \
	else \
		echo "warning: no port-check utility found (ss, lsof, netstat)."; \
		echo "trying host port $$host_port without pre-check."; \
	fi; \
	echo "starting $$name on http://localhost:$$host_port"; \
	$$engine run -d --rm --name "$$name" -p "$$host_port:$$container_port" "$$image" >/dev/null; \
	echo "started $$name (http://localhost:$$host_port)"

stop: check-container-engine
	@if $(CONTAINER_ENGINE) ps --format '{{.Names}}' | grep -Fxq "$(CONTAINER_NAME)"; then \
		echo "stopping $(CONTAINER_NAME)..."; \
		$(CONTAINER_ENGINE) stop "$(CONTAINER_NAME)" >/dev/null; \
		echo "stopped $(CONTAINER_NAME)."; \
	else \
		echo "container $(CONTAINER_NAME) is not running."; \
	fi