on: [push]

name: noentiendo CI

permissions:
  contents: write

jobs:
  desktop-build:
    name: Desktop Build and Style Check
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

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

      - name: Install system libraries
        run: sudo apt-get install libudev-dev

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

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

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

  web-build:
    name: WASM Build, Docs Build, and Web Deploy
    runs-on: ubuntu-latest
    env:
      RUSTDOCFLAGS: "-D warnings"
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

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

      - name: Install system libraries
        run: sudo apt-get install libudev-dev

      - name: Set up Node
        uses: actions/setup-node@v1
        with:
          node-version: 16

      - name: Install wasm-pack
        run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

      - name: Build WASM module
        run: wasm-pack build

      - name: Build Webpack app
        run: |
          cd www
          npm install
          npm run build

      - name: Generate documentation
        uses: actions-rs/cargo@v1
        with:
          command: doc
          args: --no-deps

      - name: Copy files
        run: |
          mkdir pages
          cp -r www/dist/* pages
          cp -r target/doc pages

      - name: Deploy to GitHub Pages
        uses: JamesIves/github-pages-deploy-action@v4
        if: github.ref == 'refs/heads/main'
        with:
          folder: pages

  tests-coverage:
    name: Tests and Coverage
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

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

      - name: Install system libraries
        run: sudo apt-get install libudev-dev

      - name: Run tests and collect coverage
        run: LLVM_PROFILE_FILE='cargo-test-%p-%m.profraw' RUSTFLAGS="-Cinstrument-coverage -Clink-dead-code" cargo test

      - name: Install coverage tools (llvm-tools-preview)
        run: rustup component add llvm-tools-preview

      - name: Install coverage tools (grcov)
        run: curl -L https://github.com/mozilla/grcov/releases/latest/download/grcov-x86_64-unknown-linux-gnu.tar.bz2 | tar jxf -

      - name: Generate coverage report
        run: mkdir ./target/coverage/ && ./grcov . -s . --binary-path ./target/debug/ -t lcov --branch --ignore-not-existing -o ./target/coverage/lcov.info

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v3
        with:
          files: ./target/coverage/lcov.info
          fail_ci_if_error: true
          token: ${{ secrets.CODECOV_TOKEN }}
