name: Package PlatformIO Library Dependencies
# trunk-ignore-all(checkov/CKV_GHA_7): Allow workflow_dispatch inputs for testing

on:
  workflow_call:
    inputs:
      pio_env:
        description: PlatformIO environment to target
        required: true
        type: string
  workflow_dispatch:
    inputs:
      pio_env:
        description: PlatformIO environment to target
        required: true
        type: string

permissions:
  contents: read

jobs:
  pkg-pio-libdeps:
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v7
        with:
          submodules: recursive

      - name: Setup Python
        uses: actions/setup-python@v6
        with:
          python-version: 3.x

      - name: Install deps
        shell: bash
        run: |
          pip install platformio

      - name: Get release version string
        run: |
          echo "long=$(./bin/buildinfo.py long)" >> $GITHUB_OUTPUT
        id: version

      - name: Fetch libdeps
        shell: bash
        run: |-
          platformio pkg install -e ${{ inputs.pio_env }}
          platformio pkg install -e ${{ inputs.pio_env }} -t platformio/tool-scons@4.40502.0
        env:
          PLATFORMIO_LIBDEPS_DIR: pio/libdeps
          PLATFORMIO_PACKAGES_DIR: pio/packages
          PLATFORMIO_CORE_DIR: pio/core
          PLATFORMIO_SETTING_ENABLE_TELEMETRY: 0
          PLATFORMIO_SETTING_CHECK_PLATFORMIO_INTERVAL: 3650
          PLATFORMIO_SETTING_CHECK_PRUNE_SYSTEM_THRESHOLD: 10240

      - name: Mangle platformio cache
        # Add "1" to epoch-timestamps of all downloads in the cache.
        # This is a hack to prevent internet access at build-time.
        run: |
          cp pio/core/.cache/downloads/usage.db pio/core/.cache/downloads/usage.db.bak
          jq -c 'with_entries(.value |= (. | tostring + "1" | tonumber))' pio/core/.cache/downloads/usage.db.bak > pio/core/.cache/downloads/usage.db

      - name: Store binaries as an artifact
        uses: actions/upload-artifact@v7
        with:
          name: platformio-deps-${{ inputs.pio_env }}-${{ steps.version.outputs.long }}
          overwrite: true
          include-hidden-files: true
          path: |
            pio/*
