on: [push, pull_request]

name: Continuous integration

jobs:
  check:
    name: Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - uses: actions-rs/cargo@v1
        with:
          command: check

  test:
    name: Test Suite
    runs-on: ubuntu-latest
    steps:
      - name: "Install dependencies"
        run: |
          sudo apt-get -qq update
          sudo apt-get install -y libxkbcommon-dev
      - uses: actions/checkout@v2
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - uses: actions-rs/cargo@v1
        with:
          command: test

  fmt:
    name: Rustfmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - run: rustup component add rustfmt
      - uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: --all -- --check

  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - run: rustup component add clippy
      - uses: actions-rs/cargo@v1
        with:
          command: clippy
          args: -- -D warnings

  coverage:
    name: Coverage
    needs: fmt
    runs-on: ubuntu-latest
    steps:
      - name: "Install dependencies"
        run: |
          sudo apt-get -qq update
          sudo apt-get install -y libxkbcommon-dev
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Install nightly toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          override: true

      - name: Install cargo-tarpaulin
        uses: actions-rs/install@v0.1
        with:
          crate: cargo-tarpaulin
          version: latest
          use-tool-cache: true

      - name: Coverage with tarpaulin
        run: cargo tarpaulin --all --all-features --timeout 600 --out Lcov -- --test-threads 1

      - name: Upload coverage
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          path-to-lcov: ./lcov.info

  publish:
    name: Publish
    needs: coverage
    runs-on: ubuntu-latest
    if: ${{ false }}  # disable for now
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - run: |
          echo build
          rustup target add wasm32-unknown-unknown
          cargo install -f wasm-bindgen-cli
          cargo build --release --target wasm32-unknown-unknown
          wasm-bindgen target/wasm32-unknown-unknown/release/shakuntala-devi-trainer.wasm --out-dir shakuntala-devi-trainer --web
      - name: Pushes to another repository
        uses: cpina/github-action-push-to-another-repository@main
        env:
          API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
        with:
          source-directory: 'shakuntala-devi-trainer'
          target-directory: 'shakuntala-devi-trainer'
          destination-github-username: 'newca12'
          destination-repository-name: 'newca12.github.com'
          user-email: newca12@users.noreply.github.com
          commit-message: Update shakuntala-devi-trainer
          target-branch: master