# Entry conditions:
# - `setup/checkout` has already happened
# - working dir is the root directory of your project (e.g. `reflex/`).
# - You have a `poetry.lock` file in the root directory of your project
# - You have a `pyproject.toml` file in the root directory of your project
#
# Exit conditions:
# - Python of version `python-version` is ready to be invoked as `python`.
# - Poetry of version `poetry-version` is ready ot be invoked as `poetry`.
# - If `run-poetry-install` is true, deps as defined in `pyproject.toml` will have been installed into the venv at `create-venv-at-path`.

name: 'Setup Reflex build environment'
description: 'Sets up Python, install poetry (cached), install project deps (cached)'
inputs:
  python-version:
    description: 'Python version setup'
    required: true
  poetry-version:
    description: 'Poetry version to install'
    required: false
    default: '1.3.1'
  run-poetry-install:
    description: 'Whether to run poetry install on current dir'
    required: false
    default: false
  create-venv-at-path:
    description: 'Path to venv (if poetry install is enabled)'
    required: false
    default: '.venv'

runs:
  using: "composite"
  steps:
    - name: Set up Python ${{ inputs.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ inputs.python-version }}

    # This is required for OS portability in presence of caching.
    #
    # The act of installing poetry has the side effect of adding
    # poetry bin path to system path.
    #
    # But, if we get a cache hit on the poetry installation, we
    # don't get this important side effect. As a result, bare calls
    # to "poetry" fails.
    - name: Prepare PATH env to include where poetry will be installed into
      shell: bash
      run: |
        echo "~/.local/bin/" >> $GITHUB_PATH

    - name: Restore cached poetry install
      id: restore-poetry-cache
      uses: actions/cache/restore@v3
      with:
        path: ~/.local
        key: ${{ runner.os }}-python-${{ inputs.python-version }}-poetry-${{ inputs.poetry-version }}

    - if: steps.restore-poetry-cache.outputs.cache-hit != 'true'
      name: Install Poetry
      uses: snok/install-poetry@v1
      with:
        version: ${{ inputs.poetry-version }}
        virtualenvs-create: true
        virtualenvs-in-project: true
        virtualenvs-path: ${{ inputs.create-venv-at-path }}

    - if: steps.restore-poetry-cache.outputs.cache-hit != 'true'
      name: Save poetry install to cache
      uses: actions/cache/save@v3
      with:
        path: ~/.local
        key: ${{ steps.restore-poetry-cache.outputs.cache-primary-key }}

    - name: Restore cached project python deps
      id: restore-pydeps-cache
      uses: actions/cache/restore@v3
      with:
        path: ${{ inputs.create-venv-at-path }}
        key: ${{ runner.os }}-python-${{ inputs.python-version }}-pydeps-${{ hashFiles('**/poetry.lock') }}

    - if: ${{ inputs.run-poetry-install == 'true' && steps.restore-pydeps-cache.outputs.cache-hit != 'true' }}
      name: Run poetry install (will get cached)
      # We skip over installing the root package (the current project code under CI)
      # Root package should not be cached - its content is not reflected in poetry.lock / cache key

      # On Windows, it is scripts/activate. On Linux and MacOS, it is bin/activate
      shell: bash
      run: |
        python -m venv ${{ inputs.create-venv-at-path }}
        source ${{ inputs.create-venv-at-path }}/*/activate
        poetry install --no-interaction --no-root

    - if: steps.restore-pydeps-cache.outputs.cache-hit != 'true'
      name: Save Python deps to cache
      uses: actions/cache/save@v3
      with:
        path: ${{ inputs.create-venv-at-path }}
        key: ${{ steps.restore-pydeps-cache.outputs.cache-primary-key }}

    - if: ${{ inputs.run-poetry-install == 'true' }}
      name: Run poetry install (root package)
      # Here we really install the root package (the current project code under CI).env:
      # This should not be cached.
      shell: bash
      run: |
        source ${{ inputs.create-venv-at-path }}/*/activate
        poetry install --only-root --no-interaction