# ==================================================================================================================== #
# Authors:                                                                                                             #
#   Patrick Lehmann                                                                                                    #
#   Unai Martinez-Corral                                                                                               #
#                                                                                                                      #
# ==================================================================================================================== #
# Copyright 2020-2026 The pyTooling Authors                                                                            #
#                                                                                                                      #
# Licensed under the Apache License, Version 2.0 (the "License");                                                      #
# you may not use this file except in compliance with the License.                                                     #
# You may obtain a copy of the License at                                                                              #
#                                                                                                                      #
#   http://www.apache.org/licenses/LICENSE-2.0                                                                         #
#                                                                                                                      #
# Unless required by applicable law or agreed to in writing, software                                                  #
# distributed under the License is distributed on an "AS IS" BASIS,                                                    #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.                                             #
# See the License for the specific language governing permissions and                                                  #
# limitations under the License.                                                                                       #
#                                                                                                                      #
# SPDX-License-Identifier: Apache-2.0                                                                                  #
# ==================================================================================================================== #
name: Verify examples

on:
  workflow_call:
    inputs:
      ubuntu_image_version:
        description: 'Ubuntu image version.'
        required: false
        default: '24.04'
        type: string
      python_version:
        description: 'Python version.'
        required: false
        default: '3.14'
        type: string

jobs:

  VerifyDocs:
    name: 👍 Verify example snippets using Python ${{ inputs.python_version }}
    runs-on: "ubuntu-${{ inputs.ubuntu_image_version }}"

    steps:
      - name: ⏬ Checkout repository
        uses: actions/checkout@v6

      - name: 🐍 Setup Python
        uses: actions/setup-python@v6
        with:
          python-version: ${{ inputs.python_version }}

      - name: 🐍 Install dependencies
        run: |
          pip3 install .

      - name: ✂ Extract code snippet from README
        shell: python
        run: |
          from pathlib import Path
          import re

          ROOT = Path('.')

          with (ROOT / 'README.md').open('r') as rptr:
              content = rptr.read()

          m = re.search(r"```py(thon)?(?P<code>.*?)```", content, re.MULTILINE|re.DOTALL)

          if m is None:
              raise Exception("Regular expression did not find the example in the README!")

          with (ROOT / 'tests/docs/example.py').open('w') as wptr:
              wptr.write(m["code"])

      - name: Print example.py
        run: cat tests/docs/example.py

      - name: ✅ Run example snippet
        working-directory: tests/docs
        run: |
          python3 example.py
