UNAME_S := $(shell uname -s)
OHIO_DB_USE_DEVPACK ?= False
devpack := OHIO_DB_USE_DEVPACK=$(OHIO_DB_USE_DEVPACK)
migrations_root := /tmp/ohio-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 ohio/

check-flake8:
	flake8 ohio/

check-style: check-flake8 check-isort

sort-imports:
	isort --recursive ohio/

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

cc: sort-imports check-style mypy


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

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

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

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

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


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

build_bin:
	ya make bin

build_docker: build_bin
	ya package package/package.json --docker --docker-repository mail/ohio --custom-version 0.0.0-$(shell date +"%s")

runserver:
	$(source_root) ./bin/bin runserver

l_runserver:
	./run_local.sh make runserver

wl_runserver:
	./run_local.sh -w make runserver

shell:
	$(source_root) ./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)
	OHIO_MIGRATIONS_ROOT=$(migrations_root) docker-compose up -d ohio_db ohio_db_migrated
else
	docker-compose up -d ohio_db ohio_db_migrated
endif

pgcli:
	PGPASSWORD="P@ssw0rd" pgcli -h localhost -p 5253 -U ohio -d ohio_db

yadeploy-testing:
	ya tool dctl put stage deploy/ohio-testing.deploy.yaml

yadeploy-testing-mimino:
	ya tool dctl put stage deploy/ohio-testing-mimino.deploy.yaml

yadeploy-production:
	ya tool dctl publish-draft stage deploy/ohio-production.deploy.yaml
