name: Automated Builds

on:
  push:
    branches:
      - '**'
    tags:
      # Run when pushing version tags, since otherwise it's impossible to
      # restart a successful build after pushing a tag
      - '*.*.*'
  pull_request:
    branches:
      - master

defaults:
  run:
    # This otherwise gets run under dash which does not support brace expansion
    shell: bash

jobs:
  # We'll only package the plugins with an entry in bundler.toml
  package:
    strategy:
      matrix:
        include:
          - { name: ubuntu-18.04,       os: ubuntu-18.04,   cross-target: '' }
          - { name: macos-10.15-x86_64, os: macos-10.15,    cross-target: '' }
          - { name: macos-11-aarch64,   os: macos-11,       cross-target: aarch64-apple-darwin }
          - { name: windows,            os: windows-latest, cross-target: '' }
    name: Package plugin binaries
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - name: Fetch all git history
        run: git fetch --force --prune --tags --unshallow

      - name: Install dependencies
        if: startsWith(matrix.os, 'ubuntu')
        run: |
          sudo apt-get update
          sudo apt-get install -y libasound2-dev libgl-dev libjack-dev libxcb1-dev libxcb-icccm4-dev libxcursor-dev libxkbcommon-dev libxcb-shape0-dev libxcb-xfixes0-dev

      - uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.name }}-${{ matrix.cross-target }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Set up Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          # FIXME: Needed for SIMD. Diopser can be compiled without SIMD support
          #        though, we'd actually need to test whether both versions
          #        compile
          # FIXME: The latest nightly won't produce the symbols needed for CLAP:
          #        https://github.com/rust-lang/rust/pull/93718
          #        https://github.com/rust-lang/rust/issues/99596
          toolchain: nightly-2022-07-15
          profile: minimal
          default: true
          # The macOS AArch64 build is done from an x86_64 macOS CI runner, so
          # it needs to be cross compiled
          target: ${{ matrix.cross-target }}
      - name: Package all targets from bundler.toml
        # Instead of hardcoding which targets to build and package, we'll
        # package everything that's got en entry in the `bundler.toml` file
        run: |
          # Building can be sped up by specifying all packages in one go
          package_args=()
          for package in $(cargo xtask known-packages); do
            package_args+=("-p" "$package")
          done

          cross_target=${{ matrix.cross-target }}
          if [[ -n $cross_target ]]; then
            package_args+=("--target" "$cross_target")
          fi

          cargo xtask bundle "${package_args[@]}" --release

      - name: Determine build archive name
        run: |
          echo "ARCHIVE_NAME=nih-plugs-$(git describe --always)-${{ matrix.name }}" >> "$GITHUB_ENV"
      - name: Move all packaged plugin into a directory
        run: |
          # GitHub Action strips the top level directory, great, have another one
          mkdir -p "$ARCHIVE_NAME/$ARCHIVE_NAME"
          mv target/bundled/* "$ARCHIVE_NAME/$ARCHIVE_NAME"
      - name: Add an OS-specific readme file with installation instructions
        run: cp ".github/workflows/readme-${{ runner.os }}.txt" "$ARCHIVE_NAME/$ARCHIVE_NAME/README.txt"
      - uses: actions/upload-artifact@v2
        with:
          name: ${{ env.ARCHIVE_NAME }}
          path: ${{ env.ARCHIVE_NAME }}