name: Pipeline

on:
  push:
  workflow_dispatch:
  schedule:
# Every Friday at 22:00 - rerun pipeline to check for dependency-based issues
    - cron: '0 22 * * 5'

jobs:
  Prepare:
    uses: pyTooling/Actions/.github/workflows/PrepareJob.yml@r7

  ConfigParams:
    uses: pyTooling/Actions/.github/workflows/ExtractConfiguration.yml@r7

  UnitTestingParams:
    uses: pyTooling/Actions/.github/workflows/Parameters.yml@r7
    with:
      package_namespace:   'pyTooling.*'
      version_file:        'Common/__init__.py'
      python_version:      '3.13'
      python_version_list: '3.11 3.12 3.13 3.14 pypy-3.11'
      system_list:         'ubuntu ubuntu-arm windows windows-arm macos macos-arm mingw64 ucrt64'
      disable_list:        'ubuntu-arm:* windows-arm:pypy-3.11'

  PlatformTestingParams:
    uses: pyTooling/Actions/.github/workflows/Parameters.yml@r7
    with:
      name:                'Platform'
      package_namespace:   'pyTooling.*'
      version_file:        'Common/__init__.py'
      python_version_list: ''
      system_list:         'ubuntu ubuntu-arm windows windows-arm macos macos-arm mingw32 mingw64 ucrt64 clang64'
      disable_list:        'ubuntu-arm:* mingw32:*'  # no ruamel-yaml for MinGW32
      pipeline-delay:      30

  InstallParams:
    uses: pyTooling/Actions/.github/workflows/Parameters.yml@r7
    needs:
      - UnitTestingParams
    with:
      package_namespace:   'pyTooling.*'
      version_file:        'Common/__init__.py'
      python_version:      ${{ needs.UnitTestingParams.outputs.python_version }}
      python_version_list: ''
      system_list:         'ubuntu ubuntu-arm windows windows-arm macos macos-arm mingw64 ucrt64'

  UnitTesting:
    uses: pyTooling/Actions/.github/workflows/UnitTesting.yml@r7
    needs:
      - ConfigParams
      - UnitTestingParams
    with:
      jobs:                     ${{ needs.UnitTestingParams.outputs.python_jobs }}
      pacboy:                   'msys/git'
      unittest_report_xml:      ${{ needs.ConfigParams.outputs.unittest_report_xml }}
      coverage_report_xml:      ${{ needs.ConfigParams.outputs.coverage_report_xml }}
      coverage_report_json:     ${{ needs.ConfigParams.outputs.coverage_report_json }}
      coverage_report_html:     ${{ needs.ConfigParams.outputs.coverage_report_html }}
      unittest_xml_artifact:    ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).unittesting_xml }}
      coverage_sqlite_artifact: ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).codecoverage_sqlite }}

  PlatformTesting:
    uses: pyTooling/Actions/.github/workflows/UnitTesting.yml@r7
    needs:
      - ConfigParams
      - PlatformTestingParams
    with:
      jobs:                     ${{ needs.PlatformTestingParams.outputs.python_jobs }}
      unittest_directory:       'unit/Platform'
      requirements:             '-r tests/unit/requirements.txt'
      unittest_report_xml:      ${{ needs.ConfigParams.outputs.unittest_report_xml }}
      coverage_report_xml:      ${{ needs.ConfigParams.outputs.coverage_report_xml }}
      coverage_report_json:     ${{ needs.ConfigParams.outputs.coverage_report_json }}
      coverage_report_html:     ${{ needs.ConfigParams.outputs.coverage_report_html }}
      unittest_xml_artifact:    ${{ fromJson(needs.PlatformTestingParams.outputs.artifact_names).unittesting_xml }}
      coverage_sqlite_artifact: ${{ fromJson(needs.PlatformTestingParams.outputs.artifact_names).codecoverage_sqlite }}

  StaticTypeCheck:
    uses: pyTooling/Actions/.github/workflows/StaticTypeCheck.yml@r7
    needs:
      - ConfigParams
      - UnitTestingParams
    with:
      python_version: ${{ needs.UnitTestingParams.outputs.python_version }}
      html_report:    ${{ needs.ConfigParams.outputs.typing_report_html }}
      html_artifact:  ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).statictyping_html }}

  DocCoverage:
    uses: pyTooling/Actions/.github/workflows/CheckDocumentation.yml@r7
    needs:
      - ConfigParams
      - UnitTestingParams
    with:
      python_version: ${{ needs.UnitTestingParams.outputs.python_version }}
      directory:      ${{ needs.UnitTestingParams.outputs.package_directory }}
