name: Lean Action CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build-and-test:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        
      - name: Install Lean (elan)
        shell: bash
        run: |
          curl https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh -sSf | sh -s -- -y
          source $HOME/.elan/env

      - name: Set up olean cache
        uses: actions/cache@v3
        with:
          path: ~/.cache
          key: oleans

      - name: Configure
        working-directory: src/lean4
        run: |
          source $HOME/.elan/env
          lake exe cache get

      - name: Build the lean project
        run: |
          source $HOME/.elan/env
          pushd src/lean4 && (lake build > lean4_build.log) && popd
        
      - name: Set up Python 3.10
        uses: actions/setup-python@v4
        with:
          python-version: '3.10'

      - name: Upgrade pip and install Hatch
        run: |
          python -m pip install --upgrade pip
          pip install build

      - name: Build the package using Hatch
        run: |
          python -m build

      - name: Install the built package
        run: |
          pip install dist/*.whl
      
      - name: Build clever lean4
        run: |
          source $HOME/.elan/env
          clever-bench-install
      
      - name: Run Python Tests
        run: |
          source $HOME/.elan/env        
          python src/tests/test_lean_parser.py
          python src/tests/test_task.py