name: 'Restore Cache AZCopy'
description: 'Restores Electron src cache via AZCopy'
inputs:
  target-platform:
    description: 'Target platform, should be linux, win, macos'
runs:
  using: "composite"
  steps:
  - name: Obtain SAS Key
    continue-on-error: true
    uses: actions/cache/restore@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
    with:
      path: sas-token
      key: sas-key-${{ inputs.target-platform }}-${{ github.run_number }}-1
      enableCrossOsArchive: true
  - name: Obtain SAS Key
    continue-on-error: true
    uses: actions/cache/restore@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
    with:
      path: sas-token
      key: sas-key-${{ inputs.target-platform }}-${{ github.run_number }}-${{ github.run_attempt }}
      enableCrossOsArchive: true
  - name: Download Src Cache from AKS
    # The cache will always exist here as a result of the checkout job
    # Either it was uploaded to Azure in the checkout job for this commit
    # or it was uploaded in the checkout job for a previous commit.
    uses: nick-fields/retry@7152eba30c6575329ac0576536151aca5a72780e # v3.0.0
    with:
      timeout_minutes: 30
      max_attempts: 3
      retry_on: error
      shell: bash
      command: |
        sas_token=$(cat sas-token)
        if [ -z "$sas_token" ]; then
          echo "SAS Token not found; exiting src cache download early..."
          exit 1
        else
          sas_token=$(jq -r '.sasToken' sas-token)
          account_name=$(jq -r '.accountName' sas-token)
          if [ "${{ inputs.target-platform }}" = "win" ]; then
            azcopy copy --log-level=ERROR \
            "https://$account_name.file.core.windows.net/${{ env.AZURE_AKS_WIN_CACHE_SHARE_NAME }}/${{ env.CACHE_PATH }}?$sas_token" $DEPSHASH.tar
          else
            azcopy copy --log-level=ERROR \
            "https://$account_name.file.core.windows.net/${{ env.AZURE_AKS_CACHE_SHARE_NAME }}/${{ env.CACHE_PATH }}?$sas_token" $DEPSHASH.tar
          fi            
        fi
    env:
      AZURE_AKS_CACHE_SHARE_NAME: linux-cache
      AZURE_AKS_WIN_CACHE_SHARE_NAME: windows-cache
  - name: Clean SAS Key
    shell: bash
    run: rm -f sas-token
  - name: Unzip and Ensure Src Cache
    if: ${{ inputs.target-platform == 'macos' }}
    shell: bash
    run: |
      echo "Downloaded cache is $(du -sh $DEPSHASH.tar | cut -f1)"
      if [ `du $DEPSHASH.tar | cut -f1` = "0" ]; then
        echo "Cache is empty - exiting"
        exit 1
      fi
      
      mkdir temp-cache
      tar -xf $DEPSHASH.tar -C temp-cache
      echo "Unzipped cache is $(du -sh temp-cache/src | cut -f1)"

      if [ -d "temp-cache/src" ]; then
        echo "Relocating Cache"
        rm -rf src
        mv temp-cache/src src

        echo "Deleting zip file"
        rm -rf $DEPSHASH.tar
      fi

      if [ ! -d "src/third_party/blink" ]; then
        echo "Cache was not correctly restored - exiting"
        exit 1
      fi

      echo "Wiping Electron Directory"
      rm -rf src/electron

  - name: Unzip and Ensure Src Cache (Windows)
    if: ${{ inputs.target-platform == 'win' }}
    shell: powershell
    run: |
      $src_cache = "$env:DEPSHASH.tar"
      $cache_size = $(Get-Item $src_cache).length
      Write-Host "Downloaded cache is $cache_size"
      if ($cache_size -eq 0) {
        Write-Host "Cache is empty - exiting"
        exit 1
      }

      $TEMP_DIR=New-Item -ItemType Directory -Path temp-cache
      $TEMP_DIR_PATH = $TEMP_DIR.FullName
      C:\ProgramData\Chocolatey\bin\7z.exe -y -snld20 x $src_cache -o"$TEMP_DIR_PATH"

  - name: Move Src Cache (Windows)
    if: ${{ inputs.target-platform == 'win' }}
    uses: nick-fields/retry@7152eba30c6575329ac0576536151aca5a72780e # v3.0.0
    with:
      timeout_minutes: 30
      max_attempts: 3
      retry_on: error
      shell: powershell  
      command: |
        if (Test-Path "temp-cache\src") {
          Write-Host "Relocating Cache"
          Remove-Item -Recurse -Force src
          Move-Item temp-cache\src src

          Write-Host "Deleting zip file"
          Remove-Item -Force $src_cache
        }
        if (-Not (Test-Path "src\third_party\blink")) {
          Write-Host "Cache was not correctly restored - exiting"
          exit 1
        }

        Write-Host "Wiping Electron Directory"
        Remove-Item -Recurse -Force src\electron