#      fail_below: 70

  Package:
    uses: pyTooling/Actions/.github/workflows/Package.yml@r7
    needs:
      - UnitTestingParams
      - UnitTesting
      - PlatformTesting
    with:
      python_version: ${{ needs.UnitTestingParams.outputs.python_version }}
      artifact:       ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).package_all }}

  Install:
    uses: pyTooling/Actions/.github/workflows/InstallPackage.yml@r7
    needs:
      - UnitTestingParams
      - InstallParams
      - Package
    with:
      jobs:         ${{ needs.InstallParams.outputs.python_jobs }}
      wheel:        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).package_all }}
      package_name: '${{ needs.UnitTestingParams.outputs.package_fullname }}.Common'

  PublishCoverageResults:
    uses: pyTooling/Actions/.github/workflows/PublishCoverageResults.yml@r7
    needs:
      - ConfigParams
      - UnitTestingParams
      - UnitTesting
      - PlatformTesting
    if: success() || failure()
    with:
      coverage_report_json:   ${{ needs.ConfigParams.outputs.coverage_report_json }}
      coverage_report_html:   ${{ needs.ConfigParams.outputs.coverage_report_html }}
      coverage_json_artifact: ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).codecoverage_json }}
      coverage_html_artifact: ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).codecoverage_html }}
      codecov:                'true'
      codacy:                 'true'
    secrets:
      CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
      CODACY_TOKEN:  ${{ secrets.CODACY_TOKEN }}

  PublishTestResults:
    uses: pyTooling/Actions/.github/workflows/PublishTestResults.yml@r7
    needs:
      - ConfigParams
      - UnitTestingParams
      - UnitTesting
      - PlatformTesting
    if: success() || failure()
    with:
      testsuite-summary-name: ${{ needs.ConfigParams.outputs.package_fullname }}
      merged_junit_filename:  ${{ fromJson(needs.ConfigParams.outputs.unittest_merged_report_xml).filename }}
      merged_junit_artifact:  ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).unittesting_xml }}
      dorny:                  'true'
      codecov:                'true'
    secrets:
      CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

#  VerifyDocs:
#    uses: pyTooling/Actions/.github/workflows/VerifyDocs.yml@r7
#    needs:
#      - UnitTestingParams
#    with:
#      python_version: ${{ needs.UnitTestingParams.outputs.python_version }}

  IntermediateCleanUp:
    uses: pyTooling/Actions/.github/workflows/IntermediateCleanUp.yml@r7
    needs:
      - UnitTestingParams
      - PublishCoverageResults
      - PublishTestResults
    if: success() || failure()
    with:
      sqlite_coverage_artifacts_prefix: ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).codecoverage_sqlite }}-
      xml_unittest_artifacts_prefix:    ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).unittesting_xml }}-

  Documentation:
    uses: pyTooling/Actions/.github/workflows/SphinxDocumentation.yml@r7
    needs:
      - ConfigParams
      - UnitTestingParams
      - PublishTestResults
      - PublishCoverageResults
#      - VerifyDocs
    if: (success() || failure()) && needs.PublishTestResults.result == 'success' && needs.PublishCoverageResults.result == 'success'
    with:
      python_version:         ${{ needs.UnitTestingParams.outputs.python_version }}
      coverage_report_json:   ${{ needs.ConfigParams.outputs.coverage_report_json }}
      unittest_xml_artifact:  ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).unittesting_xml }}
      coverage_json_artifact: ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).codecoverage_json }}
      html_artifact:          ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).documentation_html }}
      latex_artifact:         ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).documentation_latex }}

