name: "Configure Quarto Dev"
description: "Configures the image for Quarto Development"

runs:
  using: "composite"
  steps:
    - name: Cache deno std deps
      uses: actions/cache@v5
      with:
        path: ./src/resources/deno_std/cache
        key: ${{ runner.os }}-deno_std-2-${{ hashFiles('./src/resources/deno_std/deno_std.lock', './package/scripts/deno_std/deno_std.ts') }}
        restore-keys: |
          ${{ runner.os }}-deno_std-2-

    - name: Cache Cargo dependencies
      uses: actions/cache@v5
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          package/typst-gather/target
        key: ${{ runner.os }}-cargo-typst-gather-${{ hashFiles('package/typst-gather/Cargo.lock') }}
        restore-keys: |
          ${{ runner.os }}-cargo-typst-gather-

    - name: Cache Deno development cache
      uses: actions/cache@v5
      with:
        path: ./package/dist/bin/deno_cache
        key: ${{ runner.os }}-${{ runner.arch }}-deno-cache-v1-${{ hashFiles('configuration', 'src/import_map.json', 'src/vendor_deps.ts', 'tests/test-deps.ts', 'package/scripts/deno_std/deno_std.ts') }}
        restore-keys: |
          ${{ runner.os }}-${{ runner.arch }}-deno-cache-v1-

    - name: Configure Quarto (.sh)
      if: runner.os != 'Windows'
      shell: bash
      env:
        QUARTO_SKIP_DENO_CACHE_WIPE: "1"
      run: |
        # install with symlink in /usr/local/bin which in PATH on CI
        ./configure.sh

    - name: Configure Quarto (.ps1)
      if: runner.os == 'Windows'
      shell: pwsh
      env:
        QUARTO_SKIP_DENO_CACHE_WIPE: "1"
      run: |
        ./configure.cmd
        "$(Get-ChildItem -Path ./package/dist/bin/quarto.cmd | %{ $_.FullName } | Split-Path)" >> $env:GITHUB_PATH

    - shell: pwsh
      if: runner.os == 'Windows'
      run: $env:PATH -Split ";"

    - name: Basic dev mode sanity check
      shell: pwsh
      run: |
        If ( "$(quarto --version)" -ne "99.9.9") {
          echo "Unexpected version detected: $(quarto --version)"
          Exit 1
        }
        If ( $(quarto --paths | Select-String -Pattern "package[/\\]+dist[/\\]+share") -ne $null ) {
          echo "Unexpected package/dist/share path detected: $(quarto --paths)"
          Exit 1
        }
        # check if configure is modifying some files as it should not
        $modifiedFiles = git diff --name-only
        If ($modifiedFiles -ne "") {

          # Convert the list to an array
          $modifiedFilesArray = $modifiedFiles -split "`n" | ForEach-Object { $_.Trim() }

          If ($modifiedFilesArray -contains "tests/uv.lock") {
            Write-Output "::warning::test/uv.lock has been modified."
            $modifiedFilesArray = $modifiedFilesArray | Where-Object { $_ -notmatch "uv.lock" }
          }

          # Count the number of modified files
          $modifiedFilesCount = $modifiedFilesArray.Count

          If ($modifiedFilesCount -ge 1) {
            Write-Output "::error::Uncommitted changes detected."
            foreach ($file in $modifiedFilesArray) {
              Write-Output $file
            }
            Exit 1
          }
        }

    - name: Quarto Check
      if: runner.debug == '1'
      working-directory: tests
      shell: pwsh
      run: quarto check
