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

variables:
  WINDOWS_TANGO_VERSION: 10.0.2
  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}
  GIT_SUBMODULE_STRATEGY: recursive
  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-tangotest-win:
  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}"
  script:
    - New-Item build -ItemType Directory
    - cd build
    - cmake -G"${GENERATOR_VERSION}" -A"${PLATFORM}" .. -DTango_ROOT="${TANGO_PATH}" -DTango_FORCE_STATIC=ON
    - cmake --build . --config Release
    - cmake --install .
  after_script:
    - New-Item artifacts -ItemType Directory
    - if (Test-Path "C:\Program Files\TangoTest\bin") { Move-Item -Path "C:\Program Files\TangoTest\bin\TangoTest.exe" -Destination "artifacts\TangoTest-x64.exe"; }
    - if (Test-Path "C:\Program Files (x86)\TangoTest\bin") { Move-Item -Path "C:\Program Files (x86)\TangoTest\bin\TangoTest.exe" -Destination "artifacts\TangoTest-x86.exe"; }
  artifacts:
    paths:
      - artifacts
    expire_in: 1 day
  tags:
    - saas-windows-medium-amd64

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

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

release-builds:
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  tags:
    - docker, linux, amd64
  needs:
    - build-tangotest-win-32bit
    - build-tangotest-win-64bit
    - build-linux
  variables:
    # WARNING! This variable name is used in ArchiveWithSubmodules.gitlab-ci.yml template
    PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${CI_PROJECT_NAME}"
  before_script:
    - apk update
    - apk add curl
    - apk --no-cache add findutils
  script:
    - cd artifacts
    - >
      find . -type f -printf "%f\n" -exec curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file {} "${PACKAGE_REGISTRY_URL}/${CI_COMMIT_TAG}/{}" \;
  release:
    name: 'Release $CI_COMMIT_TAG'
    tag_name: '$CI_COMMIT_TAG'
    description: 'Release $CI_COMMIT_TAG'
    ref: '$CI_COMMIT_SHA'
    assets:
      links:
        - name: 'TangoTest-x64.exe'
          url: "${PACKAGE_REGISTRY_URL}/${CI_COMMIT_TAG}/TangoTest-x64.exe"
        - name: 'TangoTest-x86.exe'
          url: "${PACKAGE_REGISTRY_URL}/${CI_COMMIT_TAG}/TangoTest-x86.exe"
  rules:
    - if: $CI_COMMIT_TAG
      when: on_success
