name: Rustup

description: Install Rust with minimal profile and additional components

inputs:
  # See https://rust-lang.github.io/rustup/concepts/components.html
  clippy:
    default: false
    required: false
    type: boolean
  fmt:
    default: false
    required: false
    type: boolean
  docs:
    default: false
    required: false
    type: boolean
  restore-cache:
    default: true
    required: false
    type: boolean
  save-cache:
    default: false
    required: false
    type: boolean
  shared-key:
    default: 'warm'
    required: false
    type: string

runs:
  using: composite
  steps:
    - name: Print Inputs
      shell: bash
      run: |
        echo 'clippy: ${{ inputs.clippy }}'
        echo 'fmt: ${{ inputs.fmt }}'
        echo 'docs: ${{ inputs.docs }}'
        echo 'restore-cache: ${{ inputs.restore-cache }}'
        echo 'save-cache: ${{ inputs.save-cache }}'

    - name: Remove `profile` line on MacOS
      shell: bash
      if: runner.os == 'macOS'
      run: sed -i '' '/profile/d' rust-toolchain.toml

    - name: Remove `profile` line on non-MacOS
      shell: bash
      if: runner.os != 'macOS'
      run: sed -i '/profile/d' rust-toolchain.toml

    - name: Set minimal
      shell: bash
      run: rustup set profile minimal

    - name: Add Clippy
      shell: bash
      if: ${{ inputs.clippy == 'true' }}
      run: rustup component add clippy

    - name: Add Rustfmt
      shell: bash
      if: ${{ inputs.fmt == 'true' }}
      run: rustup component add rustfmt

    - name: Add docs
      shell: bash
      if: ${{ inputs.docs == 'true' }}
      run: rustup component add rust-docs

    - name: Install
      shell: bash
      run: |
        rustup show
        git restore .

    - name: Cache on ${{ github.ref_name }}
      uses: Swatinem/rust-cache@v2
      if: ${{ inputs.restore-cache == 'true' }}
      with:
        shared-key: ${{ inputs.shared-key }}
        save-if: ${{ inputs.save-cache == 'true' }}
