name: Rust

on: [push, pull_request, workflow_dispatch]

env:
  CARGO_TERM_COLOR: always
  DATABASE_URL: "sqlite://./dim_dev.db"

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - run: sudo apt-get update
      - run: sudo apt-get install -y libva-dev libva-drm2 libva2 sqlite3 ffmpeg
      - uses: actions/checkout@v2

      - name: Download UI artifacts
        uses: dawidd6/action-download-artifact@v2
        with:
          workflow: react.yml
          workflow_conclusion: success
          branch: master
          name: webui
          path: ui/build
          repo: ${{github.repository}}

      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: nightly-2022-05-31
          override: true
          components: cargo

      - name: Download ffmpeg artifact
        uses: dsaltares/fetch-gh-release-asset@master
        with:
          repo: "Dusk-Labs/ffmpeg-static"
          version: "tags/ffmpeg-all-0.0.1"
          file: "ffmpeg"
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Download ffprobe artifact
        uses: dsaltares/fetch-gh-release-asset@master
        with:
          repo: "Dusk-Labs/ffmpeg-static"
          version: "tags/ffmpeg-all-0.0.1"
          file: "ffprobe"
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Create release dir
        run: |
          mkdir -p release/utils
          cp ffmpeg release/utils/ffmpeg
          cp ffprobe release/utils/ffprobe
          chmod +x release/utils/ffmpeg
          chmod +x release/utils/ffprobe

      - name: Test Dim
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --tests

      - name: Build Dim
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --verbose --release

      - name: Copy dim binaries
        run: |
          cp target/release/dim release/dim
          chmod +x release/dim

      - name: Compress release dir
        run: tar -zcvf release.tar.gz release

      - name: Save artifacts
        uses: actions/upload-artifact@v2
        with:
          name: release
          path: release.tar.gz
