# Makefile variables set automatically
plugin_id=`cat plugin.json | python -c "import sys, json; print(str(json.load(sys.stdin)['id']).replace('/',''))"`
plugin_version=`cat plugin.json | python -c "import sys, json; print(str(json.load(sys.stdin)['version']).replace('/',''))"`
archive_file_name="dss-plugin-${plugin_id}-${plugin_version}.zip"
remote_url=`git config --get remote.origin.url`
last_commit_id=`git rev-parse HEAD`

.DEFAULT_GOAL := plugin

plugin: dist-clean
	@echo "[START] Archiving plugin to dist/ folder..."
	@cat plugin.json | json_pp > /dev/null
	@mkdir dist
	@echo "{\"remote_url\":\"${remote_url}\",\"last_commit_id\":\"${last_commit_id}\"}" > release_info.json
	@git archive -v -9 --format zip -o dist/${archive_file_name} HEAD
	@if [[ -d tests ]]; then \
		zip --delete dist/${archive_file_name} "tests/*"; \
	fi
	@zip -u dist/${archive_file_name} release_info.json
	@rm release_info.json
	@echo "[SUCCESS] Archiving plugin to dist/ folder: Done!"

dev: dist-clean
	@echo "[START] Archiving plugin to dist/ folder... (dev mode)"
	@cat plugin.json | json_pp > /dev/null
	@mkdir dist
	@zip -v -9 dist/${archive_file_name} -r .\
        --exclude "tests/*" \
        --exclude "env/*" \
        --exclude ".git/*" \
        --exclude ".pytest_cache/*" \
        --exclude ".venv*" \
        --exclude ".env" \
        --exclude "node_modules/*" \
        --exclude "**/node_modules/*" \
        --exclude ".ruff_cache/*" \
        --exclude ".gitattributes" \
        --exclude ".gitignore" \
        --exclude ".gitattributes" \
        --exclude "*coverage*" \
        --exclude "**/.vscode/*" \
		--exclude "*test*" \
        --exclude "**/*.venv*" \
        --exclude "playwrite/*" \
        --exclude ".gitignore" \
        --exclude "playwright-report/*" \
        --exclude ".github/*" \
        --exclude "playwright.config.ts" \
        --exclude "python-lib/.mypy_cache/*" \
        --exclude "**/.DS_Store" \
        --exclude ".DS_Store" \
        --exclude ".wlock" \
        --exclude "**/.wlock" \
        --exclude "playwright/*" \
        --exclude "documentation/*" \
        --exclude "pyproject.toml"

	@echo "[SUCCESS] Archiving plugin to dist/ folder: Done!"


dist-clean:
	rm -rf dist



##########################################
######## Dev tools: backend #########
##########################################

backend-lint-check:
	@echo "Running ruff ..."
	@ruff check

backend-mypy:
	@echo "Running mypy ..."
	@mypy -p backend --config-file=pyproject.toml

backend-unit-tests:
	@echo "Running unit tests ..."
	@PYTHONPATH=. pytest tests/unit

##########################################
######## Dev tools: frontend #########
##########################################

frontend-lint-check:
	@echo "Running lint ..."
	@cd resource/frontend && yarn run lint .

frontend-format-check:
	@echo "Running Prettier check ..."
	@cd resource/frontend && yarn run format:check

frontend-build-check:
	@echo "Running build check ..."
	@cd resource/frontend && yarn build
