stages:
  - test
  - build
  - docu

.before_script_template: &before_script_template
  before_script:
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@mygit.th-deg.de/roboct/robo_quality/rq_interfaces
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@mygit.th-deg.de/roboct/robo_quality/rq_hardware
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@mygit.th-deg.de/roboct/robo_quality/rq_controller
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@mygit.th-deg.de/roboct/robo_quality/rq_ddetection
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@mygit.th-deg.de/roboct/robo_quality/rq_trajectory
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@mygit.th-deg.de/roboct/robo_quality/rq_reconstruction
    - apt-get update && apt-get install -y wget python3-pip
    - rosdep install --from-paths . -y --ignore-src
    - colcon build --merge-install

rq-pytests:
  stage: test
  image: ros:humble
  tags: 
    - ros2
  cache:
    key: cache-$CI_COMMIT_REF_SLUG
    paths:
      - install
  <<: *before_script_template
  script:
    - source ./install/setup.bash
    - pip3 install pytest pytest-cov netCDF4 ros2-numpy Pillow pyometiff scipy
    - python3 -m pytest ./rq_controller/test/ --cov=rq_controller --cov-report term --cov-report xml:controller_coverage.xml --junitxml=controller_report.xml --cov-report html:controller_cov_html
  coverage: '/(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/'

  artifacts:
    when: always
    paths:
      - controller_report.xml
      - controller_cov_html
    reports:
      junit: controller_report.xml
      coverage_report:
        coverage_format: cobertura
        path: controller_coverage.xml

rq-pytests-communication:
  stage: test
  image: ros:humble
  tags: 
    - ros2
  cache:
    key: cache-$CI_COMMIT_REF_SLUG
    paths:
      - install
  dependencies:
    - rq-pytests
  script:
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@mygit.th-deg.de/roboct/robo_quality/rq_workflow
    - source ./install/setup.bash
    - apt-get update && apt-get install -y python3-pip
    - pip3 install pytest pytest-cov netCDF4 ros2-numpy Pillow pyometiff scipy
    - pip3 install -r ./rq_workflow/requirements.txt
    - ros2 launch ./rq_workflow/launch/echo_launch.py &
    - python3 -m pytest ./rq_workflow/test/ --junitxml=workflow_report.xml
  artifacts:
    when: always
    paths:
      - workflow_report.xml
    reports:
        junit: workflow_report.xml

  
build-ros:
  stage: build
  image: ros:humble
  tags: 
    - ros2
  cache:
    key: cache-$CI_COMMIT_REF_SLUG
    paths:
      - install
  dependencies:
    - rq-pytests
  script:
    - source ./install/setup.bash
  artifacts:
    when: always
    paths:
      - install
    name: ubuntu_humble

build-ros-win64:
  stage: build
  image: swittl/humble_win64
  tags:
    - windows
  script:
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@mygit.th-deg.de/roboct/robo_quality/rq_interfaces
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@mygit.th-deg.de/roboct/robo_quality/rq_hardware
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@mygit.th-deg.de/roboct/robo_quality/rq_controller
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@mygit.th-deg.de/roboct/robo_quality/rq_ddetection
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@mygit.th-deg.de/roboct/robo_quality/rq_trajectory
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@mygit.th-deg.de/roboct/robo_quality/rq_reconstruction
#    - 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\Tools\\VsDevCmd.bat'
    - C:\dev\ros2_humble\setup.ps1
    - colcon build --merge-install
  cache:
    key: "${CI_COMMIT_REF_SLUG}_windows"
    paths:
      - .cache

  artifacts:
    when: always
    paths:
      - install
    name: win64_humble


create-docu:
  stage: docu
  image: ros:humble
  tags: 
    - ros2
  cache:
    key: cache-$CI_COMMIT_REF_SLUG
    paths:
      - install
  dependencies:
    - rq-pytests
  before_script:
    - apt-get update && apt-get install -y wget python3-pip libgl1-mesa-glx 
    - pip3 install pdoc3 netCDF4 ros2-numpy Pillow pyometiff scipy open3d pathfinding3d
    - git clone https://github.com/wittlsn/aRTist-PythonLib.git -b artist_thd_wip
    - pip3 install ./aRTist-PythonLib/
  script:
    - source ./install/setup.bash
    - mkdir -p doc
    - python3 -m pdoc --html rq_controller -o=./doc/rq_controller -c show_source_code=True
    - python3 -m pdoc --html rq_hardware -o=./doc/rq_hardware -c show_source_code=True
  artifacts:
    when: always
    paths:
      - doc
    name: rq_docstrings