name: CI

env:
  LC_COLLATE: en_US.UTF-8
on:
  pull_request:
  push:
    branches:
      - '**'
    tags-ignore:
      - '**'
jobs:
  lint:
    permissions:
      checks: write
      contents: read
      pull-requests: write
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        python-version: [3.9]
        os: [ubuntu-latest]

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

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install build dependencies
        run: |
          python -m pip install flake8

      - uses: reviewdog/action-setup@v1
        with:
          reviewdog_version: nightly
      - run: flake8 | reviewdog -f=flake8 -reporter=github-pr-review -tee -level=error -fail-on-error
        env:
          REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  build-and-test:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        python-version: [3.9]
        os: [ubuntu-22.04, macos-13, windows-latest]

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

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install tox
        run: |
          python -m pip install --upgrade --upgrade-strategy eager tox

      - name: Install macos dependencies
        run: |
          brew upgrade icu4c pkg-config || brew install icu4c pkg-config
        if: runner.os == 'macOS'

      - name: Install linux dependencies
        run: |
          sudo apt-get update && sudo apt-get upgrade && sudo apt-get install pkg-config libicu-dev libqt5gui5 libfuse2 desktop-file-utils
        if: runner.os == 'Linux'

      - name: Build and install PyPi packages
        run: |
          export PKG_CONFIG_PATH="/usr/local/opt/icu4c/lib/pkgconfig:/opt/homebrew/opt/icu4c/lib/pkgconfig${PKG_CONFIG_PATH+:$PKG_CONFIG_PATH}";
          export PATH="/usr/local/opt/icu4c/bin:/usr/local/opt/icu4c/sbin${PATH+:$PATH}"
          python -m tox r -m build
        shell: bash

      - name: Archive production artifacts
        uses: actions/upload-artifact@v4
        with:
          name: "${{ format('ComicTagger-{0}', runner.os) }}"
          path: |
            dist/*.whl
            dist/binary/*.zip
            dist/binary/*.tar.gz
            dist/binary/*.dmg
            dist/binary/*.AppImage

      - name: PyTest
        run: |
          python -m tox r
