# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

# cSpell: ignore debuginfo rustflags swatinem

---
name: Setup Rust support
description: Set up Slint development environment

inputs:
    toolchain:
        description: "Rust toolchain to use (default: stable)"
        required: false
        default: stable
    target:
        description: "Rust target to use"
        required: false
        default: ""
    components:
        description: "Rust components to install"
        required: false
        default: ""
    key:
        description: "Extra cache keying information"
        required: false
        default: ""
    cache:
        description: "Enable Rust caching (default: true)"
        required: false
        default: "true"
    save_if:
        description: "Condition to save the cache"
        required: false
        default: ${{ github.ref == 'refs/heads/master' }}

runs:
    using: composite
    steps:
        - name: Disable debug info to avoid running out of disk space on Windows
          if: runner.os == 'Windows'
          run: |
              echo "RUSTFLAGS=-C debuginfo=0" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
          shell: powershell
        # Skia source builds end up with long paths, exceeding the 260 character limit. Enable this git for windows
        # option to use newer Windows API (https://github.com/git-for-windows/git/blob/bc3743def76f487b6dbc18b1b2645ab081c14980/Documentation/config/core.txt#L679)
        - name: Enable long path support for git checkouts
          if: runner.os == 'Windows'
          run: |
              git config --system core.longpaths true
          shell: powershell
        - name: Move cargo home close to the target/$profile directory, so that relative paths from build to cargo source are without drive letter
          if: runner.os == 'Windows'
          run: |
              echo "CARGO_HOME=${{ runner.workspace }}\cargo" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
          shell: powershell
        - name: Set GETTEXT_DIR environment variable or it will still be compiled from source.
          if: runner.os == 'macOS'
          run: |
              GETTEXT_PATH=$(brew --prefix gettext)
              echo "GETTEXT_DIR=${GETTEXT_PATH}" >> $GITHUB_ENV
              echo "Installed gettext at: ${GETTEXT_PATH}"
          shell: bash
        - name: Install Rust ${{ inputs.toolchain }}
          uses: dtolnay/rust-toolchain@stable
          with:
              toolchain: ${{ inputs.toolchain }}
              components: ${{ inputs.components }}
              target: ${{ inputs.target }}
        - uses: Swatinem/rust-cache@v2
          if: inputs.cache == 'true'
          with:
              key: ${{ inputs.key }}-1
              save-if: ${{ inputs.save_if }}
