-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMakefile
More file actions
118 lines (101 loc) · 4 KB
/
Makefile
File metadata and controls
118 lines (101 loc) · 4 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
# Usage:
# make setup # Create/update .venv and install dependencies
# make generate # Regenerate SDK code from proto sources
# make lint # Run static checks
# make test # Run unit tests
SHELL := /bin/bash
PYTHON ?= python3
VENV_DIR := .venv
VENV_PYTHON := $(VENV_DIR)/bin/python
VENV_PIP := $(VENV_PYTHON) -m pip
PROTO_REPO_URL := https://github.com/scalekit-inc/scalekit.git
PROTO_REF ?= v0.1.114.0
PROTO_SUBDIR := proto
TEMP_DIR := temp_scalekit
SCALEKIT_DIR := scalekit
BUF_DIR := buf
GOOGLE_DIR := google
PROTO_DIR := proto
PROTOC_DIR := protoc_gen_openapiv2
.PHONY: setup generate lint test tools-check create-venv prepare buf_generate restore generate_init_files cleanup copy_proto_dir
setup: create-venv
@echo "Installing SDK dependencies in $(VENV_DIR)..."
$(VENV_PIP) install --upgrade pip
$(VENV_PIP) install -e .
create-venv:
@if [ ! -d "$(VENV_DIR)" ]; then \
echo "Creating virtual environment at $(VENV_DIR)..."; \
$(PYTHON) -m venv $(VENV_DIR); \
else \
echo "Using existing virtual environment at $(VENV_DIR)..."; \
fi
tools-check:
@command -v git >/dev/null 2>&1 || (echo "missing git" && exit 1)
@command -v rsync >/dev/null 2>&1 || (echo "missing rsync" && exit 1)
@command -v buf >/dev/null 2>&1 || (echo "missing buf. install buf (https://buf.build/docs/installation/) and rerun 'make generate'" && exit 1)
generate: tools-check
@echo "Step 1: Fetching proto sources from $(PROTO_REPO_URL) at ref $(PROTO_REF)..."
@set -euo pipefail; \
tmp_dir="$$(mktemp -d)"; \
prepared=0; \
cleanup_tmp() { rm -rf "$$tmp_dir"; rm -f .dirpath; }; \
rollback_if_needed() { \
if [ "$$prepared" -eq 1 ] && [ -d "$(TEMP_DIR)" ]; then \
echo "Generation failed; restoring $(SCALEKIT_DIR) from $(TEMP_DIR)..."; \
rsync -a "$(TEMP_DIR)/" "$(SCALEKIT_DIR)/"; \
fi; \
rm -f buf.yaml buf.lock; \
if [ -f buf.work.yaml.bak ]; then mv buf.work.yaml.bak buf.work.yaml; fi; \
}; \
trap 'rollback_if_needed; cleanup_tmp' EXIT; \
git clone --depth=1 --branch "$(PROTO_REF)" "$(PROTO_REPO_URL)" "$$tmp_dir/scalekit"; \
echo "$$tmp_dir/scalekit/$(PROTO_SUBDIR)" > .dirpath; \
$(MAKE) copy_proto_dir; \
$(MAKE) prepare; prepared=1; \
$(MAKE) buf_generate; \
$(MAKE) restore; prepared=0; \
$(MAKE) generate_init_files; \
$(MAKE) cleanup
@echo "Code generation complete."
copy_proto_dir:
@proto_dir=$$(cat .dirpath); \
echo "Step 2: Syncing proto files from $$proto_dir"; \
rsync -av "$$proto_dir/" proto/; \
repo_root=$$(dirname "$$proto_dir"); \
if [ -f "$$repo_root/buf.yaml" ]; then \
echo "Copying buf.yaml from repo root and suspending buf.work.yaml..."; \
cp "$$repo_root/buf.yaml" buf.yaml; \
[ -f "$$repo_root/buf.lock" ] && cp "$$repo_root/buf.lock" buf.lock || true; \
mv buf.work.yaml buf.work.yaml.bak; \
fi
prepare:
@echo "Step 3: Preparing temp folder and preserving current scalekit package..."
rm -rf $(TEMP_DIR)
mkdir -p $(TEMP_DIR)
rsync -av --exclude 'v1' --delete $(SCALEKIT_DIR)/ $(TEMP_DIR)/
rm -rf $(BUF_DIR) $(SCALEKIT_DIR)
buf_generate:
@echo "Step 4: Running 'buf generate --include-imports'..."
buf generate --include-imports
restore:
@echo "Step 5: Restoring scalekit package non-generated files..."
rsync -av $(TEMP_DIR)/ $(SCALEKIT_DIR)/
rm -rf $(TEMP_DIR)
generate_init_files:
@echo "Step 6: Ensuring package __init__.py files exist..."
find $(BUF_DIR) -type d -exec touch {}/__init__.py \;
find $(SCALEKIT_DIR)/v1 -type d -exec touch {}/__init__.py \;
touch $(SCALEKIT_DIR)/common/__init__.py
touch $(SCALEKIT_DIR)/constants/__init__.py
touch $(SCALEKIT_DIR)/utils/__init__.py
cleanup:
@echo "Step 7: Cleaning temporary generated source directories..."
rm -rf $(GOOGLE_DIR) $(PROTO_DIR) $(PROTOC_DIR)
rm -f .dirpath buf.yaml buf.lock
@if [ -f buf.work.yaml.bak ]; then mv buf.work.yaml.bak buf.work.yaml; fi
lint: create-venv
@echo "Running static checks..."
$(VENV_PYTHON) -m compileall -q scalekit tests
test: create-venv
@echo "Running tests..."
$(VENV_PYTHON) -m unittest discover -s tests -p "test_*.py" -v