.run_on_merge_request:
  rules:
    - if: $CI_COMMIT_TAG
      when: never
    - if: '$CI_COMMIT_TITLE =~ /chore\(version\)\:.*/'
      when: never
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: on_success
    - if: "$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH"
      when: on_success

.run_on_main:
  rules:
    - if: $CI_COMMIT_TAG
      when: never
    - if: '$CI_COMMIT_TITLE =~ /chore\(version\)\:.*/'
      when: never
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: never
    - if: "$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH"
      when: on_success

stages:
  - containers
  - prechecks
  - checks
  - release
  - post-release

formatting & clippy:
  image: archlinux:latest
  stage: prechecks
  extends: [.run_on_merge_request]
  before_script:
    - ./ci/setup.sh
  script:
    - source $HOME/.cargo/env
    - cargo fmt --all -- --color=always --check
    - cargo clippy --no-deps
    # - cd examples/simple_app && cargo clippy && cd ../..
    # - cd examples/simple_app && cargo fmt --all -- --color=always --check && cd ../..

outdated & audit:
  image: archlinux:latest
  allow_failure: true
  stage: prechecks
  extends: [.run_on_merge_request]
  before_script:
    - ./ci/setup.sh
    - source $HOME/.cargo/env
    - mkdir -p .audit
    - curl -L -o .audit/audit.tar.gz "https://github.com/rustsec/rustsec/releases/download/cargo-audit%2Fv0.17.6/cargo-audit-x86_64-unknown-linux-musl-v0.17.6.tgz"
    - tar xvzf .audit/audit.tar.gz --directory .audit/
    - export PATH="$CI_PROJECT_DIR/.audit:$PATH"
    - mkdir -p .outdated
    - curl -L -o .outdated/outdated.tar.gz "https://github.com/kbknapp/cargo-outdated/releases/download/v0.13.1/cargo-outdated-0.13.1-x86_64-unknown-linux-musl.tar.gz"
    - tar xvzf .outdated/outdated.tar.gz --directory .outdated/
    - export PATH="$CI_PROJECT_DIR/.outdated:$PATH"
  script:
    - source $HOME/.cargo/env
    - cargo outdated
    - cargo audit

build and test x86-64:
  needs: ["formatting & clippy"]
  image: archlinux:latest
  stage: checks
  extends: [.run_on_merge_request]
  before_script:
    - ./ci/setup.sh
  script:
    - source $HOME/.cargo/env
    - cargo test --release

# build examples x86-64:
#   image: archlinux:latest
#   stage: checks
#   extends: [.run_on_merge_request]
#   before_script:
#     - ./ci/setup.sh
#   script:
#     - source $HOME/.cargo/env
#     - cd examples/counter && cargo build --release

release:
  image: archlinux:latest
  stage: release
  extends: [.run_on_main]
  before_script:
    - ./ci/setup.sh
    - source $HOME/.cargo/env
    - cargo install cargo-bump
  script:
    - source $HOME/.cargo/env
    - REMOTE=1 ci/release.sh
