name: Save cache
description: Save conan and ccache cache for develop branch

inputs:
  conan_dir:
    description: Path to .conan directory
    required: true
  conan_profile:
    description: Conan profile name
    required: true
  conan_hash:
    description: Hash to use as a part of conan cache key
    required: true
  conan_cache_hit:
    description: Whether conan cache has been downloaded
    required: true
  ccache_dir:
    description: Path to .ccache directory
    required: true
  ccache_cache_hit:
    description: Whether conan cache has been downloaded
    required: true
  ccache_cache_miss_rate:
    description: How many cache misses happened
  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"

runs:
  using: composite
  steps:
    - name: Find common commit
      id: git_common_ancestor
      uses: ./.github/actions/git_common_ancestor

    - name: Cleanup conan directory from extra data
      if: ${{ inputs.conan_cache_hit != 'true' }}
      shell: bash
      run: |
        conan remove "*" -s -b -f

    - name: Save conan cache
      if: ${{ inputs.conan_cache_hit != 'true' }}
      uses: actions/cache/save@v4
      with:
        path: ${{ inputs.conan_dir }}/data
        key: clio-conan_data-${{ runner.os }}-${{ inputs.build_type }}-${{ inputs.conan_profile }}-develop-${{ inputs.conan_hash }}

    - name: Save ccache cache
      if: ${{ inputs.ccache_cache_hit != 'true' || inputs.ccache_cache_miss_rate == '100.0' }}
      uses: actions/cache/save@v4
      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 }}
