# cosmicnim tasks. `just` on its own runs the demo.

so := "nim/libcosmic_ffi.so"
export REMOTE := "rickub"

default: run

# Run the demo gallery. Needs nim/libcosmic_ffi.so (see `fetch` or `build`).
run: _have-so
    nim c -r --path:nim examples/calculator.nim

# Type-check the Nim side. Needs no .so: the binding loads it lazily.
check:
    nim check --path:nim examples/calculator.nim

# Build the cdylib from source into nim/ (cold builds are slow; CI is faster)
build:
    cd cosmic_ffi && cargo build --release
    cp cosmic_ffi/target/release/libcosmic_ffi.so {{so}}

# Check the Rust without producing a library.
rust-check:
    cd cosmic_ffi && cargo clippy --release -- -D warnings

fmt:
    cd cosmic_ffi && cargo fmt

# Download the cdylib. No tag means the one matching cosmicnim.nimble.
fetch tag="":
    #!/usr/bin/env bash
    set -euo pipefail
    # Only export when non-empty: an empty COSMICNIM_TAG would beat the default.
    [ -z "{{tag}}" ] || export COSMICNIM_TAG="{{tag}}"
    nimble fetchLib

# Install the package (and its cdylib) into the local nimble store.
install:
    nimble install --verbose

# Cut a release by pushing a tag with git (an API-made tag triggers no CI)
tag version:
    git tag -a "{{version}}" -m "{{version}}"
    git push "$REMOTE" "{{version}}"

clean:
    rm -rf cosmic_ffi/target examples/nimcache examples/calculator

_have-so:
    @test -f {{so}} || { echo "missing {{so}} -- run 'just build' or 'just fetch <url>'" >&2; exit 1; }

# Run the headless tests of the example's logic (no window, no cdylib call).
test:
    nim r --hints:off --path:nim tests/tcalculator.nim
