---
include:
  - project: 'tango-controls/gitlab-ci-templates'
    file:
      - 'ArchiveWithSubmodules.gitlab-ci.yml'
      - 'cpp.gitlab-ci.yml'
      - 'PreCommit.gitlab-ci.yml'

variables:
    WINDOWS_TANGO_VERSION: 10.1.0
    MYSQL_VERSION: "5.7"
    MYSQL_VERSION_PATCH: "36"
    MSVC_VERSION: v143
    GENERATOR_VERSION: Visual Studio 17 2022
    DEPS_PATH: C:/dependencies
    LIBTANGO: libtango_${WINDOWS_TANGO_VERSION}_${MSVC_VERSION}_${ARCH}_static_release
    DOWNLOAD_URL: "https://gitlab.com/api/v4/projects/24006041/packages/generic/cppTango/${WINDOWS_TANGO_VERSION}/${LIBTANGO}.zip"
    TANGO_PATH: ${DEPS_PATH}/${LIBTANGO}
    DEBIAN_FRONTEND: noninteractive
    GIT_SUBMODULE_STRATEGY: recursive
    TANGO_LINUX_ADDITIONAL_PACKAGES: "libmariadb-dev"
    TANGO_WARNINGS_AS_ERRORS: ON

# See: https://docs.gitlab.com/ce/ci/yaml/README.html#workflowrules-templates
workflow:
  rules:
    - if: $CI_MERGE_REQUEST_IID
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

.build-template-win:
  stage: build
  rules:
    - if: '$ARCH == "x64"'
      variables:
        MYSQL: "mysql-${MYSQL_VERSION}.${MYSQL_VERSION_PATCH}-winx64"
        MYSQL_PATH: "${DEPS_PATH}/${MYSQL}"
    - if: '$ARCH == "x86"'
      variables:
        MYSQL: "mysql-${MYSQL_VERSION}.${MYSQL_VERSION_PATCH}-win32"
        MYSQL_PATH: "${DEPS_PATH}/${MYSQL}"
  before_script:
    - choco install cmake curl -y
    - if (Test-Path "${DEPS_PATH}") { Remove-Item "${DEPS_PATH}" -Recurse -Force; }
    - New-Item "${DEPS_PATH}" -ItemType Directory
      # See LIBTANGO constant at the top for the exact name
    - curl.exe -JOL "${DOWNLOAD_URL}"
    - 7z x "${LIBTANGO}.zip" -o"${DEPS_PATH}"
    - wget.exe "https://dev.mysql.com/get/Downloads/MySQL-${MYSQL_VERSION}/${MYSQL}.zip" -P "${DEPS_PATH}"
    - 7z x "${DEPS_PATH}/${MYSQL}.zip" -o"${DEPS_PATH}"
  script:
    - New-Item build -ItemType Directory
    - cd build
      # We set MySQL_VERSION to avoid a try_run invocation.  The binary
      # requires the VC redistributable which we do not have on the CI
    - cmake -G"${GENERATOR_VERSION}" -A"${PLATFORM}" .. -DTango_ROOT="${TANGO_PATH}" -DMySQL_ROOT="${MYSQL_PATH}" -DTango_FORCE_STATIC=ON -DMySQL_VERSION="${MYSQL_VERSION}.${MYSQL_VERSION_PATH}"
    - cmake --build . --config Release
    - cmake --install .
  after_script:
    - New-Item artifacts -ItemType Directory
    - if (Test-Path "C:\Program Files\TangoDatabase\bin") { Move-Item -Path "C:\Program Files\TangoDatabase\bin\Databaseds.exe" -Destination "artifacts\Databaseds-x64.exe"; }
    - if (Test-Path "C:\Program Files (x86)\TangoDatabase\bin") { Move-Item -Path "C:\Program Files (x86)\TangoDatabase\bin\Databaseds.exe" -Destination "artifacts\Databaseds-x86.exe"; }
  artifacts:
    paths:
      - artifacts
    expire_in: 1 day
  tags:
    - saas-windows-medium-amd64

build-win-64bit:
  extends: .build-template-win
  variables:
    ARCH: x64
    PLATFORM: x64

