---
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-24.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.12.0"

      - name: test dependencies
        run: |
          luarocks install busted
          luarocks remove luasystem --force

      - name: install
        run: |
          luarocks make

      - 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
            luarocks54 install busted
            luarocks54 remove luasystem --force
          run: |
            LUA_VERSION=5.4 luarocks54 make
            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
            luarocks-5.4 install busted
            luarocks-5.4 remove luasystem --force
          run: |
            LUA_VERSION=5.4 luarocks-5.4 make
            busted --exclude-tags=manual --Xoutput "--color"

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

      - name: Run tests inside NetBSD VM
        uses: vmactions/netbsd-vm@v1
        with:
          usesh: true
          prepare: |
            export PATH=/usr/pkg/bin:/usr/pkg/sbin:/usr/sbin:$PATH
            env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg_add -v pkgin
            pkgin -y update
            pkgin -y install lua54-rocks
            luarocks-5.4 install busted
            luarocks-5.4 remove luasystem --force
          run: |
            eval `luarocks-5.4 path`
            luarocks-5.4 make
            busted --exclude-tags=manual --Xoutput "--color"
