variables:
  ITKv5_VERSION: v5.4.0
  ITK_GIT_URL: https://github.com/InsightSoftwareConsortium/ITK
  ITK_SOURCE_DIR: $(Agent.BuildDirectory)/ITK-source
  ITK_BINARY_DIR: $(Agent.BuildDirectory)/ITK-build
  ELASTIX_SOURCE_DIR: $(Build.Repository.LocalPath)
  ELASTIX_BINARY_DIR: $(Agent.BuildDirectory)/Elastix-build

trigger:
  paths:
    exclude:
    - '*.md'
    - LICENSE
    - .github/workflows/*
    - dox/doxygen/*
    - tools/*
pr:
  paths:
    exclude:
    - '*.md'
    - LICENSE
    - .github/workflows/*
    - dox/doxygen/*
    - tools/*

jobs:
- job: Windows
  timeoutInMinutes: 0
  pool:
    vmImage: 'windows-2022'
  strategy:
    matrix:
      ITKv5:
        itk.version: $(ITKv5_VERSION)
  steps:
  - script: |
      git clone $(ITK_GIT_URL) --branch $(itk.version) --depth 1 "$(ITK_SOURCE_DIR)"
    displayName: Clone ITK
  - script: |
      mkdir "$(ITK_BINARY_DIR)"
      mkdir "$(ELASTIX_BINARY_DIR)"
      mkdir externalproject-build
    displayName: Make build directories
  - task: CMake@1
    displayName: 'CMake Generate ITK'
    inputs:
      cmakeArgs: -G "Visual Studio 17 2022" -T host=x64 -DBUILD_EXAMPLES=OFF -DBUILD_TESTING=OFF -DITK_LEGACY_REMOVE=ON "$(ITK_SOURCE_DIR)"
      workingDirectory: "$(ITK_BINARY_DIR)"
  - task: CMake@1
    displayName: 'CMake Build ITK'
    inputs:
      cmakeArgs: --build . --config Release -j 2
      workingDirectory: "$(ITK_BINARY_DIR)"
  - task: CMake@1
    displayName: 'CMake Generate Elastix'
    inputs:
      cmakeArgs: -G "Visual Studio 17 2022" -T host=x64 -DITK_DIR="$(ITK_BINARY_DIR)" -DBUILD_TESTING=ON -DELASTIX_USE_GTEST=ON -DUSE_ALL_COMPONENTS=ON "$(ELASTIX_SOURCE_DIR)"
      workingDirectory: "$(ELASTIX_BINARY_DIR)"
  - task: CMake@1
    displayName: 'CMake Build Elastix'
    inputs:
      cmakeArgs: --build . --config Release -j 2
      workingDirectory: "$(ELASTIX_BINARY_DIR)"
  - task: CMake@1
    displayName: 'CMake Generate externalproject'
    inputs:
      cmakeArgs: -G "Visual Studio 17 2022" -T host=x64 -DElastix_DIR=$(ELASTIX_BINARY_DIR) "$(ELASTIX_SOURCE_DIR)/dox/externalproject"
      workingDirectory: $(Agent.BuildDirectory)/externalproject-build
  - task: CMake@1
    displayName: 'CMake Build externalproject'
    inputs:
      cmakeArgs: --build . --config Release
      workingDirectory: $(Agent.BuildDirectory)/externalproject-build
  - bash: ../../../externalproject-build/Release/elastix_translation_example
    displayName: 'Run externalproject'
    workingDirectory: $(Agent.BuildDirectory)\Elastix-build\bin\Release
  - bash: python -m pip install -r "$(ELASTIX_SOURCE_DIR)/Testing/PythonTests/requirements.txt"
    displayName: 'Install PythonTests requirements'
  - bash: ctest -C Release -VV -j 2 -E "elastix_run_example_COMPARE_IM|elastix_run_3DCT_lung.MI.bspline.ASGD.001_COMPARE_TP"
    displayName: 'CTest Elastix'
    workingDirectory: $(ELASTIX_BINARY_DIR)
- job: Ubuntu
  timeoutInMinutes: 0
  pool:
    vmImage: 'ubuntu-20.04'
  strategy:
    matrix:
      ITKv5:
        itk.version: $(ITKv5_VERSION)
  steps:
  - script: |
      git clone $(ITK_GIT_URL) --branch $(itk.version) --depth 1 "$(ITK_SOURCE_DIR)"
  - script: |
      mkdir $(ITK_BINARY_DIR)
      mkdir $(ELASTIX_BINARY_DIR)
      mkdir externalproject-build
    displayName: Clone ITK
  - task: CMake@1
    displayName: 'CMake Generate ITK'
    inputs:
      cmakeArgs: -DBUILD_EXAMPLES=OFF -DBUILD_TESTING=OFF -DITK_LEGACY_REMOVE=ON $(ITK_SOURCE_DIR)
      workingDirectory: $(ITK_BINARY_DIR)
  - task: CMake@1
    displayName: 'CMake Build ITK'
    inputs:
      cmakeArgs: --build . --config Release -j 2
      workingDirectory: $(ITK_BINARY_DIR)
  - task: CMake@1
    displayName: 'CMake Generate Elastix'
    inputs:
      cmakeArgs: -DITK_DIR=$(ITK_BINARY_DIR) -DBUILD_TESTING=ON -DELASTIX_USE_GTEST=ON -DUSE_ALL_COMPONENTS=ON $(ELASTIX_SOURCE_DIR)
      workingDirectory: $(ELASTIX_BINARY_DIR)
  - task: CMake@1
    displayName: 'CMake Build Elastix'
    inputs:
      cmakeArgs: --build . --config Release -j 2
      workingDirectory: $(ELASTIX_BINARY_DIR)
  - task: CMake@1
    displayName: 'CMake Generate externalproject'
    inputs:
      cmakeArgs: -DElastix_DIR=$(Agent.BuildDirectory)/Elastix-build $(ELASTIX_SOURCE_DIR)/dox/externalproject
      workingDirectory: $(Agent.BuildDirectory)/externalproject-build
  - task: CMake@1
    displayName: 'CMake Build externalproject'
    inputs:
      cmakeArgs: --build .
      workingDirectory: $(Agent.BuildDirectory)/externalproject-build
  - bash: $(Agent.BuildDirectory)/externalproject-build/elastix_translation_example
    displayName: 'Run externalproject'
  - bash: python -m pip install -r "$(ELASTIX_SOURCE_DIR)/Testing/PythonTests/requirements.txt"
    displayName: 'Install PythonTests requirements'
  - bash: ctest -C Release -VV -j 2 -E "elastix_run_example_COMPARE_IM|elastix_run_3DCT_lung.MI.bspline.ASGD.001_COMPARE_TP"
    displayName: 'CTest Elastix'
    workingDirectory: $(ELASTIX_BINARY_DIR)
- job: macOS
  timeoutInMinutes: 0
  pool:
    vmImage: 'macOS-12'
  strategy:
    matrix:
      ITKv5:
        itk.version: $(ITKv5_VERSION)
  steps:
  - script: |
      git clone $(ITK_GIT_URL) --branch $(itk.version) --depth 1 "$(ITK_SOURCE_DIR)"
    displayName: Clone ITK
  - script: |
      mkdir $(ITK_BINARY_DIR)
      mkdir $(ELASTIX_BINARY_DIR)
      mkdir externalproject-build
    displayName: Make build directories
  - task: CMake@1
    displayName: 'CMake Generate ITK'
    inputs:
      cmakeArgs: -DBUILD_EXAMPLES=OFF -DBUILD_TESTING=OFF -DITK_LEGACY_REMOVE=ON $(ITK_SOURCE_DIR)
      workingDirectory: $(ITK_BINARY_DIR)
  - task: CMake@1
    displayName: 'CMake Build ITK'
    inputs:
      cmakeArgs: --build . --config Release -j 2
      workingDirectory: $(ITK_BINARY_DIR)
  - task: CMake@1
    displayName: 'CMake Generate Elastix'
    inputs:
      cmakeArgs: -DITK_DIR=$(ITK_BINARY_DIR) -DBUILD_TESTING=ON -DELASTIX_USE_GTEST=ON -DUSE_ALL_COMPONENTS=ON $(Build.Repository.LocalPath)
      workingDirectory: $(ELASTIX_BINARY_DIR)
  - task: CMake@1
    displayName: 'CMake Build Elastix'
    inputs:
      cmakeArgs: --build . --config Release -j 2
      workingDirectory: $(ELASTIX_BINARY_DIR)
  - task: CMake@1
    displayName: 'CMake Generate externalproject'
    inputs:
      cmakeArgs: -DElastix_DIR=$(Agent.BuildDirectory)/Elastix-build $(ELASTIX_SOURCE_DIR)/dox/externalproject
      workingDirectory: $(Agent.BuildDirectory)/externalproject-build
  - task: CMake@1
    displayName: 'CMake Build externalproject'
    inputs:
      cmakeArgs: --build .
      workingDirectory: $(Agent.BuildDirectory)/externalproject-build
  - bash: $(Agent.BuildDirectory)/externalproject-build/elastix_translation_example
    displayName: 'Run externalproject'
    # Note: On macOS, we have not yet found how to successfully install requirements.txt for Python, so for the time being, we skip the tests.
    # See also https://github.com/SuperElastix/elastix/issues/739
