name: Build and test using platformio

on:
  workflow_dispatch:
  workflow_call:
  push:
    paths:
      - '**platformio**'
  pull_request:
    paths:
      - '**platformio**'

jobs:
  platformio:
    name: Build and run PlatformIO example
    runs-on: ubuntu-latest
    steps:
      - name: Check out code from GitHub
        uses: actions/checkout@v2
        with:
          path: nanopb

      - name: Installing dependencies for local act
        if: ${{ env.ACT }}
        run: |
          sudo apt update

      - name: Installing common dependencies
        run: |
          sudo apt install -y python3-pip

      - name: Install and setup PlatformIO
        run: |
          pip3 install -U platformio
          export PATH=~/.local/bin:$PATH

      - name: Build PlatformIO package
        run: |
          cd nanopb
          pio package pack

      - name: Example - Extract PlatformIO package to example dir
        run: |
          cp -R nanopb/examples/platformio example
          mkdir -p example/lib/nanopb
          tar -xzf nanopb/Nanopb-*.tar.gz -C example/lib/nanopb

      - name: Example - Build
        run: |
          cd example
          pio run

      - name: Example - Run test without options
        run: example/.pio/build/pio_without_options/program

      - name: Example - Run test with options
        run: example/.pio/build/pio_with_options/program

      - name: Build in subdirectory with space characters
        run: |
          cp -R nanopb/examples/platformio "example with spaces"
          mkdir -p "example with spaces/lib/nanopb"
          tar -xzf nanopb/Nanopb-*.tar.gz -C "example with spaces/lib/nanopb"
          cd "example with spaces"
          pio run -e pio_with_options # ESP32 platform doesn't support spaces currently

      - name: Build with default platformio.ini
        run: |
          mkdir -p test_default_pio_conf
          cd test_default_pio_conf
          pio project init
          ln -s ../nanopb lib/nanopb
          echo "[env:native]" >> platformio.ini
          echo "platform = native" >> platformio.ini
          echo "lib_deps = Nanopb" >> platformio.ini
          echo "int main(int argc, char *argv[]){}" > src/main.cpp
          pio run
