# GitHub Actions workflow: CI for sic
#
# originally based on: https://github.com/BurntSushi/bstr/blob/master/.github/workflows/ci.yml
# which is licensed under Apache License, Version 2.0 or MIT license

name: "github_actions_ci"
on:
  pull_request:
  push:
    branches:
      - master
      - main
  schedule:
    - cron: '00 01 * * *'
env:
  CI: 1
jobs:
  test:
    name: pipeline_run_tests
    runs-on: ${{ matrix.os }}
    continue-on-error: ${{ matrix.build == 'macos' }}
    strategy:
      matrix:
        build: [pinned, stable, beta, nightly, macos, win-gnu, win-msvc]
        include:
          # pinned rust version :: ubuntu
          - build: pinned
            os: ubuntu-18.04
            rust: 1.45.0

          # latest rust stable :: ubuntu
          - build: stable
            os: ubuntu-latest
            rust: stable

          # latest rust beta :: ubuntu
          - build: beta
            os: ubuntu-latest
            rust: beta

          # latest rust nightly :: ubuntu
          - build: nightly
            os: ubuntu-latest
            rust: nightly

          # latest rust stable :: mac_os
          - build: macos
            os: macOS-latest
            rust: stable

          # latest rust stable :: windows + gnu
          - build: win-gnu
            os: windows-latest
            rust: stable-x86_64-gnu

          # latest rust stable :: windows + msvc
          - build: win-msvc
            os: windows-latest
            rust: stable

    steps:
      - name: checkout_repo
        uses: actions/checkout@v2
      - name: install_rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ matrix.rust }}
          override: true
          profile: minimal


      - name: install_nasm_linux
        if: matrix.build == 'pinned' || matrix.build == 'stable' || matrix.build == 'beta' || matrix.build == 'nightly'
        run: sh tasks/install_nasm_ubuntu.sh
      - name: install_nasm_macos
        if: matrix.build == 'macos'
        run: sh tasks/install_nasm_macos.sh

      - name: build_workspace_crates
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --verbose --all

      - name: test_workspace_crates
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --verbose --all

      - name: test_workspace_with_all_features
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --verbose --all --all-features

      - name: build_and_test_task_pack_release
        if: matrix.build == 'stable' || matrix.build == 'win-msvc'
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --verbose --all --manifest-path tasks/pack-release/Cargo.toml

      - name: build_and_test_task_publish
        if: matrix.build == 'stable' || matrix.build == 'win-msvc'
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --verbose --all --manifest-path tasks/publish/Cargo.toml


  rustfmt:
    name: pipeline_rustfmt
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
      - name: install_rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
          profile: minimal
          components: rustfmt
      - name: check_formatting
        run: |
          cargo fmt --all -- --check
      - name: check_formatting_task_pack_release
        run: |
          cargo fmt --all --manifest-path tasks/pack-release/Cargo.toml -- --check
      - name: check_formatting_task_publish
        run: |
          cargo fmt --all --manifest-path tasks/publish/Cargo.toml -- --check

  clippy:
    name: pipeline_clippy
    runs-on: ubuntu-latest
    steps:
      - name: checkout_repo
        uses: actions/checkout@v2
      - name: install_rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
          profile: minimal
          components: clippy
      - name: install_nasm
        run: sh tasks/install_nasm_ubuntu.sh
      - name: check_with_clippy
        uses: actions-rs/clippy-check@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          args: --all-features --workspace
      - name: check_with_clippy_task_pack_release
        uses: actions-rs/clippy-check@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          args: --all-features --workspace --manifest-path tasks/pack-release/Cargo.toml
      - name: check_with_clippy_task_publish_release
        uses: actions-rs/clippy-check@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          args: --all-features --workspace --manifest-path tasks/publish/Cargo.toml

  cargo_deny:
    name: pipeline_cargo_deny
    runs-on: ubuntu-latest
    strategy:
      matrix:
        checks:
          - advisories
          - bans licenses sources

    continue-on-error: ${{ matrix.checks == 'advisories' }}
    steps:
      - uses: actions/checkout@v2
      - uses: EmbarkStudios/cargo-deny-action@v1

