#!/usr/bin/env -S just --justfile
# -*- coding: utf-8 -*-
#################################################################################
# MIT License
#
# Copyright (c) 2025 Duncan Fraser
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#################################################################################
#
# File          : justfile
#
# Author        : Duncan Fraser <dfraser@thedrunkencoder.uk>
#
# Description   :
#
#################################################################################
#-----------------------------[Project Variables]-------------------------------
SRC_DIR := "srcalchemy"
TEST_DIR := "tests"
DOCS_DIR :=  "docs"
REPORT_DIR := ".reports"
TEST_RESULTS_XML := REPORT_DIR + "/test_results.xml"

export VIRTUAL_ENV := ".venv"
# List Recipes
@default:
    just -f {{ justfile() }} --list

# Edit Justfile
@edit:
    just -e {{ justfile() }}

#------------------------------------[Init]-------------------------------------
# Setup development environment
[group('init')]
@setup:
    uv sync
    uv run pre-commit install

# Run uv sync
[group('init')]
@sync:
    uv sync

# Clean output files
[group('init')]
@clean:
    -rm -rvf build dist {{ VIRTUAL_ENV }} {{ DOCS_DIR }}/build {{ REPORT_DIR }} \
    .coverage coverage.* htmlcov .mypy_cache .pytest_cache .ruff_cache \
    **/*/__pycache__ */__pycache__ *.egg-info
#------------------------------------[Build]------------------------------------
alias b := build
# Build python output
[group('build')]
@build:
    uv build

# Generate sphinx documentation
[group('build')]
@docs:
    uv run sphinx-build -M html "{{ DOCS_DIR }}/source" "{{ DOCS_DIR }}/build"

#------------------------------------[Deploy]------------------------------------
# Upload to specified registry, `index` needs to be defined in `pyproject.toml`
[group('deploy')]
@upload index="": build
    uv publish {{ if index != "" { "--index " + index } else { "" } }}

#------------------------------------[Test]-------------------------------------
# Run pytest
[group('test')]
@test:
    - uv run coverage run --source={{ SRC_DIR }} -m pytest {{ TEST_DIR }} --junitxml={{ TEST_RESULTS_XML }}
    uv run coverage report -m

# Generate coverage HTML report
[group('test')]
[arg("format", pattern="html|json|xml", help="Report format: html, json, or xml")]
@coverage format="html": test
    uv run coverage {{ format }}

#------------------------------------[Lint]-------------------------------------
# Run type checking using ty
[group('lint')]
@sast:
    - uv run ty check

# Run precommit against all files
[group('lint')]
@pre-commit:
    uv run pre-commit run --all-files

# Check formatting
[group('lint')]
@lint:
    - uv run ruff check

# Run ruff formatter
[group('lint')]
@format:
    - uv run ruff check --fix
    - uv run ruff format

#----------------------------------[Commands]-----------------------------------
# Full build, test, and lint
all: sync lint sast (coverage "html") build docs
