{% from '.github/workflows/appimage-build.sh.j2' import build %}
{% from '.github/workflows/pyinstaller-test.sh.j2' import test_bash %}
jobs:
  build:
    runs-on: ${{ '{{' }} matrix.system.image }}
    steps:
      - uses: 'actions/checkout@{{ github_latest_action_tag('actions', 'checkout') }}'
{% if settings.private %}
      - name: 'Delete old caches and artifacts'
        env:
          GH_TOKEN: ${{ '{{' }} secrets.GITHUB_TOKEN }}
        run: |
          gh cache list --json id,createdAt -q '.[] | select((now - (.createdAt | sub("\\.[0-9]+"; "") | fromdateiso8601)) > 43200) | .key' | xargs -r -L1 gh cache delete
          gh api repos/${{ '{{' }} github.repository }}/actions/artifacts --paginate -q '.artifacts[] | select((now - (.created_at | sub("\\.[0-9]+"; "") | fromdateiso8601)) > 43200) | .id' | xargs -r -I{} gh api -X DELETE repos/${{ '{{' }} github.repository }}/actions/artifacts/{}
{% endif %}
{% if settings.package_manager == 'uv' %}
      - name: 'Install yq'
        run: 'pipx install yq'
      - name: 'Install uv'
        uses: 'astral-sh/setup-uv@{{ github_latest_action_tag('astral-sh', 'setup-uv') }}'
{% else %}
      - name: 'Install Poetry'
        run: 'pipx install poetry yq'
{% endif %}
      - name: 'Set up Python'
        uses: 'actions/setup-python@{{ github_latest_action_tag('actions', 'setup-python') }}'
        with:
{% if settings.package_manager != 'uv' %}
          cache: 'poetry'
{% endif %}
          python-version: '3.13'
{% if settings.github.workflows.appimage.apt_packages %}
      - name: 'Install dependencies'
        run: 'sudo apt-get update && sudo apt-get install -y {{ settings.github.workflows.appimage.apt_packages | join(" ") }}'
{% endif %}
{% if settings.package_manager == 'uv' %}
      - name: 'Install dependencies (uv)'
        run: |
          uv sync{{ ' ' + settings.appimage.uv_sync_args | join(' ') if settings.appimage.uv_sync_args else '' }}
          uv pip install python-appimage
          wget -O generic.svg 'https://raw.githubusercontent.com/KDE/breeze-icons/refs/heads/master/icons/preferences/32/preferences-other.svg'
        shell: 'bash'
{% else %}
      - name: 'Install dependencies (Poetry)'
        run: |
          poetry install
          poetry run -- pip install python-appimage
          wget -O generic.svg 'https://raw.githubusercontent.com/KDE/breeze-icons/refs/heads/master/icons/preferences/32/preferences-other.svg'
        shell: 'bash'
{% endif %}
      - name: 'Build with python-appimage'
        run: |
          {{ build(settings) | indent(10) | trim }}
        shell: 'bash'
      - name: 'Test binary'
        run: |
          {{ test_bash(settings.appimage.test_commands) | indent(10) | trim }}
      - name: 'Upload Artifacts'
        uses: 'actions/upload-artifact@{{ github_latest_action_tag('actions', 'upload-artifact') }}'
        with:
          if-no-files-found: error
          name: {{ settings.github_project_name }}-binaries-linux-${{ '{{' }} matrix.system.arch }}
          path: |
            dist/*
  {% if not settings.private %}
      - name: 'Generate SBOM'
        uses: 'anchore/sbom-action@{{ github_latest_action_tag('anchore', 'sbom-action') }}'
        env:
          SYFT_SOURCE_NAME: '{{ settings.github_project_name }}'
          SYFT_SOURCE_VERSION: ${{ '{{' }} github.ref_name }}
        with:
          path: 'dist/'
          output-file: 'sbom.spdx.json'
          upload-artifact: false
      - name: 'Attest'
        uses: 'actions/attest@{{ github_latest_action_tag('actions', 'attest') }}'
        with:
          subject-path: 'dist/*.AppImage'
      - name: 'Attest SBOM'
        uses: 'actions/attest@{{ github_latest_action_tag('actions', 'attest') }}'
        with:
          subject-path: 'dist/*.AppImage'
          sbom-path: 'sbom.spdx.json'
  {% endif %}
      - if: github.ref_type == 'tag'
        name: Upload package
        uses: softprops/action-gh-release@{{ github_latest_action_tag('softprops', 'action-gh-release') }}
        with:
          draft: true
          fail_on_unmatched_files: true
          files: |
            dist/*.AppImage
    strategy:
      matrix:
        system:
{% if not settings.private %}
          - { arch: aarch64, image: ubuntu-24.04-arm }
{% endif %}
          - { arch: x86_64, image: ubuntu-latest }
name: 'AppImage'
'on':
  push:
    branches:
      - 'master'
    paths:
      - '**/*.py'
      - '**/*.pyi'
      - '.github/workflows/appimage.yml'
      - 'pyproject.toml'
      - 'uv.lock'
      - 'poetry.lock'
    tags:
      - 'v*.*.*'
permissions:
{% if settings.private %}
  actions: 'write'
{% endif %}
  attestations: 'write'
  contents: 'write'
  id-token: 'write'
