[private]
default:
    @just --list

# ----------------Settings----------------

set fallback := true

# ----------------Variables----------------

SQLITE_DB_FILE_PATH := 'state/keys-internal/keys.sqlite'

# ----------------Recipes----------------

# Perform initial setup of developer's system.
[group('setup')]
init: init-rocket-config

# Copy Rocket's template configuration from Rocket.toml.disk to Rocket.toml. Rocket is Rust web framework. See https://rocket.rs/guide/v0.5/configuration/#configuration
[group('setup')]
init-rocket-config:
    #!/usr/bin/env -S bash -euo pipefail
    [ ! -f Rocket.toml ] \
        && cp Rocket.toml.dist Rocket.toml \
        && echo "Rocket.toml.dist copied to Rocket.toml" \
        || echo "Rocket.toml exists already!"

# Format justfile
[group('fmt')]
[group('format')]
[group('just')]
just-fmt:
    just --unstable --fmt

# Format Rust code in all packages (aka path based dependencies)
[group('fmt')]
[group('format')]
cargo-fmt:
    cargo fmt --all

# Format all code
[group('fmt')]
[group('format')]
fmt: just-fmt cargo-fmt

alias f := fmt

# Check justfile formatting
[group('fmt')]
[group('just')]
[group('lint')]
just-lint-fmt:
    just --unstable --fmt --check

# Check Rust code formatting in all packages (aka path based dependencies)
[group('fmt')]
[group('lint')]
cargo-lint-fmt:
    cargo fmt --all -- --check

# Check formatting of all code
[group('fmt')]
[group('lint')]
lint-fmt: just-lint-fmt cargo-lint-fmt

alias lf := lint-fmt

# Lint Rust code with Clippy
[group('clippy')]
[group('lint')]
clippy-lint:
    cargo clippy --tests --no-deps --workspace

alias cl := clippy-lint

# Lint all code
[group('lint')]
lint: lint-fmt clippy-lint

alias l := lint

# Fix compilation warnings by applying compiler suggestions
[group('fix')]
cargo-fix *args:
    cargo fix --workspace {{ args }}

# Apply Clippy's lint suggestions, i.e. fix Clippy linting warnings or errors
[group('clippy')]
[group('fix')]
clippy-fix *args:
    cargo clippy --fix --tests --no-deps --workspace {{ args }}

# Fix lint and compilation warnings and errors. Pass given arguments to all sub-recipes, i.e. `just fix --allow-dirty` calls `just cargo-fix --allow-dirty` and `just clippy-fix --allow-dirty`.
[group('fix')]
fix *args: (cargo-fix args) (clippy-fix args)

# Check Rust code errors
[group('compile')]
check:
    cargo check

alias c := check

# Compile all Rust code
[group('compile')]
build *args='--workspace':
    cargo build {{ args }}

alias b := build

# Run all tests (i.e. --workspace), but when args given pass them to `cargo test`, e.g. `just test fs::tests::init`
[group('test')]
test args='--workspace':
    cargo test {{ args }}

alias t := test

# Run continuous check of Rust code errors. Detect file changes and repeat check automatically. Ctrl+c to exit. You can pass additional arguments, e.g. --notify (-N).
[group('compile')]
[group('watch')]
watch-check *args:
    cargo watch --ignore *.pot {{ args }}

alias wc := watch-check

# Run web server and automatically restart on changes. Ctrl+c to exit. You can pass additional arguments, e.g. --notify (-N).
[group('compile')]
[group('run')]
[group('watch')]
watch-run *args:
    cargo watch --exec 'run --bin hagrid' --ignore *.pot {{ args }}

alias wr := watch-run

# Run tests every time files changed. Ctrl+c to exit. You can pass additional arguments, e.g. --notify (-N).
[group('test')]
[group('watch')]
watch-test *args:
    cargo watch --exec 'test --workspace' --ignore *.pot {{ args }}

alias wt := watch-test

# Run web server
[group('run')]
run:
    cargo run

alias r := run
alias run-hagrid := run
alias hagrid := run

# Run "hagridctl" which automate some operations working with database externally, e.g. import keys
[group('run')]
run-hagridctl *args:
    cargo run --package hagridctl -- {{ args }}

alias hagridctl := run-hagridctl

# Run "tester" which allows to seed database with sample data, e.g. for testing
[group('run')]
run-tester *args:
    cargo run --package tester -- {{ args }}

alias tester := run-tester

# Clean compilation artifacts (i.e. "target" directory)
[group('clean')]
clean:
    cargo clean

# Clean changes to translation files
[group('clean')]
[group('translation')]
clean-translations:
    git restore po/

# Open database prompt
[group('database')]
@db:
    command -v sqlite3 \
        && echo "See sqlite3 CLI Documentation: https://sqlite.org/cli.html\n" \
        && sqlite3 {{ SQLITE_DB_FILE_PATH }} \
        || echo "sqlite3 command has not been found. Please, install it using system's package manager or refer to documentation https://sqlite.org/cli.html for installation." >&2

# Translate *.hbs templates of web pages
[group('translation')]
translate-templates:
    ./make-translated-templates
