name: Test and release when tagged

on: [push, pull_request]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: prefix-dev/setup-pixi@v0.8.10
        with:
          environments: lint
      - run: pixi run --environment lint lint --all-files --show-diff-on-failure

  test-linux:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        environment:
          - test-py311
          - test-py312
          - test-py313
        numpy: [null, "numpy>=1.25,<2.0.0", "numpy>=2.0.0rc1"]
        uncertainties: [null, "uncertainties==3.1.6", "uncertainties>=3.1.6,<4.0.0"]
        extras: [null]
        include:
          - environment: "test-py311" # Minimal versions
            numpy: "numpy>=1.25,<2.0.0"
            extras: matplotlib==3.5.3
          - environment: "test-py311"
            numpy: "numpy"
            uncertainties: "uncertainties"
            extras: "sparse xarray netCDF4 dask[complete]==2024.5.1 graphviz babel==2.8 mip>=1.13"
          - environment: "test-py311"
            numpy: "numpy==1.26.1"
            uncertainties: null
            extras: "babel==2.15 matplotlib==3.9.0"
    steps:
      - uses: actions/checkout@v4
      - uses: prefix-dev/setup-pixi@v0.8.10
        with:
          environments: ${{ matrix.environment }}
          log-level: vvv
      - name: Install numpy
        if: ${{ matrix.numpy != null }}
        run: pip install "${{matrix.numpy}}"

      - name: Install uncertainties
        if: ${{ matrix.uncertainties != null }}
        run: pip install "${{matrix.uncertainties}}"

      - name: Install extras
        if: ${{ matrix.extras != null }}
        run: pip install ${{matrix.extras}}

      - name: Install locales
        if: ${{ matrix.extras != null }}
        run: |
          sudo apt-get update
          sudo apt-get install language-pack-es language-pack-fr language-pack-ro
          sudo localedef -i es_ES -f UTF-8 es_ES
          sudo localedef -i fr_FR -f UTF-8 fr_FR
          sudo localedef -i ro_RO -f UTF-8 ro_RO

      - name: Install dependencies
        run: |
          sudo apt install -y graphviz
          pip install packaging

      - name: Install pytest-mpl
        if: contains(matrix.extras, 'matplotlib')
        run: pip install pytest-mpl
      - run: pixi run --environment ${{ matrix.environment }} test

  test-windows:
    runs-on: windows-latest
    strategy:
      matrix:
        environment:
          - test-py311
          - test-py312
          - test-py313
        numpy: [null, "numpy>=1.25,<2.0.0", "numpy>=2.0.0rc1"]
    steps:
      - uses: actions/checkout@v4
      - uses: prefix-dev/setup-pixi@v0.8.10
        with:
          environments: ${{ matrix.environment }}
          log-level: vvv
      - name: Install numpy
        if: ${{ matrix.numpy != null }}
        run: pip install "${{matrix.numpy}}"
      - run: pixi run --environment ${{ matrix.environment }} test

  test-macos:
    runs-on: macos-latest
    strategy:
      matrix:
        environment:
          - test-py311
          - test-py312
          - test-py313
        numpy: [null, "numpy>=1.25,<2.0.0", "numpy>=2.0.0rc1"]
    steps:
      - uses: actions/checkout@v4
      - uses: prefix-dev/setup-pixi@v0.8.10
        with:
          environments: ${{ matrix.environment }}
          log-level: vvv
      - name: Install numpy
        if: ${{ matrix.numpy != null }}
        run: pip install "${{matrix.numpy}}"
      - run: pixi run --environment ${{ matrix.environment }} test

  publish:
    if: github.ref_type == 'tag'
    needs: [test-linux, test-windows, test-macos, lint]

    runs-on: ubuntu-latest
    environment:
      name: pypi
      url: https://pypi.org/p/pint
    permissions:
      id-token: write  # for trusted publising to PyPI

    steps:
      - uses: actions/checkout@v4
      - uses: prefix-dev/setup-pixi@v0.8.10
        with:
          environments: build
      - name: Build the package
        run: pixi run --environment build build
      - name: Publish to PyPI
        run: pixi run --environment build publish
