name: Windows Build

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches:
      - master
    paths-ignore:
      - .gitignore
      - "**/*.md"
      - .clang-format
      - debian-packager/
      - bin/PCSX2_keys.ini.default
      - "plugins/onepad/**"
      - "plugins/onepad_legacy/**"
      - .travis.yml # TODO - remove with travis-ci
      - appveyor.yml # TODO - remove with appveyor
  pull_request:
    branches:
      - master
    paths-ignore:
      - .gitignore
      - "**/*.md"
      - .clang-format
      - debian-packager/
      - bin/PCSX2_keys.ini.default
      - "plugins/onepad/**"
      - "plugins/onepad_legacy/**"
      - .travis.yml # TODO - remove with travis-ci
      - appveyor.yml # TODO - remove with appveyor

jobs:
  build:
    strategy:
      # Prevent one build from failing everything (although maybe those should be included as experimental builds instead)
      fail-fast: false
      matrix:
        os: [windows-2019]
        platform: [Win32, x64]
        experimental: [false]

    name: ${{ matrix.os }}-${{ matrix.platform }}
    runs-on: ${{ matrix.os }}
    continue-on-error: ${{ matrix.experimental }}
    # Set some sort of timeout in the event of run-away builds.  We are limited on concurrent jobs so, get rid of them.
    timeout-minutes: 30

    steps:
      # NOTE - useful for debugging
      # - name: Dump GitHub context
      #   env:
      #     GITHUB_CONTEXT: ${{ toJson(github) }}
      #   run: |
      #     echo "$GITHUB_CONTEXT"

      - name: Checkout Repository
        uses: actions/checkout@v2

      - name: Cache Submodules
        id: cache-submodules
        uses: actions/cache@v2
        with:
          key: submodules-${{ hashFiles('./.gitmodules') }}
          path: |
            ./3rdparty/xz
            ./3rdparty/gtest
            ./.git/modules/

      - name: Checkout Submodules
        if: steps.cache-submodules.outputs.cache-hit != 'true'
        run: git submodule update --init --recursive --jobs 2

      - name: Prepare Artifact Git Info
        shell: bash
        run: |
          echo "##[set-output name=branch;]${GITHUB_REF#refs/heads/}"
          ARTIFACT_NAME="commit-$(git rev-parse --short "$GITHUB_SHA")"
          if [ ${{ github.event_name == 'pull_request' }} ]; then
            echo "##[set-output name=short-sha;]$(git rev-parse --short "${{ github.event.pull_request.head.sha }}")"
            if [ ! -z "${{ github.event.pull_request.number }}" ]; then
              ARTIFACT_NAME="pr-${{ github.event.pull_request.number }}-commit-$(git rev-parse --short "${{ github.event.pull_request.head.sha }}")"
            fi
          else
            echo "##[set-output name=short-sha;]$(git rev-parse --short "$GITHUB_SHA")"
          fi
          echo "##[set-output name=artifact-metadata;]${ARTIFACT_NAME}"
        id: git-vars

      - name: Verify VS Project Files
        shell: powershell
        run: .\.github\workflows\scripts\validate-vs-filters.ps1

      - name: Setup msbuild
        uses: microsoft/setup-msbuild@v1.0.1
        with:
          vs-version: 16.7

      - name: Build PCSX2
        run: msbuild "buildbot.xml" /m /v:n /t:ReleaseAll /p:Platform=${{ matrix.platform }}

      - name: Upload artifact
        uses: actions/upload-artifact@v2
        continue-on-error: true
        with:
          name: PCSX2-${{ matrix.platform }}-${{ steps.git-vars.outputs.artifact-metadata }}
          path: bin
