image: python:3.12

stages:
  - run-tests
  - build-docs
  - build-pypi

before_script:
  - apt-get -qq update && apt-get -qq install -y libgl1-mesa-glx
  - python --version ; pip --version
  - pip install --upgrade pip
  - pip install -r requirements/dev.txt

# build and deploy documentation on gitlab pages : 
# runs only on 'main' branch if docs were changed
pages:
  stage: build-docs
  script:
    - python -m pip install .[data]
    - sphinx-apidoc -f -e -d 2 --separate -l -M -o docs/source/generated pyCFS/
    - python docs/rst_process.py
    - sphinx-build -M html docs/source/ docs/build/
    - cp -r docs/source/embedded docs/build/html
    - mv docs/build/html public/
  artifacts:
    paths:
      - public
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      changes:
        - docs/source/**/*
        - pyCFS/**/*

tests:
  stage: run-tests
  script: 
    - python -m pip install .
    - pytest
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml

lint-type-tests:
  stage: run-tests
  script: 
    - flake8 pyCFS
    - mypy pyCFS
  allow_failure: true

# builds the distributables and publishes a new version on pypi : 
# is triggered via tag / need to add check if tests passed
build-and-publish:
  stage: build-pypi
  script:
    - python -m build
    - TWINE_USERNAME=${TWINE_USERNAME} TWINE_PASSWORD=${TWINE_PASSWORD} python -m twine upload --verbose --skip-existing dist/*
  rules:
    - if: $CI_COMMIT_TAG