# --- Justfile for JVM Projects (Gradle) ---
# Canonical targets; delete recipes your build.gradle does not wire (SKILL Step 5).

set shell := ["bash", "-c"]

project := `basename $(pwd)`
version := `git describe --tags --always --dirty 2>/dev/null || echo "dev"`
commit  := `git rev-parse --short HEAD 2>/dev/null || echo "unknown"`

entrypoint := `if [ -f ./gradlew ] || [ -f gradle/wrapper/gradle-wrapper.properties ]; then echo "./gradlew"; else echo "gradle"; fi`
dev_gradle_task := `for f in build.gradle build.gradle.kts settings.gradle settings.gradle.kts gradle/libs.versions.toml; do test -f "$f" || continue; if grep -qE "quarkus|io\\.quarkus" "$f" 2>/dev/null; then printf %s quarkusDev; exit 0; fi; done; for f in build.gradle build.gradle.kts settings.gradle settings.gradle.kts gradle/libs.versions.toml; do test -f "$f" || continue; if grep -qE "micronaut|io\\.micronaut" "$f" 2>/dev/null; then printf %s run; exit 0; fi; done; for f in build.gradle build.gradle.kts settings.gradle settings.gradle.kts gradle/libs.versions.toml; do test -f "$f" || continue; if grep -qE "vertx-plugin|io\\.vertx\\.vertx" "$f" 2>/dev/null; then printf %s vertxRun; exit 0; fi; done; printf %s bootRun`

docker-registry := "ghcr.io"
docker-image    := docker-registry + "/" + project

# --- Default ---
default:
    @just --list

# --- Development ---
clean:
    {{entrypoint}} clean

build:
    {{entrypoint}} build

assemble:
    {{entrypoint}} assemble

dev:
    {{entrypoint}} {{dev_gradle_task}}

# --- Testing ---
test:
    {{entrypoint}} test

check:
    {{entrypoint}} check

# --- Multi-module: export JVM_MODULE=subproject-id ---
module-build:
    {{entrypoint}} :{{ env("JVM_MODULE", "change-me-subproject") }}:build

module-test:
    {{entrypoint}} :{{ env("JVM_MODULE", "change-me-subproject") }}:test

module-check:
    {{entrypoint}} :{{ env("JVM_MODULE", "change-me-subproject") }}:check

# --- Code Quality ---
lint:
    {{entrypoint}} check

fmt:
    {{entrypoint}} spotlessApply

lint-checkstyle:
    {{entrypoint}} checkstyleMain

lint-spotbugs:
    {{entrypoint}} spotbugsMain

lint-pmd:
    {{entrypoint}} pmdMain

lint-spotless:
    {{entrypoint}} spotlessCheck

# --- Docker ---
docker-build:
    docker build \
        --build-arg VERSION={{version}} \
        --build-arg COMMIT={{commit}} \
        -t {{docker-image}}:{{version}} \
        -t {{docker-image}}:latest \
        .

docker-push:
    docker push {{docker-image}}:{{version}}
    docker push {{docker-image}}:latest

# --- Database ---
db-migrate-liquibase:
    {{entrypoint}} liquibaseUpdate

db-migrate-flyway:
    {{entrypoint}} flywayMigrate

# --- CI ---
ci: clean build
