# https://aka.ms/yaml
trigger:
  branches:
    include:
    - master
    - staging
  tags:
    include:
      - 'v*'

jobs:
- job: Build
  pool:
    vmImage: 'windows-2019' # other options: 'macOS-10.13', 'ubuntu-16.04'
  strategy:
    matrix:
      cp36-cp36m-win_amd64.whl:
        python.version: '3.6'
        python.architecture: 'x64'
      cp37-cp37m-win_amd64.whl:
        python.version: '3.7'
        python.architecture: 'x64'
      cp38-cp38-win_amd64.whl:
        python.version: '3.8'
        python.architecture: 'x64'
      cp39-cp39-win_amd64.whl:
        python.version: '3.9'
        python.architecture: 'x64'
      cp310-cp310-win_amd64.whl:
        python.version: '3.10'
        python.architecture: 'x64'
      cp311-cp311-win_amd64.whl:
        python.version: '3.11'
        python.architecture: 'x64'
      cp312-cp312-win_amd64.whl:
        python.version: '3.12'
        python.architecture: 'x64'
      cp313-cp313-win_amd64.whl:
        python.version: '3.13'
        python.architecture: 'x64'
      cp36-cp36m-win32.whl:
        python.version: '3.6'
        python.architecture: 'x86'
      cp37-cp37m-win32.whl:
        python.version: '3.7'
        python.architecture: 'x86'
      cp38-cp38-win32.whl:
        python.version: '3.8'
        python.architecture: 'x86'
      cp39-cp39-win32.whl:
        python.version: '3.9'
        python.architecture: 'x86'
      cp310-cp310-win32.whl:
        python.version: '3.10'
        python.architecture: 'x86'
      cp311-cp311-win32.whl:
        python.version: '3.11'
        python.architecture: 'x86'
      cp312-cp312-win32.whl:
        python.version: '3.12'
        python.architecture: 'x86'
      cp313-cp313-win32.whl:
        python.version: '3.13'
        python.architecture: 'x86'
    maxParallel: 6

  steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '$(python.version)'
      architecture: '$(python.architecture)'

  # Add additional tasks to run using each Python version in the matrix above

  # PowerShell
  # Run a PowerShell script on Windows, macOS, or Linux.
  - task: PowerShell@2
    inputs:
      targetType: 'inline' # Optional. Options: filePath, inline
      #arguments: # Optional
      script: | # Required when targetType == Inline
        pwd
        Invoke-WebRequest -Uri "https://www.klayout.org/downloads/klayout-microbits-4.0.zip" -OutFile klayout-microbits-4.0.zip
        dir
        Expand-Archive klayout-microbits-4.0.zip -DestinationPath klayout-microbits
        dir klayout-microbits
      #errorActionPreference: 'stop' # Optional. Options: stop, continue, silentlyContinue
      #failOnStderr: false # Optional
      #ignoreLASTEXITCODE: false # Optional
      #pwsh: false # Optional
      #workingDirectory: # Optional
    displayName: 'Download and Extract KLayout bits'

  - script: |
      # setuptools 67.0.0 is not working as of now (pypa/setuptools#4885)
      python -m pip install --upgrade pip "setuptools<76.0.0" wheel
    displayName: 'Update pip, setuptools and wheel'

  - script: |
      python -V
      set "KLAYOUT_BITS=%cd%\klayout-microbits\klayout-microbits-4.0\msvc2017\%PYTHON_ARCHITECTURE%"
      echo KLAYOUT_BITS=%KLAYOUT_BITS%
      python setup.py bdist_wheel
    displayName: 'Build KLayout'

  - bash: |
      bash `pwd`/ci-scripts/windows/fix_wheel.sh `pwd`/dist/*.whl "`pwd`/klayout-microbits/klayout-microbits-4.0/msvc2017/$PYTHON_ARCHITECTURE"
    displayName: 'Copy klayout bits dlls into wheel'

  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: |
        set -e
        echo $PATH
        export TESTSRC=.
        pip install klayout --no-index -f dist
        python testdata/pymod/import_db.py
        python testdata/pymod/import_rdb.py
        python testdata/pymod/import_tl.py
        python testdata/pymod/import_lib.py
        python testdata/pymod/import_lay.py
        python testdata/pymod/pya_tests.py
    displayName: 'Test KLayout pymod'

  - task: CopyFiles@2
    condition: always()
    inputs:
      sourceFolder: '$(Build.SourcesDirectory)'
      targetFolder: '$(Build.ArtifactStagingDirectory)'
      contents: '**/?(*.whl)'

  - task: PublishBuildArtifacts@1
    condition: always()
    inputs:
      pathtoPublish: '$(Build.ArtifactStagingDirectory)'
      artifactName: 'wheel-$(python.version).$(python.architecture)'

