name: Test and publish

on: push

jobs:
  test:
    name: Run tests
    runs-on: ubuntu-latest

    strategy:
      matrix:
        python-version:
          - "3.8"
          - "3.9"
          - "3.10"
          - "3.11"
          - "3.12"
          - "3.13"
          - "3.14"

    steps:
      - uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Install uv with Python ${{ matrix.python-version }}
        uses: astral-sh/setup-uv@v7
        with:
          python-version: ${{ matrix.python-version }}

      - name: Lint with ruff
        uses: astral-sh/ruff-action@v3

      - name: Run tests
        run: uv run --locked --all-groups --all-extras pytest -v

  publish:
    name: Publish to PyPI
    if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags/v')
    runs-on: ubuntu-latest

    needs: test

    environment:
      name: pypi
      url: https://pypi.org/p/lingua
    permissions:
      id-token: write

    steps:
      - uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@v7

      - name: Insert version number
        run: |
          version="${GITHUB_REF#*tags/v}"
          sed -i -e "s/0\.0\.0/$version/" src/lingua/__init__.py pyproject.toml

      - name: Build wheel
        run: uv build

      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
