name: CI

on:
  push:
  pull_request:
    branches: [ master ]

env:
  CARGO_TERM_COLOR: always

jobs:
  style:
    name: Check style
    runs-on: ubuntu-latest

    steps:
    - name: Checkout the repo
      uses: actions/checkout@v2

    - name: Install rust
      uses: actions-rs/toolchain@v1
      with:
        toolchain: nightly
        components: rustfmt
        profile: minimal
        override: true

    - name: Cargo fmt
      uses: actions-rs/cargo@v1
      with:
        command: fmt
        args: --all -- --check

  clippy:
    name: Run clippy
    needs: [style]
    runs-on: ubuntu-latest

    steps:
    - name: Checkout the repo
      uses: actions/checkout@v2

    - name: Install rust
      uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        components: clippy
        profile: minimal
        override: true

    - name: Clippy
      uses: actions-rs/cargo@v1
      with:
        command: clippy
        args: --all-targets -- -D warnings

    - name: Clippy without default features
      uses: actions-rs/cargo@v1
      with:
        command: clippy
        # TODO: add `--all-targets` once all warnings in examples are resolved
        args: --no-default-features --features native-tls,warp -- -D warnings

  check-wasm:
    name: linux / WASM
    needs: [clippy]
    runs-on: ubuntu-latest

    steps:
    - name: Checkout the repo
      uses: actions/checkout@v2

    - name: Install rust
      uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        target: wasm32-unknown-unknown
        profile: minimal
        override: true

    - name: Install emscripten
      uses: mymindstorm/setup-emsdk@v7

    - name: Check
      run: |
        cd matrix_sdk/examples/wasm_command_bot
        cargo check --target wasm32-unknown-unknown

  test-appservice:
    name: ${{ matrix.name }}
    needs: [clippy]

    runs-on: ${{ matrix.os || 'ubuntu-latest' }}
    strategy:
      matrix:
        name:
          - linux / appservice / stable / actix
          - macOS / appservice / stable / actix
          - linux / appservice / stable / warp
          - macOS / appservice / stable / warp

        include:
          - name: linux / appservice / stable / actix
            cargo_args: --no-default-features --features actix

          - name: macOS / appservice / stable / actix
            os: macOS-latest
            cargo_args: --no-default-features --features actix

          - name: linux / appservice / stable / warp
            cargo_args: --features warp

          - name: macOS / appservice / stable / warp
            os: macOS-latest
            cargo_args: --features warp

    steps:
      - name: Checkout
        uses: actions/checkout@v1

      - name: Install rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ matrix.rust || 'stable' }}
          target: ${{ matrix.target }}
          profile: minimal
          override: true

      - name: Clippy
        uses: actions-rs/cargo@v1
        with:
          command: clippy
          args: --manifest-path matrix_sdk_appservice/Cargo.toml ${{ matrix.cargo_args }} -- -D warnings

      - name: Build
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --manifest-path matrix_sdk_appservice/Cargo.toml ${{ matrix.cargo_args }}

      - name: Test
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --manifest-path matrix_sdk_appservice/Cargo.toml ${{ matrix.cargo_args }}

  test-features:
    name: ${{ matrix.name }}
    needs: [clippy]

    runs-on: ${{ matrix.os || 'ubuntu-latest' }}
    strategy:
      matrix:
        name:
          - linux / features-no-encryption
          - linux / features-no-sled
          - linux / features-no-encryption-and-sled
          - linux / features-sled_cryptostore
          - linux / features-rustls-tls
          - linux / features-markdown
          - linux / features-socks
          - linux / features-sso_login
          - linux / features-require_auth_for_profile_requests

        include:
          - name: linux / features-no-encryption
            cargo_args: --no-default-features --features "sled_state_store, native-tls"

          - name: linux / features-no-sled
            cargo_args: --no-default-features --features "encryption, native-tls"

          - name: linux / features-no-encryption-and-sled
            cargo_args: --no-default-features --features "native-tls"

          - name: linux / features-sled_cryptostore
            cargo_args: --no-default-features --features "encryption, sled_cryptostore, native-tls"

          - name: linux / features-rustls-tls
            cargo_args: --no-default-features --features rustls-tls

          - name: linux / features-require_auth_for_profile_requests
            cargo_args: --no-default-features --features "require_auth_for_profile_requests, native-tls"

          - name: linux / features-markdown
            cargo_args: --features markdown

          - name: linux / features-socks
            cargo_args: --features socks

          - name: linux / features-sso_login
            cargo_args: --features sso_login

    steps:
      - name: Checkout
        uses: actions/checkout@v1

      - name: Install rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ matrix.rust || 'stable' }}
          target: ${{ matrix.target }}
          profile: minimal
          override: true

      - name: Check
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --manifest-path matrix_sdk/Cargo.toml ${{ matrix.cargo_args }}

      - name: Test
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --manifest-path matrix_sdk/Cargo.toml ${{ matrix.cargo_args }}

  test:
    name: ${{ matrix.name }}
    needs: [clippy]

    runs-on: ${{ matrix.os || 'ubuntu-latest' }}
    strategy:
      matrix:
        name:
          - linux / stable
          - linux / beta
          - macOS / stable

        include:
          - name: linux / stable

          - name: linux / beta
            rust: beta

          - name: macOS / stable
            os: macOS-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v1

      - name: Install rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ matrix.rust || 'stable' }}
          target: ${{ matrix.target }}
          profile: minimal
          override: true

      - name: Build
        uses: actions-rs/cargo@v1
        with:
          command: build

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