name: Poetry Install
description: Run `poetry install` with optional artifact and metadata caching

inputs:
  args:
    description: Arguments for `poetry install`
  cache:
    description: Enable transparent Poetry artifact and metadata caching
    default: 'true'

outputs:
  cache-hit:
    description: Whether an exact cache hit occured
    value: ${{ steps.cache.outputs.cache-hit }}

runs:
  using: composite
  steps:
    - run: printf 'cache-dir=%s\n' "$(poetry config cache-dir)" >> $GITHUB_OUTPUT
      id: poetry-config
      shell: bash

    # Bust the cache every 24 hours to prevent it from expanding over time.
    - run: printf 'date=%s\n' "$(date -I)" >> $GITHUB_OUTPUT
      id: get-date
      if: inputs.cache == 'true'
      shell: bash

    - uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
      id: cache
      if: inputs.cache == 'true'
      with:
        path: |
          ${{ steps.poetry-config.outputs.cache-dir }}/artifacts
          ${{ steps.poetry-config.outputs.cache-dir }}/cache
        key: poetry-${{ steps.get-date.outputs.date }}-${{ runner.os }}-${{ hashFiles('pyproject.toml', 'poetry.lock') }}
        # The cache is cross-platform, and other platforms are used to seed cache misses.
        restore-keys: |
          poetry-${{ steps.get-date.outputs.date }}-${{ runner.os }}-
          poetry-${{ steps.get-date.outputs.date }}-
        enableCrossOsArchive: true

    - run: poetry install ${ARGS}
      shell: bash
      env:
        ARGS: ${{ inputs.args }}

    - run: poetry env info
      shell: bash

    - run: poetry show
      shell: bash
