name: Submit release to the WinGet community repository

on:
  release:
    types: [published]

jobs:
  publish-winget:
    name: Submit to WinGet repository

    # GitHub token permissions needed for winget-create to submit a PR
    permissions:
      contents: read
      pull-requests: write

    # winget-create is only supported on Windows
    runs-on: windows-latest
      
    # winget-create will read the following environment variable to access the GitHub token needed for submitting a PR
    # See https://aka.ms/winget-create-token
    env:
      WINGET_CREATE_GITHUB_TOKEN: ${{ secrets.WINGET_CREATE_GITHUB_TOKEN }}

    steps:
      - name: Submit package using wingetcreate
        run: |
          # Set the package ID based on the release info
          $packageId = if ('${{ !github.event.release.prerelease }}' -eq 'true') { 'GitHub.Copilot' } else { 'GitHub.Copilot.Prerelease' }
          
          # Get installer info from release event
          $assets = '${{ toJSON(github.event.release.assets) }}' | ConvertFrom-Json
          $packageVersion = (${{ toJSON(github.event.release.tag_name) }})
          
          # Find the download URLs for the x64 and arm64 installers separately
          # This allows overrides to be used so that wingetcreate does not have to guess the architecture from the filename
          $installerUrlx64 = $assets | Where-Object -Property name -like '*win32-x64.zip' | Select-Object -ExpandProperty browser_download_url
          $installerUrlarm64 = $assets | Where-Object -Property name -like '*win32-arm64.zip' | Select-Object -ExpandProperty browser_download_url
          
          # Update package using wingetcreate
          curl.exe -JLO https://aka.ms/wingetcreate/latest
          .\wingetcreate.exe update $packageId `
            --version $packageVersion `
            --urls "$installerUrlx64|x64" "$installerUrlarm64|arm64" `
            --out manifests

          # Add PowerShell dependency to installer manifest
          $installerManifest = Get-ChildItem -Path manifests -Filter "*.installer.yaml" -Recurse | Select-Object -First 1 -ExpandProperty FullName
          if (-not $installerManifest) {
            Write-Error "No installer manifest (*.installer.yaml) was found in the 'manifests' directory."
            exit 1
          }
          $content = Get-Content -Path $installerManifest -Raw
          $dependency = @"
          Dependencies:
            PackageDependencies:
              - PackageIdentifier: Microsoft.PowerShell
                MinimumVersion: "7.0.0"
          "@
          # Remove existing top-level Dependencies block (if any), then insert ours
          $content = $content -replace '(?m)^Dependencies:\r?\n([ ]+.+\r?\n)*', ''
          $content = $content -replace '(?m)^Installers:', "$dependency`nInstallers:"
          Set-Content -Path $installerManifest -Value $content

          # Submit the modified manifest
          $manifestPath = Split-Path $installerManifest
          .\wingetcreate.exe submit $manifestPath