name: Setup Rust and Prisma
description: Setup Rust and Prisma
inputs:
  target:
    description: toolchain target triple
    required: false
  save-cache:
    description: Whether to save the Rust cache
    required: false
    default: 'false'
  restore-cache:
    description: Whether to restore the Rust cache
    required: false
    default: 'true'
runs:
  using: 'composite'
  steps:
    - name: Install Rust
      id: toolchain
      uses: dtolnay/rust-toolchain@stable
      with:
        target: ${{ inputs.target }}
        toolchain: '1.75'
        components: clippy, rustfmt

    - name: Cache Rust Dependencies
      if: ${{ inputs.restore-cache == 'true' }}
      uses: Swatinem/rust-cache@v2
      with:
        key: ${{ inputs.target }}
        save-if: ${{ inputs.save-cache }}
        shared-key: stable-cache

    - name: Cargo config.toml
      shell: bash
      run: echo '{}' | npx -y mustache - .cargo/config.toml.mustache .cargo/config.toml

    - name: Turn Off Debuginfo and bump opt-level
      shell: bash
      if: ${{ runner.os != 'Windows' }}
      run: |
        sed '/\[profile.dev]/a\
        debug = 0
        ' Cargo.toml > Cargo.toml.tmp && mv Cargo.toml.tmp Cargo.toml

        sed '/\[profile.dev]/a\
        opt-level=1
        ' Cargo.toml > Cargo.toml.tmp && mv Cargo.toml.tmp Cargo.toml

    - name: Turn Off Debuginfo and bump opt-level
      if: ${{ runner.os == 'Windows' }}
      shell: powershell
      run: |
        (Get-Content Cargo.toml) -replace '\[profile.dev\]', '[profile.dev]
        debug = 0' | Set-Content Cargo.toml
        (Get-Content Cargo.toml) -replace '\[profile.dev\]', '[profile.dev]
        opt-level=1' | Set-Content Cargo.toml

    - name: Restore cached Prisma codegen
      id: cache-prisma-restore
      uses: actions/cache/restore@v4
      with:
        key: prisma-1-${{ runner.os }}-${{ hashFiles('./core/prisma/*', './crates/sync-generator/*', './Cargo.*') }}
        path: crates/prisma/src/**/*.rs

    - name: Generate Prisma client
      working-directory: core
      if: ${{ steps.cache-prisma-restore.outputs.cache-hit != 'true' }}
      shell: bash
      run: cargo prisma generate

    - name: Save Prisma codegen
      id: cache-prisma-save
      if: ${{ steps.cache-prisma-restore.outputs.cache-hit != 'true' }}
      uses: actions/cache/save@v4
      with:
        key: ${{ steps.cache-prisma-restore.outputs.cache-primary-key }}
        path: crates/prisma/src/**/*.rs
