UNAME_S := $(shell uname -s)
BEAGLE_DB_USE_DEVPACK ?= False
devpack := BEAGLE_DB_USE_DEVPACK=$(BEAGLE_DB_USE_DEVPACK)
migrations_root := /tmp/beagle-dev-migrations-root
arcadia_root := $(shell ya dump root)
params := $(wordlist 2,100,$(MAKECMDGOALS))
source_root := Y_PYTHON_SOURCE_ROOT=$(arcadia_root)
entry_point := Y_PYTHON_ENTRY_POINT=":main"
pytest_args := -m pytest -p no:warnings -sv

# Code style
check-isort:
	isort --recursive --check-only --diff beagle/

check-flake8:
	flake8 beagle/

check-style: check-flake8 check-isort

sort-imports:
	isort --recursive beagle/

mypy:
	MYPYPATH=$(arcadia_root) mypy -p beagle --show-traceback --disallow-incomplete-defs

cc: sort-imports check-style mypy


# Tests
test:
	$(devpack) ya make $(ARGS) -tt --test-stdout

test-unit:
	$(devpack) ya make $(ARGS) -tt --test-stdout beagle/tests/unit

test-functional:
	$(devpack) ya make $(ARGS) -tt --test-stdout beagle/tests/functional

dev-unit:
	$(devpack) $(source_root) $(entry_point) ./beagle/tests/unit/mail-beagle-beagle-tests-unit $(pytest_args) $(if $(params),$(params),beagle/tests/unit)

dev-functional:
	$(devpack) $(source_root) $(entry_point) ./beagle/tests/functional/mail-beagle-beagle-tests-functional $(pytest_args) $(if $(params),$(params),beagle/tests/functional)


# Development
deps:
	ya make --checkout beagle && ya make --checkout beagle/tests

build_bin:
	ya make bin

build_docker:
	ya package --docker --docker-repository mail/beagle/beagle package/package.json

run_bin: build_bin
	./bin/bin $(ARGS)

l_run_bin: build_bin
	./run_local.sh ./bin/bin $(ARGS)

runserver: build_bin
	./bin/bin runserver

l_runserver:
	./run_local.sh make runserver

wl_runserver:
	./run_local.sh -w make runserver

runworkers: build_bin
	./bin/bin runworkers

l_runworkers:
	./run_local.sh make runworkers

wl_runworkers:
	./run_local.sh -w make runworkers

runmock_directory: build_bin
	./bin/bin runmock --mock directory

shell: build_bin
	./bin/bin shell

l_shell:
	./run_local.sh make shell

tunnel:
	ssh -D 4444 dev -N

# Database
start_db:
ifeq ($(UNAME_S),Linux)
	# докер в убунте не может делать bind volume на директорию аркадии, смонтированной через arc
	rm -rf $(migrations_root) && mkdir -p $(migrations_root) && cp -r ./postgre $(migrations_root)
	BEAGLE_MIGRATIONS_ROOT=$(migrations_root) docker-compose up -d beagle_db_migrated
else
	docker-compose up -d beagle_db_migrated
endif

pgcli:
	PGPASSWORD="P@ssw0rd" pgcli -h localhost -p 5252 -U beagle -d beagle_db
