trigger:
- development
- master
- 2.0-cicd

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'win-$(arch)'
  buildConfiguration: 'Release'
  wasiVer: 'wasi-sdk-24.0-$(wasi-arch)-windows'
  wasiUrl: 'https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-24/$(wasiVer).tar.gz'

steps:
- task: UseDotNet@2
  displayName: 'Install .NET SDK'
  inputs:
    packageType: 'sdk'
    version: '$(dotnetVersion)'
    
- task: CmdLine@2
  displayName: 'Download WASI SDK'
  inputs:
    script: |
      curl -L -o $(wasiVer).tar.gz $(wasiUrl)

- task: CmdLine@2
  displayName: 'Unpack WASI SDK'
  inputs:
    script: |
      tar -xzf $(wasiVer).tar.gz
      echo "Contents of directory after extraction:"
      dir $(wasiVer)

- task: PowerShell@2
  displayName: 'Set Environment Path for WASI SDK'
  inputs:
    targetType: 'inline'
    script: |
      $env:WASI_SDK_PATH = "$(Get-Location)\$(wasiVer)"
      Write-Host "##vso[task.setvariable variable=WASI_SDK_PATH]$env:WASI_SDK_PATH"

- task: PowerShell@2
  displayName: 'Verify Environment Path'
  inputs:
    targetType: 'inline'
    script: |
      Write-Host "Environment path set to: $env:WASI_SDK_PATH"


- task: NuGetToolInstaller@1

- task: Cache@2
  displayName: Cache dotnet workloads
  inputs:
    key: 'dotnetWorkloads-windows'
    path: 'C:\ProgramData\dotnet\workloads'

- task: DotNetCoreCLI@2
  displayName: Install wasi-wasm
  inputs:
    command: 'custom'
    custom: 'workload'
    arguments: 'install wasi-experimental'

- task: DotNetCoreCLI@2
  displayName: Install wasm-tools
  inputs:
    command: 'custom'
    custom: 'workload'
    arguments: 'install wasm-tools'

- task: NuGetCommand@2
  displayName: 'Restore solution'
  inputs:
    restoreSolution: '$(solution)'

- task: DotNetCoreCLI@2
  displayName: Build
  inputs:
    command: 'build'
    projects: '**/*.csproj'
    arguments: '--configuration Release -r $(buildPlatform)'

- task: DotNetCoreCLI@2
  displayName: Tests
  inputs:
    command: test
    projects: '**/*Tests/*.csproj'
    arguments: '--configuration $(buildConfiguration) -r $(buildPlatform)'