---
name: "Build"

concurrency:
  # for PR's cancel the running task, if another commit is pushed
  group: ${{ github.workflow }} ${{ github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

on:
  # build on PR and push-to-main. This works for short-lived branches, and saves
  # CPU cycles on duplicated tests.
  # For long-lived branches that diverge, you'll want to run on all pushes, not
  # just on push-to-main.
  pull_request: {}
  push:
    branches:
      - master

jobs:
  test:
    strategy:
      fail-fast: false
      matrix:
        os: ['ubuntu-20.04', 'macos-13']
        luaVersion:
          - "5.1"
          - "5.2"
          - "5.3"
          - "5.4"
          - "luajit"
          - "luajit-openresty"
        include:
          - os: "macos-latest"
            luaVersion: "5.4"
          # On Windows builds:
          # 'hishamhm/gh-actions-lua' will build the PuC Rio Lua versions using
          # MSVC, and the LuaJIT version using MinGW/gcc. By running against
          # both below, we test both toolchains.
          - os: "windows-latest"
            toolchain: "msvc"
            luaVersion: "5.1"
          - os: "windows-latest"
            toolchain: "msvc"
            luaVersion: "5.2"
          - os: "windows-latest"
            toolchain: "msvc"
            luaVersion: "5.3"
          - os: "windows-latest"
            toolchain: "msvc"
            luaVersion: "5.4"
          - os: "windows-latest"
            toolchain: "mingw"  # unused, other than for display in the UI
            luaVersion: "luajit"

    runs-on: ${{ matrix.os }}

    steps:
      - name: Checkout
        uses: actions/checkout@master

      - name: Setup MSVC
        # the 'hishamhm/gh-actions-lua' step requires msvc to build PuC Rio Lua
        # versions on Windows (LuaJIT will be build using MinGW/gcc).
        if: ${{ matrix.toolchain == 'msvc' }}
        uses: ilammy/msvc-dev-cmd@v1

      # - name: install Dependencies analyzer
      #   # action step used for troubleshooting if Windows dll's build
      #   # incorrectly
      #   run: "
      #     $version = \"1.11.1\"\n
      #     echo \"Installing Dependencies version: $version\"\n
      #     $url = 'https://github.com/lucasg/Dependencies/releases/download/v'
      #         + $version + '/Dependencies_x64_Release.zip'\n
      #     $dest = Join-Path -Path $PWD -ChildPath \".dependencies\"\n

      #     # Download and extract Dependencies\n
      #     New-Item -ItemType Directory -Path \"$dest\"\n
      #     Invoke-WebRequest -Uri $url -OutFile \"$dest\\dependencies.zip\"\n
      #     Expand-Archive -Path \"$dest\\dependencies.zip\" -DestinationPath
      #         \"$dest\"\n
      #     Remove-Item -Path \"$dest\\dependencies.zip\"\n
      #     # dir \"$dest\"\n

      #     # Add Dependencies to PATH\n
      #     $env:PATH += \";$dest\"\n
      #     echo $env:PATH\n

      #     # Verify Dependencies Installation\n
      #     dir \"$dest\\*.exe\"\n
      #     dir \".\\.dependencies\Dependencies.exe\"\n
      #     .\\.dependencies\\Dependencies.exe -help\n

      - uses: hishamhm/gh-actions-lua@master
        with:
          luaVersion: ${{ matrix.luaVersion }}

      - uses: hishamhm/gh-actions-luarocks@master
        with:
          luarocksVersion: "3.11.0"

      - name: install
        run: |
          luarocks make

      - name: test dependencies
        run: |
          luarocks install busted

      - name: test
        run: |
          busted --exclude-tags=manual --Xoutput "--color"

  freebsd:
    runs-on: ubuntu-latest
    name: Run tests on FreeBSD
    steps:
      - uses: actions/checkout@v4

      - name: Run tests inside FreeBSD VM
        uses: vmactions/freebsd-vm@v1
        with:
          usesh: true
          prepare: |
            pkg install -y lua54-luarocks
          run: |
            LUA_VERSION=5.4 luarocks54 make
            luarocks54 install busted
            busted --exclude-tags=manual --Xoutput "--color"

  openbsd:
    runs-on: ubuntu-latest
    name: Run tests on OpenBSD
    steps:
      - uses: actions/checkout@v4

      - name: Run tests inside OpenBSD VM
        uses: vmactions/openbsd-vm@v1
        with:
          usesh: true
          prepare: |
            pkg_add luarocks-lua54
          run: |
            LUA_VERSION=5.4 luarocks-5.4 make
            luarocks-5.4 install busted
            busted --exclude-tags=manual --Xoutput "--color"
