# List of commands runnable with https://github.com/casey/just
#
# When adding new recipes, include a one-line comment describing the behavior
# to be displayed by `just --list` next to the name.

_default:
    echo "no default recipe; run with --list to see all recipes"
    exit 1

# Install the stable toolchain pinned in rust-toolchain; extra args go to rustup.
install-stable *args:
    rustup toolchain install "$(cat rust-toolchain)" --profile minimal --component rustfmt,clippy {{args}}

# Install the nightly toolchain pinned in .nightly-rust-version; extra args go to rustup.
install-nightly *args:
    rustup toolchain install "$(cat .nightly-rust-version)" --profile minimal --component rustfmt,clippy {{args}}

generate-jni:
    cargo run -p libsignal-jni-native_kt

generate-ffi:
    swift/build_ffi.sh --generate-ffi

generate-node:
    cargo run -p libsignal-node-native_ts

alias generate-java := generate-jni
alias generate-swift := generate-ffi
alias generate-ts := generate-node

# Regenerate bridge code for all three app languages.
[parallel]
generate-bridge: generate-jni generate-node generate-ffi

alias generate-all := generate-bridge

# Regenerate Rust dependency acknowledgments.
generate-acknowledgments:
    ./bin/regenerate_acknowledgments.sh

# Verify generated acknowledgments are up to date.
check-acknowledgments:
    ./bin/regenerate_acknowledgments.sh --check

format-jni:
    (cd java && ./gradlew spotlessApply)

format-ffi:
    (cd swift && swift format --in-place --parallel --recursive .)

format-node:
    (cd node && npm run format)

# Format Rust code with the nightly toolchain pinned in .nightly-rust-version.
format-rust *args:
    #!/usr/bin/env bash
    set -euo pipefail
    # The options in .rustfmt.toml are still unstable, so the default stable
    # toolchain would ignore them.
    toolchain="$(cat .nightly-rust-version)"
    if ! rustup component list --toolchain "$toolchain" --installed 2>/dev/null | grep -q '^rustfmt'; then
        echo "$toolchain is not installed with rustfmt; run \`just install-nightly\`" >&2
        exit 1
    fi
    cargo "+$toolchain" fmt --all {{args}}
    # Not part of the main workspace, so it needs its own invocation.
    cd rust/protocol/cross-version-testing && cargo "+$toolchain" fmt --all {{args}}

alias format-java := format-jni
alias format-swift := format-ffi
alias format-ts := format-node

# Auto-format code in Java, Rust, Swift, and TypeScript
[parallel]
format-all: format-jni format-ffi format-node format-rust
    taplo fmt

# Same as format-all, but does not actually make changes; merely fails if code is not yet formatted.
check-format-all:
    just format-rust -- --check
    taplo fmt --check
    @echo 'warning: `swift format` does not have a check mode'
    (cd node && npm run format-check)
    (cd java && ./gradlew spotlessCheck)

check-python:
    $(command -v flake8 || echo python3 -m flake8) . --exclude target,node/node_modules,node/build
    $(command -v mypy || echo python3 -m mypy) . --python-version 3.9 --strict --exclude target --exclude node/node_modules --exclude node/build

# Runs some quick local checks; useful to make sure CI will not fail immediately after push.
check-pre-commit: check-format-all check-python check-acknowledgments
    (cd node && npm run lint)
    (cd swift && ./verify_error_codes.sh)
    (cd swift && swiftlint lint --strict)
    (cd java && ./gradlew --dependency-verification strict help >/dev/null)
    shellcheck -- **/*.sh bin/verify_duplicate_crates bin/adb-run-test
    cargo test --workspace --all-features --verbose --no-fail-fast -- --include-ignored
    cargo clippy --workspace --all-targets --all-features --keep-going -- -D warnings
    env "RUSTUP_TOOLCHAIN=$(cat .nightly-rust-version)" "CARGO_TARGET_DIR=$PWD/target/without_building_boring" "CARGO_RESOLVER_LOCKFILE_PATH=$(mktemp -d)/Cargo.lock" bin/without_building_boring.sh cargo check --workspace --all-targets --all-features --keep-going -Zdirect-minimal-versions -Zlockfile-path
    @printf "\e[32mBasic pre-commit checks passed! ✅ Hopefully good to push! 🤞\e[0m\n"
