# Makefile for Go project

default: build

# All target
all: ui-user
	$(MAKE) build

ui: ui-user ui-user-node

ui-user:
	cd ui/user && \
	pnpm install && \
	pnpm run build

ui-user-node:
	cd ui/user && \
	pnpm install && \
	BUILD=node pnpm run build

clean:
	rm -rf ui/admin/build
	rm -rf ui/user/build

serve-docs:
	cd docs && \
	npm install && \
	npm run start

# Build the project

GIT_TAG := $(shell git describe --tags --exact-match 2>/dev/null | xargs -I {} echo -X 'github.com/obot-platform/obot/pkg/version.Tag={}')
GO_LD_FLAGS := "-s -w $(GIT_TAG)"
# Obot does not load Kustomize Go plugins; omit their dynamic symbol overhead.
GO_BUILD_TAGS := kustomize_disable_go_plugin_support
build:
	go build -trimpath -tags=$(GO_BUILD_TAGS) -ldflags=$(GO_LD_FLAGS) -o bin/obot .

dev:
	./tools/dev.sh $(ARGS)

dev-open: ARGS=--open-uis
dev-open: dev

otel-jaeger-up:
	docker compose -f tools/jaeger-compose.yaml up -d

otel-jaeger-down:
	docker compose -f tools/jaeger-compose.yaml down

otel-jaeger-logs:
	docker compose -f tools/jaeger-compose.yaml logs -f

telepresence-setup:
	kubectl create deployment obot-upstream --image=alpine --dry-run=client -o yaml -- sleep infinity | kubectl apply -f -
	kubectl create service clusterip obot-upstream --tcp=8080:8080 --dry-run=client -o yaml | kubectl apply -f -
	kubectl patch svc obot-upstream --type='json' -p='[{"op":"replace","path":"/spec/ports/0/name","value":"http"}]'
	kubectl apply -f tools/obot-proxy.yaml
	telepresence quit -s
	telepresence connect
	kubectl rollout restart deployment/obot-upstream
	telepresence intercept obot-upstream -p 8080:8080

# Lint the project
lint: lint-go

tidy:
	go mod tidy

GOLANGCI_LINT_VERSION ?= v2.13.0
setup-env:
	if ! command -v golangci-lint >/dev/null 2>&1; then \
		echo "Could not find golangci-lint, installing version $(GOLANGCI_LINT_VERSION)."; \
		go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION); \
	fi

lint-go: setup-env
	golangci-lint run

generate:
	go generate

test:
	go test -v -cover ./...
	cd apiclient && go test -v -cover ./... && cd ..
	cd logger && go test -v -cover ./... && cd ..

# Integration tests start an isolated obot server and require Docker plus npm
# and GHCR registry access.
test-integration:
	go test -count=1 -v -tags=integration -timeout=10m ./tests/integration/...

# Runs Go linters and validates that all generated code is committed.
validate-go-code: tidy generate lint-go no-changes

no-changes:
	@if [ -n "$$(git status --porcelain)" ]; then \
		git status --porcelain; \
		git --no-pager diff; \
		echo "Encountered dirty repo!"; \
		exit 1; \
	fi

#cut a new version for release with items in docs/docs
gen-docs-release:
	@if ! printf '%s\n' "${version}" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+$$'; then \
		echo "invalid version (expected version=vX.Y.Z)"; \
		exit 1; \
	fi
	docker run --rm --workdir=/docs -v $${PWD}/docs:/docs node:24-bookworm yarn docusaurus docs:version ${version}

# Completely remove doc version from docs site
remove-docs-version:
	@if ! printf '%s\n' "${version}" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+$$'; then \
		echo "invalid version (expected version=vX.Y.Z)"; \
		exit 1; \
	fi
	echo "removing ${version} from documentation completely"
	rm -f "./docs/versioned_sidebars/version-${version}-sidebars.json"
	rm -rf "./docs/versioned_docs/version-${version}"
	jq 'del(.[] | select(. == "${version}"))' ./docs/versions.json > tmp.json && mv tmp.json ./docs/versions.json

.PHONY: ui ui-user build all clean dev dev-open otel-jaeger-up otel-jaeger-down otel-jaeger-logs telepresence-setup lint lint-admin lint-api test-integration no-changes fmt tidy gen-docs-release deprecate-docs-release remove-docs-version
