# Adapted from https://github.com/linkml/linkml-project-copier (template/justfile).
# Project-specific recipes live in project.justfile.

# ============ Shell configuration for Windows ============

# On Windows the "bash" shell from Git for Windows is used.
# If Git is installed in a non-standard location, edit the path below.
set windows-shell := ["C:/Program Files/Git/bin/bash", "-cu"]

# ============ Variables used in recipes ============

# Load environment variables from config.public.mk or specified file
set dotenv-load := true
set dotenv-filename := x'${LINKML_ENVIRONMENT_FILENAME:-config.public.mk}'

# Set shebang line for cross-platform Python recipes (assumes presence of launcher on Windows)
shebang := if os() == 'windows' {
  'py'
} else {
  '/usr/bin/env python3'
}

# Environment variables with defaults
schema_name := env_var_or_default("LINKML_SCHEMA_NAME", "_no_schema_given_")
source_schema_dir := env_var_or_default("LINKML_SCHEMA_SOURCE_DIR", "")
config_yaml := if env_var_or_default("LINKML_GENERATORS_CONFIG_YAML", "") != "" {
  "--config-file " + env_var_or_default("LINKML_GENERATORS_CONFIG_YAML", "")
} else {
  ""
}
gen_doc_args := env_var_or_default("LINKML_GENERATORS_DOC_ARGS", "")
gen_pydantic_args := env_var_or_default("LINKML_GENERATORS_PYDANTIC_ARGS", "")

# Directory variables
src := "src"
dest := "project"
pymodel := src / schema_name / "datamodel"
source_schema_path := source_schema_dir / schema_name + ".yaml"
docdir := "docs/elements"  # Directory for generated documentation
distrib_schema_path := "docs/schema"  # Directory for publishing schema artifacts
examples_dir := "src/data/examples"

# ============== Project recipes ==============

# List all commands as default command. The prefix "_" hides the command.
_default: _status
  @just --list

# Install project dependencies
[group('project management')]
install:
  uv sync --group dev

# Updates project template and LinkML package
[group('project management')]
update: _update-template _update-linkml

# Clean generated files that are not tracked in git
[group('project management')]
clean:
  rm -rf tmp site examples/output {{distrib_schema_path}}
  rm -rf {{docdir}}/*.md

# (Re-)Generate project and documentation locally
[group('model development')]
site: gen-project gen-doc

# Deploy documentation site to Github Pages
[group('deployment')]
deploy: site
  uv run mkdocs gh-deploy --force

# Run all tests
[group('model development')]
test: _test-schema _test-python _test-examples

# Run linting
[group('model development')]
lint:
  uv run linkml-lint {{source_schema_path}}

# Generate md documentation for the schema and add artifacts
[group('model development')]
gen-doc: _gen-yaml
  uv run gen-doc {{gen_doc_args}} -d {{docdir}} {{source_schema_path}}

# Build docs and run test server
[group('model development')]
testdoc: gen-doc _serve

# Generate project files (project/) and the pydantic model (datamodel/_models.py)
[group('model development')]
gen-project:
  @# Java output is not tracked in this project
  uv run gen-project {{config_yaml}} --exclude java -d {{dest}} {{source_schema_path}}
  @# Python dataclasses are kept in project/python, only the pydantic model is used by the package
  mv {{dest}}/*.py {{dest}}/python/
  uv run gen-pydantic {{gen_pydantic_args}} {{source_schema_path}} > {{pymodel}}/_models.py
  uv run ruff format {{pymodel}}/_models.py {{dest}}/python/*.py
  @# Use default_factory=list for the input field
  uv run python modify_input_default.py

# ============== Hidden internal recipes ==============

# Show current project status
_status: _check-config
  @echo "Project: {{schema_name}}"
  @echo "Source: {{source_schema_path}}"

# Check project configuration
_check-config:
  #!{{shebang}}
  import os
  schema_name = os.getenv('LINKML_SCHEMA_NAME')
  if not schema_name:
      print('**Project not configured**:\n - See \'config.public.mk\'')
      exit(1)
  print('Project-status: Ok')

# Update project template
_update-template:
  copier update --trust --skip-answered

# Update LinkML runtime and LinkML to latest versions
_update-linkml:
  uv lock --upgrade-package linkml-runtime --upgrade-package linkml

# Test schema generation
_test-schema:
  uv run gen-project {{config_yaml}} --exclude java -d tmp {{source_schema_path}}

# Run Python unit tests with pytest
_test-python:
  uv run python -m pytest

# Run example tests
_test-examples: _ensure_examples_output
  uv run linkml-run-examples \
    --output-formats json \
    --output-formats yaml \
    --counter-example-input-directory {{examples_dir}}/invalid \
    --input-directory {{examples_dir}}/valid \
    --output-directory examples/output \
    --schema {{source_schema_path}} > examples/output/README.md

# Add the merged model to docs/schema.
_gen-yaml:
  -mkdir -p {{distrib_schema_path}}
  uv run gen-yaml {{source_schema_path}} > {{distrib_schema_path}}/{{schema_name}}.yaml

# Run documentation server
_serve:
  uv run mkdocs serve

_ensure_examples_output:  # Ensure a clean examples/output directory exists
  -mkdir -p examples/output
  -rm -rf examples/output/*.*

# ============== Include project-specific recipes ==============

import "project.justfile"
