[private]
default:
    @just --list

name := "cid"

# Alias definitions

alias t := test
alias d := document
alias c := coverage
alias p := publish

# Installs all cargo tools to build a release or test coverage
install-dev-tools:
    cargo install cargo-release cargo-sbom cargo-tarpaulin typos-cli cargo-nextest git-cliff

# Bumps {patch} (major, minor or patch) version number and does a release
bump patch: check-typos
    # Ensures that the source code is correctly formatted -> it should not modify anything
    cargo fmt

    # Checking that we do not have any untracked or uncommitted file
    git status -s | wc -l | grep '0'

    # ChangeLog update
    git-cliff -u --bump {{ patch }} -p ChangeLog
    git add ChangeLog
    git commit -m "fix(docs): updates ChangeLog thanks to git-cliff"

    # Updating all dependencies
    cargo update

    # Vendoring dependencies
    cargo vendor

    # Bumping release version upon what has been asked on command line (major, minor or patch)
    cargo release version {{ patch }} --no-confirm --execute

    # Building, testing and building doc to ensure one can build with these dependencies
    cargo build --release --offline
    cargo test --release
    cargo doc --no-deps

    # Generetaing a Software Bills of Materials in SPDX format (sorting will reduce the diff size and allow one to figure out what has really changed)
    cargo sbom | jq --sort-keys | jq '.files = (.files| sort_by(.SPDXID))' | jq '.packages = (.packages| sort_by(.SPDXID))' | jq '.relationships = (.relationships| sort_by(.spdxElementId, .relatedSpdxElement))'>{{ name }}.sbom.spdx.json

    # Creating the release
    git add Cargo.toml Cargo.lock {{ name }}.sbom.spdx.json vendor
    cargo release commit --no-confirm --execute
    cargo release tag --no-confirm --execute

# Runs tests for the project
test:
    cargo nextest run
    cargo t --doc

# Creates the documentation and open it in a browser
document:
    cargo doc --no-deps --open

# Publishing in the git repository (with tags)
git-publish:
    git push
    git push --tags

# Publishing to crates.io
rust-publish:
    cargo publish

# Publishing to git and then to crates.io
publish: git-publish rust-publish

# Runs a coverage test and open it's result in a web browser
coverage:
    cargo tarpaulin --frozen -o Html --include-files src/*.rs src/bin/*.rs
    open tarpaulin-report.html

# Check for typos in source tree, README.md, test_data  and .justfile
check-typos:
    typos src/ README.md  test_data/ .justfile docs/ --exclude *.excalidraw

# Invoke clippy in pedantic mode
clippy:
    cargo clippy -- -W clippy::pedantic
