SHELL := /bin/sh
.DEFAULT_GOAL := help
.SUFFIXES:
.PHONY: test

# help: @ Lists available make tasks
help:
	@egrep -oh '[0-9a-zA-Z_\.\-]+:.*?@ .*' $(MAKEFILE_LIST) | \
	awk 'BEGIN {FS = ":.*?@ "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' | sort

# clean: @ Cleans entire project, deleting all docker images/volumes
clean: clean.artifacts clean.elixir clean.db

# clean.artifacts: @ Deletes all build artifacts
clean.artifacts:
	rm -r _build coverdeps deps doc erl_crash.dump elixir-*.tar
	find -name '*.ez' -exec rm {} +

# clean.elixir: @ Deletes elixir docker image
clean.elixir:
	docker compose stop elixir
	docker compose rm -v elixir
	docker image rm -f pageantry_elixir

# clean.elixir: @ Deletes db docker image
clean.db:
	docker compose stop db
	docker compose rm -v db
	docker volume rm -f db

# rebuild.elixir: @ Rebuilds elixir docker image
rebuild.elixir:
	docker compose stop elixir
	docker compose build --pull elixir

# check: @ Runs all tests and linters
check: lint test

# lint: @ Runs linting
lint: lint.format lint.elixir lint.credo lint.sobelow

# lint.format: @ Runs format linting
lint.format:
	docker compose run --rm elixir mix format --check-formatted $(SRC)

# lint.elixir: @ Runs compiler warnings
lint.elixir:
	docker compose run --rm elixir mix compile --all-warnings --warnings-as-errors

# lint.credo: @ Runs credo linting
lint.credo:
	docker compose run --rm elixir mix credo $(SRC) --strict

# lint.sobelow: @ Runs sobelow linting
lint.sobelow:
	docker compose run --rm elixir mix sobelow --config

# test: @ Runs all tests
test:
	docker compose run --rm elixir mix test $(TEST)

# coverage: @ Runs tests with coverage output
# COVERAGE=.detail for detail, COVERAGE=.html for html output
coverage:
	docker compose run --rm elixir mix coveralls$(COVERAGE)

# watch: @ Runs all tests in "watch" mode
watch:
	docker compose run --rm elixir mix test.watch $(TEST)

# run: @ Runs server
run: run.db

# run: @ Starts db
run.db:
	docker compose up -d db

# shell: @ Opens bash shell in elixir environment
shell: CMD ?= /bin/bash
shell:
	docker compose run --rm elixir $(CMD)

# shell.db: @ Opens psql shell to db
shell.db: run.db
	docker exec -u postgres -it elixir_db_1 psql

# shell: @ Opens iex shell in elixir environment
iex:
	docker compose run --rm -e MIX_ENV=test elixir iex -S mix
