.push-zamok: &push-zamok
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
  - \[ -d ${ZAMOK_SOURCE} \] && 
        rsync -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" -rqz ${ZAMOK_SOURCE}/. v-lafeychine@zamok.crans.org:www/skavos/${ZAMOK_TARGET} || true

default:
  image: vlafeychine/skavos
  interruptible: true
  cache:
    paths:
      - .cache/cargo/
      - target/

variables:
  CARGO_HOME: "$CI_PROJECT_DIR/.cache/cargo/"
  RUSTFLAGS: "-D warnings"


stages: [ check, build, tests, docs ]

format:
  stage: check
  script:
    - cargo deny check
    - cargo fmt --check

lint:
  stage: check
  script:
    - cargo clippy --all-targets --all-features --no-deps

build:
  stage: build
  script:
    - cargo build

tests:
  stage: tests
  variables:
    CARGO_INCREMENTAL: 0
    EXCL_ENUM_STRUCT: "^([[:space:]]*)(pub |pub(([[:alpha:]]|[[:space:]]|[:])+) )?(enum|struct) [^\\{]*\\{"
    EXCL_ENUM_FN_STRUCT: "^([[:space:]]*)(pub |pub(([[:alpha:]]|[[:space:]]|[:])+) )?(enum|(const )?fn|struct) "
    EXCL_LINE: "//!|#\\[|use|unreachable!|^\\}|${EXCL_ENUM_STRUCT}"
    EXCL_START: "#\\[no_coverage\\]|${EXCL_ENUM_STRUCT}"
    EXCL_STOP: "^\\}"
    EXCL_BR_LINE: "#\\[|assert(_eq)?!|(error|warn|info|debug|trace)!|^[[:space:]]*\\}(,)?$|${EXCL_ENUM_FN_STRUCT}"
    EXCL_BR_START: "#\\[no_coverage\\]|^mod tests \\{|${EXCL_ENUM_STRUCT}"
    EXCL_BR_STOP: "^\\}"
    RUSTDOCFLAGS: "${RUSTFLAGS}"
    RUSTFLAGS: "-Zno-profiler-runtime -Cinstrument-coverage"
    ZAMOK_SOURCE: coverage
    ZAMOK_TARGET: coverage/${CI_COMMIT_REF_SLUG}

  cache:
    paths:
      - .cache/cargo/

  script:
    - cargo test
    - grcov target/output.profraw -s . -b ./target/x86_64/debug/ --llvm --ignore-not-existing --excl-line "${EXCL_LINE}" --excl-start "${EXCL_START}" --excl-stop "${EXCL_STOP}" --excl-br-line "${EXCL_BR_LINE}" --excl-br-start "${EXCL_BR_START}" --excl-br-stop "${EXCL_BR_STOP}" --ignore "*cargo/*" --ignore "*alloc/*" --ignore "*core/*" --ignore "*portable-simd/*" --ignore "*stdarch/*" --ignore "*target/*" -t cobertura -o ./coverage.xml || true
    - grcov target/output.profraw -s . -b ./target/x86_64/debug/ --llvm --ignore-not-existing --excl-line "${EXCL_LINE}" --excl-start "${EXCL_START}" --excl-stop "${EXCL_STOP}" --excl-br-line "${EXCL_BR_LINE}" --excl-br-start "${EXCL_BR_START}" --excl-br-stop "${EXCL_BR_STOP}" --ignore "*cargo/*" --ignore "*alloc/*" --ignore "*core/*" --ignore "*portable-simd/*" --ignore "*stdarch/*" --ignore "*target/*" -t lcov -o ./coverage.lcov &&
        genhtml --no-function-coverage --precision 2 ./coverage.lcov -o ./coverage || true
    - *push-zamok

  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: ./coverage.xml

  coverage: '/^  lines......: \d+.\d+%/'

docs:
  stage: docs
  variables:
    ZAMOK_SOURCE: target/doc
    ZAMOK_TARGET: doc

  only:
    refs: [ main ]

  script:
    - cargo doc --no-deps
    - *push-zamok