build-win-32bit:
  extends: .build-template-win
  variables:
    ARCH: x86
    PLATFORM: Win32

macos:
  stage: build
  tags:
    - macos
    - aarch64
    - shell
    - tahoe
  variables:
    PIXI_HOME: "$CI_PROJECT_DIR/.pixi_install"
    PIXI_NO_PATH_UPDATE: "true"
  parallel:
    matrix:
      - PIXI_ARCH: ["aarch64", "x86_64"]
        ENV_TO_TEST: ["default", "tango95"]
  before_script:
    - curl -fsSL https://pixi.sh/install.sh | bash
    - export PATH=${PIXI_HOME}/bin:$PATH
  script:
    - pixi run -e $ENV_TO_TEST build

run-tests-with-mariadb:
  stage: test
  dependencies: []
  tags:
    - amd64
    - docker
    - linux
  image: debian:bookworm
  before_script:
    - apt-get update
    - >
      apt-get install -y
      build-essential
      cmake
      libboost-test-dev
      libtango-dev
      libmariadb-dev-compat
      mariadb-server
      pipx
      python3
      sudo
    - pipx install gcovr==8.4
    - pipx ensurepath
    - source ~/.bashrc
    - sudo cp ci/99_non-default-port.cnf /etc/mysql/mariadb.conf.d
    - sudo cp ci/debian.cnf /etc/mysql
    - service mariadb restart
    - sudo mysql --defaults-file=/etc/mysql/debian.cnf -u root -e "SET PASSWORD = PASSWORD('secret');"
    - echo "TANGO_HOST=127.0.0.1:10000" > /etc/tangorc
  script:
    - mkdir build
    - cd build
    - cmake -DTDB_ENABLE_COVERAGE=ON -DBUILD_TESTING=ON -DMYSQL_ADMIN=root -DMYSQL_HOST="127.0.0.1:4711" -DMYSQL_ADMIN_PASSWD=secret ..
    - make
    - ctest --output-on-failure
    # coverage
    - cd ..
    - mkdir coverage
    - cd coverage
    - >
      gcovr
      --cobertura coverage.xml
      --html-details coverage.html
      --html-self-contained
      --object-directory ${CI_PROJECT_DIR}/build/CMakeFiles/Databaseds.dir
      --root ${CI_PROJECT_DIR}
      --txt
  artifacts:
    when: always
    reports:
      junit: build/JU_*.xml
      coverage_report:
        coverage_format: cobertura
        path: coverage/coverage.xml
    paths:
      - coverage
  coverage: '/^TOTAL.*\s+(\d+\%)$/'
  environment:
    name: code-coverage/$CI_COMMIT_REF_SLUG
    auto_stop_in: 1 week
    url: "https://${CI_PROJECT_NAMESPACE}.gitlab.io/-/${CI_PROJECT_NAME}/-/jobs/${CI_JOB_ID}/artifacts/coverage/coverage.html"

run-tests-with-mysql:
  stage: test
  dependencies: []
  tags:
    - amd64
    - docker
    - linux
  image: ubuntu:noble
  before_script:
    - apt-get update
    - >
      apt-get install -y
      build-essential
      cmake
      libboost-test-dev
      libtango-dev
      libmysqlclient-dev
      mysql-server
      sudo
    - service mysql restart
    - sudo mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'secret';"
    - echo "TANGO_HOST=127.0.0.1:10000" > /etc/tangorc
  script:
    - mkdir build
    - cd build
    - cmake -DBUILD_TESTING=ON -DMYSQL_ADMIN=root -DMYSQL_ADMIN_PASSWD=secret ..
    - make
    - ctest --output-on-failure
  artifacts:
    reports:
      junit: build/JU_*.xml

release_job:
  dependencies: []
  tags:
    - amd64
    - docker
    - linux
  image: registry.gitlab.com/gitlab-org/cli:latest
  needs:
    - run-tests-with-mariadb
    - run-tests-with-mysql
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - echo "Running the release job."
  release:
    tag_name: $CI_COMMIT_TAG
    name: 'Release $CI_COMMIT_TAG'
    description: 'Release $CI_COMMIT_TAG'
    ref: '$CI_COMMIT_SHA'
