name: Restore cache
description: Find and restores conan and ccache cache
inputs:
  conan_dir:
    description: Path to .conan directory
    required: true
  conan_profile:
    description: Conan profile name
    required: true
  ccache_dir:
    description: Path to .ccache directory
    required: true
  build_type:
    description: Current build type (e.g. Release, Debug)
    required: true
    default: Release
  code_coverage:
    description: Whether code coverage is on
    required: true
    default: 'false'
outputs:
  conan_hash:
    description: Hash to use as a part of conan cache key
    value: ${{ steps.conan_hash.outputs.hash }}
  conan_cache_hit:
    description: True if conan cache has been downloaded
    value: ${{ steps.conan_cache.outputs.cache-hit }}
  ccache_cache_hit:
    description: True if ccache cache has been downloaded
    value: ${{ steps.ccache_cache.outputs.cache-hit }}
runs:
  using: composite
  steps:
    - name: Find common commit
      id: git_common_ancestor
      uses: ./.github/actions/git_common_ancestor

    - name: Calculate conan hash
      id: conan_hash
      shell: bash
      run: |
        conan info . -j info.json -o clio:tests=True
        packages_info=$(cat info.json | jq '.[] | "\(.display_name): \(.id)"' | grep -v 'clio')
        echo "$packages_info"
        hash=$(echo "$packages_info" | shasum -a 256 | cut -d ' ' -f 1)
        rm info.json
        echo "hash=$hash" >> $GITHUB_OUTPUT

    - name: Restore conan cache
      uses: actions/cache/restore@v4
      id: conan_cache
      with:
        path: ${{ inputs.conan_dir }}/data
        key: clio-conan_data-${{ runner.os }}-${{ inputs.build_type }}-${{ inputs.conan_profile }}-develop-${{ steps.conan_hash.outputs.hash }}

    - name: Restore ccache cache
      uses: actions/cache/restore@v4
      id: ccache_cache
      if: ${{ env.CCACHE_DISABLE != '1' }}
      with:
        path: ${{ inputs.ccache_dir }}
        key: clio-ccache-${{ runner.os }}-${{ inputs.build_type }}${{ inputs.code_coverage == 'true' && '-code_coverage' || '' }}-${{ inputs.conan_profile }}-develop-${{ steps.git_common_ancestor.outputs.commit }}