#  PDFDocumentation:
#    uses: pyTooling/Actions/.github/workflows/LaTeXDocumentation.yml@r7
#    needs:
#      - UnitTestingParams
#      - Documentation
#    with:
#      document: sphinx_reports
#      latex_artifact: ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).documentation_latex }}
#      pdf_artifact: ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).documentation_pdf }}

  PublishToGitHubPages:
    uses: pyTooling/Actions/.github/workflows/PublishToGitHubPages.yml@r7
    needs:
      - UnitTestingParams
      - Documentation
#      - PDFDocumentation
      - PublishCoverageResults
      - StaticTypeCheck
    with:
      doc:      ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).documentation_html }}
      coverage: ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).codecoverage_html }}
      typing:   ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).statictyping_html }}

  TriggerTaggedRelease:
    uses: pyTooling/Actions/.github/workflows/TagReleaseCommit.yml@r7
    needs:
      - Prepare
      - UnitTesting
      - PlatformTesting
#      - StaticTypeCheck
      - Package
      - Install
      - PublishToGitHubPages
    if: needs.Prepare.outputs.is_release_commit
    permissions:
      contents: write  # required for create tag
      actions:  write  # required for trigger workflow
    with:
      version:  ${{ needs.Prepare.outputs.version }}
      auto_tag: ${{ needs.Prepare.outputs.is_release_commit }}
    secrets: inherit

  ReleasePage:
    uses: pyTooling/Actions/.github/workflows/PublishReleaseNotes.yml@r7
    if: needs.Prepare.outputs.is_release_tag == 'true'
    needs:
      - Prepare
      - UnitTesting
      - PlatformTesting
#      - StaticTypeCheck
      - Package
      - Install
      - PublishToGitHubPages
    with:
      tag: ${{ needs.Prepare.outputs.version }}
    secrets: inherit

  PublishOnPyPI:
    uses: pyTooling/Actions/.github/workflows/PublishOnPyPI.yml@r7
    if: startsWith(github.ref, 'refs/tags')
    needs:
      - UnitTestingParams
      - ReleasePage
    with:
      python_version: ${{ needs.UnitTestingParams.outputs.python_version }}
      requirements:   '-r dist/requirements.txt'
      artifact:       ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).package_all }}
    secrets:
      PYPI_TOKEN:     ${{ secrets.PYPI_TOKEN }}

  ArtifactCleanUp:
    uses: pyTooling/Actions/.github/workflows/ArtifactCleanUp.yml@r7
    needs:
      - UnitTestingParams
      - PlatformTestingParams
      - StaticTypeCheck
      - PlatformTesting
      - Documentation
#      - PDFDocumentation
      - PublishTestResults
      - PublishCoverageResults
      - PublishToGitHubPages
#      - PublishOnPyPI
      - Install
      - IntermediateCleanUp
    with:
      package: ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).package_all }}
      remaining: |
        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).unittesting_xml }}-*
        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).unittesting_html }}-*
        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).codecoverage_sqlite }}-*
        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).codecoverage_xml }}-*
        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).codecoverage_json }}-*
        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).codecoverage_html }}-*
        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).unittesting_xml }}
        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).unittesting_html }}
        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).codecoverage_sqlite }}
        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).codecoverage_xml }}
        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).codecoverage_json }}
        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).codecoverage_html }}
        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).statictyping_html }}
        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).documentation_html }}
        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).documentation_latex }}
        ${{ fromJson(needs.PlatformTestingParams.outputs.artifact_names).unittesting_xml }}-*
        ${{ fromJson(needs.PlatformTestingParams.outputs.artifact_names).unittesting_html }}-*
        ${{ fromJson(needs.PlatformTestingParams.outputs.artifact_names).codecoverage_sqlite }}-*
        ${{ fromJson(needs.PlatformTestingParams.outputs.artifact_names).codecoverage_xml }}-*
        ${{ fromJson(needs.PlatformTestingParams.outputs.artifact_names).codecoverage_json }}-*
        ${{ fromJson(needs.PlatformTestingParams.outputs.artifact_names).codecoverage_html }}-*
#        ${{ fromJson(needs.UnitTestingParams.outputs.artifact_names).documentation_pdf }}
