name: Build
on: [push, pull_request]
jobs:

  Test-Scripts:
    runs-on: windows-latest
    steps:

      - name: Checkout
        uses: actions/checkout@v2

      - name: Test scripts
        shell: pwsh
        run: |
          $pesterConfig = @{
            Run = @{
              Path = ".\tests\common.Tests.ps1"
              Exit = $true
            }
            TestResult = @{
              Enabled = $true
              OutputPath = "testResults.xml"
              OutputFormat = "NUnitXML"
            }
            Output = @{
              Verbosity = "Detailed"
            }
          }
          Invoke-Pester -Configuration $pesterConfig

      - name: Upload test results
        uses: actions/upload-artifact@v2
        if: ${{ always() }}
        with:
          name: Pester-Test-Results
          path: testResults.xml

  Build:
    runs-on: windows-latest
    needs: Test-Scripts
    strategy:
      fail-fast: false
      matrix:
        config: [x86, x64, x64-standalone]
        build-type: [system]
        include:
          - { config: x64-standalone, build-type: user }
    steps:

      - name: Checkout
        uses: actions/checkout@v2

      - name: Setup MSYS2
        uses: msys2/setup-msys2@v2

      - name: Check OpenOCD repository
        shell: pwsh
        run: |
          $OpenOCDCommit = ((Get-Content .\config\repositories.json | ConvertFrom-Json).repositories | Where-Object { $_.href -like '*openocd.git' } | ForEach-Object { git ls-remote $_.href $_.tree }).Split("`t")[0]
          $Bitness = (Get-Content .\config\${{ matrix.config }}.json | ConvertFrom-Json).bitness
          Add-Content -Path $env:GITHUB_ENV -Value "OPENOCD_BUILD_ID=openocd-$OpenOCDCommit-$Bitness"

      - name: Cache OpenOCD build
        uses: actions/cache@v3
        with:
          path: build/openocd-install
          key: ${{ env.OPENOCD_BUILD_ID }}

      - name: Build ${{ matrix.config }} (${{ matrix.build-type }})
        shell: pwsh
        run: |
          Set-StrictMode -Version Latest
          $ErrorActionPreference = 'Stop'
          $ProgressPreference = 'SilentlyContinue'

          .\build.ps1 .\config\${{ matrix.config }}.json -MSYS2Path (msys2 -c 'cygpath -m /').TrimEnd('\/') -SkipSigning -Compression zlib -BuildType ${{ matrix.build-type }}

      - name: Upload build artifacts
        uses: actions/upload-artifact@v2
        with:
          name: Package-${{ matrix.config }}-${{ matrix.build-type }}
          path: bin/

  Test-Installer:
    runs-on: windows-${{ matrix.winver }}
    needs: Build
    strategy:
      fail-fast: false
      matrix:
        winver: ['2022']
        config: [x64, x64-standalone]
        build-type: [system]
        include:
          - { winver: '2022', config: x64-standalone, build-type: user }
    steps:

      - name: Checkout
        uses: actions/checkout@v2

      - name: Download installer artifact
        uses: actions/download-artifact@v2
        with:
          name: Package-${{ matrix.config }}-${{ matrix.build-type }}
          path: bin

      - name: Test ${{ matrix.config }} (${{ matrix.build-type }})
        shell: pwsh
        run: docker run --rm -v "$(Get-Location):C:\repo" mcr.microsoft.com/windows/servercore:ltsc${{ matrix.winver }}
          powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command "cd C:\repo; .\tests\setup.ps1"

      - name: Upload installer logs
        uses: actions/upload-artifact@v2
        if: ${{ always() }}
        with:
          name: Installer-Logs-Windows-${{ matrix.winver }}-${{ matrix.config }}-${{ matrix.build-type }}
          path: logs/
