.PHONY: dev test race build lint lint-tools vulncheck proto proto-go proto-python proto-tools proto-check yaml-tools validate-yaml validate-manifests python-check web-install web-build verify compose-up compose-down integration-smoke loadtest

PROTOC_GEN_GO_VERSION := v1.36.5
PROTOC_GEN_GO_GRPC_VERSION := v1.5.1
GRPCIO_TOOLS_VERSION := 1.71.0
GOLANGCI_LINT_VERSION := v2.12.2
GOVULNCHECK_VERSION := v1.7.0
YAMLLINT_VERSION := 1.37.1
PROTO_FILE := api/proto/configcenter/v1/config.proto
PROTO_GO_FILES := pkg/proto/v1/config.pb.go pkg/proto/v1/config_grpc.pb.go
PROTO_PY_FILES := sdk/python/configcenter/v1/config_pb2.py sdk/python/configcenter/v1/config_pb2_grpc.py
PROTO_TOOLS_STAMP := .tools/.proto-tools-$(PROTOC_GEN_GO_VERSION)-$(PROTOC_GEN_GO_GRPC_VERSION)-$(GRPCIO_TOOLS_VERSION)
LINT_TOOLS_STAMP := .tools/.lint-tools-$(GOLANGCI_LINT_VERSION)-$(GOVULNCHECK_VERSION)
YAML_TOOLS_STAMP := .tools/.yaml-tools-$(YAMLLINT_VERSION)

dev:
	go run ./cmd/server

test:
	GOCACHE=/tmp/configcenter-go-cache go test ./...
	cd pkg/sdk/gohttp && GOCACHE=/tmp/configcenter-gohttp-cache go test ./...

race:
	GOCACHE=/tmp/configcenter-go-cache go test -race ./...
	cd pkg/sdk/gohttp && GOCACHE=/tmp/configcenter-gohttp-cache go test -race ./...

build:
	mkdir -p bin
	GOCACHE=/tmp/configcenter-go-cache go build -buildvcs=false -trimpath -o bin/configcenter ./cmd/server

lint-tools: $(LINT_TOOLS_STAMP)

$(LINT_TOOLS_STAMP):
	mkdir -p .tools/bin
	GOBIN=$(CURDIR)/.tools/bin go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
	GOBIN=$(CURDIR)/.tools/bin go install golang.org/x/vuln/cmd/govulncheck@$(GOVULNCHECK_VERSION)
	touch $(LINT_TOOLS_STAMP)

lint: lint-tools
	$(CURDIR)/.tools/bin/golangci-lint run ./...
	cd pkg/sdk/gohttp && $(CURDIR)/.tools/bin/golangci-lint run -c $(CURDIR)/.golangci.yml ./...

vulncheck: lint-tools
	$(CURDIR)/.tools/bin/govulncheck ./...
	cd pkg/sdk/gohttp && $(CURDIR)/.tools/bin/govulncheck ./...

proto-tools: $(PROTO_TOOLS_STAMP)

$(PROTO_TOOLS_STAMP):
	rm -rf .tools/bin .tools/python
	mkdir -p .tools/bin .tools/python
	GOBIN=$(CURDIR)/.tools/bin go install google.golang.org/protobuf/cmd/protoc-gen-go@$(PROTOC_GEN_GO_VERSION)
	GOBIN=$(CURDIR)/.tools/bin go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@$(PROTOC_GEN_GO_GRPC_VERSION)
	python3 -m pip install --disable-pip-version-check --target .tools/python grpcio-tools==$(GRPCIO_TOOLS_VERSION)
	touch $(PROTO_TOOLS_STAMP)

proto-go: proto-tools
	PATH="$(CURDIR)/.tools/bin:$$PATH" protoc -I api/proto \
		--go_out=. --go_opt=module=github.com/longpeng/configcenter \
		--go-grpc_out=. --go-grpc_opt=module=github.com/longpeng/configcenter \
		$(PROTO_FILE)

proto-python: proto-tools
	PYTHONPATH="$(CURDIR)/.tools/python" python3 -m grpc_tools.protoc -I api/proto \
		--python_out=sdk/python --grpc_python_out=sdk/python $(PROTO_FILE)
	touch sdk/python/configcenter/v1/__init__.py

proto: proto-go proto-python

proto-check: proto
	git diff --exit-code -- $(PROTO_FILE) $(PROTO_GO_FILES) $(PROTO_PY_FILES) sdk/python/configcenter/v1/__init__.py

yaml-tools: $(YAML_TOOLS_STAMP)

$(YAML_TOOLS_STAMP):
	rm -rf .tools/yamllint
	mkdir -p .tools/yamllint
	python3 -m pip install --disable-pip-version-check --target .tools/yamllint yamllint==$(YAMLLINT_VERSION)
	touch $(YAML_TOOLS_STAMP)

validate-yaml: yaml-tools
	PYTHONPATH="$(CURDIR)/.tools/yamllint" python3 -m yamllint -c .yamllint.yml compose.yaml deploy .github/workflows

validate-manifests: validate-yaml
	docker compose config --quiet
	sh scripts/check-migrations.sh

python-check: proto-tools
	PYTHONPATH="$(CURDIR)/.tools/python:$(CURDIR)/sdk/python" python3 -m compileall -q sdk/python/configcenter

web-install:
	npm --prefix web ci

web-build: web-install
	npm --prefix web run build

verify: proto-check test race lint vulncheck python-check web-build validate-manifests

compose-up:
	docker compose up --build

compose-down:
	docker compose down

integration-smoke:
	sh scripts/integration-smoke.sh

loadtest:
	GOCACHE=/tmp/configcenter-go-cache go run ./cmd/loadtest -duration=30s -concurrency=32
