name: Linux Tests

on: [push, pull_request]

jobs:
  Test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.7, 3.8, 3.9, "3.10"]
        libiio: ['main', 'v0.25']

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

      - name: Install dependencies
        run: |
          export LIBIIO_BRANCH=${{ matrix.libiio }}
          bash ./.github/scripts/install_libiio.sh
          bash ./.github/scripts/install_pydeps.sh
          bash ./.github/scripts/install_part_libs.sh

      - name: Test
        run: pytest -v --cov=adi

  Test-Emulated-HW:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: [3.7, 3.8, 3.9, "3.10"]
        libiio: ['main', 'v0.25']

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

      - name: Install dependencies
        run: |
          export LIBIIO_BRANCH=${{ matrix.libiio }}
          bash ./.github/scripts/install_libiio.sh
          bash ./.github/scripts/install_part_libs.sh
          bash ./.github/scripts/install_iioemu.sh
          bash ./.github/scripts/install_pydeps.sh
          sudo apt install -y python3-tk

      - name: Test
        run: |
                pytest -vs --cov=adi --scan-verbose --emu --junitxml="results.xml" -k 'not prod'
      - name: Publish Test Report
        uses: mikepenz/action-junit-report@v4
        if: success() || failure() # always run even if the previous step fails
        with:
          report_paths: 'results.xml'

      - name: Report coverage
        if: (github.event_name != 'pull_request') && (matrix.python-version == 3.6)
        env:
          COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
        run: coveralls

      - name: Publish Unit Test Results
        uses: EnricoMi/publish-unit-test-result-action@v1
        if: always()
        with:
          files: results.xml

      - name: Test without optional dependencies
        run: |
                pip uninstall -y paramiko
                pytest -vs --scan-verbose --emu --junitxml="results.xml" -k 'not prod'
      - name: Publish Test Report
        uses: mikepenz/action-junit-report@v4
        if: success() || failure() # always run even if the previous step fails
        with:
          report_paths: 'results.xml'


  Lint:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: [3.7, 3.8, 3.9, "3.10"]

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

      - name: Install dependencies
        run: |
          bash ./.github/scripts/install_libiio.sh
          bash ./.github/scripts/install_part_libs.sh
          bash ./.github/scripts/install_pydeps.sh

      - name: Lint
        run: |
          pre-commit run --all-files 2> /dev/null
          ec=$?
          git diff -U0 > log.txt && cat log.txt
          exit $ec

  Doc:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python 3.7
        uses: actions/setup-python@v2
        with:
          python-version: 3.7

      - name: Install dependencies
        run: |
          bash ./.github/scripts/install_libiio.sh
          bash ./.github/scripts/install_part_libs.sh
          bash ./.github/scripts/install_pydeps.sh
          pip install -r requirements_doc.txt

      - name: Build doc
        run: |
          cd doc && make html
          cd ..

  DeployMainDoc:
    runs-on: ubuntu-latest
    needs: [Test,Lint,Doc]
    if: github.ref == 'refs/heads/main'

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python 3.7
        uses: actions/setup-python@v2
        with:
          python-version: 3.7

      - name: Install dependencies
        run: |
          bash ./.github/scripts/install_libiio.sh
          bash ./.github/scripts/install_part_libs.sh
          bash ./.github/scripts/install_pydeps.sh
          pip install -r requirements_doc.txt

      - name: Build doc and release
        run: |
          cd doc && make html
          cd ..

      - name: Publish doc
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./doc/build/html
          destination_dir: main

  DeployDevelopmentDoc:
    runs-on: ubuntu-latest
    needs: [Test,Lint,Doc]
    # Only run on pull requests to main and non-forks
    if: github.event_name == 'pull_request' && github.base_ref == 'main' && ! github.event.pull_request.head.repo.fork
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python 3.7
        uses: actions/setup-python@v2
        with:
          python-version: 3.7

      - name: Install dependencies
        run: |
          bash ./.github/scripts/install_libiio.sh
          bash ./.github/scripts/install_part_libs.sh
          bash ./.github/scripts/install_pydeps.sh
          pip install -r requirements_doc.txt

      - name: Build doc and release
        run: |
          export GIT_BRANCH=${{ github.head_ref }}
          export DEV_BUILD=1
          cd doc && make html
          cd ..

      - name: Publish doc
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./doc/build/html
          destination_dir: prs/${{ github.head_ref }}

      - name: Add comment to PR
        uses: mshick/add-pr-comment@v2
        with:
          message: |
            Generated documentation for this PR is available at [Link](https://analogdevicesinc.github.io/pyadi-iio/prs/${{ github.head_ref }}/index.html)

  Deploy:
    runs-on: ubuntu-latest
    needs: [Test,Lint,Doc]
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python 3.7
        uses: actions/setup-python@v2
        with:
          python-version: 3.7

      - name: Install dependencies
        run: |
          bash ./.github/scripts/install_libiio.sh
          bash ./.github/scripts/install_part_libs.sh
          bash ./.github/scripts/install_pydeps.sh
          pip install -r requirements_doc.txt
          pip install setuptools wheel twine build

      - name: Build doc and release
        run: |
          cd doc && make html
          cd ..
          python -m build

      - name: Publish doc
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./doc/build/html

      - name: Publish package
        uses: pypa/gh-action-pypi-publish@master
        with:
          user: __token__
          password: ${{ secrets.PYPI_TOKEN }}