name: VS-CI-Tests

on:
  pull_request:
    types: [opened, synchronize]
  workflow_dispatch:
  push:
    branches:
      - 'master'

env:
  CHOCO_CACHE_DIR: "${{ github.workspace }}/choco-cache"

jobs:

  Windows-CMake-MSVC:
    runs-on: windows-2019
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 1
        fetch-tags: true

    - name: Dependencies Cache
      uses: actions/cache@v3
      env:
        cache-name: cache-chocolatey
      with:
        # cache Chocolatey packages to speed-up the deployment.
        path: |
          ${{ env.CHOCO_CACHE_DIR }}
        key: windows-${{ hashFiles('choco-packages.config') }}

    # Use Chocolatey to install binary dependencies.
    - name: Binary Dependencies (Chocolatey)
      run: |
        choco config set cacheLocation ${{ env.CHOCO_CACHE_DIR }}
        choco install choco-packages.config --no-progress --installargs 'ADD_CMAKE_TO_PATH=System'

    # Use vcpkg to install devel library dependencies.
    - name: Library Dependencies (vcpkg)
      uses: lukka/run-vcpkg@v11
      with:
        vcpkgGitCommitId: '6f29f12e82a8293156836ad81cc9bf5af41fe836'

    - name: Create Build Directory
      working-directory: ${{github.workspace}}
      run: |
        mkdir build

    - name: Configure Build
      working-directory: ${{github.workspace}}
      run: |
        $env:ChocolateyInstall = Convert-Path "$((Get-Command choco).Path)\..\.."
        Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
        refreshenv
        cmake -S . -B build -G "Visual Studio 16 2019" -A x64 "-DCMAKE_TOOLCHAIN_FILE=${{env.VCPKG_ROOT}}/scripts/buildsystems/vcpkg.cmake" -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS=/bigobj -DSOUFFLE_DOMAIN_64BIT=ON -DCMAKE_FIND_LIBRARY_PREFIXES=";lib" -DCMAKE_FIND_LIBRARY_SUFFIXES=".lib;.dll" -DSOUFFLE_USE_CURSES=OFF -DSOUFFLE_USE_ZLIB=ON -DSOUFFLE_USE_SQLITE=ON -DCMAKE_FIND_DEBUG_MODE=FALSE -DSOUFFLE_BASH_COMPLETION=OFF

    - name: Build
      working-directory: ${{github.workspace}}
      run: cmake --build build --config Release -j4

    # Run the tests, Visual Studio must be in the environment because cl.exe is required for compiled Souffle.
    - name: Check interpreter
      working-directory: ${{github.workspace}}/build
      shell: cmd
      run: |
        pushd "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build" & call vcvars64.bat & popd
        ctest --output-on-failure --build-config Release --progress -j4 -L interpreted

    - name: Check others
      working-directory: ${{github.workspace}}/build
      shell: cmd
      run: |
        pushd "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build" & call vcvars64.bat & popd
        ctest --output-on-failure --build-config Release --progress -j2 -LE interpreted

