name: "Install Ruby"

inputs:
  ruby-version:
    required: true

runs:
  using: "composite"
  steps:
    - name: Setup env
      shell: bash
      run: |
        echo "PATH=/opt/rubies/${{ inputs.ruby-version }}/bin:$PATH" >> $GITHUB_ENV
        echo "GEM_HOME=~/.gem/ruby/${{ inputs.ruby-version }}" >> $GITHUB_ENV

    - name: Cache
      uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4
      with:
        path: |
          /opt/rubies/${{ inputs.ruby-version }}
          ~/.gem/ruby/${{ inputs.ruby-version }}
        key: ruby-static-${{ runner.os }}-${{ inputs.ruby-version }}

    - name: Build static ruby
      working-directory: /tmp
      shell: bash
      run: |
        if [ -d /opt/rubies/${{inputs.ruby-version }} ]; then
          echo "Ruby ${{ inputs.ruby-version }} already installed, skipping build"
        else
          git clone https://github.com/rbenv/ruby-build.git
          PREFIX=/usr/local sudo -E ./ruby-build/install.sh
          CC=clang RUBY_CONFIGURE_OPTS="--disable-shared" sudo -E ruby-build ${{ inputs.ruby-version }} /opt/rubies/${{ inputs.ruby-version }}
        fi
