# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

name: Upload component to ESP-IDF component registry

on:
    workflow_dispatch:
        inputs:
            private:
              type: boolean
              default: true
              required: false
              description: "Private build? True means artifacts are only built. False means the artefacts are published to components.espressif.com"
            release:
                type: boolean
                default: false
                required: false
                description: "Release? Uploads to esp-idf component registry as release if true; otherwise as slint/slint-nightly"

jobs:
    upload_components:
        runs-on: ubuntu-22.04
        container: espressif/idf:latest
        steps:
            - uses: actions/checkout@v6
            - run: apt-get update && apt-get install -y yq
            - name: Make subsequent git calls work
              run: git config --global --add safe.directory '*'
            - name: Extract Version and determine name
              working-directory: api/cpp/esp-idf/slint
              id: version
              env:
                  RELEASE_INPUT: ${{ github.event.inputs.release }}
              run: |
                version=$(grep -oP '(?<=GIT_TAG v)[0-9]+\.[0-9]+\.[0-9]+' CMakeLists.txt)
                if [[ -z "$version" ]]; then
                  echo "Version not found"
                  exit 1
                fi
                if [ "$RELEASE_INPUT" != "true" ]; then
                  echo "PKG_NAME=slint-nightly" >> $GITHUB_OUTPUT
                  nightly_version_suffix=`git log -1 --format=%cd --date="format:%Y%m%d%H"`
                  version="$version+nightly.$nightly_version_suffix"
                  git show HEAD:./idf_component.yml | yq -y --arg nightly_version "${version}" '.version = $nightly_version' > idf_component.yml
                  sed -i 's/GIT_TAG v[0-9]*\.[0-9]*\.[0-9]*/GIT_TAG ${{ github.sha }}/' CMakeLists.txt
                  sed -i '1iset(SLINT_NIGHTLY true)' CMakeLists.txt
                else
                  echo "PKG_NAME=slint" >> $GITHUB_OUTPUT
                fi
                echo "VERSION=$version" >> $GITHUB_OUTPUT
            - name: Remove use of nightly version
              if: github.event.inputs.release == 'true'
              working-directory: api/cpp/esp-idf/slint
              run: |
                sed -i "s/find_package(Slint)/find_package(Slint ${{ steps.version.outputs.VERSION }})/g" CMakeLists.txt
            - name: Upload component
              uses: espressif/upload-components-ci-action@v2
              with:
                  namespace: "slint"
                  api_token: ${{ secrets.ESP_IDF_COMPONENTS_TOKEN }}
                  components: ${{ steps.version.outputs.PKG_NAME }}:api/cpp/esp-idf/slint
                  dry_run: ${{ github.event.inputs.private == 'true' }}
            - name: Package component
              if: github.event.inputs.release != 'true'
              working-directory: "api/cpp/esp-idf/slint"
              run: |
                  . ${IDF_PATH}/export.sh
                  compote component pack --name ${{ steps.version.outputs.PKG_NAME }}
            - name: Archive component
              if: github.event.inputs.release != 'true'
              uses: actions/upload-artifact@v7
              with:
                  path: "api/cpp/esp-idf/slint/dist/*"