- job: 'Deploy'
  displayName: 'Combine Windows wheels and deploy to PyPI'
  dependsOn: Build
  pool:
    vmImage: 'windows-2019' # other options: 'macOS-10.13', 'ubuntu-16.04'
  steps:
    - checkout: none #skip checking out the default repository resource
    - task: DownloadBuildArtifacts@0
      displayName: 'Download Build Artifacts wheel-3.13.x64'
      inputs:
        artifactName: 'wheel-3.13.x64'
        downloadPath: '$(System.DefaultWorkingDirectory)'
    - task: DownloadBuildArtifacts@0
      displayName: 'Download Build Artifacts wheel-3.12.x64'
      inputs:
        artifactName: 'wheel-3.12.x64'
        downloadPath: '$(System.DefaultWorkingDirectory)'
    - task: DownloadBuildArtifacts@0
      displayName: 'Download Build Artifacts wheel-3.11.x64'
      inputs:
        artifactName: 'wheel-3.11.x64'
        downloadPath: '$(System.DefaultWorkingDirectory)'
    - task: DownloadBuildArtifacts@0
      displayName: 'Download Build Artifacts wheel-3.10.x64'
      inputs:
        artifactName: 'wheel-3.10.x64'
        downloadPath: '$(System.DefaultWorkingDirectory)'
    - task: DownloadBuildArtifacts@0
      displayName: 'Download Build Artifacts wheel-3.9.x64'
      inputs:
        artifactName: 'wheel-3.9.x64'
        downloadPath: '$(System.DefaultWorkingDirectory)'
    - task: DownloadBuildArtifacts@0
      displayName: 'Download Build Artifacts wheel-3.8.x64'
      inputs:
        artifactName: 'wheel-3.8.x64'
        downloadPath: '$(System.DefaultWorkingDirectory)'
    - task: DownloadBuildArtifacts@0
      displayName: 'Download Build Artifacts wheel-3.7.x64'
      inputs:
        artifactName: 'wheel-3.7.x64'
        downloadPath: '$(System.DefaultWorkingDirectory)'
    - task: DownloadBuildArtifacts@0
      displayName: 'Download Build Artifacts wheel-3.6.x64'
      inputs:
        artifactName: 'wheel-3.6.x64'
        downloadPath: '$(System.DefaultWorkingDirectory)'
    - task: DownloadBuildArtifacts@0
      displayName: 'Download Build Artifacts wheel-3.13.x86'
      inputs:
        artifactName: 'wheel-3.13.x86'
        downloadPath: '$(System.DefaultWorkingDirectory)'
    - task: DownloadBuildArtifacts@0
      displayName: 'Download Build Artifacts wheel-3.12.x86'
      inputs:
        artifactName: 'wheel-3.12.x86'
        downloadPath: '$(System.DefaultWorkingDirectory)'
    - task: DownloadBuildArtifacts@0
      displayName: 'Download Build Artifacts wheel-3.11.x86'
      inputs:
        artifactName: 'wheel-3.11.x86'
        downloadPath: '$(System.DefaultWorkingDirectory)'
    - task: DownloadBuildArtifacts@0
      displayName: 'Download Build Artifacts wheel-3.10.x86'
      inputs:
        artifactName: 'wheel-3.10.x86'
        downloadPath: '$(System.DefaultWorkingDirectory)'
    - task: DownloadBuildArtifacts@0
      displayName: 'Download Build Artifacts wheel-3.9.x86'
      inputs:
        artifactName: 'wheel-3.9.x86'
        downloadPath: '$(System.DefaultWorkingDirectory)'
    - task: DownloadBuildArtifacts@0
      displayName: 'Download Build Artifacts wheel-3.8.x86'
      inputs:
        artifactName: 'wheel-3.8.x86'
        downloadPath: '$(System.DefaultWorkingDirectory)'
    - task: DownloadBuildArtifacts@0
      displayName: 'Download Build Artifacts wheel-3.7.x86'
      inputs:
        artifactName: 'wheel-3.7.x86'
        downloadPath: '$(System.DefaultWorkingDirectory)'
    - task: DownloadBuildArtifacts@0
      displayName: 'Download Build Artifacts wheel-3.6.x86'
      inputs:
        artifactName: 'wheel-3.6.x86'
        downloadPath: '$(System.DefaultWorkingDirectory)'
    - task: CopyFiles@2
      condition: always()
      inputs:
        sourceFolder: '$(System.DefaultWorkingDirectory)'
        targetFolder: '$(Build.ArtifactStagingDirectory)'
        contents: '**/?(*.whl)'
        flattenFolders: true
    - task: PublishBuildArtifacts@1
      condition: always()
      inputs:
        pathtoPublish: '$(Build.ArtifactStagingDirectory)'
        artifactName: 'windows_wheels'
    - bash: |
        pip install -U twine
        twine upload --skip-existing $BUILD_ARTIFACTSTAGINGDIRECTORY/*
      env:
        TWINE_USERNAME: $(TWINE_USERNAME)
        TWINE_PASSWORD: $(TWINE_PASSWORD)
        TWINE_REPOSITORY_URL: $(TWINE_REPOSITORY_URL)
      displayName: 'Uploading to PyPI'
      condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/')

