trigger: none
pr: none
schedules:
- cron: "0 23 * * 1-5"
  displayName: Nightly Build
  branches:
    include:
    - master
  always: true

pool:
  vmImage: 'windows-latest'

variables:
  CMAKE_VERSION: 3.30.3

jobs:
- job: Nightly
  displayName: Nightly
  steps:
  - checkout: self

  - script: |
      cmake -G "Visual Studio 17 2022" -B build -A x64 -D BX_CONFIG_DEBUG=ON -D BGFX_CONFIG_MAX_FRAME_BUFFERS=256 -D BABYLON_DEBUG_TRACE=ON
    displayName: 'Generate solution'

  - script: |
      cd Apps
      npm run getNightly
    displayName: 'Download nightly'

  - task: MSBuild@1
    inputs:
      solution: 'build/BabylonNative.sln'
      maximumCpuCount: true
      configuration: 'RelWithDebInfo'
    displayName: 'Build'

  - script: |
      reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\Playground.exe" /f
      reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\Playground.exe" /v DumpType /t REG_DWORD /d 2 /f
      reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\Playground.exe" /v DumpCount /t REG_DWORD /d 1 /f
      reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\Playground.exe" /v DumpFolder /t REG_SZ /d "$(Build.ArtifactStagingDirectory)/Dumps" /f
      reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\UnitTests.exe" /f
      reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\UnitTests.exe" /v DumpType /t REG_DWORD /d 2 /f
      reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\UnitTests.exe" /v DumpCount /t REG_DWORD /d 1 /f
      reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\UnitTests.exe" /v DumpFolder /t REG_SZ /d "$(Build.ArtifactStagingDirectory)/Dumps" /f
    displayName: 'Enable Crash Dump'

  - script: |
      Playground.exe app:///Scripts/validation_native.js
    workingDirectory: build/Apps/Playground/RelWithDebInfo
    displayName: 'Run VisualizationTests'

  - script: |
      UnitTests.exe
    workingDirectory: build/Apps/UnitTests/RelWithDebInfo
    displayName: 'Run UnitTests'

  - task: PublishBuildArtifacts@1
    displayName: Upload Errors images artifact
    condition: failed()
    inputs:
      PathtoPublish: build/Apps/Playground/Errors
      ArtifactName: ErrorImages

  - task: CopyFiles@2
    inputs:
      sourceFolder: "build/Apps/Playground/RelWithDebInfo"
      contents: Playground.*
      targetFolder: "$(Build.ArtifactStagingDirectory)/Dumps"
      cleanTargetFolder: false
    displayName: "Stage Playground Crash Dump"
    condition: failed()

  - task: CopyFiles@2
    inputs:
      sourceFolder: "build/Apps/UnitTests/RelWithDebInfo"
      contents: UnitTests.*
      targetFolder: "$(Build.ArtifactStagingDirectory)/Dumps"
      cleanTargetFolder: false
    displayName: "Stage UnitTests Crash Dump"
    condition: failed()

  - task: PublishBuildArtifacts@1
    inputs:
      artifactName: "Crash Dumps"
      pathtoPublish: "$(Build.ArtifactStagingDirectory)/Dumps"
    displayName: "Publish Crash Dumps"
    condition: failed()

  - task: PublishBuildArtifacts@1
    displayName: Upload Playground
    inputs:
      PathtoPublish: build/Apps/Playground
      ArtifactName: BabylonNativeNightlyPlayground

  - task: PublishBuildArtifacts@1
    displayName: Upload UnitTests
    inputs:
      PathtoPublish: build/Apps/UnitTests
      ArtifactName: BabylonNativeNightlyUnitTests
