CACHE_PORT ?= 6379
CACHE_URL ?= redis://localhost:$(CACHE_PORT)
API_KEY ?=
API_ROOT_URL ?=
APP_NAME ?=
export

run_redis = docker run --rm -d --name worker-redis -p $(CACHE_PORT):$(CACHE_PORT) redis:7.4-alpine
stop_redis = docker stop worker-redis

srcs = worker testcase
isort = isort $(srcs)
black = black $(srcs)

## help: Show this help.
.PHONY: help
help: Makefile
	@sed -n 's/^##\s//p' $<

## install: Install all requirements.
.PHONY: install
install: install-worker install-extra

## install-worker: Install worker requirements.
.PHONY: install-worker
install-worker:
	@pip install -e .

## install-extra: Install extra requirements.
.PHONY: install-extra
install-extra:
	@pip install --upgrade -r requirements_extra.txt

## test: Run tests and show code coverage.
.PHONY: test
test: test-worker test-lambda-app
	@coverage report

## test-worker: Run worker tests.
.PHONY: test-worker
test-worker:
	@$(run_redis)
	@coverage run --branch --source=worker/ -m unittest -v
	@$(stop_redis)

## test-lambda-app: Run specific lambda app test.
.PHONY: test-lambda-app
test-lambda-app:
	@$(run_redis)
	@python testcase/run_lambda_app.py -a 3083 -s 1502041854 -e 1502043349 -i 600 -d true
	@$(stop_redis)

## store-test-coverage: Run tests and store coverage.
.PHONY: store-test-coverage
store-test-coverage:
	@-python3 .github/workflows/code_coverage.py

## lint: Run linter.
.PHONY: lint
lint:
	@flake8 $(srcs)
	@$(isort) --check
	@$(black) --check

## format: Format all files.
.PHONY: format
format:
	@$(black)
	@$(isort)

## clean: Clean autogenerated files.
.PHONY: clean
clean:
	@-python3 -Bc "for p in __import__('pathlib').Path('.').rglob('*.py[co]'): p.unlink()"
	@-python3 -Bc "for p in __import__('pathlib').Path('.').rglob('__pycache__'): p.rmdir()"
	@-rm -rf *.egg-info
	@-rm -rf .pytest_cache
	@-rm -rf htmlcov
	@-rm -rf .coverage
	@-rm -rf build
	@-rm -rf dist