# ============================================================================
# quality.mk - Code Quality Targets
# ============================================================================
# Provides: linting, formatting, type-checking, pre-publish gates
# Include in main Makefile with: -include .makefiles/quality.mk
#
# Extracted from: claude-mpm production Makefile (97 targets)
# Dependencies: common.mk (for colors, ENV system)
# Last updated: 2025-11-21
# ============================================================================
# ============================================================================
# Quality Target Declarations
# ============================================================================
.PHONY: lint-ruff lint-fix lint-mypy quality quality-ci pre-publish
.PHONY: clean-system-files clean-test-artifacts clean-deprecated clean-pre-publish
# ============================================================================
# Individual Linting Targets
# ============================================================================
lint-ruff: ## Run Ruff linter and formatter check
@echo "$(YELLOW)๐ Running Ruff linter...$(NC)"
@if command -v ruff &> /dev/null; then \
ruff check $(SRC_DIR)/ $(TESTS_DIR)/ $(RUFF_ARGS) || exit 1; \
echo "$(GREEN)โ Ruff linting passed$(NC)"; \
echo "$(YELLOW)๐ Checking code formatting...$(NC)"; \
ruff format --check $(SRC_DIR)/ $(TESTS_DIR)/ || exit 1; \
echo "$(GREEN)โ Ruff format check passed$(NC)"; \
else \
echo "$(RED)โ ruff not found. Install with: pip install ruff$(NC)"; \
exit 1; \
fi
lint-mypy: ## Run mypy type checker
@echo "$(YELLOW)๐ Running mypy type checker...$(NC)"
@if command -v mypy &> /dev/null; then \
mypy $(SRC_DIR)/ --ignore-missing-imports --no-error-summary || true; \
echo "$(YELLOW)โน MyPy check complete (informational)$(NC)"; \
else \
echo "$(YELLOW)โ mypy not found. Install with: pip install mypy$(NC)"; \
fi
# ============================================================================
# Auto-Fix Target
# ============================================================================
lint-fix: ## Auto-fix linting issues (ruff format + ruff check --fix)
@echo "$(YELLOW)๐ง Auto-fixing code issues with Ruff...$(NC)"
@if command -v ruff &> /dev/null; then \
echo "$(YELLOW)Fixing linting issues...$(NC)"; \
ruff check $(SRC_DIR)/ $(TESTS_DIR)/ --fix || true; \
echo "$(GREEN)โ Ruff linting fixes applied$(NC)"; \
echo "$(YELLOW)Formatting code...$(NC)"; \
ruff format $(SRC_DIR)/ $(TESTS_DIR)/ || true; \
echo "$(GREEN)โ Code formatted$(NC)"; \
else \
echo "$(RED)โ ruff not found. Install with: pip install ruff$(NC)"; \
exit 1; \
fi
@echo ""
@echo "$(GREEN)โ
Auto-fix complete. Run 'make quality' to verify.$(NC)"
# ============================================================================
# Combined Quality Checks
# ============================================================================
quality: ## Run all quality checks (ruff + mypy)
@echo "$(BLUE)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ$(NC)"
@echo "$(BLUE)Running all quality checks...$(NC)"
@echo "$(BLUE)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ$(NC)"
@$(MAKE) lint-ruff
@$(MAKE) lint-mypy
@echo ""
@echo "$(GREEN)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ$(NC)"
@echo "$(GREEN)โ
All quality checks passed!$(NC)"
@echo "$(GREEN)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ$(NC)"
quality-ci: ## Quality checks for CI/CD (strict, fail fast)
@echo "$(BLUE)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ$(NC)"
@echo "$(BLUE)Running CI quality checks (strict mode)...$(NC)"
@echo "$(BLUE)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ$(NC)"
@set -e; \
echo "$(YELLOW)๐ Ruff check (no fixes)...$(NC)"; \
ruff check $(SRC_DIR)/ $(TESTS_DIR)/ --no-fix; \
echo "$(YELLOW)๐ Type checking...$(NC)"; \
mypy $(SRC_DIR)/ --ignore-missing-imports; \
echo "$(YELLOW)๐งช Running tests (parallel)...$(NC)"; \
$(PYTHON) -m pytest $(TESTS_DIR)/ $(PYTEST_ARGS) --tb=short
@echo ""
@echo "$(GREEN)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ$(NC)"
@echo "$(GREEN)โ
CI quality checks passed!$(NC)"
@echo "$(GREEN)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ$(NC)"
# ============================================================================
# Pre-Publish Cleanup Targets
# ============================================================================
clean-system-files: ## Remove system files (.DS_Store, __pycache__, *.pyc)
@echo "$(YELLOW)๐งน Cleaning system files...$(NC)"
@find . -name ".DS_Store" -not -path "*/venv/*" -not -path "*/.venv/*" -delete 2>/dev/null || true
@find . -type d -name "__pycache__" -not -path "*/venv/*" -not -path "*/.venv/*" -exec rm -rf {} + 2>/dev/null || true
@find . -type f \( -name "*.pyc" -o -name "*.pyo" \) -not -path "*/venv/*" -not -path "*/.venv/*" -delete 2>/dev/null || true
@echo "$(GREEN)โ System files cleaned$(NC)"
clean-test-artifacts: ## Remove test artifacts (HTML, JSON reports in root)
@echo "$(YELLOW)๐งน Cleaning test artifacts from root...$(NC)"
@rm -f dashboard_test.html report_qa_test.html coverage.json 2>/dev/null || true
@rm -rf htmlcov/ .coverage .pytest_cache/ 2>/dev/null || true
@echo "$(GREEN)โ Test artifacts cleaned$(NC)"
clean-deprecated: ## Remove explicitly deprecated files
@echo "$(YELLOW)๐งน Removing deprecated files...$(NC)"
@# Add project-specific deprecated file patterns here
@echo "$(GREEN)โ Deprecated files removed$(NC)"
clean-pre-publish: clean-system-files clean-test-artifacts clean-deprecated ## Complete pre-publish cleanup
@echo ""
@echo "$(GREEN)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ$(NC)"
@echo "$(GREEN)โ
Pre-publish cleanup complete!$(NC)"
@echo "$(GREEN)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ$(NC)"
# ============================================================================
# Pre-Publish Quality Gate
# ============================================================================
pre-publish: clean-pre-publish ## Comprehensive pre-release quality gate
@echo "$(BLUE)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ$(NC)"
@echo "$(BLUE)๐ Pre-Publish Quality Gate$(NC)"
@echo "$(BLUE)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ$(NC)"
@echo ""
@echo "$(YELLOW)Step 1/4: Checking working directory...$(NC)"
@if [ -n "$$(git status --porcelain)" ]; then \
echo "$(RED)โ Working directory is not clean$(NC)"; \
echo "$(YELLOW)Please commit or stash your changes first$(NC)"; \
git status --short; \
exit 1; \
fi
@echo "$(GREEN)โ Working directory is clean$(NC)"
@echo ""
@echo "$(YELLOW)Step 2/4: Running all linters...$(NC)"
@$(MAKE) quality
@echo ""
@echo "$(YELLOW)Step 3/4: Running tests...$(NC)"
@if command -v pytest >/dev/null 2>&1; then \
$(PYTHON) -m pytest $(TESTS_DIR)/ $(PYTEST_ARGS) || exit 1; \
else \
echo "$(YELLOW)โ pytest not found, skipping tests$(NC)"; \
fi
@echo "$(GREEN)โ Tests passed$(NC)"
@echo ""
@echo "$(YELLOW)Step 4/4: Checking for common issues...$(NC)"
@echo "Checking for debug prints..."
@! grep -r "print(" $(SRC_DIR)/ --include="*.py" | grep -v "#" | grep -v "logger" || \
echo "$(YELLOW)โ Found print statements (non-blocking for CLI tools)$(NC)"
@echo "Checking for TODO/FIXME..."
@! grep -r "TODO\|FIXME" $(SRC_DIR)/ --include="*.py" | head -5 || \
echo "$(YELLOW)โ Found TODO/FIXME comments (non-blocking)$(NC)"
@echo "$(GREEN)โ Common issues check complete$(NC)"
@echo ""
@echo "$(GREEN)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ$(NC)"
@echo "$(GREEN)โ
Pre-publish checks PASSED!$(NC)"
@echo "$(GREEN)Ready for release.$(NC)"
@echo "$(GREEN)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ$(NC)"
# ============================================================================
# Usage Examples
# ============================================================================
# Quick development workflow:
# make lint-fix # Auto-fix all issues
# make quality # Verify all checks pass
#
# Before committing:
# make quality # Run all quality checks
#
# Before releasing:
# make pre-publish # Comprehensive quality gate
#
# CI/CD integration:
# make quality-ci # Strict, fail-fast checks
#
# Cleanup:
# make clean-pre-publish # Remove artifacts and temp files
# ============================================================================