name: prebuild

on:
  push:
    branches:
      - master
    tags:
      - '*'
  pull_request:
    branches:
      - master
  workflow_dispatch:

jobs:
  prebuild:
    strategy:
      matrix:
        config:
          - { os: macos-latest, arch: x64 }
          - { os: ubuntu-18.04, arch: x64 }
          - { os: windows-latest, arch: x64 }
          - { os: windows-latest, arch: ia32 }
    runs-on: ${{ matrix.config.os }}
    steps:
      - if: matrix.config.os == 'ubuntu-18.04'
        run: |
          sudo apt-get update
          sudo apt-get install libudev-dev
      - uses: actions/setup-node@v2
        with:
          node-version: 14.x
      - uses: actions/checkout@v2
        with:
          submodules: recursive
      - run: npm install
      # Supported Node versions: https://nodejs.org/en/about/releases/
      - run: npm run prebuild -- -a ${{ matrix.config.arch }}
      - if: startsWith(github.ref, 'refs/tags/')
        shell: bash
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          assets=()
          for asset in ./prebuilds/*; do
            assets+=("-a" "$asset")
          done
          tag_name="${GITHUB_REF##*/}"
          hub release create -m "$tag_name" "$tag_name" || true
          hub release edit "${assets[@]}" -m "" "$tag_name"
