.DEFAULT_GOAL := help

# Pretty print values cf. https://misc.flogisoft.com/bash/tip_colors_and_formatting
RESET := \033[0m # Reset all formatting
GREEN := \033[0;32m # Resets before setting 16b colour (32 -- green)
YELLOW := \033[0;33m
ORANGE := \033[0;38;5;208m # Reset then set 256b colour (208 -- orange)
PEACH := \033[0;38;5;216m


## ---------------------------------------------------------------------------------- ##
## ------------------------- Help usage builder ------------------------------------- ##
## ---------------------------------------------------------------------------------- ##
# use '# >>> Build commands' to create section
# use '# target: target description' to create help for target
.PHONY: help
help:
	@echo "Usage:"
	@cat $(MAKEFILE_LIST) | grep -E '^# >>>|^# [A-Za-z0-9_.-]+:' | sed -E 's/^# //' | awk ' \
		BEGIN { \
			green="\033[32m"; \
			yellow="\033[33m"; \
			reset="\033[0m"; \
			section=""; \
		} \
		/^>>>/ { \
			section=substr($$0, 5); \
			printf "\n" green ">>> %s" reset "\n", section; \
			next; \
		} \
		/^([A-Za-z0-9_.-]+):/ { \
			target=$$1; \
			gsub(/:$$/, "", target); \
			description=substr($$0, index($$0, ":") + 2); \
			if (description == "") { description="-"; } \
			printf " - " yellow "%-35s" reset " %s\n", target, description; \
		} \
	'

# required for pushd to work..
SHELL = /bin/bash

# >>> Tests commands

.PHONY: clean
# clean: Cleanup compiled and cache py files
clean:
	make test-clean
	find . -type f \( -iname '*.c' -o -iname '*.pyc' -o -iname '*.so' -o -iname '*.orig' \) -exec rm '{}' ';'
	find . -type d -name "build" -prune -exec rm -rf '{}' ';'


.PHONY: test
# test: run test-clean and tests
test:
	make test-clean
	make test-only


.PHONY: test-clean
# test-clean: run test-clean and tests
test-clean:
	rm -rf coverage.xml htmlcov junit.xml pylint.log result
	find . -type d -name "__pycache__" -prune -exec rm -rf '{}' ';'
	find . -type f \( -iname '.coverage.*' \) -exec rm '{}' ';'


.PHONY: test-only
# test-only: Run tests only without cleanup
test-only:
	PYTHONHASHSEED=random \
	py.test -x -vv -r xw -p no:sugar \
	--cov-report=term-missing --cov-report=html \
	--cov=rhodecode rhodecode \
	--ignore=rhodecode/tests/vcs_operations \
	--ignore=rhodecode/tests/database

	PYTHONHASHSEED=random \
		py.test -x -vv -r xw -p no:sugar \
		rhodecode/tests/vcs_operations

	PYTHONHASHSEED=random \
		py.test -x -vv -r xw -p no:sugar \
		rhodecode/tests/database

.PHONY: test-simple
# test-simple: Run tests only for main test suite without coverage
test-simple:
	PYTHONHASHSEED=random \
	py.test -x -vv -r xw -p no:sugar \
	--ignore=rhodecode/tests/vcs_operations \
	--ignore=rhodecode/tests/database


.PHONY: test-custom
# test-custom: Run tests without coverage, only run tests matching PATTERN
test-custom:
	PYTHONHASHSEED=random \
	py.test $(if $(PATTERN),-k "$(PATTERN)",) -q -r f --tb=short -p no:sugar \
	--ignore=rhodecode/tests/vcs_operations \
	--ignore=rhodecode/tests/database

.PHONY: test-changed
# test-changed: Run tests for changed files only
test-changed:
	@echo "Finding changed Python files..."
	@git diff --name-only HEAD~1 | grep '\.py$$' | grep -E '(test_|tests/)' > .changed_tests || true
	@if [ -s .changed_tests ]; then \
		echo "Running tests for changed files:"; \
		cat .changed_tests; \
		PYTHONHASHSEED=random py.test -v -p no:sugar $$(cat .changed_tests); \
	else \
		echo "No test files changed, running quick smoke tests..."; \
		make test-smoke; \
	fi
	@rm -f .changed_tests


.PHONY: test-fast
# test-fast: Fastest possible test execution for development using pytest-xdist parallelism
test-fast:
	make test-clean
	PYTHONHASHSEED=0 PYTHONWARNINGS=ignore py.test -n 4 -q -p no:sugar --tb=short \
	--ignore=rhodecode/apps/file_store/tests \
	--ignore=rhodecode/tests/vcs_operations \
	--ignore=rhodecode/tests/database \
	-k "not (test_api_merge_pull_request or test_api_merge_pull_request_as_another_user or (test_filestore_backends and objectstore) or test_merge_pull_request_enabled or test_merge_pull_request_forbidden_by_pre_push_hook or test_merge_rebase_source_is_updated_bookmark)"


.PHONY: test-dev
# test-dev: Run tests skipping known problematic tests (merge PR, objectstore backend issues)
test-dev:
	PYTHONHASHSEED=random PYTHONWARNINGS=ignore \
	py.test -q -p no:sugar --tb=short --log-cli-level=WARNING \
	--ignore=rhodecode/tests/vcs_operations \
	--ignore=rhodecode/tests/database \
	-k "not (test_api_merge_pull_request or test_api_merge_pull_request_as_another_user or (test_filestore_backends and objectstore) or test_merge_pull_request_enabled or test_merge_pull_request_forbidden_by_pre_push_hook or test_merge_rebase_source_is_updated_bookmark)" \
	rhodecode

# >>> Docs commands

.PHONY: docs
# docs: build docs
docs:
	(cd docs; docker run --rm -v $(PWD):/project --workdir=/project/docs sphinx-doc-build-rc make clean html SPHINXOPTS="-W")


.PHONY: docs-clean
# docs-clean: Cleanup docs
docs-clean:
	(cd docs; docker run --rm -v $(PWD):/project --workdir=/project/docs sphinx-doc-build-rc make clean)


.PHONY: docs-cleanup
# docs-cleanup: Cleanup docs
docs-cleanup:
	(cd docs; docker run --rm -v $(PWD):/project --workdir=/project/docs sphinx-doc-build-rc make cleanup)

# >>> Dev commands

.PHONY: web-build
# web-build: Build JS packages static/js
web-build:
	rm -rf node_modules
	docker run -it --rm -v $(PWD):/project --workdir=/project rhodecode/static-files-build:24 -c "npm install && /project/node_modules/.bin/grunt"
	# run static file check
	./rhodecode/tests/scripts/static-file-check.sh rhodecode/public/
	rm -rf node_modules


.PHONY: dev-sh
# dev-sh: make dev-sh
dev-sh:
	sudo echo "deb [trusted=yes] https://apt.fury.io/rsteube/ /" | sudo tee -a "/etc/apt/sources.list.d/fury.list"
	sudo apt-get update
	sudo apt-get install -y zsh carapace-bin
	rm -rf /home/rhodecode/.oh-my-zsh
	curl https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh
	@echo "source <(carapace _carapace)" > /home/rhodecode/.zsrc
	@echo "${RC_DEV_CMD_HELP}"
	@PROMPT='%(?.%F{green}√.%F{red}?%?)%f %B%F{240}%1~%f%b %# ' zsh


.PHONY: dev-cleanup
# dev-cleanup: Cleanup: pip freeze | grep -v "^-e" | grep -v "@" | xargs pip uninstall -y
dev-cleanup:
	pip freeze | grep -v "^-e" | grep -v "@" | xargs pip uninstall -y
	rm -rf /tmp/*


.PHONY: dev-env
# dev-env: make dev-env based on the requirements files and install develop of packages
## Cleanup: pip freeze | grep -v "^-e" | grep -v "@" | xargs pip uninstall -y
dev-env:
	sudo -u root chown rhodecode:rhodecode /home/rhodecode/.cache/pip/
	pip install build virtualenv
	pushd ../rhodecode-vcsserver/ && make dev-env && popd
	pip wheel --wheel-dir=/home/rhodecode/.cache/pip/wheels -r requirements.txt -r requirements_rc_tools.txt -r requirements_test.txt -r requirements_debug.txt
	pip install --no-index --find-links=/home/rhodecode/.cache/pip/wheels -r requirements.txt -r requirements_rc_tools.txt -r requirements_test.txt -r requirements_debug.txt
	pip install -e .


.PHONY: sh
# sh: shortcut for make dev-sh dev-env
sh:
	make dev-env
	make dev-sh


## Allows changes of workers e.g make dev-srv-g workers=2
workers?=1

.PHONY: dev-srv
# dev-srv: run gunicorn web server with reloader, use workers=N to set multiworker mode, workers=N allows changes of workers
dev-srv:
	gunicorn --paste=.dev/dev.ini --bind=0.0.0.0:10020 --config=.dev/gunicorn_config.py --timeout=120 --reload --workers=$(workers)

.PHONY: ruff-check
# ruff-check: run a ruff analysis
ruff-check:
	ruff check --exclude rhodecode/lib/dbmigrate --exclude .eggs  --exclude .dev .

.PHONY: ruff-format
# Get changed Python files
ruff-format:

	@echo "Finding changed Python files..."
	@git diff --name-only | grep '\.py$$' > .changed_files || true
	@if [ -s .changed_files ]; then \
		echo "Running Ruff format on the following files:"; \
		cat .changed_files; \
		cat .changed_files | xargs ruff format; \
	else \
		echo "No Python files changed."; \
	fi
	@rm -f .changed_files

.PHONY: upgrade-db
# upgrade-db: Apply database migrations using rc-upgrade-db
upgrade-db:
	/usr/local/bin/rhodecode_bin/bin/rc-upgrade-db \
	/home/rhodecode/rhodecode-enterprise-ce/.dev/dev.ini \
	--force-yes