stages:
  - format
  - build_and_test
  - quality
  - package

.env:
  image: axiomteam/gecko-ci:v0.0.7
  tags:
    - p2plegal
  before_script:
    - rustup show
    - rustc --version && cargo --version

format:
  extends: .env
  rules:
    - if: $CI_COMMIT_REF_NAME =~ /^wip*$/
      when: manual
    - if: $CI_COMMIT_TAG || $CI_MERGE_REQUEST_ID
    - when: manual
  stage: format
  script:
    - cargo fmt -- --version
    - cargo fmt -- --check
    - flutter format --set-exit-if-changed lib packages/dubp_rs/lib/dubp.dart

build_and_test:
  extends: .env
  rules:
    - if: $CI_COMMIT_REF_NAME =~ /^wip*$/
      when: manual
    - if: $CI_COMMIT_TAG || $CI_MERGE_REQUEST_ID
    - when: manual
  stage: build_and_test
  tags:
    - p2plegal
  script: 
    - RUSTFLAGS="-D warnings" cargo make
    - cd packages/dubp_rs && flutter pub get && cd ../..
    - flutter analyze
    - cargo test
    # - flutter test

lint:
  extends: .env
  rules:
    - if: $CI_COMMIT_REF_NAME =~ /^wip*$/
      when: manual
    - if: $CI_COMMIT_TAG || $CI_MERGE_REQUEST_ID
    - when: manual
  stage: quality
  script:
    - cargo clippy -- -V
    - cargo clippy --all --tests -- -D warnings --verbose
    
audit_dependencies:
  extends: .env
  rules:
    - if: $CI_COMMIT_REF_NAME =~ /^wip*$/
      when: manual
    - if: $CI_COMMIT_TAG || $CI_MERGE_REQUEST_ID
    - when: manual
  stage: quality
  script:
    - cargo deny -V
    - cargo deny check

releases:test:
  extends: .env
  stage: package
  allow_failure: true
  rules:
    - if: $CI_COMMIT_TAG
      when: never
    - when: manual
  script:
    - bash "release/android/build-apk.sh" "$(date +%Y%m%d).$(date +%H%M).$(date +%S)+0"
  artifacts:
    paths: &releases_artifacts
      - work/bin/
    expire_in: 72h

releases:x64:
  extends: .env
  stage: package
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - bash "release/android/build-apk.sh" "${CI_COMMIT_TAG#v}"
  artifacts:
    paths: *releases_artifacts
    expire_in: 1 mos
