# Installs Rust and checks formatting + linting

name: Lint Rust

on:
  workflow_call:
  push:
    branches:
      - main
    tags:
      - "v*"
  pull_request:
    paths-ignore:
      - "src/**"
      - "package.json"
      - "package-lock.json"
      - "yarn.lock"
      - "pnpm-lock.yaml"
      - "README.md"

jobs:
  build:
    strategy:
      fail-fast: true
      matrix:
        os: [macos-latest]

    runs-on: ${{ matrix.os }}

    steps:
      - name: Disable git core.autocrlf on Windows
        if: matrix.os == 'windows-latest'
        run: git config --global core.autocrlf false

      - name: Checkout repository code
        uses: actions/checkout@v4

      - name: Set up pnpm package manager
        uses: pnpm/action-setup@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version-file: .node-version
          cache: "pnpm"

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy

      - name: Install dependencies (Ubuntu only)
        if: matrix.os == 'ubuntu-latest'
        run: |
          sudo apt-get update
          sudo apt install libwebkit2gtk-4.1-dev build-essential curl wget file libxdo-dev libssl-dev libayatana-appindicator3-dev librsvg2-dev

      - name: Install frontend dependencies
        run: pnpm install --frozen-lockfile

      - name: Install nodecar dependencies
        working-directory: ./nodecar
        run: |
          pnpm install --ignore-workspace --frozen-lockfile

      - name: Build nodecar binary
        shell: bash
        working-directory: ./nodecar
        run: |
          if [[ "${{ matrix.os }}" == "ubuntu-latest" ]]; then
            pnpm run build:linux-x64
          elif [[ "${{ matrix.os }}" == "macos-latest" ]]; then
            pnpm run build:aarch64
          elif [[ "${{ matrix.os }}" == "windows-latest" ]]; then
            pnpm run build:win-x64
          fi

      - name: Copy nodecar binary to Tauri binaries
        shell: bash
        run: |
          mkdir -p src-tauri/binaries
          if [[ "${{ matrix.os }}" == "ubuntu-latest" ]]; then
            cp nodecar/dist/nodecar src-tauri/binaries/nodecar-x86_64-unknown-linux-gnu
          elif [[ "${{ matrix.os }}" == "macos-latest" ]]; then
            cp nodecar/dist/nodecar src-tauri/binaries/nodecar-aarch64-apple-darwin
          elif [[ "${{ matrix.os }}" == "windows-latest" ]]; then
            cp nodecar/dist/nodecar.exe src-tauri/binaries/nodecar-x86_64-pc-windows-msvc.exe
          fi

      - name: Create empty 'dist' directory
        run: mkdir dist

      - name: Run rustfmt check
        run: cargo fmt --all -- --check
        working-directory: src-tauri

      - name: Run clippy check and deny warnings
        run: cargo clippy --all-targets --all-features -- -D warnings -D clippy::all
        working-directory: src-tauri

      - name: Run Rust unit tests
        run: cargo test
        working-directory: src-tauri
