include makefiles/const.mk
include makefiles/dependency.mk
include makefiles/release.mk
include makefiles/develop.mk
include makefiles/build.mk
include makefiles/e2e.mk

.DEFAULT_GOAL := all
all: build

# ==============================================================================
# Targets

## test: Run tests
test: unit-test-core test-cli-gen
	@$(OK) unit-tests pass

## test-cli-gen: Run the unit tests for cli gen
test-cli-gen: 
	@mkdir -p ./bin/doc
	@go run ./hack/docgen/cli/gen.go ./bin/doc

## unit-test-core: Run the unit tests for core
unit-test-core:
	go test -coverprofile=coverage.txt $(shell go list ./pkg/... ./cmd/... ./apis/... | grep -v apiserver | grep -v applicationconfiguration)
	go test $(shell go list ./references/... | grep -v apiserver)

## build: Build vela cli binary
build: vela-cli kubectl-vela
	@$(OK) build succeed

## build-cli: Clean build
build-cleanup:
	@echo "===========> Cleaning all build output"
	@rm -rf _bin

## fmt: Run go fmt against code
fmt: goimports installcue
	go fmt ./...
	$(GOIMPORTS) -local github.com/oam-dev/kubevela -w $$(go list -f {{.Dir}} ./...)
	$(CUE) fmt ./vela-templates/definitions/internal/*
	$(CUE) fmt ./vela-templates/definitions/deprecated/*
	$(CUE) fmt ./vela-templates/definitions/registry/*
	$(CUE) fmt ./pkg/stdlib/pkgs/*
	$(CUE) fmt ./pkg/stdlib/op.cue
	$(CUE) fmt ./pkg/workflow/tasks/template/static/*

## sdk_fmt: Run go fmt against code
sdk_fmt:
	./hack/sdk/reviewable.sh

## vet: Run go vet against code
vet:
	@$(INFO) go vet
	@go vet $(shell go list ./...|grep -v scaffold)

## staticcheck: Run the staticcheck
staticcheck: staticchecktool
	@$(INFO) staticcheck
	@$(STATICCHECK) $(shell go list ./...|grep -v scaffold)

## lint: Run the golangci-lint
lint: golangci
	@$(INFO) lint
	@$(GOLANGCILINT) run --skip-dirs 'scaffold'

## reviewable: Run the reviewable
reviewable: manifests fmt vet lint staticcheck helm-doc-gen sdk_fmt
	go mod tidy

# check-diff: Execute auto-gen code commands and ensure branch is clean.
check-diff: reviewable
	git --no-pager diff
	git diff --quiet || ($(ERR) please run 'make reviewable' to include all changes && false)
	@$(OK) branch is clean

## docker-push: Push the docker image
docker-push:
	@echo "===========> Pushing docker image"
	@docker push $(VELA_CORE_IMAGE)

## image-cleanup: Delete Docker images
image-cleanup:
ifneq (, $(shell which docker))
# Delete Docker images

ifneq ($(shell docker images -q $(VELA_CORE_TEST_IMAGE)),)
	docker rmi -f $(VELA_CORE_TEST_IMAGE)
endif

ifneq ($(shell docker images -q $(VELA_RUNTIME_ROLLOUT_TEST_IMAGE)),)
	docker rmi -f $(VELA_RUNTIME_ROLLOUT_TEST_IMAGE)
endif

endif

## image-load: load docker image to the kind cluster
image-load:
	docker build -t $(VELA_CORE_TEST_IMAGE) -f Dockerfile.e2e .
	kind load docker-image $(VELA_CORE_TEST_IMAGE) || { echo >&2 "kind not installed or error loading image: $(VELA_CORE_TEST_IMAGE)"; exit 1; }

## image-load-runtime-cluster: Load the run-time cluster image
image-load-runtime-cluster:
	/bin/sh hack/e2e/build_runtime_rollout.sh
	docker build -t $(VELA_RUNTIME_ROLLOUT_TEST_IMAGE) -f runtime/rollout/e2e/Dockerfile.e2e runtime/rollout/e2e/
	rm -rf runtime/rollout/e2e/tmp
	kind load docker-image $(VELA_RUNTIME_ROLLOUT_TEST_IMAGE)  || { echo >&2 "kind not installed or error loading image: $(VELA_RUNTIME_ROLLOUT_TEST_IMAGE)"; exit 1; }
	kind load docker-image $(VELA_RUNTIME_ROLLOUT_TEST_IMAGE) --name=$(RUNTIME_CLUSTER_NAME) || echo "no worker cluster"

## core-test: Run tests
core-test:
	go test ./pkg/... -coverprofile cover.out

## manager: Build vela core manager binary
manager:
	$(GOBUILD_ENV) go build -o bin/manager -a -ldflags $(LDFLAGS) ./cmd/core/main.go

## vela-runtime-rollout-manager: Build vela runtime rollout manager binary
vela-runtime-rollout-manager:
	$(GOBUILD_ENV) go build -o ./runtime/rollout/bin/manager -a -ldflags $(LDFLAGS) ./runtime/rollout/cmd/main.go

## manifests: Generate manifests e.g. CRD, RBAC etc.
manifests: installcue kustomize
	go generate $(foreach t,pkg apis,./$(t)/...)
	# TODO(yangsoon): kustomize will merge all CRD into a whole file, it may not work if we want patch more than one CRD in this way
	$(KUSTOMIZE) build config/crd -o config/crd/base/core.oam.dev_applications.yaml
	./hack/crd/cleanup.sh
	go run ./hack/crd/dispatch/dispatch.go config/crd/base charts/vela-core/crds
	rm -f config/crd/base/*
	./vela-templates/gen_definitions.sh


HOSTOS := $(shell uname -s | tr '[:upper:]' '[:lower:]')
HOSTARCH := $(shell uname -m)
ifeq ($(HOSTARCH),x86_64)
HOSTARCH := amd64
endif


## check-license-header: Check license header
check-license-header:
	./hack/licence/header-check.sh

## def-gen: Install definitions
def-install:
	./hack/utils/installdefinition.sh

## helm-doc-gen: Generate helm chart README.md
helm-doc-gen: helmdoc
	readme-generator -v charts/vela-core/values.yaml -r charts/vela-core/README.md

## help: Display help information
help: Makefile
	@echo ""
	@echo "Usage:"
	@echo ""
	@echo "  make [target]"
	@echo ""
	@echo "Targets:"
	@echo ""
	@awk -F ':|##' '/^[^\.%\t][^\t]*:.*##/{printf "  \033[36m%-20s\033[0m %s\n", $$1, $$NF}' $(MAKEFILE_LIST) | sort
	@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' |  sed -e 's/^/ /'
